main.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * Copyright (c) 2004-2009 Sergey Lyubka
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. *
  22. * $Id: main.c 518 2010-05-03 12:55:35Z valenok $
  23. */
  24. #if defined(_WIN32)
  25. #define _CRT_SECURE_NO_WARNINGS /* Disable deprecation warning in VS2005 */
  26. #endif /* _WIN32 */
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <signal.h>
  30. #include <string.h>
  31. #include <errno.h>
  32. #include <limits.h>
  33. #include "mongoose.h"
  34. #ifdef _WIN32
  35. #include <windows.h>
  36. #include <winsvc.h>
  37. #define DIRSEP '\\'
  38. #define snprintf _snprintf
  39. #if !defined(__LCC__)
  40. #define strdup(x) _strdup(x)
  41. #endif /* !MINGW */
  42. #define sleep(x) Sleep((x) * 1000)
  43. #else
  44. #include <sys/wait.h>
  45. #include <unistd.h> /* For pause() */
  46. #define DIRSEP '/'
  47. #endif /* _WIN32 */
  48. static int exit_flag; /* Program termination flag */
  49. #if !defined(CONFIG_FILE)
  50. #define CONFIG_FILE "mongoose.conf"
  51. #endif /* !CONFIG_FILE */
  52. static void
  53. signal_handler(int sig_num)
  54. {
  55. #if !defined(_WIN32)
  56. if (sig_num == SIGCHLD) {
  57. do {
  58. } while (waitpid(-1, &sig_num, WNOHANG) > 0);
  59. } else
  60. #endif /* !_WIN32 */
  61. {
  62. exit_flag = sig_num;
  63. }
  64. }
  65. /*
  66. * Show usage string and exit.
  67. */
  68. static void
  69. show_usage_and_exit(void)
  70. {
  71. mg_show_usage_string(stderr);
  72. exit(EXIT_FAILURE);
  73. }
  74. /*
  75. * Edit the passwords file.
  76. */
  77. static int
  78. mg_edit_passwords(const char *fname, const char *domain,
  79. const char *user, const char *pass)
  80. {
  81. struct mg_context *ctx;
  82. int retval;
  83. ctx = mg_start();
  84. (void) mg_set_option(ctx, "auth_realm", domain);
  85. retval = mg_modify_passwords_file(ctx, fname, user, pass);
  86. mg_stop(ctx);
  87. return (retval);
  88. }
  89. static void
  90. process_command_line_arguments(struct mg_context *ctx, char *argv[])
  91. {
  92. const char *config_file = CONFIG_FILE;
  93. char line[512], opt[512], *vals[100],
  94. val[512], path[FILENAME_MAX], *p;
  95. FILE *fp;
  96. size_t i, line_no = 0;
  97. /* First find out, which config file to open */
  98. for (i = 1; argv[i] != NULL && argv[i][0] == '-'; i += 2)
  99. if (argv[i + 1] == NULL)
  100. show_usage_and_exit();
  101. if (argv[i] != NULL && argv[i + 1] != NULL) {
  102. /* More than one non-option arguments are given */
  103. show_usage_and_exit();
  104. } else if (argv[i] != NULL) {
  105. /* Just one non-option argument is given, this is config file */
  106. config_file = argv[i];
  107. } else {
  108. /* No config file specified. Look for one where binary lives */
  109. if ((p = strrchr(argv[0], DIRSEP)) != 0) {
  110. (void) snprintf(path, sizeof(path), "%.*s%s",
  111. (int) (p - argv[0]) + 1, argv[0], config_file);
  112. config_file = path;
  113. }
  114. }
  115. fp = fopen(config_file, "r");
  116. /* If config file was set in command line and open failed, exit */
  117. if (fp == NULL && argv[i] != NULL) {
  118. (void) fprintf(stderr, "cannot open config file %s: %s\n",
  119. config_file, strerror(errno));
  120. exit(EXIT_FAILURE);
  121. }
  122. /* Reset temporary value holders */
  123. (void) memset(vals, 0, sizeof(vals));
  124. if (fp != NULL) {
  125. (void) printf("Loading config file %s\n", config_file);
  126. /* Loop over the lines in config file */
  127. while (fgets(line, sizeof(line), fp) != NULL) {
  128. line_no++;
  129. /* Ignore empty lines and comments */
  130. if (line[0] == '#' || line[0] == '\n')
  131. continue;
  132. if (sscanf(line, "%s %[^\r\n#]", opt, val) != 2) {
  133. fprintf(stderr, "%s: line %d is invalid\n",
  134. config_file, (int) line_no);
  135. exit(EXIT_FAILURE);
  136. }
  137. if (mg_set_option(ctx, opt, val) != 1)
  138. exit(EXIT_FAILURE);
  139. }
  140. (void) fclose(fp);
  141. }
  142. /* Now pass through the command line options */
  143. for (i = 1; argv[i] != NULL && argv[i][0] == '-'; i += 2)
  144. if (mg_set_option(ctx, &argv[i][1], argv[i + 1]) != 1)
  145. exit(EXIT_FAILURE);
  146. }
  147. int
  148. main(int argc, char *argv[])
  149. {
  150. struct mg_context *ctx;
  151. char ports[1024], web_root[1024];
  152. if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'A') {
  153. if (argc != 6)
  154. show_usage_and_exit();
  155. exit(mg_edit_passwords(argv[2], argv[3], argv[4], argv[5]) ==
  156. MG_SUCCESS ? EXIT_SUCCESS : EXIT_FAILURE);
  157. }
  158. if (argc == 2 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")))
  159. show_usage_and_exit();
  160. #ifndef _WIN32
  161. (void) signal(SIGCHLD, signal_handler);
  162. #endif /* _WIN32 */
  163. (void) signal(SIGTERM, signal_handler);
  164. (void) signal(SIGINT, signal_handler);
  165. if ((ctx = mg_start()) == NULL) {
  166. (void) printf("%s\n", "Cannot initialize Mongoose context");
  167. exit(EXIT_FAILURE);
  168. }
  169. process_command_line_arguments(ctx, argv);
  170. (void) mg_get_option(ctx, "ports", ports, sizeof(ports));
  171. if (ports[0] == '\0' &&
  172. mg_set_option(ctx, "ports", "8080") != MG_SUCCESS)
  173. exit(EXIT_FAILURE);
  174. (void) mg_get_option(ctx, "ports", ports, sizeof(ports));
  175. (void) mg_get_option(ctx, "root", web_root, sizeof(web_root));
  176. (void) printf("Mongoose %s started on port(s) \"%s\", "
  177. "serving directory \"%s\"\n", mg_version(), ports, web_root);
  178. fflush(stdout);
  179. while (exit_flag == 0)
  180. sleep(1);
  181. (void) printf("Exiting on signal %d, "
  182. "waiting for all threads to finish...", exit_flag);
  183. fflush(stdout);
  184. mg_stop(ctx);
  185. (void) printf("%s", " done.\n");
  186. return (EXIT_SUCCESS);
  187. }