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,
26 volatile int force_exit = 0;
27 struct lws_context *context;
29 static char *config_dir = "/etc/lwsws/conf.d";
32 * strings and objects from the config file parsing are created here
34 #define LWSWS_CONFIG_STRING_SIZE (32 * 1024)
37 /* singlethreaded version --> no locks */
39 void test_server_lock(int care)
42 void test_server_unlock(int care)
55 /* list of supported protocols and callbacks */
57 static struct lws_protocols protocols[] = {
58 /* first protocol must always be HTTP handler */
60 "http-only", /* name */
61 callback_http, /* callback */
62 sizeof (struct per_session_data__http), /* per_session_data_size */
63 0, /* max frame size / rx buffer */
68 static const struct lws_extension exts[] = {
71 lws_extension_callback_pm_deflate,
74 { NULL, NULL, NULL /* terminator */ }
77 static const char * const plugin_dirs[] = {
78 INSTALL_DATADIR"/libwebsockets-test-server/plugins/",
82 static struct option options[] = {
83 { "help", no_argument, NULL, 'h' },
84 { "debug", required_argument, NULL, 'd' },
85 { "configdir", required_argument, NULL, 'c' },
86 #ifndef LWS_NO_DAEMONIZE
87 { "daemonize", no_argument, NULL, 'D' },
92 void signal_cb(uv_signal_t *watcher, int signum)
94 lwsl_err("Signal %d caught, exiting...\n", watcher->signum);
95 switch (watcher->signum) {
100 signal(SIGABRT, SIG_DFL);
104 lws_libuv_stop(context);
107 int main(int argc, char **argv)
109 struct lws_context_creation_info info;
111 int opts = 0, cs_len = LWSWS_CONFIG_STRING_SIZE - 1;
114 int syslog_options = LOG_PID | LOG_PERROR;
116 #ifndef LWS_NO_DAEMONIZE
120 memset(&info, 0, sizeof info);
123 n = getopt_long(argc, argv, "hd:c:D", options, NULL);
127 #ifndef LWS_NO_DAEMONIZE
131 syslog_options &= ~LOG_PERROR;
133 printf("Daemonizing...\n");
137 debug_level = atoi(optarg);
140 strncpy(config_dir, optarg, sizeof(config_dir) - 1);
141 config_dir[sizeof(config_dir) - 1] = '\0';
144 fprintf(stderr, "Usage: lwsws [-c <config dir>] "
145 "[-d <log bitfield>] [-D] [--help]\n");
150 #if !defined(LWS_NO_DAEMONIZE) && !defined(WIN32)
152 * normally lock path would be /var/lock/lwsts or similar, to
153 * simplify getting started without having to take care about
154 * permissions or running as root, set to /tmp/.lwsts-lock
156 if (daemonize && lws_daemonize("/tmp/.lwsts-lock")) {
157 fprintf(stderr, "Failed to daemonize\n");
161 lwsl_notice("Daemonized\n");
165 /* we will only try to log things according to our debug_level */
166 setlogmask(LOG_UPTO (LOG_DEBUG));
167 openlog("lwsws", syslog_options, LOG_DAEMON);
170 lws_set_log_level(debug_level, lwsl_emit_syslog);
172 lwsl_notice("lwsws libwebsockets web server - license GPL2.1\n");
173 lwsl_notice("(C) Copyright 2010-2016 Andy Green <andy@warmcat.com>\n");
175 cs = config_strings = malloc(LWSWS_CONFIG_STRING_SIZE);
176 if (!config_strings) {
177 lwsl_err("Unable to allocate config strings heap\n");
181 memset(&info, 0, sizeof(info));
183 info.max_http_header_pool = 16;
184 info.options = opts | LWS_SERVER_OPTION_VALIDATE_UTF8 |
185 LWS_SERVER_OPTION_EXPLICIT_VHOSTS |
186 LWS_SERVER_OPTION_LIBUV;
188 info.plugin_dirs = plugin_dirs;
189 lwsl_notice("Using config dir: \"%s\"\n", config_dir);
192 * first go through the config for creating the outer context
194 if (lwsws_get_config_globals(&info, config_dir, &cs, &cs_len))
197 context = lws_create_context(&info);
198 if (context == NULL) {
199 lwsl_err("libwebsocket init failed\n");
204 * then create the vhosts...
206 * protocols and extensions are the global list of possible
207 * protocols and extensions offered serverwide. The vhosts
208 * in the config files enable the ones they want to offer
211 * The first protocol is always included for http support.
214 info.protocols = protocols;
215 info.extensions = exts;
217 if (!lwsws_get_config_vhosts(context, &info, config_dir,
222 lws_uv_sigint_cfg(context, 1, signal_cb);
223 lws_uv_initloop(context, NULL, 0);
225 lws_libuv_run(context, 0);
228 lws_context_destroy(context);
229 free(config_strings);
230 fprintf(stderr, "lwsws exited cleanly\n");
239 free(config_strings);