main.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. // Copyright (c) 2004-2011 Sergey Lyubka
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE.
  20. #if defined(_WIN32)
  21. #define _CRT_SECURE_NO_WARNINGS // Disable deprecation warning in VS2005
  22. #else
  23. #define _XOPEN_SOURCE 600 // For PATH_MAX on linux
  24. #endif
  25. #include <sys/stat.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <signal.h>
  29. #include <string.h>
  30. #include <errno.h>
  31. #include <limits.h>
  32. #include <stddef.h>
  33. #include <stdarg.h>
  34. #include <ctype.h>
  35. #include "mongoose.h"
  36. #ifdef _WIN32
  37. #include <windows.h>
  38. #include <winsvc.h>
  39. #define PATH_MAX MAX_PATH
  40. #define S_ISDIR(x) ((x) & _S_IFDIR)
  41. #define DIRSEP '\\'
  42. #define snprintf _snprintf
  43. #define vsnprintf _vsnprintf
  44. #define sleep(x) Sleep((x) * 1000)
  45. #define WINCDECL __cdecl
  46. #else
  47. #include <sys/wait.h>
  48. #include <unistd.h>
  49. #define DIRSEP '/'
  50. #define WINCDECL
  51. #endif // _WIN32
  52. #define MAX_OPTIONS 40
  53. #define MAX_CONF_FILE_LINE_SIZE (8 * 1024)
  54. static int exit_flag;
  55. static char server_name[40]; // Set by init_server_name()
  56. static char config_file[PATH_MAX]; // Set by process_command_line_arguments()
  57. static struct mg_context *ctx; // Set by start_mongoose()
  58. #if !defined(CONFIG_FILE)
  59. #define CONFIG_FILE "mongoose.conf"
  60. #endif /* !CONFIG_FILE */
  61. static void WINCDECL signal_handler(int sig_num) {
  62. exit_flag = sig_num;
  63. }
  64. static void die(const char *fmt, ...) {
  65. va_list ap;
  66. char msg[200];
  67. va_start(ap, fmt);
  68. vsnprintf(msg, sizeof(msg), fmt, ap);
  69. va_end(ap);
  70. #if defined(_WIN32)
  71. MessageBox(NULL, msg, "Error", MB_OK);
  72. #else
  73. fprintf(stderr, "%s\n", msg);
  74. #endif
  75. exit(EXIT_FAILURE);
  76. }
  77. static void show_usage_and_exit(void) {
  78. const char **names;
  79. int i;
  80. fprintf(stderr, "Mongoose version %s (c) Sergey Lyubka, built %s\n",
  81. mg_version(), __DATE__);
  82. fprintf(stderr, "Usage:\n");
  83. fprintf(stderr, " mongoose -A <htpasswd_file> <realm> <user> <passwd>\n");
  84. fprintf(stderr, " mongoose <config_file>\n");
  85. fprintf(stderr, " mongoose [-option value ...]\n");
  86. fprintf(stderr, "\nOPTIONS:\n");
  87. names = mg_get_valid_option_names();
  88. for (i = 0; names[i] != NULL; i += 3) {
  89. fprintf(stderr, " -%s %s (default: \"%s\")\n",
  90. names[i], names[i + 1], names[i + 2] == NULL ? "" : names[i + 2]);
  91. }
  92. fprintf(stderr, "\nSee http://code.google.com/p/mongoose/wiki/MongooseManual"
  93. " for more details.\n");
  94. fprintf(stderr, "Example:\n mongoose -s cert.pem -p 80,443s -d no\n");
  95. exit(EXIT_FAILURE);
  96. }
  97. static void verify_document_root(const char *root) {
  98. const char *p, *path;
  99. char buf[PATH_MAX];
  100. struct stat st;
  101. path = root;
  102. if ((p = strchr(root, ',')) != NULL && (size_t) (p - root) < sizeof(buf)) {
  103. memcpy(buf, root, p - root);
  104. buf[p - root] = '\0';
  105. path = buf;
  106. }
  107. if (stat(path, &st) != 0 || !S_ISDIR(st.st_mode)) {
  108. die("Invalid root directory: [%s]: %s", root, strerror(errno));
  109. }
  110. }
  111. static char *sdup(const char *str) {
  112. char *p;
  113. if ((p = (char *) malloc(strlen(str) + 1)) != NULL) {
  114. strcpy(p, str);
  115. }
  116. return p;
  117. }
  118. static void set_option(char **options, const char *name, const char *value) {
  119. int i;
  120. if (!strcmp(name, "document_root") || !(strcmp(name, "r"))) {
  121. verify_document_root(value);
  122. }
  123. for (i = 0; i < MAX_OPTIONS - 3; i++) {
  124. if (options[i] == NULL) {
  125. options[i] = sdup(name);
  126. options[i + 1] = sdup(value);
  127. options[i + 2] = NULL;
  128. break;
  129. }
  130. }
  131. if (i == MAX_OPTIONS - 3) {
  132. die("%s", "Too many options specified");
  133. }
  134. }
  135. static void process_command_line_arguments(char *argv[], char **options) {
  136. char line[MAX_CONF_FILE_LINE_SIZE], opt[sizeof(line)], val[sizeof(line)], *p;
  137. FILE *fp = NULL;
  138. size_t i, cmd_line_opts_start = 1, line_no = 0;
  139. options[0] = NULL;
  140. // Should we use a config file ?
  141. if (argv[1] != NULL && argv[1][0] != '-') {
  142. snprintf(config_file, sizeof(config_file), "%s", argv[1]);
  143. cmd_line_opts_start = 2;
  144. } else if ((p = strrchr(argv[0], DIRSEP)) == NULL) {
  145. // No command line flags specified. Look where binary lives
  146. snprintf(config_file, sizeof(config_file), "%s", CONFIG_FILE);
  147. } else {
  148. snprintf(config_file, sizeof(config_file), "%.*s%c%s",
  149. (int) (p - argv[0]), argv[0], DIRSEP, CONFIG_FILE);
  150. }
  151. fp = fopen(config_file, "r");
  152. // If config file was set in command line and open failed, die
  153. if (cmd_line_opts_start == 2 && fp == NULL) {
  154. die("Cannot open config file %s: %s", config_file, strerror(errno));
  155. }
  156. // Load config file settings first
  157. if (fp != NULL) {
  158. fprintf(stderr, "Loading config file %s\n", config_file);
  159. // Loop over the lines in config file
  160. while (fgets(line, sizeof(line), fp) != NULL) {
  161. line_no++;
  162. // Ignore empty lines and comments
  163. for (i = 0; isspace(* (unsigned char *) &line[i]); ) i++;
  164. if (line[i] == '#' || line[i] == '\0')
  165. continue;
  166. if (sscanf(line, "%s %[^\r\n#]", opt, val) != 2) {
  167. die("%s: line %d is invalid", config_file, (int) line_no);
  168. }
  169. set_option(options, opt, val);
  170. }
  171. (void) fclose(fp);
  172. }
  173. // Handle command line flags. They override config file and default settings.
  174. for (i = cmd_line_opts_start; argv[i] != NULL; i += 2) {
  175. if (argv[i][0] != '-' || argv[i + 1] == NULL) {
  176. show_usage_and_exit();
  177. }
  178. set_option(options, &argv[i][1], argv[i + 1]);
  179. }
  180. }
  181. static void init_server_name(void) {
  182. snprintf(server_name, sizeof(server_name), "Mongoose web server v. %s",
  183. mg_version());
  184. }
  185. static void *mongoose_callback(enum mg_event ev, struct mg_connection *conn) {
  186. if (ev == MG_EVENT_LOG) {
  187. printf("%s\n", (const char *) mg_get_request_info(conn)->ev_data);
  188. }
  189. // Returning NULL marks request as not handled, signalling mongoose to
  190. // proceed with handling it.
  191. return NULL;
  192. }
  193. static void start_mongoose(int argc, char *argv[]) {
  194. char *options[MAX_OPTIONS];
  195. int i;
  196. // Edit passwords file if -A option is specified
  197. if (argc > 1 && !strcmp(argv[1], "-A")) {
  198. if (argc != 6) {
  199. show_usage_and_exit();
  200. }
  201. exit(mg_modify_passwords_file(argv[2], argv[3], argv[4], argv[5]) ?
  202. EXIT_SUCCESS : EXIT_FAILURE);
  203. }
  204. // Show usage if -h or --help options are specified
  205. if (argc == 2 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))) {
  206. show_usage_and_exit();
  207. }
  208. /* Update config based on command line arguments */
  209. process_command_line_arguments(argv, options);
  210. /* Setup signal handler: quit on Ctrl-C */
  211. signal(SIGTERM, signal_handler);
  212. signal(SIGINT, signal_handler);
  213. /* Start Mongoose */
  214. ctx = mg_start(&mongoose_callback, NULL, (const char **) options);
  215. for (i = 0; options[i] != NULL; i++) {
  216. free(options[i]);
  217. }
  218. if (ctx == NULL) {
  219. die("%s", "Failed to start Mongoose.");
  220. }
  221. }
  222. #ifdef _WIN32
  223. static SERVICE_STATUS ss;
  224. static SERVICE_STATUS_HANDLE hStatus;
  225. static const char *service_magic_argument = "--";
  226. static void WINAPI ControlHandler(DWORD code) {
  227. if (code == SERVICE_CONTROL_STOP || code == SERVICE_CONTROL_SHUTDOWN) {
  228. ss.dwWin32ExitCode = 0;
  229. ss.dwCurrentState = SERVICE_STOPPED;
  230. }
  231. SetServiceStatus(hStatus, &ss);
  232. }
  233. static void WINAPI ServiceMain(void) {
  234. ss.dwServiceType = SERVICE_WIN32;
  235. ss.dwCurrentState = SERVICE_RUNNING;
  236. ss.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
  237. hStatus = RegisterServiceCtrlHandler(server_name, ControlHandler);
  238. SetServiceStatus(hStatus, &ss);
  239. while (ss.dwCurrentState == SERVICE_RUNNING) {
  240. Sleep(1000);
  241. }
  242. mg_stop(ctx);
  243. ss.dwCurrentState = SERVICE_STOPPED;
  244. ss.dwWin32ExitCode = (DWORD) -1;
  245. SetServiceStatus(hStatus, &ss);
  246. }
  247. #define ID_TRAYICON 100
  248. #define ID_QUIT 101
  249. #define ID_EDIT_CONFIG 102
  250. #define ID_SEPARATOR 103
  251. #define ID_INSTALL_SERVICE 104
  252. #define ID_REMOVE_SERVICE 105
  253. #define ID_ICON 200
  254. static NOTIFYICONDATA TrayIcon;
  255. static void edit_config_file(void) {
  256. const char **names, *value;
  257. FILE *fp;
  258. int i;
  259. char cmd[200];
  260. // Create config file if it is not present yet
  261. if ((fp = fopen(config_file, "r")) != NULL) {
  262. fclose(fp);
  263. } else if ((fp = fopen(config_file, "a+")) != NULL) {
  264. fprintf(fp,
  265. "# Mongoose web server configuration file.\n"
  266. "# Lines starting with '#' and empty lines are ignored.\n"
  267. "# For detailed description of every option, visit\n"
  268. "# http://code.google.com/p/mongoose/wiki/MongooseManual\n\n");
  269. names = mg_get_valid_option_names();
  270. for (i = 0; names[i] != NULL; i += 3) {
  271. value = mg_get_option(ctx, names[i]);
  272. fprintf(fp, "# %s %s\n", names[i + 1], *value ? value : "<value>");
  273. }
  274. fclose(fp);
  275. }
  276. snprintf(cmd, sizeof(cmd), "notepad.exe %s", config_file);
  277. WinExec(cmd, SW_SHOW);
  278. }
  279. static void show_error(void) {
  280. char buf[256];
  281. FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  282. NULL, GetLastError(),
  283. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  284. buf, sizeof(buf), NULL);
  285. MessageBox(NULL, buf, "Error", MB_OK);
  286. }
  287. static int manage_service(int action) {
  288. static const char *service_name = "Mongoose";
  289. SC_HANDLE hSCM = NULL, hService = NULL;
  290. SERVICE_DESCRIPTION descr = {server_name};
  291. char path[PATH_MAX + 20]; // Path to executable plus magic argument
  292. int success = 1;
  293. if ((hSCM = OpenSCManager(NULL, NULL, action == ID_INSTALL_SERVICE ?
  294. GENERIC_WRITE : GENERIC_READ)) == NULL) {
  295. success = 0;
  296. show_error();
  297. } else if (action == ID_INSTALL_SERVICE) {
  298. GetModuleFileName(NULL, path, sizeof(path));
  299. strncat(path, " ", sizeof(path));
  300. strncat(path, service_magic_argument, sizeof(path));
  301. hService = CreateService(hSCM, service_name, service_name,
  302. SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
  303. SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
  304. path, NULL, NULL, NULL, NULL, NULL);
  305. if (hService) {
  306. ChangeServiceConfig2(hService, SERVICE_CONFIG_DESCRIPTION, &descr);
  307. } else {
  308. show_error();
  309. }
  310. } else if (action == ID_REMOVE_SERVICE) {
  311. if ((hService = OpenService(hSCM, service_name, DELETE)) == NULL ||
  312. !DeleteService(hService)) {
  313. show_error();
  314. }
  315. } else if ((hService = OpenService(hSCM, service_name,
  316. SERVICE_QUERY_STATUS)) == NULL) {
  317. success = 0;
  318. }
  319. CloseServiceHandle(hService);
  320. CloseServiceHandle(hSCM);
  321. return success;
  322. }
  323. static LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam,
  324. LPARAM lParam) {
  325. static SERVICE_TABLE_ENTRY service_table[] = {
  326. {server_name, (LPSERVICE_MAIN_FUNCTION) ServiceMain},
  327. {NULL, NULL}
  328. };
  329. int service_installed;
  330. char buf[200], *service_argv[] = {__argv[0], NULL};
  331. POINT pt;
  332. HMENU hMenu;
  333. switch (msg) {
  334. case WM_CREATE:
  335. if (__argv[1] != NULL &&
  336. !strcmp(__argv[1], service_magic_argument)) {
  337. start_mongoose(1, service_argv);
  338. StartServiceCtrlDispatcher(service_table);
  339. exit(EXIT_SUCCESS);
  340. } else {
  341. start_mongoose(__argc, __argv);
  342. }
  343. break;
  344. case WM_COMMAND:
  345. switch (LOWORD(wParam)) {
  346. case ID_QUIT:
  347. mg_stop(ctx);
  348. Shell_NotifyIcon(NIM_DELETE, &TrayIcon);
  349. PostQuitMessage(0);
  350. break;
  351. case ID_EDIT_CONFIG:
  352. edit_config_file();
  353. break;
  354. case ID_INSTALL_SERVICE:
  355. case ID_REMOVE_SERVICE:
  356. manage_service(LOWORD(wParam));
  357. break;
  358. }
  359. break;
  360. case WM_USER:
  361. switch (lParam) {
  362. case WM_RBUTTONUP:
  363. case WM_LBUTTONUP:
  364. case WM_LBUTTONDBLCLK:
  365. hMenu = CreatePopupMenu();
  366. AppendMenu(hMenu, MF_STRING | MF_GRAYED, ID_SEPARATOR, server_name);
  367. AppendMenu(hMenu, MF_SEPARATOR, ID_SEPARATOR, "");
  368. service_installed = manage_service(0);
  369. snprintf(buf, sizeof(buf), "NT service: %s installed",
  370. service_installed ? "" : "not");
  371. AppendMenu(hMenu, MF_STRING | MF_GRAYED, ID_SEPARATOR, buf);
  372. AppendMenu(hMenu, MF_STRING | (service_installed ? MF_GRAYED : 0),
  373. ID_INSTALL_SERVICE, "Install service");
  374. AppendMenu(hMenu, MF_STRING | (!service_installed ? MF_GRAYED : 0),
  375. ID_REMOVE_SERVICE, "Deinstall service");
  376. AppendMenu(hMenu, MF_SEPARATOR, ID_SEPARATOR, "");
  377. AppendMenu(hMenu, MF_STRING, ID_EDIT_CONFIG, "Edit config file");
  378. AppendMenu(hMenu, MF_STRING, ID_QUIT, "Exit");
  379. GetCursorPos(&pt);
  380. SetForegroundWindow(hWnd);
  381. TrackPopupMenu(hMenu, 0, pt.x, pt.y, 0, hWnd, NULL);
  382. PostMessage(hWnd, WM_NULL, 0, 0);
  383. DestroyMenu(hMenu);
  384. break;
  385. }
  386. break;
  387. case WM_CLOSE:
  388. mg_stop(ctx);
  389. Shell_NotifyIcon(NIM_DELETE, &TrayIcon);
  390. PostQuitMessage(0);
  391. return 0; // We've just sent our own quit message, with proper hwnd.
  392. }
  393. return DefWindowProc(hWnd, msg, wParam, lParam);
  394. }
  395. int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR cmdline, int show) {
  396. WNDCLASS cls;
  397. HWND hWnd;
  398. MSG msg;
  399. init_server_name();
  400. memset(&cls, 0, sizeof(cls));
  401. cls.lpfnWndProc = (WNDPROC) WindowProc;
  402. cls.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  403. cls.lpszClassName = server_name;
  404. RegisterClass(&cls);
  405. hWnd = CreateWindow(cls.lpszClassName, server_name, WS_OVERLAPPEDWINDOW,
  406. 0, 0, 0, 0, NULL, NULL, NULL, NULL);
  407. ShowWindow(hWnd, SW_HIDE);
  408. TrayIcon.cbSize = sizeof(TrayIcon);
  409. TrayIcon.uID = ID_TRAYICON;
  410. TrayIcon.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
  411. TrayIcon.hIcon = LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(ID_ICON),
  412. IMAGE_ICON, 16, 16, 0);
  413. TrayIcon.hWnd = hWnd;
  414. snprintf(TrayIcon.szTip, sizeof(TrayIcon.szTip), "%s", server_name);
  415. TrayIcon.uCallbackMessage = WM_USER;
  416. Shell_NotifyIcon(NIM_ADD, &TrayIcon);
  417. while (GetMessage(&msg, hWnd, 0, 0) > 0) {
  418. TranslateMessage(&msg);
  419. DispatchMessage(&msg);
  420. }
  421. // Return the WM_QUIT value.
  422. return msg.wParam;
  423. }
  424. #elif defined(USE_COCOA)
  425. #import <Cocoa/Cocoa.h>
  426. @interface Mongoose : NSObject<NSApplicationDelegate>
  427. - (void) openBrowser;
  428. - (void) shutDown;
  429. @end
  430. @implementation Mongoose
  431. - (void) openBrowser {
  432. [[NSWorkspace sharedWorkspace]
  433. openURL:[NSURL URLWithString:
  434. [NSString stringWithUTF8String:"http://www.yahoo.com"]]];
  435. }
  436. - (void) editConfig {
  437. [[NSWorkspace sharedWorkspace]
  438. openFile:@"mongoose.conf" withApplication:@"TextEdit"];
  439. }
  440. - (void)shutDown{
  441. [NSApp terminate:nil];
  442. }
  443. @end
  444. int main(int argc, char *argv[]) {
  445. init_server_name();
  446. start_mongoose(argc, argv);
  447. [NSAutoreleasePool new];
  448. [NSApplication sharedApplication];
  449. // Add delegate to process menu item actions
  450. Mongoose *myDelegate = [[Mongoose alloc] autorelease];
  451. [NSApp setDelegate: myDelegate];
  452. // Run this app as agent
  453. ProcessSerialNumber psn = { 0, kCurrentProcess };
  454. TransformProcessType(&psn, kProcessTransformToBackgroundApplication);
  455. SetFrontProcess(&psn);
  456. // Add status bar menu
  457. id menu = [[NSMenu new] autorelease];
  458. // Add version menu item
  459. [menu addItem:[[[NSMenuItem alloc]
  460. //initWithTitle:[NSString stringWithFormat:@"%s", server_name]
  461. initWithTitle:[NSString stringWithUTF8String:server_name]
  462. action:@selector(noexist) keyEquivalent:@""] autorelease]];
  463. // Add configuration menu item
  464. [menu addItem:[[[NSMenuItem alloc]
  465. initWithTitle:@"Edit configuration"
  466. action:@selector(editConfig) keyEquivalent:@""] autorelease]];
  467. // Add connect menu item
  468. [menu addItem:[[[NSMenuItem alloc]
  469. initWithTitle:@"Open web root in a browser"
  470. action:@selector(openBrowser) keyEquivalent:@""] autorelease]];
  471. // Separator
  472. [menu addItem:[NSMenuItem separatorItem]];
  473. // Add quit menu item
  474. [menu addItem:[[[NSMenuItem alloc]
  475. initWithTitle:@"Quit"
  476. action:@selector(shutDown) keyEquivalent:@"q"] autorelease]];
  477. // Attach menu to the status bar
  478. id item = [[[NSStatusBar systemStatusBar]
  479. statusItemWithLength:NSVariableStatusItemLength] retain];
  480. [item setHighlightMode:YES];
  481. [item setImage:[NSImage imageNamed:@"mongoose_22x22.png"]];
  482. [item setMenu:menu];
  483. // Run the app
  484. [NSApp activateIgnoringOtherApps:YES];
  485. [NSApp run];
  486. mg_stop(ctx);
  487. return EXIT_SUCCESS;
  488. }
  489. #else
  490. int main(int argc, char *argv[]) {
  491. init_server_name();
  492. start_mongoose(argc, argv);
  493. printf("%s started on port(s) %s with web root [%s]\n",
  494. server_name, mg_get_option(ctx, "listening_ports"),
  495. mg_get_option(ctx, "document_root"));
  496. while (exit_flag == 0) {
  497. sleep(1);
  498. }
  499. printf("Exiting on signal %d, waiting for all threads to finish...",
  500. exit_flag);
  501. fflush(stdout);
  502. mg_stop(ctx);
  503. printf("%s", " done.\n");
  504. return EXIT_SUCCESS;
  505. }
  506. #endif /* _WIN32 */