2002-08-02 19:57:46 +00:00
|
|
|
/*
|
|
|
|
* synergy -- mouse and keyboard sharing utility
|
|
|
|
* Copyright (C) 2002 Chris Schoeneman
|
|
|
|
*
|
|
|
|
* This package is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* found in the file COPYING that should have accompanied this file.
|
|
|
|
*
|
|
|
|
* This package is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*/
|
|
|
|
|
2002-06-04 12:26:23 +00:00
|
|
|
#include "CUnixPlatform.h"
|
|
|
|
#include "CLog.h"
|
2002-06-10 22:06:45 +00:00
|
|
|
#include <cstring>
|
2002-06-04 12:26:23 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <syslog.h>
|
2002-06-19 12:21:26 +00:00
|
|
|
|
2002-06-04 12:26:23 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// CUnixPlatform
|
|
|
|
//
|
|
|
|
|
|
|
|
CUnixPlatform::CUnixPlatform()
|
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
CUnixPlatform::~CUnixPlatform()
|
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
bool
|
2002-09-02 17:30:04 +00:00
|
|
|
CUnixPlatform::installDaemon(const char*, const char*,
|
|
|
|
const char*, const char*, bool)
|
2002-06-04 12:26:23 +00:00
|
|
|
{
|
|
|
|
// daemons don't require special installation
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2002-06-14 18:08:20 +00:00
|
|
|
CUnixPlatform::EResult
|
2002-09-02 17:30:04 +00:00
|
|
|
CUnixPlatform::uninstallDaemon(const char*, bool)
|
2002-06-04 12:26:23 +00:00
|
|
|
{
|
|
|
|
// daemons don't require special installation
|
2002-06-14 18:08:20 +00:00
|
|
|
return kSuccess;
|
2002-06-04 12:26:23 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
int
|
2002-06-17 13:31:21 +00:00
|
|
|
CUnixPlatform::daemonize(const char* name, DaemonFunc func)
|
2002-06-04 12:26:23 +00:00
|
|
|
{
|
|
|
|
// fork so shell thinks we're done and so we're not a process
|
|
|
|
// group leader
|
|
|
|
switch (fork()) {
|
|
|
|
case -1:
|
|
|
|
// failed
|
2002-06-08 21:48:00 +00:00
|
|
|
return -1;
|
2002-06-04 12:26:23 +00: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
|
2002-06-14 18:08:20 +00:00
|
|
|
installDaemonLogger(name);
|
2002-06-04 12:26:23 +00:00
|
|
|
|
2002-06-08 21:48:00 +00:00
|
|
|
// invoke function
|
|
|
|
return func(this, 1, &name);
|
|
|
|
}
|
|
|
|
|
2002-06-14 18:08:20 +00:00
|
|
|
void
|
2002-06-17 13:31:21 +00:00
|
|
|
CUnixPlatform::installDaemonLogger(const char* name)
|
2002-06-14 18:08:20 +00:00
|
|
|
{
|
|
|
|
openlog(name, 0, LOG_DAEMON);
|
|
|
|
CLog::setOutputter(&CUnixPlatform::deamonLogger);
|
|
|
|
}
|
|
|
|
|
2002-09-02 17:30:04 +00:00
|
|
|
bool
|
|
|
|
CUnixPlatform::canInstallDaemon(const char*, bool) const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
CUnixPlatform::isDaemonInstalled(const char*, bool) const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
const char*
|
2002-06-17 13:31:21 +00:00
|
|
|
CUnixPlatform::getBasename(const char* pathname) const
|
2002-06-04 12:26:23 +00:00
|
|
|
{
|
|
|
|
if (pathname == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* basename = strrchr(pathname, '/');
|
|
|
|
if (basename != NULL) {
|
|
|
|
return basename + 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return pathname;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
CString
|
|
|
|
CUnixPlatform::getUserDirectory() const
|
2002-06-04 12:26:23 +00:00
|
|
|
{
|
2002-06-19 11:58:48 +00:00
|
|
|
#if HAVE_GETPWUID_R
|
|
|
|
struct passwd pwent;
|
|
|
|
struct passwd* pwentp;
|
2002-11-03 18:09:28 +00:00
|
|
|
#if defined(_SC_GETPW_R_SIZE_MAX)
|
2002-06-19 11:58:48 +00:00
|
|
|
long size = sysconf(_SC_GETPW_R_SIZE_MAX);
|
2002-11-03 18:09:28 +00:00
|
|
|
#else
|
|
|
|
long size = BUFSIZ;
|
|
|
|
#endif
|
2002-06-19 11:58:48 +00:00
|
|
|
char* buffer = new char[size];
|
|
|
|
getpwuid_r(getuid(), &pwent, buffer, size, &pwentp);
|
|
|
|
delete[] buffer;
|
|
|
|
#else
|
|
|
|
struct passwd* pwentp = getpwuid(getuid());
|
|
|
|
#endif
|
|
|
|
if (pwentp != NULL && pwentp->pw_dir != NULL) {
|
|
|
|
return pwentp->pw_dir;
|
2002-06-04 12:26:23 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return CString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
CString
|
|
|
|
CUnixPlatform::getSystemDirectory() const
|
2002-06-04 12:26:23 +00:00
|
|
|
{
|
|
|
|
return "/etc";
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
CString
|
2002-06-17 13:31:21 +00:00
|
|
|
CUnixPlatform::addPathComponent(const CString& prefix,
|
|
|
|
const CString& suffix) const
|
2002-06-04 12:26:23 +00:00
|
|
|
{
|
|
|
|
CString path;
|
|
|
|
path.reserve(prefix.size() + 1 + suffix.size());
|
|
|
|
path += prefix;
|
2002-06-08 21:48:00 +00:00
|
|
|
if (path.size() == 0 || path[path.size() - 1] != '/') {
|
|
|
|
path += '/';
|
|
|
|
}
|
2002-06-04 12:26:23 +00:00
|
|
|
path += suffix;
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
bool
|
2002-06-17 13:31:21 +00:00
|
|
|
CUnixPlatform::deamonLogger(int priority, const char* msg)
|
2002-06-04 12:26:23 +00: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-08 21:48:00 +00:00
|
|
|
return true;
|
2002-06-04 12:26:23 +00:00
|
|
|
}
|