27 */ |
27 */ |
28 |
28 |
29 |
29 |
30 #include <stdio.h> |
30 #include <stdio.h> |
31 #include <stdlib.h> |
31 #include <stdlib.h> |
|
32 #include <unistd.h> |
|
33 #include <signal.h> |
|
34 #include <errno.h> |
|
35 #include <pthread.h> |
32 |
36 |
33 #include "../util/pool.h" |
37 #include "../util/pool.h" |
34 #include "../public/nsapi.h" |
38 #include "../public/nsapi.h" |
35 #include "../util/plist.h" |
39 #include "../util/plist.h" |
36 |
40 |
37 #include "webserver.h" |
41 #include "webserver.h" |
38 |
42 |
39 #include "httprequest.h" |
43 #include "httprequest.h" |
40 |
44 |
|
45 |
|
46 int std_pipe_fds[2]; |
|
47 |
41 void test() { |
48 void test() { |
42 |
49 |
43 } |
50 } |
44 |
51 |
|
52 /* |
|
53 * SIGUSR1: reload the configuration files |
|
54 */ |
|
55 void sig_usr1_reload(int sig) { |
|
56 printf("reload\n"); |
|
57 |
|
58 signal(SIGUSR1, sig_usr1_reload); |
|
59 } |
|
60 |
|
61 /* |
|
62 * SIGTERM: stop the server |
|
63 */ |
|
64 void sig_term(int sig) { |
|
65 exit(EXIT_SUCCESS); |
|
66 } |
|
67 |
|
68 void* log_pipe_thread(void *data) { |
|
69 FILE *log_out = fopen("log.txt", "a"); |
|
70 |
|
71 char buf[1024]; |
|
72 ssize_t r; |
|
73 while((r = read(std_pipe_fds[0], buf, 1024)) > 0) { |
|
74 fwrite(buf, 1, r, log_out); |
|
75 fflush(log_out); |
|
76 } |
|
77 |
|
78 fclose(log_out); |
|
79 } |
|
80 |
45 int main(int argc, char **argv) { |
81 int main(int argc, char **argv) { |
|
82 //test(); |
|
83 |
|
84 /* if the -c parameter is specified, we don't create a daemon */ |
|
85 int d = 1; |
|
86 for(int i=0;i<argc;i++) { |
|
87 char *p = argv[i]; |
|
88 if(p[0] == '-' && p[1] == 'c') { |
|
89 d = 0; |
|
90 break; |
|
91 } |
|
92 } |
|
93 if(d) { |
|
94 /* create daemon */ |
|
95 pid_t pid = fork(); |
|
96 if(pid < 0) { |
|
97 return EXIT_FAILURE; |
|
98 } else if(pid > 0) { |
|
99 return EXIT_SUCCESS; |
|
100 } |
|
101 |
|
102 if(setsid() < 0) { |
|
103 printf("setsid failed\n"); |
|
104 return EXIT_FAILURE; |
|
105 } |
|
106 |
|
107 for(int i=0;i<3;i++) { |
|
108 close(i); |
|
109 } |
|
110 |
|
111 /* stdio redirection */ |
|
112 /* create pipes */ |
|
113 if(pipe(std_pipe_fds) != 0) { |
|
114 perror("pipe"); |
|
115 return EXIT_FAILURE; |
|
116 } |
|
117 //FILE *ws_out = fdopen(std_pipe_fds[1], "w"); |
|
118 //*stdout = *ws_out; |
|
119 //*stderr = *ws_out; |
|
120 dup2(std_pipe_fds[1], 1); |
|
121 dup2(std_pipe_fds[1], 2); |
|
122 |
|
123 pthread_t tid; |
|
124 pthread_create(&tid, NULL, log_pipe_thread, NULL); |
|
125 } |
|
126 |
46 pool_init(NULL, NULL, NULL); |
127 pool_init(NULL, NULL, NULL); |
47 |
128 |
48 //test(); |
129 /* add signal handler */ |
|
130 signal(SIGUSR1, sig_usr1_reload); |
|
131 signal(SIGTERM, sig_term); |
49 |
132 |
|
133 /* start webserver */ |
50 int status; |
134 int status; |
51 status = webserver_init(); |
135 status = webserver_init(); |
52 if(status != 0) { |
136 if(status != 0) { |
53 fprintf(stderr, "Cannot initialize server!\n"); |
137 fprintf(stderr, "Cannot initialize server!\n"); |
54 return EXIT_FAILURE; |
138 return EXIT_FAILURE; |