main.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. * libwebsockets web server application
  3. *
  4. * Written in 2010-2020 by Andy Green <[email protected]>
  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. #include <stdio.h>
  22. #include <stdlib.h>
  23. #if defined(LWS_HAS_GETOPT_LONG) || defined(WIN32)
  24. #include <getopt.h>
  25. #endif
  26. #include <signal.h>
  27. #include <string.h>
  28. #include <sys/stat.h>
  29. #include <fcntl.h>
  30. #include <assert.h>
  31. #ifndef _WIN32
  32. #include <dirent.h>
  33. #include <syslog.h>
  34. #include <sys/time.h>
  35. #include <unistd.h>
  36. #include <sys/wait.h>
  37. #else
  38. #include <io.h>
  39. #include "gettimeofday.h"
  40. #include <uv.h>
  41. int fork(void)
  42. {
  43. fprintf(stderr, "Sorry Windows doesn't support fork().\n");
  44. return 0;
  45. }
  46. #endif
  47. #include <libwebsockets.h>
  48. #include <uv.h>
  49. #if defined(LWS_HAVE_MALLOC_TRIM)
  50. #include <malloc.h>
  51. #endif
  52. static struct lws_context *context;
  53. static lws_sorted_usec_list_t sul_lwsws;
  54. static char config_dir[128];
  55. static int opts = 0, do_reload = 1;
  56. static uv_loop_t loop;
  57. static uv_signal_t signal_outer[2];
  58. static int pids[32];
  59. void lwsl_emit_stderr(int level, const char *line);
  60. #define LWSWS_CONFIG_STRING_SIZE (32 * 1024)
  61. static const struct lws_extension exts[] = {
  62. #if !defined(LWS_WITHOUT_EXTENSIONS)
  63. {
  64. "permessage-deflate",
  65. lws_extension_callback_pm_deflate,
  66. "permessage-deflate"
  67. },
  68. #endif
  69. { NULL, NULL, NULL /* terminator */ }
  70. };
  71. #if defined(LWS_WITH_PLUGINS)
  72. static const char * const plugin_dirs[] = {
  73. INSTALL_DATADIR"/libwebsockets-test-server/plugins/",
  74. NULL
  75. };
  76. #endif
  77. #if defined(LWS_HAS_GETOPT_LONG) || defined(WIN32)
  78. static struct option options[] = {
  79. { "help", no_argument, NULL, 'h' },
  80. { "debug", required_argument, NULL, 'd' },
  81. { "configdir", required_argument, NULL, 'c' },
  82. { NULL, 0, 0, 0 }
  83. };
  84. #endif
  85. void signal_cb(uv_signal_t *watcher, int signum)
  86. {
  87. switch (watcher->signum) {
  88. case SIGTERM:
  89. case SIGINT:
  90. break;
  91. case SIGHUP:
  92. if (lws_context_is_deprecated(context))
  93. return;
  94. lwsl_notice("Dropping listen sockets\n");
  95. lws_context_deprecate(context, NULL);
  96. return;
  97. default:
  98. signal(SIGABRT, SIG_DFL);
  99. abort();
  100. break;
  101. }
  102. lwsl_err("Signal %d caught\n", watcher->signum);
  103. uv_signal_stop(watcher);
  104. uv_signal_stop(&signal_outer[1]);
  105. lws_context_destroy(context);
  106. }
  107. static void
  108. lwsws_min(lws_sorted_usec_list_t *sul)
  109. {
  110. lwsl_debug("%s\n", __func__);
  111. #if defined(LWS_HAVE_MALLOC_TRIM)
  112. malloc_trim(4 * 1024);
  113. #endif
  114. lws_sul_schedule(context, 0, &sul_lwsws, lwsws_min, 60 * LWS_US_PER_SEC);
  115. }
  116. static int
  117. context_creation(void)
  118. {
  119. int cs_len = LWSWS_CONFIG_STRING_SIZE - 1;
  120. struct lws_context_creation_info info;
  121. char *cs, *config_strings;
  122. void *foreign_loops[1];
  123. cs = config_strings = malloc(LWSWS_CONFIG_STRING_SIZE);
  124. if (!config_strings) {
  125. lwsl_err("Unable to allocate config strings heap\n");
  126. return -1;
  127. }
  128. memset(&info, 0, sizeof(info));
  129. info.external_baggage_free_on_destroy = config_strings;
  130. info.pt_serv_buf_size = 8192;
  131. info.options = opts | LWS_SERVER_OPTION_VALIDATE_UTF8 |
  132. LWS_SERVER_OPTION_EXPLICIT_VHOSTS |
  133. LWS_SERVER_OPTION_LIBUV;
  134. #if defined(LWS_WITH_PLUGINS)
  135. info.plugin_dirs = plugin_dirs;
  136. #endif
  137. lwsl_notice("Using config dir: \"%s\"\n", config_dir);
  138. /*
  139. * first go through the config for creating the outer context
  140. */
  141. if (lwsws_get_config_globals(&info, config_dir, &cs, &cs_len))
  142. goto init_failed;
  143. foreign_loops[0] = &loop;
  144. info.foreign_loops = foreign_loops;
  145. info.pcontext = &context;
  146. context = lws_create_context(&info);
  147. if (context == NULL) {
  148. lwsl_err("libwebsocket init failed\n");
  149. goto init_failed;
  150. }
  151. /*
  152. * then create the vhosts... protocols are entirely coming from
  153. * plugins, so we leave it NULL
  154. */
  155. info.extensions = exts;
  156. if (lwsws_get_config_vhosts(context, &info, config_dir, &cs, &cs_len))
  157. return 1;
  158. lws_sul_schedule(context, 0, &sul_lwsws, lwsws_min, 60 * LWS_US_PER_SEC);
  159. return 0;
  160. init_failed:
  161. free(config_strings);
  162. return 1;
  163. }
  164. /*
  165. * root-level sighup handler
  166. */
  167. static void
  168. reload_handler(int signum)
  169. {
  170. #ifndef _WIN32
  171. int m;
  172. switch (signum) {
  173. case SIGHUP: /* reload */
  174. fprintf(stderr, "root process receives reload\n");
  175. if (!do_reload) {
  176. fprintf(stderr, "passing HUP to child processes\n");
  177. for (m = 0; m < (int)LWS_ARRAY_SIZE(pids); m++)
  178. if (pids[m])
  179. kill(pids[m], SIGHUP);
  180. sleep(1);
  181. }
  182. do_reload = 1;
  183. break;
  184. case SIGINT:
  185. case SIGTERM:
  186. case SIGKILL:
  187. fprintf(stderr, "master process waiting 2s...\n");
  188. sleep(2); /* give children a chance to deal with the signal */
  189. fprintf(stderr, "killing service processes\n");
  190. for (m = 0; m < (int)LWS_ARRAY_SIZE(pids); m++)
  191. if (pids[m])
  192. kill(pids[m], SIGTERM);
  193. exit(0);
  194. }
  195. #else
  196. // kill() implementation needed for WIN32
  197. #endif
  198. }
  199. int main(int argc, char **argv)
  200. {
  201. int n = 0, budget = 100, debug_level = 1024 + 7;
  202. #ifndef _WIN32
  203. int m;
  204. int status;//, syslog_options = LOG_PID | LOG_PERROR;
  205. #endif
  206. strcpy(config_dir, "/etc/lwsws");
  207. while (n >= 0) {
  208. #if defined(LWS_HAS_GETOPT_LONG) || defined(WIN32)
  209. n = getopt_long(argc, argv, "hd:c:", options, NULL);
  210. #else
  211. n = getopt(argc, argv, "hd:c:");
  212. #endif
  213. if (n < 0)
  214. continue;
  215. switch (n) {
  216. case 'd':
  217. debug_level = atoi(optarg);
  218. break;
  219. case 'c':
  220. lws_strncpy(config_dir, optarg, sizeof(config_dir));
  221. break;
  222. case 'h':
  223. fprintf(stderr, "Usage: lwsws [-c <config dir>] "
  224. "[-d <log bitfield>] [--help]\n");
  225. exit(1);
  226. }
  227. }
  228. #ifndef _WIN32
  229. /*
  230. * We leave our original process up permanently, because that
  231. * suits systemd.
  232. *
  233. * Otherwise we get into problems when reload spawns new processes and
  234. * the original one dies randomly.
  235. */
  236. signal(SIGHUP, reload_handler);
  237. signal(SIGINT, reload_handler);
  238. fprintf(stderr, "Root process is %u\n", (unsigned int)getpid());
  239. while (1) {
  240. if (do_reload) {
  241. do_reload = 0;
  242. n = fork();
  243. if (n == 0) /* new */
  244. break;
  245. /* old */
  246. if (n > 0)
  247. for (m = 0; m < (int)LWS_ARRAY_SIZE(pids); m++)
  248. if (!pids[m]) {
  249. pids[m] = n;
  250. break;
  251. }
  252. }
  253. #ifndef _WIN32
  254. sleep(2);
  255. n = waitpid(-1, &status, WNOHANG);
  256. if (n > 0)
  257. for (m = 0; m < (int)LWS_ARRAY_SIZE(pids); m++)
  258. if (pids[m] == n) {
  259. pids[m] = 0;
  260. break;
  261. }
  262. #else
  263. // !!! implemenation needed
  264. #endif
  265. }
  266. #endif
  267. /* child process */
  268. lws_set_log_level(debug_level, lwsl_emit_stderr_notimestamp);
  269. lwsl_notice("lwsws libwebsockets web server - license CC0 + MIT\n");
  270. lwsl_notice("(C) Copyright 2010-2020 Andy Green <[email protected]>\n");
  271. #if (UV_VERSION_MAJOR > 0) // Travis...
  272. uv_loop_init(&loop);
  273. #else
  274. fprintf(stderr, "Your libuv is too old!\n");
  275. return 0;
  276. #endif
  277. uv_signal_init(&loop, &signal_outer[0]);
  278. uv_signal_start(&signal_outer[0], signal_cb, SIGINT);
  279. uv_signal_init(&loop, &signal_outer[1]);
  280. uv_signal_start(&signal_outer[1], signal_cb, SIGHUP);
  281. if (context_creation()) {
  282. lwsl_err("Context creation failed\n");
  283. return 1;
  284. }
  285. lws_service(context, 0);
  286. lwsl_err("%s: closing\n", __func__);
  287. for (n = 0; n < 2; n++) {
  288. uv_signal_stop(&signal_outer[n]);
  289. uv_close((uv_handle_t *)&signal_outer[n], NULL);
  290. }
  291. /* cancel the per-minute sul */
  292. lws_sul_cancel(&sul_lwsws);
  293. lws_context_destroy(context);
  294. (void)budget;
  295. #if (UV_VERSION_MAJOR > 0) // Travis...
  296. while ((n = uv_loop_close(&loop)) && --budget)
  297. uv_run(&loop, UV_RUN_ONCE);
  298. #endif
  299. fprintf(stderr, "lwsws exited cleanly: %d\n", n);
  300. #ifndef _WIN32
  301. closelog();
  302. #endif
  303. context = NULL;
  304. return 0;
  305. }