2 * libwebsockets web server application
4 * Copyright (C) 2010-2016 Andy Green <andy@warmcat.com>
6 * This file is made available under the Creative Commons CC0 1.0
7 * Universal Public Domain Dedication.
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.
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
20 #include "lws_config.h"
38 #include "gettimeofday.h"
42 fprintf(stderr, "Sorry Windows doesn't support fork().\n");
47 #include "../lib/libwebsockets.h"
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;
58 #define LWSWS_CONFIG_STRING_SIZE (32 * 1024)
60 static const struct lws_extension exts[] = {
63 lws_extension_callback_pm_deflate,
66 { NULL, NULL, NULL /* terminator */ }
69 static const char * const plugin_dirs[] = {
70 INSTALL_DATADIR"/libwebsockets-test-server/plugins/",
74 static struct option options[] = {
75 { "help", no_argument, NULL, 'h' },
76 { "debug", required_argument, NULL, 'd' },
77 { "configdir", required_argument, NULL, 'c' },
81 void signal_cb(uv_signal_t *watcher, int signum)
83 switch (watcher->signum) {
89 if (lws_context_is_deprecated(context))
91 lwsl_notice("Dropping listen sockets\n");
92 lws_context_deprecate(context, NULL);
96 signal(SIGABRT, SIG_DFL);
100 lwsl_err("Signal %d caught\n", watcher->signum);
101 lws_libuv_stop(context);
105 context_creation(void)
107 int cs_len = LWSWS_CONFIG_STRING_SIZE - 1;
108 struct lws_context_creation_info info;
109 char *cs, *config_strings;
111 cs = config_strings = malloc(LWSWS_CONFIG_STRING_SIZE);
112 if (!config_strings) {
113 lwsl_err("Unable to allocate config strings heap\n");
117 memset(&info, 0, sizeof(info));
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;
125 info.plugin_dirs = plugin_dirs;
126 lwsl_notice("Using config dir: \"%s\"\n", config_dir);
129 * first go through the config for creating the outer context
131 if (lwsws_get_config_globals(&info, config_dir, &cs, &cs_len))
134 context = lws_create_context(&info);
135 if (context == NULL) {
136 lwsl_err("libwebsocket init failed\n");
140 lws_uv_sigint_cfg(context, 1, signal_cb);
141 lws_uv_initloop(context, &loop, 0);
144 * then create the vhosts... protocols are entirely coming from
145 * plugins, so we leave it NULL
148 info.extensions = exts;
150 if (lwsws_get_config_vhosts(context, &info, config_dir,
157 free(config_strings);
164 * root-level sighup handler
168 reload_handler(int signum)
175 case SIGHUP: /* reload */
176 fprintf(stderr, "root process receives reload\n");
178 fprintf(stderr, "passing HUP to child processes\n");
179 for (m = 0; m < ARRAY_SIZE(pids); m++)
181 kill(pids[m], SIGHUP);
189 fprintf(stderr, "killing service processes\n");
190 for (m = 0; m < ARRAY_SIZE(pids); m++)
192 kill(pids[m], SIGTERM);
196 // kill() implementation needed for WIN32
200 int main(int argc, char **argv)
202 int n = 0, debug_level = 7;
205 int status, syslog_options = LOG_PID | LOG_PERROR;
208 strcpy(config_dir, "/etc/lwsws");
210 n = getopt_long(argc, argv, "hd:c:", options, NULL);
215 debug_level = atoi(optarg);
218 strncpy(config_dir, optarg, sizeof(config_dir) - 1);
219 config_dir[sizeof(config_dir) - 1] = '\0';
222 fprintf(stderr, "Usage: lwsws [-c <config dir>] "
223 "[-d <log bitfield>] [-D] [--help]\n");
229 * We leave our original process up permanently, because that
232 * Otherwise we get into problems when reload spawns new processes and
233 * the original one dies randomly.
236 signal(SIGHUP, reload_handler);
237 signal(SIGINT, reload_handler);
239 fprintf(stderr, "Root process is %u\n", getpid());
245 if (n == 0) /* new */
249 for (m = 0; m < ARRAY_SIZE(pids); m++)
251 // fprintf(stderr, "added child pid %d\n", n);
259 n = waitpid(-1, &status, WNOHANG);
261 for (m = 0; m < ARRAY_SIZE(pids); m++)
263 // fprintf(stderr, "reaped child pid %d\n", pids[m]);
268 // !!! implemenation needed
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);
280 lws_set_log_level(debug_level, lwsl_emit_syslog);
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");
285 #if (UV_VERSION_MAJOR > 0) // Travis...
288 fprintf(stderr, "Your libuv is too old!\n");
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);
295 if (context_creation()) {
296 lwsl_err("Context creation failed\n");
300 lws_libuv_run(context, 0);
302 uv_signal_stop(&signal_outer);
303 lws_context_destroy(context);
305 #if (UV_VERSION_MAJOR > 0) // Travis...
306 lws_close_all_handles_in_loop(&loop);
308 while (n++ < 4096 && uv_loop_close(&loop))
309 uv_run(&loop, UV_RUN_NOWAIT);
312 lws_context_destroy2(context);
314 fprintf(stderr, "lwsws exited cleanly\n");