lwsws cleanup and allocate config strings dynamically
[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 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.
10  *
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.
15  *
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,
19  * MA  02110-1301  USA
20  */
21
22 #include "lwsws.h"
23
24 int debug_level = 7;
25
26 volatile int force_exit = 0;
27 struct lws_context *context;
28
29 static char *config_dir = "/etc/lwsws/conf.d";
30
31 /*
32  * strings and objects from the config file parsing are created here
33  */
34 #define LWSWS_CONFIG_STRING_SIZE (32 * 1024)
35 char *config_strings;
36
37 /* singlethreaded version --> no locks */
38
39 void test_server_lock(int care)
40 {
41 }
42 void test_server_unlock(int care)
43 {
44 }
45
46
47 enum demo_protocols {
48         /* always first */
49         PROTOCOL_HTTP = 0,
50
51         /* always last */
52         DEMO_PROTOCOL_COUNT
53 };
54
55 /* list of supported protocols and callbacks */
56
57 static struct lws_protocols protocols[] = {
58         /* first protocol must always be HTTP handler */
59         {
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 */
64         },
65         { }
66 };
67
68 static const struct lws_extension exts[] = {
69         {
70                 "permessage-deflate",
71                 lws_extension_callback_pm_deflate,
72                 "permessage-deflate"
73         },
74         { NULL, NULL, NULL /* terminator */ }
75 };
76
77 static const char * const plugin_dirs[] = {
78                 INSTALL_DATADIR"/libwebsockets-test-server/plugins/",
79                 NULL
80 };
81
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' },
88 #endif
89         { NULL, 0, 0, 0 }
90 };
91
92 void signal_cb(uv_signal_t *watcher, int signum)
93 {
94         lwsl_err("Signal %d caught, exiting...\n", watcher->signum);
95         switch (watcher->signum) {
96         case SIGTERM:
97         case SIGINT:
98                 break;
99         default:
100                 signal(SIGABRT, SIG_DFL);
101                 abort();
102                 break;
103         }
104         lws_libuv_stop(context);
105 }
106
107 int main(int argc, char **argv)
108 {
109         struct lws_context_creation_info info;
110         char *cs;
111         int opts = 0, cs_len = LWSWS_CONFIG_STRING_SIZE - 1;
112         int n = 0;
113 #ifndef _WIN32
114         int syslog_options = LOG_PID | LOG_PERROR;
115 #endif
116 #ifndef LWS_NO_DAEMONIZE
117         int daemonize = 0;
118 #endif
119
120         memset(&info, 0, sizeof info);
121
122         while (n >= 0) {
123                 n = getopt_long(argc, argv, "hd:c:D", options, NULL);
124                 if (n < 0)
125                         continue;
126                 switch (n) {
127 #ifndef LWS_NO_DAEMONIZE
128                 case 'D':
129                         daemonize = 1;
130                         #ifndef _WIN32
131                         syslog_options &= ~LOG_PERROR;
132                         #endif
133                         printf("Daemonizing...\n");
134                         break;
135 #endif
136                 case 'd':
137                         debug_level = atoi(optarg);
138                         break;
139                 case 'c':
140                         strncpy(config_dir, optarg, sizeof(config_dir) - 1);
141                         config_dir[sizeof(config_dir) - 1] = '\0';
142                         break;
143                 case 'h':
144                         fprintf(stderr, "Usage: lwsws [-c <config dir>] "
145                                         "[-d <log bitfield>] [-D] [--help]\n");
146                         exit(1);
147                 }
148         }
149
150 #if !defined(LWS_NO_DAEMONIZE) && !defined(WIN32)
151         /*
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
155          */
156         if (daemonize && lws_daemonize("/tmp/.lwsts-lock")) {
157                 fprintf(stderr, "Failed to daemonize\n");
158                 return 10;
159         }
160         if (daemonize)
161                 lwsl_notice("Daemonized\n");
162 #endif
163
164 #ifndef _WIN32
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);
168 #endif
169
170         lws_set_log_level(debug_level, lwsl_emit_syslog);
171
172         lwsl_notice("lwsws libwebsockets web server - license GPL2.1\n");
173         lwsl_notice("(C) Copyright 2010-2016 Andy Green <andy@warmcat.com>\n");
174
175         cs = config_strings = malloc(LWSWS_CONFIG_STRING_SIZE);
176         if (!config_strings) {
177                 lwsl_err("Unable to allocate config strings heap\n");
178                 return -1;
179         }
180
181         memset(&info, 0, sizeof(info));
182
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;
187
188         info.plugin_dirs = plugin_dirs;
189         lwsl_notice("Using config dir: \"%s\"\n", config_dir);
190
191         /*
192          *  first go through the config for creating the outer context
193          */
194         if (lwsws_get_config_globals(&info, config_dir, &cs, &cs_len))
195                 goto init_failed;
196
197         context = lws_create_context(&info);
198         if (context == NULL) {
199                 lwsl_err("libwebsocket init failed\n");
200                 goto init_failed;
201         }
202
203         /*
204          * then create the vhosts...
205          *
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
209          * per vhost.
210          *
211          * The first protocol is always included for http support.
212          */
213
214         info.protocols = protocols;
215         info.extensions = exts;
216
217         if (!lwsws_get_config_vhosts(context, &info, config_dir,
218                                      &cs, &cs_len)) {
219
220                 /* run the server */
221
222                 lws_uv_sigint_cfg(context, 1, signal_cb);
223                 lws_uv_initloop(context, NULL, 0);
224
225                 lws_libuv_run(context, 0);
226         }
227
228         lws_context_destroy(context);
229         free(config_strings);
230         fprintf(stderr, "lwsws exited cleanly\n");
231
232 #ifndef _WIN32
233         closelog();
234 #endif
235
236         return 0;
237
238 init_failed:
239         free(config_strings);
240
241         return 1;
242 }