12
12
#endif
13
13
14
14
main_ctx_t g_main_ctx ;
15
+ printf_t printf_fn = printf ;
15
16
16
17
static void init_arg_kv (int maxsize ) {
17
18
g_main_ctx .arg_kv_size = 0 ;
@@ -217,7 +218,7 @@ int parse_opt(int argc, char** argv, const char* options) {
217
218
while (* ++ p ) {
218
219
int arg_type = get_arg_type (* p , options );
219
220
if (arg_type == UNDEFINED_OPTION ) {
220
- printf ("Invalid option '%c'\n" , * p );
221
+ printf_fn ("Invalid option '%c'\n" , * p );
221
222
return -20 ;
222
223
} else if (arg_type == NO_ARGUMENT ) {
223
224
save_arg_kv (p , 1 , OPTION_ENABLE , 0 );
@@ -230,7 +231,7 @@ int parse_opt(int argc, char** argv, const char* options) {
230
231
save_arg_kv (p , 1 , argv [++ i ], 0 );
231
232
break ;
232
233
} else {
233
- printf ("Option '%c' requires param\n" , * p );
234
+ printf_fn ("Option '%c' requires param\n" , * p );
234
235
return -30 ;
235
236
}
236
237
}
@@ -288,7 +289,7 @@ int parse_opt_long(int argc, char** argv, const option_t* long_options, int size
288
289
char * delim = strchr (arg , OPTION_DELIM );
289
290
if (delim ) {
290
291
if (delim == arg || delim == arg + arg_len - 1 || delim - arg > MAX_OPTION ) {
291
- printf ("Invalid option '%s'\n" , argv [i ]);
292
+ printf_fn ("Invalid option '%s'\n" , argv [i ]);
292
293
return -10 ;
293
294
}
294
295
memcpy (opt , arg , delim - arg );
@@ -308,7 +309,7 @@ int parse_opt_long(int argc, char** argv, const option_t* long_options, int size
308
309
save_arg_list (arg );
309
310
continue ;
310
311
} else {
311
- printf ("Invalid option: '%s'\n" , argv [i ]);
312
+ printf_fn ("Invalid option: '%s'\n" , argv [i ]);
312
313
return -10 ;
313
314
}
314
315
}
@@ -328,7 +329,7 @@ int parse_opt_long(int argc, char** argv, const option_t* long_options, int size
328
329
// --port 80
329
330
value = argv [++ i ];
330
331
} else if (pOption -> arg_type == REQUIRED_ARGUMENT ) {
331
- printf ("Option '%s' requires parament\n" , opt );
332
+ printf_fn ("Option '%s' requires parament\n" , opt );
332
333
return -20 ;
333
334
} else {
334
335
// arg_type == OPTIONAL_ARGUMENT
@@ -623,48 +624,55 @@ static void kill_proc(int pid) {
623
624
}
624
625
625
626
void signal_handle (const char * signal ) {
627
+ if (signal_handle_noexit (signal )) exit (0 );
628
+ }
629
+
630
+ bool signal_handle_noexit (const char * signal ) {
626
631
if (strcmp (signal , "start" ) == 0 ) {
627
632
if (g_main_ctx .oldpid > 0 ) {
628
- printf ("%s is already running, pid=%d\n" , g_main_ctx .program_name , g_main_ctx .oldpid );
629
- exit ( 0 ) ;
633
+ printf_fn ("%s is already running, pid=%d\n" , g_main_ctx .program_name , g_main_ctx .oldpid );
634
+ return true ;
630
635
}
631
636
} else if (strcmp (signal , "stop" ) == 0 ) {
632
637
if (g_main_ctx .oldpid > 0 ) {
633
638
kill_proc (g_main_ctx .oldpid );
634
- printf ("%s stop/waiting\n" , g_main_ctx .program_name );
639
+ printf_fn ("%s stop/waiting\n" , g_main_ctx .program_name );
635
640
} else {
636
- printf ("%s is already stopped\n" , g_main_ctx .program_name );
641
+ printf_fn ("%s is already stopped\n" , g_main_ctx .program_name );
637
642
}
638
- exit ( 0 ) ;
643
+ return true ;
639
644
} else if (strcmp (signal , "restart" ) == 0 ) {
640
645
if (g_main_ctx .oldpid > 0 ) {
641
646
kill_proc (g_main_ctx .oldpid );
642
- printf ("%s stop/waiting\n" , g_main_ctx .program_name );
647
+ printf_fn ("%s stop/waiting\n" , g_main_ctx .program_name );
643
648
hv_sleep (1 );
644
649
}
645
650
} else if (strcmp (signal , "status" ) == 0 ) {
646
651
if (g_main_ctx .oldpid > 0 ) {
647
- printf ("%s start/running, pid=%d\n" , g_main_ctx .program_name , g_main_ctx .oldpid );
652
+ printf_fn ("%s start/running, pid=%d\n" , g_main_ctx .program_name , g_main_ctx .oldpid );
648
653
} else {
649
- printf ("%s stop/waiting \n" , g_main_ctx .program_name );
654
+ printf_fn ("%s is already stopped \n" , g_main_ctx .program_name );
650
655
}
651
- exit ( 0 ) ;
656
+ return true ;
652
657
} else if (strcmp (signal , "reload" ) == 0 ) {
653
658
if (g_main_ctx .oldpid > 0 ) {
654
- printf ("reload confile [%s]\n" , g_main_ctx .confile );
659
+ printf_fn ("reload confile [%s]\n" , g_main_ctx .confile );
655
660
#ifdef OS_UNIX
656
661
kill (g_main_ctx .oldpid , SIGNAL_RELOAD );
657
662
#else
658
663
SetEvent (s_hEventReload );
659
664
#endif
665
+ hv_sleep (1 );
666
+ } else {
667
+ printf_fn ("%s is already stopped\n" , g_main_ctx .program_name );
660
668
}
661
- hv_sleep (1 );
662
- exit (0 );
669
+ return true;
663
670
} else {
664
- printf ("Invalid signal: '%s'\n" , signal );
665
- exit ( 0 ) ;
671
+ printf_fn ("Invalid signal: '%s'\n" , signal );
672
+ return true ;
666
673
}
667
- printf ("%s start/running\n" , g_main_ctx .program_name );
674
+ printf_fn ("%s start/running\n" , g_main_ctx .program_name );
675
+ return false;
668
676
}
669
677
670
678
// master-workers processes
0 commit comments