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"
37 #include "gettimeofday.h"
40 #include "../lib/libwebsockets.h"
42 static struct lws_context *context;
44 #define LWSWS_CONFIG_STRING_SIZE (32 * 1024)
46 static const struct lws_extension exts[] = {
49 lws_extension_callback_pm_deflate,
52 { NULL, NULL, NULL /* terminator */ }
55 static const char * const plugin_dirs[] = {
56 INSTALL_DATADIR"/libwebsockets-test-server/plugins/",
60 static struct option options[] = {
61 { "help", no_argument, NULL, 'h' },
62 { "debug", required_argument, NULL, 'd' },
63 { "configdir", required_argument, NULL, 'c' },
64 #ifndef LWS_NO_DAEMONIZE
65 { "daemonize", no_argument, NULL, 'D' },
70 void signal_cb(uv_signal_t *watcher, int signum)
72 lwsl_err("Signal %d caught, exiting...\n", watcher->signum);
73 switch (watcher->signum) {
78 signal(SIGABRT, SIG_DFL);
82 lws_libuv_stop(context);
85 int main(int argc, char **argv)
87 struct lws_context_creation_info info;
89 int opts = 0, cs_len = LWSWS_CONFIG_STRING_SIZE - 1;
92 int syslog_options = LOG_PID | LOG_PERROR;
94 #ifndef LWS_NO_DAEMONIZE
101 memset(&info, 0, sizeof info);
102 strcpy(config_dir, "/etc/lwsws");
104 n = getopt_long(argc, argv, "hd:c:D", options, NULL);
108 #ifndef LWS_NO_DAEMONIZE
112 syslog_options &= ~LOG_PERROR;
114 printf("Daemonizing...\n");
118 debug_level = atoi(optarg);
121 strncpy(config_dir, optarg, sizeof(config_dir) - 1);
122 config_dir[sizeof(config_dir) - 1] = '\0';
125 fprintf(stderr, "Usage: lwsws [-c <config dir>] "
126 "[-d <log bitfield>] [-D] [--help]\n");
131 #if !defined(LWS_NO_DAEMONIZE) && !defined(WIN32)
133 * normally lock path would be /var/lock/lwsts or similar, to
134 * simplify getting started without having to take care about
135 * permissions or running as root, set to /tmp/.lwsts-lock
137 if (daemonize && lws_daemonize("/tmp/.lwsts-lock")) {
138 fprintf(stderr, "Failed to daemonize\n");
142 lwsl_notice("Daemonized\n");
146 /* we will only try to log things according to our debug_level */
147 setlogmask(LOG_UPTO (LOG_DEBUG));
148 openlog("lwsws", syslog_options, LOG_DAEMON);
151 lws_set_log_level(debug_level, lwsl_emit_syslog);
153 lwsl_notice("lwsws libwebsockets web server - license CC0 + LGPL2.1\n");
154 lwsl_notice("(C) Copyright 2010-2016 Andy Green <andy@warmcat.com>\n");
156 cs = config_strings = malloc(LWSWS_CONFIG_STRING_SIZE);
157 if (!config_strings) {
158 lwsl_err("Unable to allocate config strings heap\n");
162 memset(&info, 0, sizeof(info));
164 info.max_http_header_pool = 16;
165 info.options = opts | LWS_SERVER_OPTION_VALIDATE_UTF8 |
166 LWS_SERVER_OPTION_EXPLICIT_VHOSTS |
167 LWS_SERVER_OPTION_LIBUV;
169 info.plugin_dirs = plugin_dirs;
170 lwsl_notice("Using config dir: \"%s\"\n", config_dir);
173 * first go through the config for creating the outer context
175 if (lwsws_get_config_globals(&info, config_dir, &cs, &cs_len))
178 context = lws_create_context(&info);
179 if (context == NULL) {
180 lwsl_err("libwebsocket init failed\n");
185 * then create the vhosts... protocols are entirely coming from
186 * plugins, so we leave it NULL
189 info.extensions = exts;
191 if (!lwsws_get_config_vhosts(context, &info, config_dir,
196 lws_uv_sigint_cfg(context, 1, signal_cb);
197 lws_uv_initloop(context, NULL, 0);
199 lws_libuv_run(context, 0);
202 lws_context_destroy(context);
203 free(config_strings);
205 fprintf(stderr, "lwsws exited cleanly\n");
214 free(config_strings);