lwsws license to cc0
[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 #else
36 #include <io.h>
37 #include "gettimeofday.h"
38 #endif
39
40 #include "../lib/libwebsockets.h"
41
42 static struct lws_context *context;
43
44 #define LWSWS_CONFIG_STRING_SIZE (32 * 1024)
45
46 static const struct lws_extension exts[] = {
47         {
48                 "permessage-deflate",
49                 lws_extension_callback_pm_deflate,
50                 "permessage-deflate"
51         },
52         { NULL, NULL, NULL /* terminator */ }
53 };
54
55 static const char * const plugin_dirs[] = {
56         INSTALL_DATADIR"/libwebsockets-test-server/plugins/",
57         NULL
58 };
59
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' },
66 #endif
67         { NULL, 0, 0, 0 }
68 };
69
70 void signal_cb(uv_signal_t *watcher, int signum)
71 {
72         lwsl_err("Signal %d caught, exiting...\n", watcher->signum);
73         switch (watcher->signum) {
74         case SIGTERM:
75         case SIGINT:
76                 break;
77         default:
78                 signal(SIGABRT, SIG_DFL);
79                 abort();
80                 break;
81         }
82         lws_libuv_stop(context);
83 }
84
85 int main(int argc, char **argv)
86 {
87         struct lws_context_creation_info info;
88         char *cs;
89         int opts = 0, cs_len = LWSWS_CONFIG_STRING_SIZE - 1;
90         int n = 0;
91 #ifndef _WIN32
92         int syslog_options = LOG_PID | LOG_PERROR;
93 #endif
94 #ifndef LWS_NO_DAEMONIZE
95         int daemonize = 0;
96 #endif
97         int debug_level = 7;
98         char config_dir[128];
99         char *config_strings;
100
101         memset(&info, 0, sizeof info);
102         strcpy(config_dir, "/etc/lwsws");
103         while (n >= 0) {
104                 n = getopt_long(argc, argv, "hd:c:D", options, NULL);
105                 if (n < 0)
106                         continue;
107                 switch (n) {
108 #ifndef LWS_NO_DAEMONIZE
109                 case 'D':
110                         daemonize = 1;
111                         #ifndef _WIN32
112                         syslog_options &= ~LOG_PERROR;
113                         #endif
114                         printf("Daemonizing...\n");
115                         break;
116 #endif
117                 case 'd':
118                         debug_level = atoi(optarg);
119                         break;
120                 case 'c':
121                         strncpy(config_dir, optarg, sizeof(config_dir) - 1);
122                         config_dir[sizeof(config_dir) - 1] = '\0';
123                         break;
124                 case 'h':
125                         fprintf(stderr, "Usage: lwsws [-c <config dir>] "
126                                         "[-d <log bitfield>] [-D] [--help]\n");
127                         exit(1);
128                 }
129         }
130
131 #if !defined(LWS_NO_DAEMONIZE) && !defined(WIN32)
132         /*
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
136          */
137         if (daemonize && lws_daemonize("/tmp/.lwsts-lock")) {
138                 fprintf(stderr, "Failed to daemonize\n");
139                 return 10;
140         }
141         if (daemonize)
142                 lwsl_notice("Daemonized\n");
143 #endif
144
145 #ifndef _WIN32
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);
149 #endif
150
151         lws_set_log_level(debug_level, lwsl_emit_syslog);
152
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");
155
156         cs = config_strings = malloc(LWSWS_CONFIG_STRING_SIZE);
157         if (!config_strings) {
158                 lwsl_err("Unable to allocate config strings heap\n");
159                 return -1;
160         }
161
162         memset(&info, 0, sizeof(info));
163
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;
168
169         info.plugin_dirs = plugin_dirs;
170         lwsl_notice("Using config dir: \"%s\"\n", config_dir);
171
172         /*
173          *  first go through the config for creating the outer context
174          */
175         if (lwsws_get_config_globals(&info, config_dir, &cs, &cs_len))
176                 goto init_failed;
177
178         context = lws_create_context(&info);
179         if (context == NULL) {
180                 lwsl_err("libwebsocket init failed\n");
181                 goto init_failed;
182         }
183
184         /*
185          * then create the vhosts... protocols are entirely coming from
186          * plugins, so we leave it NULL
187          */
188
189         info.extensions = exts;
190
191         if (!lwsws_get_config_vhosts(context, &info, config_dir,
192                                      &cs, &cs_len)) {
193
194                 /* run the server */
195
196                 lws_uv_sigint_cfg(context, 1, signal_cb);
197                 lws_uv_initloop(context, NULL, 0);
198
199                 lws_libuv_run(context, 0);
200         }
201
202         lws_context_destroy(context);
203         free(config_strings);
204
205         fprintf(stderr, "lwsws exited cleanly\n");
206
207 #ifndef _WIN32
208         closelog();
209 #endif
210
211         return 0;
212
213 init_failed:
214         free(config_strings);
215
216         return 1;
217 }