2002-06-04 16:26:23 +04:00
|
|
|
#include "CUnixPlatform.h"
|
|
|
|
#include "CLog.h"
|
2002-06-11 02:06:45 +04:00
|
|
|
#include <cstring>
|
2002-06-04 16:26:23 +04:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2002-06-09 01:48:00 +04:00
|
|
|
#include <sys/wait.h>
|
2002-06-04 16:26:23 +04:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
|
|
|
|
//
|
|
|
|
// CUnixPlatform
|
|
|
|
//
|
|
|
|
|
|
|
|
CUnixPlatform::CUnixPlatform()
|
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
CUnixPlatform::~CUnixPlatform()
|
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
bool
|
|
|
|
CUnixPlatform::installDaemon(
|
|
|
|
const char*,
|
|
|
|
const char*,
|
|
|
|
const char*,
|
|
|
|
const char*)
|
2002-06-04 16:26:23 +04:00
|
|
|
{
|
|
|
|
// daemons don't require special installation
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
bool
|
|
|
|
CUnixPlatform::uninstallDaemon(
|
|
|
|
const char*)
|
2002-06-04 16:26:23 +04:00
|
|
|
{
|
|
|
|
// daemons don't require special installation
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
int
|
|
|
|
CUnixPlatform::daemonize(
|
|
|
|
const char* name,
|
|
|
|
DaemonFunc func)
|
2002-06-04 16:26:23 +04:00
|
|
|
{
|
|
|
|
// fork so shell thinks we're done and so we're not a process
|
|
|
|
// group leader
|
|
|
|
switch (fork()) {
|
|
|
|
case -1:
|
|
|
|
// failed
|
2002-06-09 01:48:00 +04:00
|
|
|
return -1;
|
2002-06-04 16:26:23 +04:00
|
|
|
|
|
|
|
case 0:
|
|
|
|
// child
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// parent exits
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// become leader of a new session
|
|
|
|
setsid();
|
|
|
|
|
|
|
|
// chdir to root so we don't keep mounted filesystems points busy
|
|
|
|
chdir("/");
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
dup(1);
|
|
|
|
|
|
|
|
// hook up logger
|
|
|
|
setDaemonLogger(name);
|
|
|
|
|
2002-06-09 01:48:00 +04:00
|
|
|
// invoke function
|
|
|
|
return func(this, 1, &name);
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
int
|
|
|
|
CUnixPlatform::restart(
|
|
|
|
RestartFunc func,
|
|
|
|
int minErrorCode)
|
2002-06-09 01:48:00 +04:00
|
|
|
{
|
|
|
|
for (;;) {
|
|
|
|
switch (fork()) {
|
2002-06-11 02:06:45 +04:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
// parent process. wait for child to exit.
|
|
|
|
int status;
|
|
|
|
if (wait(&status) == -1) {
|
|
|
|
// wait failed. this is unexpected so bail.
|
|
|
|
log((CLOG_CRIT "wait() failed"));
|
|
|
|
return minErrorCode;
|
|
|
|
}
|
2002-06-09 01:48:00 +04:00
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
// what happened? if the child exited normally with a
|
|
|
|
// status less than 16 then the child was deliberately
|
|
|
|
// terminated so we also terminate.
|
|
|
|
if (WIFEXITED(status) && WEXITSTATUS(status) < minErrorCode) {
|
|
|
|
return WEXITSTATUS(status);
|
|
|
|
}
|
2002-06-09 20:53:57 +04:00
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
// did child die horribly?
|
|
|
|
if (WIFSIGNALED(status)) {
|
|
|
|
switch (WTERMSIG(status)) {
|
|
|
|
case SIGHUP:
|
|
|
|
case SIGINT:
|
|
|
|
case SIGQUIT:
|
|
|
|
case SIGTERM:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// uh oh. bail out.
|
|
|
|
return 16;
|
|
|
|
}
|
2002-06-09 20:53:57 +04:00
|
|
|
}
|
|
|
|
}
|
2002-06-09 01:48:00 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case -1:
|
|
|
|
// fork() failed. log the error and proceed as a child
|
|
|
|
log((CLOG_WARN "fork() failed; cannot automatically restart on error"));
|
|
|
|
// fall through
|
|
|
|
|
|
|
|
case 0:
|
|
|
|
// child process
|
|
|
|
return func();
|
|
|
|
}
|
|
|
|
}
|
2002-06-04 16:26:23 +04:00
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
const char*
|
|
|
|
CUnixPlatform::getBasename(
|
|
|
|
const char* pathname) const
|
2002-06-04 16:26:23 +04:00
|
|
|
{
|
|
|
|
if (pathname == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* basename = strrchr(pathname, '/');
|
|
|
|
if (basename != NULL) {
|
|
|
|
return basename + 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return pathname;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
CString
|
|
|
|
CUnixPlatform::getUserDirectory() const
|
2002-06-04 16:26:23 +04:00
|
|
|
{
|
|
|
|
// FIXME -- use geteuid? shouldn't run this setuid anyway.
|
|
|
|
struct passwd* pwent = getpwuid(getuid());
|
|
|
|
if (pwent != NULL && pwent->pw_dir != NULL) {
|
|
|
|
return pwent->pw_dir;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return CString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
CString
|
|
|
|
CUnixPlatform::getSystemDirectory() const
|
2002-06-04 16:26:23 +04:00
|
|
|
{
|
|
|
|
return "/etc";
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
CString
|
|
|
|
CUnixPlatform::addPathComponent(
|
|
|
|
const CString& prefix,
|
|
|
|
const CString& suffix) const
|
2002-06-04 16:26:23 +04:00
|
|
|
{
|
|
|
|
CString path;
|
|
|
|
path.reserve(prefix.size() + 1 + suffix.size());
|
|
|
|
path += prefix;
|
2002-06-09 01:48:00 +04:00
|
|
|
if (path.size() == 0 || path[path.size() - 1] != '/') {
|
|
|
|
path += '/';
|
|
|
|
}
|
2002-06-04 16:26:23 +04:00
|
|
|
path += suffix;
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
void
|
|
|
|
CUnixPlatform::setDaemonLogger(
|
|
|
|
const char* name)
|
2002-06-04 16:26:23 +04:00
|
|
|
{
|
|
|
|
openlog(name, 0, LOG_DAEMON);
|
|
|
|
CLog::setOutputter(&CUnixPlatform::deamonLogger);
|
|
|
|
}
|
|
|
|
|
2002-06-11 02:06:45 +04:00
|
|
|
bool
|
|
|
|
CUnixPlatform::deamonLogger(
|
|
|
|
int priority,
|
|
|
|
const char* msg)
|
2002-06-04 16:26:23 +04:00
|
|
|
{
|
|
|
|
// convert priority
|
|
|
|
switch (priority) {
|
|
|
|
case CLog::kFATAL:
|
|
|
|
case CLog::kERROR:
|
|
|
|
priority = LOG_ERR;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CLog::kWARNING:
|
|
|
|
priority = LOG_WARNING;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CLog::kNOTE:
|
|
|
|
priority = LOG_NOTICE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CLog::kINFO:
|
|
|
|
priority = LOG_INFO;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
priority = LOG_DEBUG;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// log it
|
|
|
|
syslog(priority, "%s", msg);
|
2002-06-09 01:48:00 +04:00
|
|
|
return true;
|
2002-06-04 16:26:23 +04:00
|
|
|
}
|