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