main.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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. #if defined(_WIN32) || defined(USE_COCOA)
  98. static void create_config_file(const char *path) {
  99. const char **names, *value;
  100. FILE *fp;
  101. int i;
  102. // Create config file if it is not present yet
  103. if ((fp = fopen(path, "r")) != NULL) {
  104. fclose(fp);
  105. } else if ((fp = fopen(path, "a+")) != NULL) {
  106. fprintf(fp, "%s",
  107. "# Mongoose web server configuration file.\n"
  108. "# For detailed description of every option, visit\n"
  109. "# https://github.com/valenok/mongoose/blob/master/UserManual.md\n"
  110. "# Lines starting with '#' and empty lines are ignored.\n"
  111. "# To make a change, remove leading '#', modify option's value,\n"
  112. "# save this file and then restart Mongoose.\n\n");
  113. names = mg_get_valid_option_names();
  114. for (i = 0; names[i] != NULL; i += 3) {
  115. value = mg_get_option(ctx, names[i]);
  116. fprintf(fp, "# %s %s\n", names[i + 1], *value ? value : "<value>");
  117. }
  118. fclose(fp);
  119. }
  120. }
  121. #endif
  122. static void verify_document_root(const char *root) {
  123. const char *p, *path;
  124. char buf[PATH_MAX];
  125. struct stat st;
  126. path = root;
  127. if ((p = strchr(root, ',')) != NULL && (size_t) (p - root) < sizeof(buf)) {
  128. memcpy(buf, root, p - root);
  129. buf[p - root] = '\0';
  130. path = buf;
  131. }
  132. if (stat(path, &st) != 0 || !S_ISDIR(st.st_mode)) {
  133. die("Invalid root directory: [%s]: %s", root, strerror(errno));
  134. }
  135. }
  136. static char *sdup(const char *str) {
  137. char *p;
  138. if ((p = (char *) malloc(strlen(str) + 1)) != NULL) {
  139. strcpy(p, str);
  140. }
  141. return p;
  142. }
  143. static void set_option(char **options, const char *name, const char *value) {
  144. int i;
  145. if (!strcmp(name, "document_root") || !(strcmp(name, "r"))) {
  146. verify_document_root(value);
  147. }
  148. for (i = 0; i < MAX_OPTIONS - 3; i++) {
  149. if (options[i] == NULL) {
  150. options[i] = sdup(name);
  151. options[i + 1] = sdup(value);
  152. options[i + 2] = NULL;
  153. break;
  154. }
  155. }
  156. if (i == MAX_OPTIONS - 3) {
  157. die("%s", "Too many options specified");
  158. }
  159. }
  160. static void process_command_line_arguments(char *argv[], char **options) {
  161. char line[MAX_CONF_FILE_LINE_SIZE], opt[sizeof(line)], val[sizeof(line)], *p;
  162. FILE *fp = NULL;
  163. size_t i, cmd_line_opts_start = 1, line_no = 0;
  164. options[0] = NULL;
  165. // Should we use a config file ?
  166. if (argv[1] != NULL && argv[1][0] != '-') {
  167. snprintf(config_file, sizeof(config_file), "%s", argv[1]);
  168. cmd_line_opts_start = 2;
  169. } else if ((p = strrchr(argv[0], DIRSEP)) == NULL) {
  170. // No command line flags specified. Look where binary lives
  171. snprintf(config_file, sizeof(config_file), "%s", CONFIG_FILE);
  172. } else {
  173. snprintf(config_file, sizeof(config_file), "%.*s%c%s",
  174. (int) (p - argv[0]), argv[0], DIRSEP, CONFIG_FILE);
  175. }
  176. fp = fopen(config_file, "r");
  177. // If config file was set in command line and open failed, die
  178. if (cmd_line_opts_start == 2 && fp == NULL) {
  179. die("Cannot open config file %s: %s", config_file, strerror(errno));
  180. }
  181. // Load config file settings first
  182. if (fp != NULL) {
  183. fprintf(stderr, "Loading config file %s\n", config_file);
  184. // Loop over the lines in config file
  185. while (fgets(line, sizeof(line), fp) != NULL) {
  186. line_no++;
  187. // Ignore empty lines and comments
  188. for (i = 0; isspace(* (unsigned char *) &line[i]); ) i++;
  189. if (line[i] == '#' || line[i] == '\0')
  190. continue;
  191. if (sscanf(line, "%s %[^\r\n#]", opt, val) != 2) {
  192. die("%s: line %d is invalid", config_file, (int) line_no);
  193. }
  194. set_option(options, opt, val);
  195. }
  196. (void) fclose(fp);
  197. }
  198. // Handle command line flags. They override config file and default settings.
  199. for (i = cmd_line_opts_start; argv[i] != NULL; i += 2) {
  200. if (argv[i][0] != '-' || argv[i + 1] == NULL) {
  201. show_usage_and_exit();
  202. }
  203. set_option(options, &argv[i][1], argv[i + 1]);
  204. }
  205. }
  206. static void init_server_name(void) {
  207. snprintf(server_name, sizeof(server_name), "Mongoose web server v. %s",
  208. mg_version());
  209. }
  210. static void *mongoose_callback(enum mg_event ev, struct mg_connection *conn) {
  211. if (ev == MG_EVENT_LOG) {
  212. printf("%s\n", (const char *) mg_get_request_info(conn)->ev_data);
  213. }
  214. // Returning NULL marks request as not handled, signalling mongoose to
  215. // proceed with handling it.
  216. return NULL;
  217. }
  218. static void start_mongoose(int argc, char *argv[]) {
  219. char *options[MAX_OPTIONS];
  220. int i;
  221. // Edit passwords file if -A option is specified
  222. if (argc > 1 && !strcmp(argv[1], "-A")) {
  223. if (argc != 6) {
  224. show_usage_and_exit();
  225. }
  226. exit(mg_modify_passwords_file(argv[2], argv[3], argv[4], argv[5]) ?
  227. EXIT_SUCCESS : EXIT_FAILURE);
  228. }
  229. // Show usage if -h or --help options are specified
  230. if (argc == 2 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))) {
  231. show_usage_and_exit();
  232. }
  233. /* Update config based on command line arguments */
  234. process_command_line_arguments(argv, options);
  235. /* Setup signal handler: quit on Ctrl-C */
  236. signal(SIGTERM, signal_handler);
  237. signal(SIGINT, signal_handler);
  238. /* Start Mongoose */
  239. ctx = mg_start(&mongoose_callback, NULL, (const char **) options);
  240. for (i = 0; options[i] != NULL; i++) {
  241. free(options[i]);
  242. }
  243. if (ctx == NULL) {
  244. die("%s", "Failed to start Mongoose.");
  245. }
  246. }
  247. #ifdef _WIN32
  248. static SERVICE_STATUS ss;
  249. static SERVICE_STATUS_HANDLE hStatus;
  250. static const char *service_magic_argument = "--";
  251. static void WINAPI ControlHandler(DWORD code) {
  252. if (code == SERVICE_CONTROL_STOP || code == SERVICE_CONTROL_SHUTDOWN) {
  253. ss.dwWin32ExitCode = 0;
  254. ss.dwCurrentState = SERVICE_STOPPED;
  255. }
  256. SetServiceStatus(hStatus, &ss);
  257. }
  258. static void WINAPI ServiceMain(void) {
  259. ss.dwServiceType = SERVICE_WIN32;
  260. ss.dwCurrentState = SERVICE_RUNNING;
  261. ss.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
  262. hStatus = RegisterServiceCtrlHandler(server_name, ControlHandler);
  263. SetServiceStatus(hStatus, &ss);
  264. while (ss.dwCurrentState == SERVICE_RUNNING) {
  265. Sleep(1000);
  266. }
  267. mg_stop(ctx);
  268. ss.dwCurrentState = SERVICE_STOPPED;
  269. ss.dwWin32ExitCode = (DWORD) -1;
  270. SetServiceStatus(hStatus, &ss);
  271. }
  272. #define ID_TRAYICON 100
  273. #define ID_QUIT 101
  274. #define ID_EDIT_CONFIG 102
  275. #define ID_SEPARATOR 103
  276. #define ID_INSTALL_SERVICE 104
  277. #define ID_REMOVE_SERVICE 105
  278. #define ID_ICON 200
  279. static NOTIFYICONDATA TrayIcon;
  280. static void edit_config_file(void) {
  281. char cmd[200];
  282. create_config_file(config_file);
  283. snprintf(cmd, sizeof(cmd), "notepad.exe %s", config_file);
  284. WinExec(cmd, SW_SHOW);
  285. }
  286. static void show_error(void) {
  287. char buf[256];
  288. FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  289. NULL, GetLastError(),
  290. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  291. buf, sizeof(buf), NULL);
  292. MessageBox(NULL, buf, "Error", MB_OK);
  293. }
  294. static int manage_service(int action) {
  295. static const char *service_name = "Mongoose";
  296. SC_HANDLE hSCM = NULL, hService = NULL;
  297. SERVICE_DESCRIPTION descr = {server_name};
  298. char path[PATH_MAX + 20]; // Path to executable plus magic argument
  299. int success = 1;
  300. if ((hSCM = OpenSCManager(NULL, NULL, action == ID_INSTALL_SERVICE ?
  301. GENERIC_WRITE : GENERIC_READ)) == NULL) {
  302. success = 0;
  303. show_error();
  304. } else if (action == ID_INSTALL_SERVICE) {
  305. GetModuleFileName(NULL, path, sizeof(path));
  306. strncat(path, " ", sizeof(path));
  307. strncat(path, service_magic_argument, sizeof(path));
  308. hService = CreateService(hSCM, service_name, service_name,
  309. SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
  310. SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
  311. path, NULL, NULL, NULL, NULL, NULL);
  312. if (hService) {
  313. ChangeServiceConfig2(hService, SERVICE_CONFIG_DESCRIPTION, &descr);
  314. } else {
  315. show_error();
  316. }
  317. } else if (action == ID_REMOVE_SERVICE) {
  318. if ((hService = OpenService(hSCM, service_name, DELETE)) == NULL ||
  319. !DeleteService(hService)) {
  320. show_error();
  321. }
  322. } else if ((hService = OpenService(hSCM, service_name,
  323. SERVICE_QUERY_STATUS)) == NULL) {
  324. success = 0;
  325. }
  326. CloseServiceHandle(hService);
  327. CloseServiceHandle(hSCM);
  328. return success;
  329. }
  330. static LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam,
  331. LPARAM lParam) {
  332. static SERVICE_TABLE_ENTRY service_table[] = {
  333. {server_name, (LPSERVICE_MAIN_FUNCTION) ServiceMain},
  334. {NULL, NULL}
  335. };
  336. int service_installed;
  337. char buf[200], *service_argv[] = {__argv[0], NULL};
  338. POINT pt;
  339. HMENU hMenu;
  340. switch (msg) {
  341. case WM_CREATE:
  342. if (__argv[1] != NULL &&
  343. !strcmp(__argv[1], service_magic_argument)) {
  344. start_mongoose(1, service_argv);
  345. StartServiceCtrlDispatcher(service_table);
  346. exit(EXIT_SUCCESS);
  347. } else {
  348. start_mongoose(__argc, __argv);
  349. }
  350. break;
  351. case WM_COMMAND:
  352. switch (LOWORD(wParam)) {
  353. case ID_QUIT:
  354. mg_stop(ctx);
  355. Shell_NotifyIcon(NIM_DELETE, &TrayIcon);
  356. PostQuitMessage(0);
  357. break;
  358. case ID_EDIT_CONFIG:
  359. edit_config_file();
  360. break;
  361. case ID_INSTALL_SERVICE:
  362. case ID_REMOVE_SERVICE:
  363. manage_service(LOWORD(wParam));
  364. break;
  365. }
  366. break;
  367. case WM_USER:
  368. switch (lParam) {
  369. case WM_RBUTTONUP:
  370. case WM_LBUTTONUP:
  371. case WM_LBUTTONDBLCLK:
  372. hMenu = CreatePopupMenu();
  373. AppendMenu(hMenu, MF_STRING | MF_GRAYED, ID_SEPARATOR, server_name);
  374. AppendMenu(hMenu, MF_SEPARATOR, ID_SEPARATOR, "");
  375. service_installed = manage_service(0);
  376. snprintf(buf, sizeof(buf), "NT service: %s installed",
  377. service_installed ? "" : "not");
  378. AppendMenu(hMenu, MF_STRING | MF_GRAYED, ID_SEPARATOR, buf);
  379. AppendMenu(hMenu, MF_STRING | (service_installed ? MF_GRAYED : 0),
  380. ID_INSTALL_SERVICE, "Install service");
  381. AppendMenu(hMenu, MF_STRING | (!service_installed ? MF_GRAYED : 0),
  382. ID_REMOVE_SERVICE, "Deinstall service");
  383. AppendMenu(hMenu, MF_SEPARATOR, ID_SEPARATOR, "");
  384. AppendMenu(hMenu, MF_STRING, ID_EDIT_CONFIG, "Edit config file");
  385. AppendMenu(hMenu, MF_STRING, ID_QUIT, "Exit");
  386. GetCursorPos(&pt);
  387. SetForegroundWindow(hWnd);
  388. TrackPopupMenu(hMenu, 0, pt.x, pt.y, 0, hWnd, NULL);
  389. PostMessage(hWnd, WM_NULL, 0, 0);
  390. DestroyMenu(hMenu);
  391. break;
  392. }
  393. break;
  394. case WM_CLOSE:
  395. mg_stop(ctx);
  396. Shell_NotifyIcon(NIM_DELETE, &TrayIcon);
  397. PostQuitMessage(0);
  398. return 0; // We've just sent our own quit message, with proper hwnd.
  399. }
  400. return DefWindowProc(hWnd, msg, wParam, lParam);
  401. }
  402. int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR cmdline, int show) {
  403. WNDCLASS cls;
  404. HWND hWnd;
  405. MSG msg;
  406. init_server_name();
  407. memset(&cls, 0, sizeof(cls));
  408. cls.lpfnWndProc = (WNDPROC) WindowProc;
  409. cls.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  410. cls.lpszClassName = server_name;
  411. RegisterClass(&cls);
  412. hWnd = CreateWindow(cls.lpszClassName, server_name, WS_OVERLAPPEDWINDOW,
  413. 0, 0, 0, 0, NULL, NULL, NULL, NULL);
  414. ShowWindow(hWnd, SW_HIDE);
  415. TrayIcon.cbSize = sizeof(TrayIcon);
  416. TrayIcon.uID = ID_TRAYICON;
  417. TrayIcon.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
  418. TrayIcon.hIcon = LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(ID_ICON),
  419. IMAGE_ICON, 16, 16, 0);
  420. TrayIcon.hWnd = hWnd;
  421. snprintf(TrayIcon.szTip, sizeof(TrayIcon.szTip), "%s", server_name);
  422. TrayIcon.uCallbackMessage = WM_USER;
  423. Shell_NotifyIcon(NIM_ADD, &TrayIcon);
  424. while (GetMessage(&msg, hWnd, 0, 0) > 0) {
  425. TranslateMessage(&msg);
  426. DispatchMessage(&msg);
  427. }
  428. // Return the WM_QUIT value.
  429. return msg.wParam;
  430. }
  431. #elif defined(USE_COCOA)
  432. #import <Cocoa/Cocoa.h>
  433. @interface Mongoose : NSObject<NSApplicationDelegate>
  434. - (void) openBrowser;
  435. - (void) shutDown;
  436. @end
  437. @implementation Mongoose
  438. - (void) openBrowser {
  439. [[NSWorkspace sharedWorkspace]
  440. openURL:[NSURL URLWithString:
  441. [NSString stringWithUTF8String:"http://www.yahoo.com"]]];
  442. }
  443. - (void) editConfig {
  444. create_config_file(config_file);
  445. [[NSWorkspace sharedWorkspace]
  446. openFile:[NSString stringWithUTF8String:config_file]
  447. withApplication:@"TextEdit"];
  448. }
  449. - (void)shutDown{
  450. [NSApp terminate:nil];
  451. }
  452. @end
  453. int main(int argc, char *argv[]) {
  454. init_server_name();
  455. start_mongoose(argc, argv);
  456. [NSAutoreleasePool new];
  457. [NSApplication sharedApplication];
  458. // Add delegate to process menu item actions
  459. Mongoose *myDelegate = [[Mongoose alloc] autorelease];
  460. [NSApp setDelegate: myDelegate];
  461. // Run this app as agent
  462. ProcessSerialNumber psn = { 0, kCurrentProcess };
  463. TransformProcessType(&psn, kProcessTransformToBackgroundApplication);
  464. SetFrontProcess(&psn);
  465. // Add status bar menu
  466. id menu = [[NSMenu new] autorelease];
  467. // Add version menu item
  468. [menu addItem:[[[NSMenuItem alloc]
  469. //initWithTitle:[NSString stringWithFormat:@"%s", server_name]
  470. initWithTitle:[NSString stringWithUTF8String:server_name]
  471. action:@selector(noexist) keyEquivalent:@""] autorelease]];
  472. // Add configuration menu item
  473. [menu addItem:[[[NSMenuItem alloc]
  474. initWithTitle:@"Edit configuration"
  475. action:@selector(editConfig) keyEquivalent:@""] autorelease]];
  476. // Add connect menu item
  477. [menu addItem:[[[NSMenuItem alloc]
  478. initWithTitle:@"Open web root in a browser"
  479. action:@selector(openBrowser) keyEquivalent:@""] autorelease]];
  480. // Separator
  481. [menu addItem:[NSMenuItem separatorItem]];
  482. // Add quit menu item
  483. [menu addItem:[[[NSMenuItem alloc]
  484. initWithTitle:@"Quit"
  485. action:@selector(shutDown) keyEquivalent:@"q"] autorelease]];
  486. // Attach menu to the status bar
  487. id item = [[[NSStatusBar systemStatusBar]
  488. statusItemWithLength:NSVariableStatusItemLength] retain];
  489. [item setHighlightMode:YES];
  490. [item setImage:[NSImage imageNamed:@"mongoose_22x22.png"]];
  491. [item setMenu:menu];
  492. // Run the app
  493. [NSApp activateIgnoringOtherApps:YES];
  494. [NSApp run];
  495. mg_stop(ctx);
  496. return EXIT_SUCCESS;
  497. }
  498. #else
  499. int main(int argc, char *argv[]) {
  500. init_server_name();
  501. start_mongoose(argc, argv);
  502. printf("%s started on port(s) %s with web root [%s]\n",
  503. server_name, mg_get_option(ctx, "listening_ports"),
  504. mg_get_option(ctx, "document_root"));
  505. while (exit_flag == 0) {
  506. sleep(1);
  507. }
  508. printf("Exiting on signal %d, waiting for all threads to finish...",
  509. exit_flag);
  510. fflush(stdout);
  511. mg_stop(ctx);
  512. printf("%s", " done.\n");
  513. return EXIT_SUCCESS;
  514. }
  515. #endif /* _WIN32 */