1212#endif
1313
1414main_ctx_t g_main_ctx ;
15+ printf_t printf_fn = printf ;
1516
1617static void init_arg_kv (int maxsize ) {
1718 g_main_ctx .arg_kv_size = 0 ;
@@ -217,7 +218,7 @@ int parse_opt(int argc, char** argv, const char* options) {
217218 while (* ++ p ) {
218219 int arg_type = get_arg_type (* p , options );
219220 if (arg_type == UNDEFINED_OPTION ) {
220- printf ("Invalid option '%c'\n" , * p );
221+ printf_fn ("Invalid option '%c'\n" , * p );
221222 return -20 ;
222223 } else if (arg_type == NO_ARGUMENT ) {
223224 save_arg_kv (p , 1 , OPTION_ENABLE , 0 );
@@ -230,7 +231,7 @@ int parse_opt(int argc, char** argv, const char* options) {
230231 save_arg_kv (p , 1 , argv [++ i ], 0 );
231232 break ;
232233 } else {
233- printf ("Option '%c' requires param\n" , * p );
234+ printf_fn ("Option '%c' requires param\n" , * p );
234235 return -30 ;
235236 }
236237 }
@@ -288,7 +289,7 @@ int parse_opt_long(int argc, char** argv, const option_t* long_options, int size
288289 char * delim = strchr (arg , OPTION_DELIM );
289290 if (delim ) {
290291 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 ]);
292293 return -10 ;
293294 }
294295 memcpy (opt , arg , delim - arg );
@@ -308,7 +309,7 @@ int parse_opt_long(int argc, char** argv, const option_t* long_options, int size
308309 save_arg_list (arg );
309310 continue ;
310311 } else {
311- printf ("Invalid option: '%s'\n" , argv [i ]);
312+ printf_fn ("Invalid option: '%s'\n" , argv [i ]);
312313 return -10 ;
313314 }
314315 }
@@ -328,7 +329,7 @@ int parse_opt_long(int argc, char** argv, const option_t* long_options, int size
328329 // --port 80
329330 value = argv [++ i ];
330331 } else if (pOption -> arg_type == REQUIRED_ARGUMENT ) {
331- printf ("Option '%s' requires parament\n" , opt );
332+ printf_fn ("Option '%s' requires parament\n" , opt );
332333 return -20 ;
333334 } else {
334335 // arg_type == OPTIONAL_ARGUMENT
@@ -614,48 +615,55 @@ static void kill_proc(int pid) {
614615}
615616
616617void signal_handle (const char * signal ) {
618+ if (signal_handle_isexit (signal )) exit (0 );
619+ }
620+
621+ bool signal_handle_isexit (const char * signal ) {
617622 if (strcmp (signal , "start" ) == 0 ) {
618623 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 ;
621626 }
622627 } else if (strcmp (signal , "stop" ) == 0 ) {
623628 if (g_main_ctx .oldpid > 0 ) {
624629 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 );
626631 } 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 );
628633 }
629- exit ( 0 ) ;
634+ return true ;
630635 } else if (strcmp (signal , "restart" ) == 0 ) {
631636 if (g_main_ctx .oldpid > 0 ) {
632637 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 );
634639 hv_sleep (1 );
635640 }
636641 } else if (strcmp (signal , "status" ) == 0 ) {
637642 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 );
639644 } 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 );
641646 }
642- exit ( 0 ) ;
647+ return true ;
643648 } else if (strcmp (signal , "reload" ) == 0 ) {
644649 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 );
646651#ifdef OS_UNIX
647652 kill (g_main_ctx .oldpid , SIGNAL_RELOAD );
648653#else
649654 SetEvent (s_hEventReload );
650655#endif
656+ hv_sleep (1 );
657+ } else {
658+ printf_fn ("%s is already stopped\n" , g_main_ctx .program_name );
651659 }
652- hv_sleep (1 );
653- exit (0 );
660+ return true;
654661 } else {
655- printf ("Invalid signal: '%s'\n" , signal );
656- exit ( 0 ) ;
662+ printf_fn ("Invalid signal: '%s'\n" , signal );
663+ return true ;
657664 }
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;
659667}
660668
661669// master-workers processes
0 commit comments