6f367719c705c9a58764f076c14f7db81c0d95a6
[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, debug_level = 7;
203 #ifndef _WIN32
204         int m;
205         int status, syslog_options = LOG_PID | LOG_PERROR;
206 #endif
207
208         strcpy(config_dir, "/etc/lwsws");
209         while (n >= 0) {
210                 n = getopt_long(argc, argv, "hd:c:", options, NULL);
211                 if (n < 0)
212                         continue;
213                 switch (n) {
214                 case 'd':
215                         debug_level = atoi(optarg);
216                         break;
217                 case 'c':
218                         strncpy(config_dir, optarg, sizeof(config_dir) - 1);
219                         config_dir[sizeof(config_dir) - 1] = '\0';
220                         break;
221                 case 'h':
222                         fprintf(stderr, "Usage: lwsws [-c <config dir>] "
223                                         "[-d <log bitfield>] [-D] [--help]\n");
224                         exit(1);
225                 }
226         }
227 #ifndef _WIN32
228         /*
229          * We leave our original process up permanently, because that
230          * suits systemd.
231          *
232          * Otherwise we get into problems when reload spawns new processes and
233          * the original one dies randomly.
234          */
235
236         signal(SIGHUP, reload_handler);
237         signal(SIGINT, reload_handler);
238
239         fprintf(stderr, "Root process is %u\n", getpid());
240
241         while (1) {
242                 if (do_reload) {
243                         do_reload = 0;
244                         n = fork();
245                         if (n == 0) /* new */
246                                 break;
247                         /* old */
248                         if (n > 0)
249                                 for (m = 0; m < ARRAY_SIZE(pids); m++)
250                                         if (!pids[m]) {
251                                                 // fprintf(stderr, "added child pid %d\n", n);
252                                                 pids[m] = n;
253                                                 break;
254                                         }
255                 }
256 #ifndef _WIN32
257                 sleep(2);
258
259                 n = waitpid(-1, &status, WNOHANG);
260                 if (n > 0)
261                         for (m = 0; m < ARRAY_SIZE(pids); m++)
262                                 if (pids[m] == n) {
263                                         // fprintf(stderr, "reaped child pid %d\n", pids[m]);
264                                         pids[m] = 0;
265                                         break;
266                                 }
267 #else
268 // !!! implemenation needed
269 #endif
270         }
271 #endif
272         /* child process */
273
274 #ifndef _WIN32
275         /* we will only try to log things according to our debug_level */
276         setlogmask(LOG_UPTO (LOG_DEBUG));
277         openlog("lwsws", syslog_options, LOG_DAEMON);
278 #endif
279
280         lws_set_log_level(debug_level, lwsl_emit_syslog);
281
282         lwsl_notice("lwsws libwebsockets web server - license CC0 + LGPL2.1\n");
283         lwsl_notice("(C) Copyright 2010-2016 Andy Green <andy@warmcat.com>\n");
284
285 #if (UV_VERSION_MAJOR > 0) // Travis...
286         uv_loop_init(&loop);
287 #else
288         fprintf(stderr, "Your libuv is too old!\n");
289         return 0;
290 #endif
291         uv_signal_init(&loop, &signal_outer);
292         uv_signal_start(&signal_outer, signal_cb, SIGINT);
293         uv_signal_start(&signal_outer, signal_cb, SIGHUP);
294
295         if (context_creation()) {
296                 lwsl_err("Context creation failed\n");
297                 return 1;
298         }
299
300         lws_libuv_run(context, 0);
301
302         uv_signal_stop(&signal_outer);
303         lws_context_destroy(context);
304
305 #if (UV_VERSION_MAJOR > 0) // Travis...
306         n = 0;
307         while (n++ < 1024 && uv_loop_close(&loop))
308                 uv_run(&loop, UV_RUN_NOWAIT);
309 #endif
310
311         lws_context_destroy2(context);
312
313         fprintf(stderr, "lwsws exited cleanly\n");
314
315 #ifndef _WIN32
316         closelog();
317 #endif
318
319         return 0;
320 }