main.c 6.8 KB

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