1 |
torben |
26 |
#include <iostream> |
2 |
torben |
153 |
#include <sstream> |
3 |
torben |
26 |
#include <signal.h> |
4 |
|
|
#include <fcntl.h> |
5 |
torben |
114 |
#include <stdlib.h> |
6 |
|
|
#include <cstring> |
7 |
torben |
26 |
|
8 |
torben |
146 |
#include <sys/types.h> |
9 |
|
|
#include <pwd.h> |
10 |
|
|
#include <grp.h> |
11 |
|
|
|
12 |
|
|
|
13 |
torben |
152 |
#include "ConfigFile.h" |
14 |
torben |
26 |
#include "daemon.h" |
15 |
|
|
#include "common.h" |
16 |
|
|
|
17 |
|
|
|
18 |
|
|
|
19 |
|
|
using namespace std; |
20 |
|
|
|
21 |
torben |
146 |
|
22 |
torben |
26 |
void signal_handler(int sig) |
23 |
|
|
{ |
24 |
torben |
146 |
switch (sig) |
25 |
|
|
{ |
26 |
|
|
case SIGHUP: |
27 |
|
|
Common::instance()->logMessage("hangup signal catched"); |
28 |
|
|
break; |
29 |
torben |
26 |
|
30 |
torben |
146 |
case SIGTERM: |
31 |
|
|
Common::instance()->logMessage("terminate signal catched...exiting"); |
32 |
|
|
Common::instance()->mainContinue = false; |
33 |
|
|
break; |
34 |
torben |
153 |
default: |
35 |
|
|
std::stringstream ss; |
36 |
|
|
ss << "Received unknown signal: " << sig; |
37 |
|
|
Common::instance()->logMessage(ss.str()); |
38 |
|
|
break; |
39 |
torben |
26 |
} |
40 |
|
|
} |
41 |
|
|
|
42 |
torben |
146 |
void lookup_uid_values() |
43 |
|
|
{ |
44 |
|
|
Common* cmn = Common::instance(); |
45 |
|
|
|
46 |
torben |
152 |
std::string userstr = cmn->GetConfigfile()->GetValue("smsdaemon","user"); |
47 |
|
|
std::string groupstr = cmn->GetConfigfile()->GetValue("smsdaemon","group"); |
48 |
|
|
|
49 |
|
|
passwd* pass = getpwnam(userstr.c_str()); |
50 |
torben |
146 |
if (pass != 0) |
51 |
|
|
{ |
52 |
|
|
cmn->uid = pass->pw_uid; |
53 |
|
|
} |
54 |
|
|
else |
55 |
|
|
{ |
56 |
torben |
152 |
cmn->logMessage( string("could not lookup userid: ") + userstr ); |
57 |
torben |
146 |
exit(1); |
58 |
|
|
} |
59 |
|
|
|
60 |
torben |
155 |
|
61 |
torben |
152 |
group* grp = getgrnam(groupstr.c_str() ); |
62 |
torben |
146 |
if (grp != 0) |
63 |
|
|
{ |
64 |
|
|
cmn->gid = grp->gr_gid; |
65 |
|
|
} |
66 |
|
|
else |
67 |
|
|
{ |
68 |
torben |
152 |
cmn->logMessage( string("could not lookup groupid: ") + groupstr ); |
69 |
torben |
146 |
exit(1); |
70 |
|
|
} |
71 |
|
|
} |
72 |
|
|
|
73 |
|
|
|
74 |
torben |
26 |
void daemonCleanup() |
75 |
|
|
{ |
76 |
|
|
unlink( Common::instance()->pidfile.c_str() ); |
77 |
|
|
} |
78 |
|
|
|
79 |
|
|
void daemonize() |
80 |
torben |
146 |
{ |
81 |
torben |
26 |
int i, lfp; |
82 |
|
|
char str[10]; |
83 |
|
|
Common* cmn = Common::instance(); |
84 |
|
|
|
85 |
|
|
if (getuid() != 0) |
86 |
|
|
{ |
87 |
|
|
cout << "Please start this daemon as root\n" << endl; |
88 |
|
|
daemon_shutdown(0); |
89 |
|
|
} |
90 |
|
|
|
91 |
torben |
146 |
|
92 |
torben |
26 |
if (getppid() == 1) /* already a daemon */ |
93 |
|
|
return; |
94 |
torben |
146 |
|
95 |
torben |
26 |
i=fork(); |
96 |
torben |
146 |
|
97 |
torben |
26 |
if (i<0) /* fork error */ |
98 |
|
|
daemon_shutdown(FORK_ERROR); |
99 |
|
|
if (i>0) /* parent exits */ |
100 |
|
|
daemon_shutdown(0); |
101 |
|
|
/* child daemon continues */ |
102 |
torben |
146 |
|
103 |
torben |
26 |
setsid(); /* obtain a new process group */ |
104 |
torben |
146 |
|
105 |
torben |
26 |
for (i=getdtablesize(); i>=0; --i) |
106 |
|
|
close(i); /*close all descriptors*/ |
107 |
torben |
146 |
|
108 |
torben |
26 |
i=open("/dev/null", O_RDWR); /* handle std. io */ |
109 |
|
|
dup(i); |
110 |
torben |
146 |
dup(i); |
111 |
|
|
|
112 |
torben |
26 |
umask(027); /* set newly created file permissions */ |
113 |
torben |
146 |
|
114 |
torben |
26 |
chdir("/tmp"); /* change running directory*/ |
115 |
torben |
146 |
|
116 |
torben |
26 |
/*attempt to create lockfile and put a file-lock on it*/ |
117 |
|
|
lfp=open(cmn->pidfile.c_str(), O_RDWR|O_CREAT, 0640); |
118 |
|
|
if (lfp<0) /* can not open */ |
119 |
|
|
daemon_shutdown(CANT_OPEN_LOCK); |
120 |
|
|
if (lockf(lfp,F_TLOCK,0) < 0) /* can not lock */ |
121 |
|
|
daemon_shutdown(ALREADY_LOCKED); |
122 |
torben |
146 |
|
123 |
torben |
26 |
/* first instance continues */ |
124 |
|
|
sprintf(str, "%d\n", getpid() ); /* record pid to lockfile */ |
125 |
|
|
write(lfp, str, strlen(str) ); |
126 |
torben |
86 |
//signal(SIGCHLD, SIG_IGN); /* ignore child */ |
127 |
torben |
26 |
signal(SIGTSTP, SIG_IGN); /* ignore tty signals */ |
128 |
|
|
signal(SIGTTOU, SIG_IGN); |
129 |
|
|
signal(SIGTTIN, SIG_IGN); |
130 |
|
|
signal(SIGHUP, signal_handler); /* catch hangup signal */ |
131 |
torben |
146 |
signal(SIGTERM, signal_handler); /* catch kill signal */ |
132 |
torben |
155 |
signal(SIGINT, signal_handler); /* keyboard interrupt */ |
133 |
|
|
signal(SIGQUIT, signal_handler); /* QUIT */ |
134 |
torben |
26 |
|
135 |
|
|
/*this program is now a daemon*/ |
136 |
|
|
setegid( cmn->gid ); |
137 |
|
|
seteuid( cmn->uid ); |
138 |
|
|
} |