2 * libwebsockets web server application
4 * Copyright (C) 2010-2016 Andy Green <andy@warmcat.com>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation:
9 * version 2.1 of the License.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
24 static struct lws_context *context;
26 #define LWSWS_CONFIG_STRING_SIZE (32 * 1024)
28 static const struct lws_extension exts[] = {
31 lws_extension_callback_pm_deflate,
34 { NULL, NULL, NULL /* terminator */ }
37 static const char * const plugin_dirs[] = {
38 INSTALL_DATADIR"/libwebsockets-test-server/plugins/",
42 static struct option options[] = {
43 { "help", no_argument, NULL, 'h' },
44 { "debug", required_argument, NULL, 'd' },
45 { "configdir", required_argument, NULL, 'c' },
46 #ifndef LWS_NO_DAEMONIZE
47 { "daemonize", no_argument, NULL, 'D' },
52 void signal_cb(uv_signal_t *watcher, int signum)
54 lwsl_err("Signal %d caught, exiting...\n", watcher->signum);
55 switch (watcher->signum) {
60 signal(SIGABRT, SIG_DFL);
64 lws_libuv_stop(context);
67 int main(int argc, char **argv)
69 struct lws_context_creation_info info;
71 int opts = 0, cs_len = LWSWS_CONFIG_STRING_SIZE - 1;
74 int syslog_options = LOG_PID | LOG_PERROR;
76 #ifndef LWS_NO_DAEMONIZE
83 memset(&info, 0, sizeof info);
84 strcpy(config_dir, "/etc/lwsws");
86 n = getopt_long(argc, argv, "hd:c:D", options, NULL);
90 #ifndef LWS_NO_DAEMONIZE
94 syslog_options &= ~LOG_PERROR;
96 printf("Daemonizing...\n");
100 debug_level = atoi(optarg);
103 strncpy(config_dir, optarg, sizeof(config_dir) - 1);
104 config_dir[sizeof(config_dir) - 1] = '\0';
107 fprintf(stderr, "Usage: lwsws [-c <config dir>] "
108 "[-d <log bitfield>] [-D] [--help]\n");
113 #if !defined(LWS_NO_DAEMONIZE) && !defined(WIN32)
115 * normally lock path would be /var/lock/lwsts or similar, to
116 * simplify getting started without having to take care about
117 * permissions or running as root, set to /tmp/.lwsts-lock
119 if (daemonize && lws_daemonize("/tmp/.lwsts-lock")) {
120 fprintf(stderr, "Failed to daemonize\n");
124 lwsl_notice("Daemonized\n");
128 /* we will only try to log things according to our debug_level */
129 setlogmask(LOG_UPTO (LOG_DEBUG));
130 openlog("lwsws", syslog_options, LOG_DAEMON);
133 lws_set_log_level(debug_level, lwsl_emit_syslog);
135 lwsl_notice("lwsws libwebsockets web server - license GPL2.1\n");
136 lwsl_notice("(C) Copyright 2010-2016 Andy Green <andy@warmcat.com>\n");
138 cs = config_strings = malloc(LWSWS_CONFIG_STRING_SIZE);
139 if (!config_strings) {
140 lwsl_err("Unable to allocate config strings heap\n");
144 memset(&info, 0, sizeof(info));
146 info.max_http_header_pool = 16;
147 info.options = opts | LWS_SERVER_OPTION_VALIDATE_UTF8 |
148 LWS_SERVER_OPTION_EXPLICIT_VHOSTS |
149 LWS_SERVER_OPTION_LIBUV;
151 info.plugin_dirs = plugin_dirs;
152 lwsl_notice("Using config dir: \"%s\"\n", config_dir);
155 * first go through the config for creating the outer context
157 if (lwsws_get_config_globals(&info, config_dir, &cs, &cs_len))
160 context = lws_create_context(&info);
161 if (context == NULL) {
162 lwsl_err("libwebsocket init failed\n");
167 * then create the vhosts... protocols are entirely coming from
168 * plugins, so we leave it NULL
171 info.extensions = exts;
173 if (!lwsws_get_config_vhosts(context, &info, config_dir,
178 lws_uv_sigint_cfg(context, 1, signal_cb);
179 lws_uv_initloop(context, NULL, 0);
181 lws_libuv_run(context, 0);
184 lws_context_destroy(context);
185 free(config_strings);
187 fprintf(stderr, "lwsws exited cleanly\n");
196 free(config_strings);