1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
|
/*
* Dibbler - a portable DHCPv6
*
* authors: Tomasz Mrugalski <thomson@klub.com.pl>
* Marek Senderski <msend@o2.pl>
* changes: Michal Kowalczuk <michal@kowalczuk.eu>
*
* released under GNU GPL v2 only licence
*
* $Id: daemon.cpp,v 1.14 2008-11-13 22:29:55 thomson Exp $
*
*/
#include <iostream>
#include <string>
#include <fstream>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
#include <linux/ioctl.h>
#include <sys/stat.h>
#include <errno.h>
#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "Portable.h"
#include "Logger.h"
extern int status();
extern int run();
using namespace std;
/**
* checks if pid file exists, and returns its content (or -2 if unable to read)
*
* @param file
*
* @return pid value, -1 when file is not found and -2 when unable to read
*/
pid_t getPID(const char * file) {
/* check if the file exists */
struct stat buf;
int i = stat(file, &buf);
if (i!=0)
return -1;
ifstream pidfile(file);
if (!pidfile.is_open())
return -2;
pid_t pid;
pidfile >> pid;
return pid;
}
pid_t getClientPID() {
return getPID(CLNTPID_FILE);
}
pid_t getServerPID() {
return getPID(SRVPID_FILE);
}
pid_t getRelayPID() {
return getPID(RELPID_FILE);
}
void daemon_init() {
//FIXME: daemon should close all open files
//fclose(stdin);
//fclose(stdout);
//fclose(stderr);
pid_t childpid;
cout << "Starting daemon..." << endl;
logger::EchoOff();
if (getppid()!=1) {
#ifdef SIGTTOU
signal(SIGTTOU, SIG_IGN);
#endif
#ifdef SIGTTIN
signal(SIGTTIN, SIG_IGN);
#endif
#ifdef SIGTSTP
signal(SIGTSTP, SIG_IGN);
#endif
if ( (childpid = fork()) <0 ) {
Log(Crit) << "Can't fork first child." << endl;
return;
} else if (childpid > 0)
exit(0); // parent process
if (setpgrp() == -1) {
Log(Crit) << "Can't change process group." << endl;
return;
}
signal( SIGHUP, SIG_IGN);
if ( (childpid = fork()) <0) {
cout << "Can't fork second child." << endl;
return;
} else if (childpid > 0)
exit(0); // first child
} // getppid()!=1
umask(DEFAULT_UMASK);
}
void daemon_die() {
logger::Terminate();
logger::EchoOn();
}
int init(const char * pidfile, const char * workdir) {
string tmp;
/*FIXME: buf needs to fit "/proc/%d/exe", where %d is pid_t
* (on my system it's 20 B exactly with positive PID. However this is not
* portable.) */
char buf[20];
char cmd[256];
pid_t pid = getPID(pidfile);
if (pid != -1) {
/* XXX: ISO C++ doesn't support 'j' length modifier nor 'll' nor
* PRIdMAX macro. So, long int is the biggest printable type.
* God bless pid_t to fit into long int. */
if (snprintf(buf, sizeof(buf), "/proc/%ld", (long int)pid)
>= (int)sizeof(buf)) {
Log(Crit) << "Buffer for string `/proc/" << pid << "' too small"
<< LogEnd;
return 0;
};
if (!access(buf, F_OK)) {
if(snprintf(buf, sizeof(buf), "/proc/%ld/exe", (long int)pid)
>= (int)sizeof(buf)) {
Log(Crit) << "Buffer for string `/proc/" << pid << "/exe' too small"
<< LogEnd;
return 0;
}
ssize_t len=readlink(buf, cmd, sizeof(cmd));
if(len!=-1) {
cmd[len]=0;
if(strstr(cmd, "dibbler")==NULL) {
Log(Warning) << "Process is running but it is not Dibbler (pid=" << pid
<< ", name " << cmd << ")." << LogEnd;
} else {
Log(Crit) << "Process already running and it seems to be Dibbler (pid="
<< pid << ", name " << cmd << ")." << LogEnd;
return 0;
}
} else {
Log(Crit) << "Process already running (pid=" << pid << ", file " << pidfile
<< " is present)." << LogEnd;
return 0;
}
} else {
Log(Warning) << "Pid file found (pid=" << pid << ", file " << pidfile
<< "), but process " << pid << " does not exist." << LogEnd;
}
}
unlink(pidfile);
ofstream pidFile(pidfile);
if (!pidFile.is_open()) {
Log(Crit) << "Unable to create " << pidfile << " file." << LogEnd;
return 0;
}
pidFile << getpid();
pidFile.close();
Log(Notice) << "My pid (" << getpid() << ") is stored in " << pidfile << LogEnd;
if (chdir(workdir)) {
Log(Crit) << "Unable to change directory to " << workdir << "." << LogEnd;
return 0;
}
umask(DEFAULT_UMASK);
return 1;
}
void die(const char * pidfile) {
if (unlink(pidfile)) {
Log(Warning) << "Unable to delete " << pidfile << "." << LogEnd;
}
}
int start(const char * pidfile, const char * workdir) {
int result;
daemon_init();
result = run();
daemon_die();
return result;
}
int stop(const char * pidfile) {
int saved_errno;
int ptrace_failed, p_status;
pid_t pid = getPID(pidfile);
if (pid==-1) {
cout << "Process is not running." << endl;
return -1;
}
if (pid==-2) {
cout << "Unable to read file " << pidfile << ". Are you running as root?" << endl;
return -1;
}
ptrace_failed = ptrace(PTRACE_ATTACH, pid, NULL, NULL);
if (ptrace_failed) {
saved_errno = errno;
cout << "Attaching to process " << pid << " failed: "
<< strerror(saved_errno) << endl;
cout << "Warning: Can not guarantee for remote process termination" << endl;
}
cout << "Sending TERM signal to process " << pid << endl;
if (-1 == kill(pid, SIGTERM)) {
saved_errno = errno;
cout << "Signal sending failed: " << strerror(saved_errno) << endl;
if (!ptrace_failed) ptrace(PTRACE_DETACH, pid, NULL, NULL);
return -1;
}
if (!ptrace_failed) {
cout << "Waiting for signalled process termination... " << flush;
do {
if (-1 == waitpid(pid, &p_status, 0)) {
saved_errno = errno;
cout << "Failed: " << strerror(saved_errno) << endl;
ptrace(PTRACE_DETACH, pid, NULL, NULL);
return -1;
}
ptrace(PTRACE_CONT, pid, NULL,
WIFSTOPPED(p_status) ? WSTOPSIG(p_status) : NULL);
} while (! (WIFEXITED(p_status) || WIFSIGNALED(p_status)) );
cout << "Done." << endl;
}
return 0;
}
int install() {
return 0;
}
int uninstall() {
return 0;
}
/** things to do just after started */
void logStart(const char * note, const char * logname, const char * logfile) {
std::cout << DIBBLER_COPYRIGHT1 << " " << note << std::endl;
std::cout << DIBBLER_COPYRIGHT2 << std::endl;
std::cout << DIBBLER_COPYRIGHT3 << std::endl;
std::cout << DIBBLER_COPYRIGHT4 << std::endl;
logger::setLogName(logname);
logger::Initialize(logfile);
logger::EchoOff();
Log(Notice) << DIBBLER_COPYRIGHT1 << " " << note << LogEnd;
logger::EchoOn();
}
/** things to do just before end
*/
void logEnd() {
logger::Terminate();
}
|