2012-06-10 16:50:54 +00:00
|
|
|
/*
|
2024-09-17 19:00:25 +00:00
|
|
|
* Deskflow -- mouse and keyboard sharing utility
|
2025-01-24 00:11:17 +00:00
|
|
|
* SPDX-FileCopyrightText: (C) 2012 - 2016 Symless Ltd.
|
|
|
|
|
* SPDX-FileCopyrightText: (C) 2002 Chris Schoeneman
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
|
2012-06-10 16:50:54 +00:00
|
|
|
*/
|
|
|
|
|
|
2014-02-28 12:55:14 +00:00
|
|
|
#include "arch/unix/ArchDaemonUnix.h"
|
|
|
|
|
|
|
|
|
|
#include "arch/unix/XArchUnix.h"
|
2014-02-28 12:36:45 +00:00
|
|
|
#include "base/Log.h"
|
|
|
|
|
|
2012-06-10 16:50:54 +00:00
|
|
|
#include <cstdlib>
|
2024-07-02 19:07:06 +00:00
|
|
|
#include <errno.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <unistd.h>
|
2012-06-10 16:50:54 +00:00
|
|
|
|
|
|
|
|
//
|
2014-11-11 13:51:47 +00:00
|
|
|
// ArchDaemonUnix
|
2012-06-10 16:50:54 +00:00
|
|
|
//
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
ArchDaemonUnix::ArchDaemonUnix()
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
// do nothing
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
ArchDaemonUnix::~ArchDaemonUnix()
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
// do nothing
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef __APPLE__
|
|
|
|
|
|
|
|
|
|
// In Mac OS X, fork()'d child processes can't use most APIs (the frameworks
|
2024-09-17 19:00:25 +00:00
|
|
|
// that Deskflow uses in fact prevent it and make the process just up and die),
|
2012-06-10 16:50:54 +00:00
|
|
|
// so need to exec a copy of the program that doesn't fork so isn't limited.
|
2024-10-17 18:04:35 +00:00
|
|
|
int execSelfNonDaemonized()
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
extern char **NXArgv;
|
|
|
|
|
char **selfArgv = NXArgv;
|
|
|
|
|
|
2024-09-17 19:00:25 +00:00
|
|
|
setenv("_DESKFLOW_DAEMONIZED", "", 1);
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
execvp(selfArgv[0], selfArgv);
|
|
|
|
|
return 0;
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
bool alreadyDaemonized()
|
|
|
|
|
{
|
|
|
|
|
return getenv("_DESKFLOW_DAEMONIZED") != NULL;
|
|
|
|
|
}
|
2024-07-02 19:07:06 +00:00
|
|
|
|
2012-06-10 16:50:54 +00:00
|
|
|
#endif
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
int ArchDaemonUnix::daemonize(const char *name, DaemonFunc func)
|
|
|
|
|
{
|
2012-06-10 16:50:54 +00:00
|
|
|
#ifdef __APPLE__
|
2024-07-02 19:07:06 +00:00
|
|
|
if (alreadyDaemonized())
|
|
|
|
|
return func(1, &name);
|
2012-06-10 16:50:54 +00:00
|
|
|
#endif
|
2024-07-02 19:07:06 +00:00
|
|
|
|
|
|
|
|
// fork so shell thinks we're done and so we're not a process
|
|
|
|
|
// group leader
|
|
|
|
|
switch (fork()) {
|
|
|
|
|
case -1:
|
|
|
|
|
// failed
|
|
|
|
|
throw XArchDaemonFailed(new XArchEvalUnix(errno));
|
|
|
|
|
|
|
|
|
|
case 0:
|
|
|
|
|
// child
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
// parent exits
|
|
|
|
|
exit(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// become leader of a new session
|
|
|
|
|
setsid();
|
|
|
|
|
|
2012-06-10 16:50:54 +00:00
|
|
|
#ifndef __APPLE__
|
2024-07-02 19:07:06 +00:00
|
|
|
// NB: don't run chdir on apple; causes strange behaviour.
|
|
|
|
|
// chdir to root so we don't keep mounted filesystems points busy
|
|
|
|
|
// TODO: this is a bit of a hack - can we find a better solution?
|
|
|
|
|
int chdirErr = chdir("/");
|
|
|
|
|
if (chdirErr)
|
|
|
|
|
// NB: file logging actually isn't working at this point!
|
|
|
|
|
LOG((CLOG_ERR "chdir error: %i", chdirErr));
|
2012-06-10 16:50:54 +00:00
|
|
|
#endif
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
// mask off permissions for any but owner
|
|
|
|
|
umask(077);
|
|
|
|
|
|
|
|
|
|
// close open files. we only expect stdin, stdout, stderr to be open.
|
|
|
|
|
close(0);
|
|
|
|
|
close(1);
|
|
|
|
|
close(2);
|
|
|
|
|
|
|
|
|
|
// attach file descriptors 0, 1, 2 to /dev/null so inadvertent use
|
|
|
|
|
// of standard I/O safely goes in the bit bucket.
|
|
|
|
|
open("/dev/null", O_RDONLY);
|
|
|
|
|
open("/dev/null", O_RDWR);
|
|
|
|
|
|
|
|
|
|
int dupErr = dup(1);
|
|
|
|
|
|
|
|
|
|
if (dupErr < 0) {
|
|
|
|
|
// NB: file logging actually isn't working at this point!
|
|
|
|
|
LOG((CLOG_ERR "dup error: %i", dupErr));
|
|
|
|
|
}
|
|
|
|
|
|
2012-06-10 16:50:54 +00:00
|
|
|
#ifdef __APPLE__
|
2024-07-02 19:07:06 +00:00
|
|
|
return execSelfNonDaemonized();
|
2012-06-10 16:50:54 +00:00
|
|
|
#endif
|
2024-07-02 19:07:06 +00:00
|
|
|
|
|
|
|
|
// invoke function
|
|
|
|
|
return func(1, &name);
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|