/[projects]/smsdaemon/daemon.cpp
ViewVC logotype

Contents of /smsdaemon/daemon.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 152 - (show annotations) (download)
Mon Dec 8 11:30:07 2008 UTC (15 years, 5 months ago) by torben
File size: 2621 byte(s)
Make it possible to configure which user the daemon should run as

1 #include <iostream>
2
3 #include <signal.h>
4 #include <fcntl.h>
5 #include <stdlib.h>
6 #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"
15 #include "common.h"
16
17
18
19 using namespace std;
20
21
22 void signal_handler(int sig)
23 {
24 switch (sig)
25 {
26 case SIGHUP:
27 Common::instance()->logMessage("hangup signal catched");
28 break;
29
30 case SIGTERM:
31 Common::instance()->logMessage("terminate signal catched...exiting");
32 Common::instance()->mainContinue = false;
33 break;
34 }
35 }
36
37 void lookup_uid_values()
38 {
39 Common* cmn = Common::instance();
40
41 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 if (pass != 0)
46 {
47 cmn->uid = pass->pw_uid;
48 }
49 else
50 {
51 cmn->logMessage( string("could not lookup userid: ") + userstr );
52 exit(1);
53 }
54
55 group* grp = getgrnam(groupstr.c_str() );
56 if (grp != 0)
57 {
58 cmn->gid = grp->gr_gid;
59 }
60 else
61 {
62 cmn->logMessage( string("could not lookup groupid: ") + groupstr );
63 exit(1);
64 }
65 }
66
67
68 void daemonCleanup()
69 {
70 unlink( Common::instance()->pidfile.c_str() );
71 }
72
73 void daemonize()
74 {
75 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
86 if (getppid() == 1) /* already a daemon */
87 return;
88
89 i=fork();
90
91 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
97 setsid(); /* obtain a new process group */
98
99 for (i=getdtablesize(); i>=0; --i)
100 close(i); /*close all descriptors*/
101
102 i=open("/dev/null", O_RDWR); /* handle std. io */
103 dup(i);
104 dup(i);
105
106 umask(027); /* set newly created file permissions */
107
108 chdir("/tmp"); /* change running directory*/
109
110 /*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
117 /* first instance continues */
118 sprintf(str, "%d\n", getpid() ); /* record pid to lockfile */
119 write(lfp, str, strlen(str) );
120 //signal(SIGCHLD, SIG_IGN); /* ignore child */
121 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 signal(SIGTERM, signal_handler); /* catch kill signal */
126
127 /*this program is now a daemon*/
128 setegid( cmn->gid );
129 seteuid( cmn->uid );
130 }

  ViewVC Help
Powered by ViewVC 1.1.20