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