main.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. #else
  35. #include <io.h>
  36. #include "gettimeofday.h"
  37. #endif
  38. #include "../lib/libwebsockets.h"
  39. static struct lws_context *context;
  40. #define LWSWS_CONFIG_STRING_SIZE (32 * 1024)
  41. static const struct lws_extension exts[] = {
  42. {
  43. "permessage-deflate",
  44. lws_extension_callback_pm_deflate,
  45. "permessage-deflate"
  46. },
  47. { NULL, NULL, NULL /* terminator */ }
  48. };
  49. static const char * const plugin_dirs[] = {
  50. INSTALL_DATADIR"/libwebsockets-test-server/plugins/",
  51. NULL
  52. };
  53. static struct option options[] = {
  54. { "help", no_argument, NULL, 'h' },
  55. { "debug", required_argument, NULL, 'd' },
  56. { "configdir", required_argument, NULL, 'c' },
  57. #ifndef LWS_NO_DAEMONIZE
  58. { "daemonize", no_argument, NULL, 'D' },
  59. #endif
  60. { NULL, 0, 0, 0 }
  61. };
  62. void signal_cb(uv_signal_t *watcher, int signum)
  63. {
  64. lwsl_err("Signal %d caught, exiting...\n", watcher->signum);
  65. switch (watcher->signum) {
  66. case SIGTERM:
  67. case SIGINT:
  68. break;
  69. default:
  70. signal(SIGABRT, SIG_DFL);
  71. abort();
  72. break;
  73. }
  74. lws_libuv_stop(context);
  75. }
  76. int main(int argc, char **argv)
  77. {
  78. struct lws_context_creation_info info;
  79. char *cs;
  80. int opts = 0, cs_len = LWSWS_CONFIG_STRING_SIZE - 1;
  81. int n = 0;
  82. #ifndef _WIN32
  83. int syslog_options = LOG_PID | LOG_PERROR;
  84. #endif
  85. #ifndef LWS_NO_DAEMONIZE
  86. int daemonize = 0;
  87. #endif
  88. int debug_level = 7;
  89. char config_dir[128];
  90. char *config_strings;
  91. memset(&info, 0, sizeof info);
  92. strcpy(config_dir, "/etc/lwsws");
  93. while (n >= 0) {
  94. n = getopt_long(argc, argv, "hd:c:D", options, NULL);
  95. if (n < 0)
  96. continue;
  97. switch (n) {
  98. #ifndef LWS_NO_DAEMONIZE
  99. case 'D':
  100. daemonize = 1;
  101. #ifndef _WIN32
  102. syslog_options &= ~LOG_PERROR;
  103. #endif
  104. printf("Daemonizing...\n");
  105. break;
  106. #endif
  107. case 'd':
  108. debug_level = atoi(optarg);
  109. break;
  110. case 'c':
  111. strncpy(config_dir, optarg, sizeof(config_dir) - 1);
  112. config_dir[sizeof(config_dir) - 1] = '\0';
  113. break;
  114. case 'h':
  115. fprintf(stderr, "Usage: lwsws [-c <config dir>] "
  116. "[-d <log bitfield>] [-D] [--help]\n");
  117. exit(1);
  118. }
  119. }
  120. #if !defined(LWS_NO_DAEMONIZE) && !defined(WIN32)
  121. /*
  122. * normally lock path would be /var/lock/lwsts or similar, to
  123. * simplify getting started without having to take care about
  124. * permissions or running as root, set to /tmp/.lwsts-lock
  125. */
  126. if (daemonize && lws_daemonize("/tmp/.lwsts-lock")) {
  127. fprintf(stderr, "Failed to daemonize\n");
  128. return 10;
  129. }
  130. if (daemonize)
  131. lwsl_notice("Daemonized\n");
  132. #endif
  133. #ifndef _WIN32
  134. /* we will only try to log things according to our debug_level */
  135. setlogmask(LOG_UPTO (LOG_DEBUG));
  136. openlog("lwsws", syslog_options, LOG_DAEMON);
  137. #endif
  138. lws_set_log_level(debug_level, lwsl_emit_syslog);
  139. lwsl_notice("lwsws libwebsockets web server - license CC0 + LGPL2.1\n");
  140. lwsl_notice("(C) Copyright 2010-2016 Andy Green <andy@warmcat.com>\n");
  141. cs = config_strings = malloc(LWSWS_CONFIG_STRING_SIZE);
  142. if (!config_strings) {
  143. lwsl_err("Unable to allocate config strings heap\n");
  144. return -1;
  145. }
  146. memset(&info, 0, sizeof(info));
  147. info.max_http_header_pool = 16;
  148. info.options = opts | LWS_SERVER_OPTION_VALIDATE_UTF8 |
  149. LWS_SERVER_OPTION_EXPLICIT_VHOSTS |
  150. LWS_SERVER_OPTION_LIBUV;
  151. info.plugin_dirs = plugin_dirs;
  152. lwsl_notice("Using config dir: \"%s\"\n", config_dir);
  153. /*
  154. * first go through the config for creating the outer context
  155. */
  156. if (lwsws_get_config_globals(&info, config_dir, &cs, &cs_len))
  157. goto init_failed;
  158. context = lws_create_context(&info);
  159. if (context == NULL) {
  160. lwsl_err("libwebsocket init failed\n");
  161. goto init_failed;
  162. }
  163. /*
  164. * then create the vhosts... protocols are entirely coming from
  165. * plugins, so we leave it NULL
  166. */
  167. info.extensions = exts;
  168. if (!lwsws_get_config_vhosts(context, &info, config_dir,
  169. &cs, &cs_len)) {
  170. /* run the server */
  171. lws_uv_sigint_cfg(context, 1, signal_cb);
  172. lws_uv_initloop(context, NULL, 0);
  173. lws_libuv_run(context, 0);
  174. }
  175. lws_context_destroy(context);
  176. free(config_strings);
  177. fprintf(stderr, "lwsws exited cleanly\n");
  178. #ifndef _WIN32
  179. closelog();
  180. #endif
  181. return 0;
  182. init_failed:
  183. free(config_strings);
  184. return 1;
  185. }