forked from GerHobbelt/civet-webserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
619 lines (526 loc) · 19.1 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
// Copyright (c) 2004-2012 Sergey Lyubka
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#include "mongoose.h"
#ifdef _WIN32
#include "win32/resource.h"
#endif // _WIN32
#define MAX_OPTIONS (1 + 27 /* NUM_OPTIONS */ * 3 /* once as defaults, once from config file, once from command line */)
#define MAX_CONF_FILE_LINE_SIZE (8 * 1024)
static volatile int exit_flag = 0;
static char server_name[40]; // Set by init_server_name()
static char config_file[PATH_MAX]; // Set by process_command_line_arguments()
static struct mg_context *ctx = NULL; // Set by start_mongoose()
#if !defined(CONFIG_FILE)
#define CONFIG_FILE "mongoose.conf"
#endif /* !CONFIG_FILE */
static void WINCDECL signal_handler(int sig_num) {
exit_flag = sig_num;
}
static const char *default_options[] = {
"document_root", "./test",
"listening_ports", "8080", // "8081,8082s"
//"ssl_certificate", "ssl_cert.pem",
"num_threads", "5",
"error_log_file", "./log/%Y/%m/tws_ib_if_srv-%Y%m%d.%H-IP-%[s]-%[p]-error.log",
"access_log_file", "./log/%Y/%m/tws_ib_if_srv-%Y%m%d.%H-IP-%[s]-%[p]-access.log",
"keep_alive_timeout", "5",
NULL
};
#if defined(_WIN32)
static int error_dialog_shown_previously = 0;
#endif
void die(const char *fmt, ...) {
va_list ap;
char msg[200];
va_start(ap, fmt);
vsnprintf(msg, sizeof(msg), fmt, ap);
va_end(ap);
#if defined(_WIN32)
if (!error_dialog_shown_previously)
{
MessageBoxA(NULL, msg, "Error", MB_OK);
error_dialog_shown_previously = 1;
}
#else
fprintf(stderr, "%s\n", msg);
#endif
exit(EXIT_FAILURE);
}
static void show_usage_and_exit(const struct mg_context *ctx) {
const char **names;
int i;
fprintf(stderr, "Mongoose version %s (c) Sergey Lyubka, built %s\n",
mg_version(), __DATE__);
fprintf(stderr, "Usage:\n");
fprintf(stderr, " mongoose -A <htpasswd_file> <realm> <user> <passwd>\n");
fprintf(stderr, " mongoose <config_file>\n");
fprintf(stderr, " mongoose [-option value ...]\n");
fprintf(stderr, "\nOPTIONS:\n");
names = mg_get_valid_option_names();
for (i = 0; names[i] != NULL; i += MG_ENTRIES_PER_CONFIG_OPTION) {
fprintf(stderr, " %s%s %s (default: \"%s\")\n",
(names[i][0] ? "-" : " "),
names[i], names[i + 1], names[i + 2] == NULL ? "" : names[i + 2]);
}
fprintf(stderr, "\nSee http://code.google.com/p/mongoose/wiki/MongooseManual"
" for more details.\n");
fprintf(stderr, "Example:\n mongoose -s cert.pem -p 80,443s -d no\n");
exit(EXIT_FAILURE);
}
static void verify_document_root(const char *root) {
struct mgstat st;
char buf[PATH_MAX];
getcwd(buf, sizeof(buf));
if (mg_stat(root, &st) != 0 || !st.is_directory) {
die("Invalid root directory: [%s]: %s; current directory = [%s]", root, mg_strerror(ERRNO), buf);
}
}
static void set_option(char **options, const char *name, const char *value) {
int i;
if (mg_get_option_long_name(name))
name = mg_get_option_long_name(name);
for (i = 0; i < MAX_OPTIONS * 2; i += 2) {
// replace option value when it was set before: command line overrules config file, which overrules global defaults.
if (options[i] == NULL) {
options[i] = mg_strdup(name);
options[i + 1] = mg_strdup(value);
break;
} else if (strcmp(options[i], name) == 0) {
free(options[i + 1]);
options[i + 1] = mg_strdup(value);
break;
}
}
if (i > MAX_OPTIONS * 2 - 2) {
die("Too many options specified");
}
}
static void process_command_line_arguments(char *argv[], char **options) {
char line[MAX_CONF_FILE_LINE_SIZE], opt[sizeof(line)], val[sizeof(line)], *p;
FILE *fp = NULL;
size_t i;
int cmd_line_opts_start = 1;
int line_no = 0;
options[0] = NULL;
// Should we use a config file ?
if (argv[1] != NULL && argv[2] == NULL) {
snprintf(config_file, sizeof(config_file), "%s", argv[1]);
cmd_line_opts_start = 2;
} else if ((p = strrchr(argv[0], DIRSEP)) == NULL) {
// No command line flags specified. Look where binary lives
snprintf(config_file, sizeof(config_file), "%s", CONFIG_FILE);
} else {
snprintf(config_file, sizeof(config_file), "%.*s%c%s",
(int) (p - argv[0]), argv[0], DIRSEP, CONFIG_FILE);
}
fp = mg_fopen(config_file, "r");
// If config file was set in command line and open failed, exit
if (argv[1] != NULL && argv[2] == NULL && fp == NULL) {
die("Cannot open config file %s: %s", config_file, mg_strerror(ERRNO));
}
// use the default values for starters (so that all options have a known reasonable value):
for (i = 0; default_options[i]; i += 2) {
set_option(options, default_options[i], default_options[i+1]);
}
// Load config file settings first
if (fp != NULL) {
fprintf(stderr, "Loading config file %s\n", config_file);
// Loop over the lines in config file
while (fgets(line, sizeof(line), fp) != NULL) {
if (!line_no && !memcmp(line,"\xEF\xBB\xBF", 3)) {
// strip UTF-8 BOM
p = line+3;
} else {
p = line;
}
line_no++;
// Ignore empty lines (with optional, ignored, whitespace) and comments
if (line[0] == '#')
continue;
// MS sscanf() says: The return value is EOF for an error or if the end of the string is reached before the first conversion.
// Hence we make sure we don't feed it an empty line --> -1 will only have one meaning then.
if (line[strspn(line, " \t\r\n")] == 0)
continue;
if (2 == sscanf(line, "%s %[^\r\n#]", opt, val)) {
set_option(options, opt, val);
continue;
} else {
die("%s: line %d is invalid", config_file, line_no);
break;
}
}
(void) mg_fclose(fp);
}
// Handle command line flags. They override config file and default settings.
for (i = cmd_line_opts_start; argv[i] != NULL; i += 2) {
if (argv[i][0] != '-' || argv[i + 1] == NULL) {
show_usage_and_exit(ctx);
}
set_option(options, &argv[i][1], argv[i + 1]);
}
}
static void init_server_name(void) {
snprintf(server_name, sizeof(server_name), "Mongoose web server v%s",
mg_version());
}
static void *mongoose_callback(enum mg_event event, struct mg_connection *conn) {
const struct mg_request_info *request_info = mg_get_request_info(conn);
if (event == MG_INIT0)
{
verify_document_root(mg_get_conn_option(conn, "document_root"));
return (void *)1;
}
#if defined(_WIN32)
if (event == MG_EVENT_LOG &&
strstr(request_info->log_message, "cannot bind to") &&
!strcmp(request_info->log_severity, "error"))
{
if (!error_dialog_shown_previously)
{
MessageBoxA(NULL, request_info->log_message, "Error", MB_OK);
error_dialog_shown_previously = 1;
}
return 0;
}
#endif
if (event != MG_NEW_REQUEST) {
// This callback currently only handles new requests
return NULL;
}
{
int file_found;
struct mgstat fst;
assert(request_info->phys_path);
file_found = (0 == mg_stat(request_info->phys_path, &fst) && !fst.is_directory);
if (file_found) {
return NULL; // let mongoose handle the default of 'file exists'...
}
#ifdef _WIN32
// Send the systray icon as favicon
if (!strcmp("/favicon.ico", request_info->uri)) {
HMODULE module;
HRSRC icon;
DWORD len;
void *data;
module = GetModuleHandle(NULL);
icon = FindResource(module, MAKEINTRESOURCE(IDR_FAVICON), RT_RCDATA);
data = LockResource(LoadResource(module, icon));
len = SizeofResource(module, icon);
mg_add_response_header(conn, 0, "Content-Type", "image/x-icon");
mg_add_response_header(conn, 0, "Cache-Control", "no-cache");
mg_add_response_header(conn, 0, "Content-Length", "%u", (unsigned int)len);
//mg_add_response_header(conn, 0, "Connection", suggest_connection_header(conn)); -- not needed any longer
mg_write_http_response_head(conn, 200, NULL);
if ((int)len != mg_write(conn, data, len)) {
mg_send_http_error(conn, 580, NULL, "not all data was written to the socket (len: %u)", (unsigned int)len); // internal error in our custom handler or client closed connection prematurely
}
return (void *)1;
}
#endif
}
return NULL;
}
#if defined(_WIN32)
static BOOL WINAPI mg_win32_break_handler(DWORD signal_type)
{
switch(signal_type)
{
// Handle the CTRL-C signal.
case CTRL_C_EVENT:
// CTRL-CLOSE: confirm that the user wants to exit.
case CTRL_CLOSE_EVENT:
case CTRL_BREAK_EVENT:
exit_flag = 1000 + signal_type;
//mg_signal_stop(ctx);
return TRUE;
// Pass other signals to the next handler.
case CTRL_LOGOFF_EVENT:
case CTRL_SHUTDOWN_EVENT:
default:
return FALSE;
}
}
#endif
static void start_mongoose(int argc, char *argv[]) {
char *options[MAX_OPTIONS * 2] = { NULL };
int i;
struct mg_user_class_t userdef = {
0,
&mongoose_callback
};
/* Edit passwords file if -A option is specified */
if (argc > 1 && !strcmp(argv[1], "-A")) {
if (argc != 6) {
show_usage_and_exit(ctx);
}
exit(mg_modify_passwords_file(argv[2], argv[3], argv[4], argv[5]) ?
EXIT_SUCCESS : EXIT_FAILURE);
}
/* Show usage if -h or --help options are specified */
if (argc == 2 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))) {
show_usage_and_exit(ctx);
}
/* Update config based on command line arguments */
process_command_line_arguments(argv, options);
/* Setup signal handler: quit on Ctrl-C */
signal(SIGTERM, signal_handler);
signal(SIGINT, signal_handler);
signal(SIGABRT, signal_handler);
signal(SIGILL, signal_handler);
signal(SIGSEGV, signal_handler);
signal(SIGFPE, signal_handler);
// SIGINT and SIGTERM are pretty darn useless for Win32 applications.
// See http://msdn.microsoft.com/en-us/library/ms685049%28VS.85%29.aspx
#if defined(_WIN32)
if (!SetConsoleCtrlHandler(mg_win32_break_handler, TRUE))
{
die("Failed to set up the Win32 console Ctrl-Break handler.");
}
#endif
/* Start Mongoose */
ctx = mg_start(&userdef, (const char **)options);
for (i = 0; options[i] != NULL; i++) {
free(options[i]);
}
if (ctx == NULL) {
die("Failed to start Mongoose. Maybe some options are "
"assigned bad values?\nTry to run with '-e error_log.txt' "
"and check error_log.txt for more information.");
}
}
#if defined(_WIN32) && defined(MONGOOSE_AS_SERVICE)
static SERVICE_STATUS ss;
static SERVICE_STATUS_HANDLE hStatus;
static const char *service_magic_argument = "--";
static void WINAPI ControlHandler(DWORD code) {
if (code == SERVICE_CONTROL_STOP || code == SERVICE_CONTROL_SHUTDOWN) {
ss.dwWin32ExitCode = 0;
ss.dwCurrentState = SERVICE_STOPPED;
}
SetServiceStatus(hStatus, &ss);
}
static void WINAPI ServiceMain(void) {
ss.dwServiceType = SERVICE_WIN32;
ss.dwCurrentState = SERVICE_RUNNING;
ss.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
hStatus = RegisterServiceCtrlHandlerA(server_name, ControlHandler);
SetServiceStatus(hStatus, &ss);
while (ss.dwCurrentState == SERVICE_RUNNING) {
mg_sleep(1000);
}
mg_stop(ctx);
ss.dwCurrentState = SERVICE_STOPPED;
ss.dwWin32ExitCode = (DWORD) -1;
SetServiceStatus(hStatus, &ss);
}
#define ID_TRAYICON 100
#define ID_QUIT 101
#define ID_EDIT_CONFIG 102
#define ID_SEPARATOR 103
#define ID_INSTALL_SERVICE 104
#define ID_REMOVE_SERVICE 105
static NOTIFYICONDATAA TrayIcon;
static void edit_config_file(struct mg_context *ctx) {
const char **names, *value;
FILE *fp;
int i;
char cmd[200];
// Create config file if it is not present yet
if ((fp = mg_fopen(config_file, "r")) != NULL) {
mg_fclose(fp);
} else if ((fp = fopen(config_file, "a+")) != NULL) {
fprintf(fp,
"# Mongoose web server configuration file.\n"
"# Lines starting with '#' and empty lines are ignored.\n"
"# For detailed description of every option, visit\n"
"# http://code.google.com/p/mongoose/wiki/MongooseManual\n\n");
names = mg_get_valid_option_names();
for (i = 0; names[i] != NULL; i += MG_ENTRIES_PER_CONFIG_OPTION) {
value = mg_get_option(ctx, names[i + 1]);
fprintf(fp, "# %s %s\n", names[i + 1], *value ? value : "<value>");
}
mg_fclose(fp);
}
snprintf(cmd, sizeof(cmd), "notepad.exe %s", config_file);
WinExec(cmd, SW_SHOW);
}
static void show_error(void) {
char buf[256];
FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
buf, sizeof(buf), NULL);
MessageBoxA(NULL, buf, "Error", MB_OK);
}
static int manage_service(int action) {
static const char *service_name = "Mongoose";
SC_HANDLE hSCM = NULL, hService = NULL;
SERVICE_DESCRIPTIONA descr = {server_name};
char path[PATH_MAX + 20]; // Path to executable plus magic argument
int success = 1;
if ((hSCM = OpenSCManager(NULL, NULL, action == ID_INSTALL_SERVICE ?
GENERIC_WRITE : GENERIC_READ)) == NULL) {
success = 0;
show_error();
} else if (action == ID_INSTALL_SERVICE) {
GetModuleFileNameA(NULL, path, sizeof(path));
strncat(path, " ", sizeof(path));
strncat(path, service_magic_argument, sizeof(path));
hService = CreateServiceA(hSCM, service_name, service_name,
SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
path, NULL, NULL, NULL, NULL, NULL);
if (hService) {
ChangeServiceConfig2(hService, SERVICE_CONFIG_DESCRIPTION, &descr);
} else {
show_error();
}
} else if (action == ID_REMOVE_SERVICE) {
if ((hService = OpenServiceA(hSCM, service_name, DELETE)) == NULL ||
!DeleteService(hService)) {
show_error();
}
} else if ((hService = OpenServiceA(hSCM, service_name,
SERVICE_QUERY_STATUS)) == NULL) {
success = 0;
}
CloseServiceHandle(hService);
CloseServiceHandle(hSCM);
return success;
}
static LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam,
LPARAM lParam) {
static SERVICE_TABLE_ENTRYA service_table[] = {
{server_name, (LPSERVICE_MAIN_FUNCTIONA) ServiceMain},
{NULL, NULL}
};
int service_installed;
char buf[200], *service_argv[] = {__argv[0], NULL};
POINT pt;
HMENU hMenu;
switch (msg) {
case WM_CREATE:
if (__argv[1] != NULL &&
!strcmp(__argv[1], service_magic_argument)) {
start_mongoose(1, service_argv);
StartServiceCtrlDispatcherA(service_table);
exit(EXIT_SUCCESS);
} else {
start_mongoose(__argc, __argv);
}
break;
case WM_COMMAND:
switch (LOWORD(wParam)) {
case ID_QUIT:
mg_stop(ctx);
Shell_NotifyIconA(NIM_DELETE, &TrayIcon);
PostQuitMessage(EXIT_SUCCESS);
break;
case ID_EDIT_CONFIG:
edit_config_file(ctx);
break;
case ID_INSTALL_SERVICE:
case ID_REMOVE_SERVICE:
manage_service(LOWORD(wParam));
break;
}
break;
case WM_USER:
switch (lParam) {
case WM_RBUTTONUP:
case WM_LBUTTONUP:
case WM_LBUTTONDBLCLK:
hMenu = CreatePopupMenu();
AppendMenuA(hMenu, MF_STRING | MF_GRAYED, ID_SEPARATOR, server_name);
AppendMenuA(hMenu, MF_SEPARATOR, ID_SEPARATOR, "");
service_installed = manage_service(0);
snprintf(buf, sizeof(buf), "NT service: %s installed",
service_installed ? "" : "not");
AppendMenuA(hMenu, MF_STRING | MF_GRAYED, ID_SEPARATOR, buf);
AppendMenuA(hMenu, MF_STRING | (service_installed ? MF_GRAYED : 0),
ID_INSTALL_SERVICE, "Install service");
AppendMenuA(hMenu, MF_STRING | (!service_installed ? MF_GRAYED : 0),
ID_REMOVE_SERVICE, "Deinstall service");
AppendMenuA(hMenu, MF_SEPARATOR, ID_SEPARATOR, "");
AppendMenuA(hMenu, MF_STRING, ID_EDIT_CONFIG, "Edit config file");
AppendMenuA(hMenu, MF_STRING, ID_QUIT, "Exit");
GetCursorPos(&pt);
SetForegroundWindow(hWnd);
TrackPopupMenu(hMenu, 0, pt.x, pt.y, 0, hWnd, NULL);
PostMessage(hWnd, WM_NULL, 0, 0);
DestroyMenu(hMenu);
break;
}
break;
case WM_CLOSE:
mg_stop(ctx);
Shell_NotifyIconA(NIM_DELETE, &TrayIcon);
PostQuitMessage(EXIT_SUCCESS);
return 0; // We've just sent our own quit message, with proper hwnd.
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR cmdline, int show) {
WNDCLASSA cls;
HWND hWnd;
MSG msg;
init_server_name();
memset(&cls, 0, sizeof(cls));
cls.lpfnWndProc = (WNDPROC) WindowProc;
cls.hIcon = LoadIcon(NULL, IDI_APPLICATION);
cls.lpszClassName = server_name;
RegisterClassA(&cls);
hWnd = CreateWindowA(cls.lpszClassName, server_name, WS_OVERLAPPEDWINDOW,
0, 0, 0, 0, NULL, NULL, NULL, NULL);
ShowWindow(hWnd, SW_HIDE);
TrayIcon.cbSize = sizeof(TrayIcon);
TrayIcon.uID = ID_TRAYICON;
TrayIcon.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
TrayIcon.hIcon = LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON),
IMAGE_ICON, 16, 16, 0);
TrayIcon.hWnd = hWnd;
snprintf(TrayIcon.szTip, sizeof(TrayIcon.szTip), "%s", server_name);
TrayIcon.uCallbackMessage = WM_USER;
Shell_NotifyIconA(NIM_ADD, &TrayIcon);
while (GetMessage(&msg, hWnd, 0, 0) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
// Return the WM_QUIT value.
return msg.wParam;
}
#else
int main(int argc, char *argv[]) {
init_server_name();
start_mongoose(argc, argv);
printf("%s started on port(s) %s with web root [%s]\n",
server_name, mg_get_option(ctx, "listening_ports"),
mg_get_option(ctx, "document_root"));
while (exit_flag == 0 && !mg_get_stop_flag(ctx)) {
mg_sleep(100);
}
printf("Exiting on signal %d/%d, waiting for all threads to finish...",
exit_flag, mg_get_stop_flag(ctx));
fflush(stdout);
mg_stop(ctx);
printf(" done.\n");
return EXIT_SUCCESS;
}
#endif /* _WIN32 */