1 |
torben |
26 |
#include <iostream> |
2 |
|
|
|
3 |
|
|
#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 |
26 |
} |
35 |
|
|
} |
36 |
|
|
|
37 |
torben |
146 |
void lookup_uid_values() |
38 |
|
|
{ |
39 |
|
|
Common* cmn = Common::instance(); |
40 |
|
|
|
41 |
torben |
152 |
std::string userstr = cmn->GetConfigfile()->GetValue("smsdaemon","user"); |
42 |
|
|
std::string groupstr = cmn->GetConfigfile()->GetValue("smsdaemon","group"); |
43 |
|
|
|
44 |
|
|
passwd* pass = getpwnam(userstr.c_str()); |
45 |
torben |
146 |
if (pass != 0) |
46 |
|
|
{ |
47 |
|
|
cmn->uid = pass->pw_uid; |
48 |
|
|
} |
49 |
|
|
else |
50 |
|
|
{ |
51 |
torben |
152 |
cmn->logMessage( string("could not lookup userid: ") + userstr ); |
52 |
torben |
146 |
exit(1); |
53 |
|
|
} |
54 |
|
|
|
55 |
torben |
152 |
group* grp = getgrnam(groupstr.c_str() ); |
56 |
torben |
146 |
if (grp != 0) |
57 |
|
|
{ |
58 |
|
|
cmn->gid = grp->gr_gid; |
59 |
|
|
} |
60 |
|
|
else |
61 |
|
|
{ |
62 |
torben |
152 |
cmn->logMessage( string("could not lookup groupid: ") + groupstr ); |
63 |
torben |
146 |
exit(1); |
64 |
|
|
} |
65 |
|
|
} |
66 |
|
|
|
67 |
|
|
|
68 |
torben |
26 |
void daemonCleanup() |
69 |
|
|
{ |
70 |
|
|
unlink( Common::instance()->pidfile.c_str() ); |
71 |
|
|
} |
72 |
|
|
|
73 |
|
|
void daemonize() |
74 |
torben |
146 |
{ |
75 |
torben |
26 |
int i, lfp; |
76 |
|
|
char str[10]; |
77 |
|
|
Common* cmn = Common::instance(); |
78 |
|
|
|
79 |
|
|
if (getuid() != 0) |
80 |
|
|
{ |
81 |
|
|
cout << "Please start this daemon as root\n" << endl; |
82 |
|
|
daemon_shutdown(0); |
83 |
|
|
} |
84 |
|
|
|
85 |
torben |
146 |
|
86 |
torben |
26 |
if (getppid() == 1) /* already a daemon */ |
87 |
|
|
return; |
88 |
torben |
146 |
|
89 |
torben |
26 |
i=fork(); |
90 |
torben |
146 |
|
91 |
torben |
26 |
if (i<0) /* fork error */ |
92 |
|
|
daemon_shutdown(FORK_ERROR); |
93 |
|
|
if (i>0) /* parent exits */ |
94 |
|
|
daemon_shutdown(0); |
95 |
|
|
/* child daemon continues */ |
96 |
torben |
146 |
|
97 |
torben |
26 |
setsid(); /* obtain a new process group */ |
98 |
torben |
146 |
|
99 |
torben |
26 |
for (i=getdtablesize(); i>=0; --i) |
100 |
|
|
close(i); /*close all descriptors*/ |
101 |
torben |
146 |
|
102 |
torben |
26 |
i=open("/dev/null", O_RDWR); /* handle std. io */ |
103 |
|
|
dup(i); |
104 |
torben |
146 |
dup(i); |
105 |
|
|
|
106 |
torben |
26 |
umask(027); /* set newly created file permissions */ |
107 |
torben |
146 |
|
108 |
torben |
26 |
chdir("/tmp"); /* change running directory*/ |
109 |
torben |
146 |
|
110 |
torben |
26 |
/*attempt to create lockfile and put a file-lock on it*/ |
111 |
|
|
lfp=open(cmn->pidfile.c_str(), O_RDWR|O_CREAT, 0640); |
112 |
|
|
if (lfp<0) /* can not open */ |
113 |
|
|
daemon_shutdown(CANT_OPEN_LOCK); |
114 |
|
|
if (lockf(lfp,F_TLOCK,0) < 0) /* can not lock */ |
115 |
|
|
daemon_shutdown(ALREADY_LOCKED); |
116 |
torben |
146 |
|
117 |
torben |
26 |
/* first instance continues */ |
118 |
|
|
sprintf(str, "%d\n", getpid() ); /* record pid to lockfile */ |
119 |
|
|
write(lfp, str, strlen(str) ); |
120 |
torben |
86 |
//signal(SIGCHLD, SIG_IGN); /* ignore child */ |
121 |
torben |
26 |
signal(SIGTSTP, SIG_IGN); /* ignore tty signals */ |
122 |
|
|
signal(SIGTTOU, SIG_IGN); |
123 |
|
|
signal(SIGTTIN, SIG_IGN); |
124 |
|
|
signal(SIGHUP, signal_handler); /* catch hangup signal */ |
125 |
torben |
146 |
signal(SIGTERM, signal_handler); /* catch kill signal */ |
126 |
torben |
26 |
|
127 |
|
|
/*this program is now a daemon*/ |
128 |
|
|
setegid( cmn->gid ); |
129 |
|
|
seteuid( cmn->uid ); |
130 |
|
|
} |