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
|
/*
* 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.3 2008-11-13 22:29:55 thomson Exp $
*
*/
#include <iostream>
#include <string>
#include <fstream>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include "Portable.h"
#include "Logger.h"
extern int status();
extern int run();
using namespace std;
int getPID(const char * file) {
ifstream pidfile(file);
if (!pidfile.is_open())
return -1;
int pid;
pidfile >> pid;
return pid;
}
int getClientPID() {
return getPID(CLNTPID_FILE);
}
int getServerPID() {
return getPID(SRVPID_FILE);
}
int getRelayPID() {
return getPID(RELPID_FILE);
}
void daemon_init() {
//FIXME: daemon should close all open files
//fclose(stdin);
//fclose(stdout);
//fclose(stderr);
int 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 0
// TODO: daemon spawning in Mac OS is done a bit differently
if (setpgrp(0) == -1) {
Log(Crit) << "Can't change process group." << endl;
return;
}
#endif
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;
char buf[20];
char cmd[256];
int pid = getPID(pidfile);
if (pid != -1) {
sprintf(buf,"/proc/%d", pid);
if (!access(buf, F_OK)) {
sprintf(buf, "/proc/%d/exe", pid);
int 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 pid = getPID(pidfile);
if (pid==-1) {
cout << "Process is not running." << endl;
return -1;
}
cout << "Sending KILL signal to process " << pid << endl;
kill(pid, SIGTERM);
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();
}
|