Line 0
Link Here
|
|
|
1 |
|
2 |
$FreeBSD$ |
3 |
|
4 |
--- src/main.c.orig |
5 |
+++ src/main.c |
6 |
@@ -1,6 +1,5 @@ |
7 |
#include "main.h" |
8 |
#include "log.h" |
9 |
- |
10 |
apr_pool_t *masterPool; |
11 |
struct configuration *config; |
12 |
|
13 |
@@ -14,8 +13,9 @@ |
14 |
|
15 |
void usage(char *prog_name) |
16 |
{ |
17 |
- fprintf(stderr, "%s [-h] [-c /etc/ramond.conf]\n", prog_name); |
18 |
+ fprintf(stderr, "%s [-h] [-d] [-c /etc/ramond.conf]\n", prog_name); |
19 |
fprintf(stderr, " -h : print this help.\n"); |
20 |
+ fprintf(stderr, " -d : do not daemonize.\n"); |
21 |
fprintf(stderr, " -c : path to config file.\n"); |
22 |
} |
23 |
|
24 |
@@ -824,11 +824,68 @@ |
25 |
pcap_close(fd); |
26 |
} |
27 |
|
28 |
+/** |
29 |
+ * daemonize ramond. |
30 |
+ */ |
31 |
+void daemonize(void) |
32 |
+{ |
33 |
+ pid_t pid, sid; |
34 |
+ int i, pidfile; |
35 |
+ |
36 |
+ char pidstr[32]; |
37 |
+ |
38 |
+ pid = fork(); |
39 |
+ |
40 |
+ if(pid < 0) |
41 |
+ exit(EXIT_FAILURE); |
42 |
+ else if(pid > 0) |
43 |
+ exit(EXIT_SUCCESS); |
44 |
+ |
45 |
+ umask(027); |
46 |
+ if((chdir("/")) < 0) |
47 |
+ exit(EXIT_FAILURE); |
48 |
+ |
49 |
+ sid = setsid(); |
50 |
+ if(sid < 0) |
51 |
+ exit(EXIT_FAILURE); |
52 |
+ |
53 |
+ pid = fork(); |
54 |
+ |
55 |
+ if(pid < 0) |
56 |
+ exit(EXIT_FAILURE); |
57 |
+ else if(pid > 0) |
58 |
+ exit(EXIT_SUCCESS); |
59 |
+ |
60 |
+ /* Cleanup open FDs */ |
61 |
+ for(i = getdtablesize(); i>=0; --i) |
62 |
+ close(i); |
63 |
+ |
64 |
+ i = open("/dev/null", O_RDWR); /* (re)open stdin */ |
65 |
+ dup(i); /* stdout */ |
66 |
+ dup(i); /* stderr */ |
67 |
+ |
68 |
+ pidfile = open("/var/run/ramond.pid", O_RDWR|O_CREAT, 0640); |
69 |
+ if(pidfile < 0) |
70 |
+ exit(EXIT_FAILURE); |
71 |
+ if(flock(pidfile, F_TLOCK) < 0) |
72 |
+ exit(EXIT_SUCCESS); |
73 |
+ |
74 |
+ sprintf(pidstr, "%d\n", getpid()); |
75 |
+ write(pidfile, pidstr, strlen(pidstr)); |
76 |
+ |
77 |
+ signal(SIGTSTP,SIG_IGN); /* ignore tty signals */ |
78 |
+ signal(SIGTTOU,SIG_IGN); |
79 |
+ signal(SIGTTIN,SIG_IGN); |
80 |
+} |
81 |
+ |
82 |
int main(int argc, char *argv[]) |
83 |
{ |
84 |
int socket; |
85 |
struct ra_info data; |
86 |
|
87 |
+ int debug = 0; |
88 |
+ int i = 0; |
89 |
+ |
90 |
if(argc > 6) |
91 |
{ |
92 |
usage(argv[0]); |
93 |
@@ -842,6 +899,20 @@ |
94 |
|
95 |
signal(SIGCHLD, sigchld_handler); |
96 |
|
97 |
+ for(i = 0; i < argc; i++) |
98 |
+ { |
99 |
+ if(!strcmp(argv[i], "-d")) |
100 |
+ { |
101 |
+ debug = 1; |
102 |
+ break; |
103 |
+ } |
104 |
+ } |
105 |
+ |
106 |
+ if(!debug) |
107 |
+ { |
108 |
+ daemonize(); |
109 |
+ } |
110 |
+ |
111 |
/* Find the config file */ |
112 |
if(!parseConfigFile(argc,argv)) |
113 |
{ |