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
@@ -614,48 +615,55 @@ static void kill_proc(int pid) {
614
615
}
615
616
616
617
void signal_handle (const char * signal ) {
618
+ if (signal_handle_noexit (signal )) exit (0 );
619
+ }
620
+
621
+ bool signal_handle_noexit (const char * signal ) {
617
622
if (strcmp (signal , "start" ) == 0 ) {
618
623
if (g_main_ctx .oldpid > 0 ) {
619
- printf ("%s is already running, pid=%d\n" , g_main_ctx .program_name , g_main_ctx .oldpid );
620
- exit ( 0 ) ;
624
+ printf_fn ("%s is already running, pid=%d\n" , g_main_ctx .program_name , g_main_ctx .oldpid );
625
+ return true ;
621
626
}
622
627
} else if (strcmp (signal , "stop" ) == 0 ) {
623
628
if (g_main_ctx .oldpid > 0 ) {
624
629
kill_proc (g_main_ctx .oldpid );
625
- printf ("%s stop/waiting\n" , g_main_ctx .program_name );
630
+ printf_fn ("%s stop/waiting\n" , g_main_ctx .program_name );
626
631
} else {
627
- printf ("%s is already stopped\n" , g_main_ctx .program_name );
632
+ printf_fn ("%s is already stopped\n" , g_main_ctx .program_name );
628
633
}
629
- exit ( 0 ) ;
634
+ return true ;
630
635
} else if (strcmp (signal , "restart" ) == 0 ) {
631
636
if (g_main_ctx .oldpid > 0 ) {
632
637
kill_proc (g_main_ctx .oldpid );
633
- printf ("%s stop/waiting\n" , g_main_ctx .program_name );
638
+ printf_fn ("%s stop/waiting\n" , g_main_ctx .program_name );
634
639
hv_sleep (1 );
635
640
}
636
641
} else if (strcmp (signal , "status" ) == 0 ) {
637
642
if (g_main_ctx .oldpid > 0 ) {
638
- printf ("%s start/running, pid=%d\n" , g_main_ctx .program_name , g_main_ctx .oldpid );
643
+ printf_fn ("%s start/running, pid=%d\n" , g_main_ctx .program_name , g_main_ctx .oldpid );
639
644
} else {
640
- printf ("%s stop/waiting \n" , g_main_ctx .program_name );
645
+ printf_fn ("%s is already stopped \n" , g_main_ctx .program_name );
641
646
}
642
- exit ( 0 ) ;
647
+ return true ;
643
648
} else if (strcmp (signal , "reload" ) == 0 ) {
644
649
if (g_main_ctx .oldpid > 0 ) {
645
- printf ("reload confile [%s]\n" , g_main_ctx .confile );
650
+ printf_fn ("reload confile [%s]\n" , g_main_ctx .confile );
646
651
#ifdef OS_UNIX
647
652
kill (g_main_ctx .oldpid , SIGNAL_RELOAD );
648
653
#else
649
654
SetEvent (s_hEventReload );
650
655
#endif
656
+ hv_sleep (1 );
657
+ } else {
658
+ printf_fn ("%s is already stopped\n" , g_main_ctx .program_name );
651
659
}
652
- hv_sleep (1 );
653
- exit (0 );
660
+ return true;
654
661
} else {
655
- printf ("Invalid signal: '%s'\n" , signal );
656
- exit ( 0 ) ;
662
+ printf_fn ("Invalid signal: '%s'\n" , signal );
663
+ return true ;
657
664
}
658
- printf ("%s start/running\n" , g_main_ctx .program_name );
665
+ printf_fn ("%s start/running\n" , g_main_ctx .program_name );
666
+ return false;
659
667
}
660
668
661
669
// master-workers processes
0 commit comments