1af5f1b23a3b4b913ba0c499f22d8a46367e913b
[platform/upstream/libwebsockets.git] / lwsws / main.c
1 /*
2  * libwebsockets web server application
3  *
4  * Copyright (C) 2010-2016 Andy Green <andy@warmcat.com>
5  *
6  * This file is made available under the Creative Commons CC0 1.0
7  * Universal Public Domain Dedication.
8  *
9  * The person who associated a work with this deed has dedicated
10  * the work to the public domain by waiving all of his or her rights
11  * to the work worldwide under copyright law, including all related
12  * and neighboring rights, to the extent allowed by law. You can copy,
13  * modify, distribute and perform the work, even for commercial purposes,
14  * all without asking permission.
15  *
16  * The test apps are intended to be adapted for use in your code, which
17  * may be proprietary.  So unlike the library itself, they are licensed
18  * Public Domain.
19  */
20 #include "lws_config.h"
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <getopt.h>
25 #include <signal.h>
26 #include <string.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29 #include <assert.h>
30 #ifndef _WIN32
31 #include <dirent.h>
32 #include <syslog.h>
33 #include <sys/time.h>
34 #include <unistd.h>
35 #include <sys/wait.h>
36 #else
37 #include <io.h>
38 #include "gettimeofday.h"
39
40 int fork(void)
41 {
42         fprintf(stderr, "Sorry Windows doesn't support fork().\n");
43         return 0;
44 }
45 #endif
46
47 #include "../lib/libwebsockets.h"
48
49 #include <uv.h>
50
51 static struct lws_context *context;
52 static char config_dir[128];
53 static int opts = 0, do_reload = 1;
54 static uv_loop_t loop;
55 static uv_signal_t signal_outer;
56 static int pids[32];
57
58 #define LWSWS_CONFIG_STRING_SIZE (32 * 1024)
59
60 static const struct lws_extension exts[] = {
61         {
62                 "permessage-deflate",
63                 lws_extension_callback_pm_deflate,
64                 "permessage-deflate"
65         },
66         { NULL, NULL, NULL /* terminator */ }
67 };
68
69 static const char * const plugin_dirs[] = {
70         INSTALL_DATADIR"/libwebsockets-test-server/plugins/",
71         NULL
72 };
73
74 static struct option options[] = {
75         { "help",       no_argument,            NULL, 'h' },
76         { "debug",      required_argument,      NULL, 'd' },
77         { "configdir",  required_argument,      NULL, 'c' },
78         { NULL, 0, 0, 0 }
79 };
80
81 void signal_cb(uv_signal_t *watcher, int signum)
82 {
83         switch (watcher->signum) {
84         case SIGTERM:
85         case SIGINT:
86                 break;
87
88         case SIGHUP:
89                 if (lws_context_is_deprecated(context))
90                         return;
91                 lwsl_notice("Dropping listen sockets\n");
92                 lws_context_deprecate(context, NULL);
93                 return;
94
95         default:
96                 signal(SIGABRT, SIG_DFL);
97                 abort();
98                 break;
99         }
100         lwsl_err("Signal %d caught\n", watcher->signum);
101         lws_libuv_stop(context);
102 }
103
104 static int
105 context_creation(void)
106 {
107         int cs_len = LWSWS_CONFIG_STRING_SIZE - 1;
108         struct lws_context_creation_info info;
109         char *cs, *config_strings;
110
111         cs = config_strings = malloc(LWSWS_CONFIG_STRING_SIZE);
112         if (!config_strings) {
113                 lwsl_err("Unable to allocate config strings heap\n");
114                 return -1;
115         }
116
117         memset(&info, 0, sizeof(info));
118
119         info.external_baggage_free_on_destroy = config_strings;
120         info.max_http_header_pool = 16;
121         info.options = opts | LWS_SERVER_OPTION_VALIDATE_UTF8 |
122                               LWS_SERVER_OPTION_EXPLICIT_VHOSTS |
123                               LWS_SERVER_OPTION_LIBUV;
124
125         info.plugin_dirs = plugin_dirs;
126         lwsl_notice("Using config dir: \"%s\"\n", config_dir);
127
128         /*
129          *  first go through the config for creating the outer context
130          */
131         if (lwsws_get_config_globals(&info, config_dir, &cs, &cs_len))
132                 goto init_failed;
133
134         context = lws_create_context(&info);
135         if (context == NULL) {
136                 lwsl_err("libwebsocket init failed\n");
137                 goto init_failed;
138         }
139
140         lws_uv_sigint_cfg(context, 1, signal_cb);
141         lws_uv_initloop(context, &loop, 0);
142
143         /*
144          * then create the vhosts... protocols are entirely coming from
145          * plugins, so we leave it NULL
146          */
147
148         info.extensions = exts;
149
150         if (lwsws_get_config_vhosts(context, &info, config_dir,
151                                     &cs, &cs_len))
152                 return 1;
153
154         return 0;
155
156 init_failed:
157         free(config_strings);
158
159         return 1;
160 }
161
162
163 /*
164  * root-level sighup handler
165  */
166
167 static void
168 reload_handler(int signum)
169 {
170 #ifndef _WIN32
171         int m;
172
173         switch (signum) {
174
175         case SIGHUP: /* reload */
176                 fprintf(stderr, "root process receives reload\n");
177                 if (!do_reload) {
178                         fprintf(stderr, "passing HUP to child processes\n");
179                         for (m = 0; m < ARRAY_SIZE(pids); m++)
180                                 if (pids[m])
181                                         kill(pids[m], SIGHUP);
182                         sleep(1);
183                 }
184                 do_reload = 1;
185                 break;
186         case SIGINT:
187         case SIGTERM:
188         case SIGKILL:
189                 fprintf(stderr, "killing service processes\n");
190                 for (m = 0; m < ARRAY_SIZE(pids); m++)
191                         if (pids[m])
192                                 kill(pids[m], SIGTERM);
193                 exit(0);
194         }
195 #else
196         // kill() implementation needed for WIN32
197 #endif
198 }
199
200 int main(int argc, char **argv)
201 {
202         int n = 0, m, debug_level = 7;
203 #ifndef _WIN32
204         int status, syslog_options = LOG_PID | LOG_PERROR;
205 #endif
206
207         strcpy(config_dir, "/etc/lwsws");
208         while (n >= 0) {
209                 n = getopt_long(argc, argv, "hd:c:", options, NULL);
210                 if (n < 0)
211                         continue;
212                 switch (n) {
213                 case 'd':
214                         debug_level = atoi(optarg);
215                         break;
216                 case 'c':
217                         strncpy(config_dir, optarg, sizeof(config_dir) - 1);
218                         config_dir[sizeof(config_dir) - 1] = '\0';
219                         break;
220                 case 'h':
221                         fprintf(stderr, "Usage: lwsws [-c <config dir>] "
222                                         "[-d <log bitfield>] [-D] [--help]\n");
223                         exit(1);
224                 }
225         }
226 #ifndef _WIN32
227         /*
228          * We leave our original process up permanently, because that
229          * suits systemd.
230          *
231          * Otherwise we get into problems when reload spawns new processes and
232          * the original one dies randomly.
233          */
234
235         signal(SIGHUP, reload_handler);
236         signal(SIGINT, reload_handler);
237
238         fprintf(stderr, "Root process is %u\n", getpid());
239
240         while (1) {
241                 if (do_reload) {
242                         do_reload = 0;
243                         n = fork();
244                         if (n == 0) /* new */
245                                 break;
246                         /* old */
247                         if (n > 0)
248                                 for (m = 0; m < ARRAY_SIZE(pids); m++)
249                                         if (!pids[m]) {
250                                                 // fprintf(stderr, "added child pid %d\n", n);
251                                                 pids[m] = n;
252                                                 break;
253                                         }
254                 }
255 #ifndef _WIN32
256                 sleep(2);
257
258                 n = waitpid(-1, &status, WNOHANG);
259                 if (n > 0)
260                         for (m = 0; m < ARRAY_SIZE(pids); m++)
261                                 if (pids[m] == n) {
262                                         // fprintf(stderr, "reaped child pid %d\n", pids[m]);
263                                         pids[m] = 0;
264                                         break;
265                                 }
266 #else
267 // !!! implemenation needed
268 #endif
269         }
270 #endif
271         /* child process */
272
273 #ifndef _WIN32
274         /* we will only try to log things according to our debug_level */
275         setlogmask(LOG_UPTO (LOG_DEBUG));
276         openlog("lwsws", syslog_options, LOG_DAEMON);
277 #endif
278
279         lws_set_log_level(debug_level, lwsl_emit_syslog);
280
281         lwsl_notice("lwsws libwebsockets web server - license CC0 + LGPL2.1\n");
282         lwsl_notice("(C) Copyright 2010-2016 Andy Green <andy@warmcat.com>\n");
283
284 #if (UV_VERSION_MAJOR > 0) // Travis...
285         uv_loop_init(&loop);
286 #else
287         fprintf(stderr, "Your libuv is too old!\n");
288         return 0;
289 #endif
290         uv_signal_init(&loop, &signal_outer);
291         uv_signal_start(&signal_outer, signal_cb, SIGINT);
292         uv_signal_start(&signal_outer, signal_cb, SIGHUP);
293
294         if (context_creation()) {
295                 lwsl_err("Context creation failed\n");
296                 return 1;
297         }
298
299         lws_libuv_run(context, 0);
300
301         uv_signal_stop(&signal_outer);
302         lws_context_destroy(context);
303
304 #if (UV_VERSION_MAJOR > 0) // Travis...
305         n = 0;
306         while (n++ < 1024 && uv_loop_close(&loop))
307                 uv_run(&loop, UV_RUN_NOWAIT);
308 #endif
309
310         lws_context_destroy2(context);
311
312         fprintf(stderr, "lwsws exited cleanly\n");
313
314 #ifndef _WIN32
315         closelog();
316 #endif
317
318         return 0;
319 }