1 |
torben |
328 |
/* |
2 |
|
|
* UNIX Damon Server Programming Sample Program |
3 |
|
|
* To compile: cc -o exampled exampled.c |
4 |
|
|
* TO run: ./exampled |
5 |
|
|
* To test daemon: ps aux | grep exampled |
6 |
|
|
* To test log: tail -f /tmp/exampled.log |
7 |
|
|
* To test signal: kill -HUP `cat /tmp/exampled.lock` |
8 |
|
|
* To terminate: kill `cat /tmp/exampled.lock` |
9 |
|
|
*/ |
10 |
|
|
|
11 |
|
|
#include <stdlib.h> |
12 |
|
|
#include <stdio.h> |
13 |
|
|
#include <fcntl.h> |
14 |
|
|
#include <signal.h> |
15 |
|
|
#include <unistd.h> |
16 |
|
|
#include <string.h> |
17 |
|
|
#include <time.h> |
18 |
|
|
#include <sys/types.h> |
19 |
|
|
#include <sys/stat.h> |
20 |
|
|
|
21 |
|
|
#define RUNNING_DIR "/tmp" |
22 |
|
|
#define LOCK_FILE "exampled.lock" |
23 |
|
|
#define LOG_FILE "exampled.log" |
24 |
|
|
static const char *months[] = { |
25 |
|
|
"Jan","Feb","Mar","Apr","May","Jun", |
26 |
|
|
"Jul","Aug","Sep","Oct","Nov","Dec"}; |
27 |
|
|
|
28 |
|
|
void log_message(char *filename, char *message) |
29 |
|
|
{ |
30 |
|
|
FILE *logfile; |
31 |
|
|
char buf[30]; |
32 |
|
|
long n = time(NULL); |
33 |
|
|
struct tm *now; |
34 |
|
|
now = localtime(&n); |
35 |
|
|
|
36 |
|
|
sprintf(buf, "%02d:%02d:%02d %s %02d %02d", |
37 |
|
|
now->tm_hour, now->tm_min, now->tm_sec,months[now->tm_mon], |
38 |
|
|
now->tm_mday,now->tm_year % 100); |
39 |
|
|
|
40 |
|
|
logfile = fopen(filename, "a"); |
41 |
|
|
if (!logfile) |
42 |
|
|
return; |
43 |
|
|
fprintf(logfile, "%s: %s\n", buf, message); |
44 |
|
|
fclose(logfile); |
45 |
|
|
} |
46 |
|
|
|
47 |
|
|
void signal_handler(int sig) |
48 |
|
|
{ |
49 |
|
|
switch(sig) { |
50 |
|
|
case SIGHUP: |
51 |
|
|
log_message(LOG_FILE, "hangup signal catched"); |
52 |
|
|
break; |
53 |
|
|
case SIGTERM: |
54 |
|
|
log_message(LOG_FILE, "terminate signal catched...exiting"); |
55 |
|
|
unlink(LOCK_FILE); |
56 |
|
|
exit(0); |
57 |
|
|
break; |
58 |
|
|
} |
59 |
|
|
} |
60 |
|
|
|
61 |
|
|
void daemonize() |
62 |
|
|
{ |
63 |
|
|
int i, lfp; |
64 |
|
|
char str[10]; |
65 |
|
|
|
66 |
|
|
if (getppid() == 1) /* already a daemon */ |
67 |
|
|
return; |
68 |
|
|
|
69 |
|
|
i=fork(); |
70 |
|
|
|
71 |
|
|
if (i<0) /* fork error */ |
72 |
|
|
exit(1); |
73 |
|
|
if (i>0) /* parent exits */ |
74 |
|
|
exit(0); |
75 |
|
|
/* child daemon continues */ |
76 |
|
|
|
77 |
|
|
setsid(); /* obtain a new process group */ |
78 |
|
|
|
79 |
|
|
for (i=getdtablesize(); i>=0; --i) |
80 |
|
|
close(i); /*close all descriptors*/ |
81 |
|
|
|
82 |
|
|
i=open("/dev/null", O_RDWR); /* handle std. io */ |
83 |
|
|
dup(i); |
84 |
|
|
dup(i); |
85 |
|
|
|
86 |
|
|
umask(027); /* set newly created file permissions */ |
87 |
|
|
|
88 |
|
|
chdir(RUNNING_DIR); /* change running directory*/ |
89 |
|
|
|
90 |
|
|
//attempt to create lockfile and put a file-lock on it |
91 |
|
|
lfp=open(LOCK_FILE,O_RDWR|O_CREAT, 0640); |
92 |
|
|
if (lfp<0) /* can not open */ |
93 |
|
|
exit(1); |
94 |
|
|
if (lockf(lfp,F_TLOCK,0) < 0) /* can not lock */ |
95 |
|
|
exit(0); |
96 |
|
|
|
97 |
|
|
/* first instance continues */ |
98 |
|
|
sprintf(str, "%d\n", getpid() ); /* record pid to lockfile */ |
99 |
|
|
write(lfp, str, strlen(str) ); |
100 |
|
|
signal(SIGCHLD, SIG_IGN); /* ignore child */ |
101 |
|
|
signal(SIGTSTP, SIG_IGN); /* ignore tty signals */ |
102 |
|
|
signal(SIGTTOU, SIG_IGN); |
103 |
|
|
signal(SIGTTIN, SIG_IGN); |
104 |
|
|
signal(SIGHUP, signal_handler); /* catch hangup signal */ |
105 |
|
|
signal(SIGTERM, signal_handler); /* catch kill signal */ |
106 |
|
|
log_message(LOG_FILE, "exampled started ..."); |
107 |
|
|
} |
108 |
|
|
|
109 |
|
|
int main() |
110 |
|
|
{ |
111 |
|
|
daemonize(); |
112 |
|
|
while(1) |
113 |
|
|
sleep(1); /*run*/ |
114 |
|
|
return 0; |
115 |
|
|
} |