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
@@ -623,48 +624,55 @@ static void kill_proc(int pid) {
623624}
624625
625626void signal_handle (const char * signal ) {
627+ if (signal_handle_noexit (signal )) exit (0 );
628+ }
629+
630+ bool signal_handle_noexit (const char * signal ) {
626631 if (strcmp (signal , "start" ) == 0 ) {
627632 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 ;
630635 }
631636 } else if (strcmp (signal , "stop" ) == 0 ) {
632637 if (g_main_ctx .oldpid > 0 ) {
633638 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 );
635640 } 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 );
637642 }
638- exit ( 0 ) ;
643+ return true ;
639644 } else if (strcmp (signal , "restart" ) == 0 ) {
640645 if (g_main_ctx .oldpid > 0 ) {
641646 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 );
643648 hv_sleep (1 );
644649 }
645650 } else if (strcmp (signal , "status" ) == 0 ) {
646651 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 );
648653 } 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 );
650655 }
651- exit ( 0 ) ;
656+ return true ;
652657 } else if (strcmp (signal , "reload" ) == 0 ) {
653658 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 );
655660#ifdef OS_UNIX
656661 kill (g_main_ctx .oldpid , SIGNAL_RELOAD );
657662#else
658663 SetEvent (s_hEventReload );
659664#endif
665+ hv_sleep (1 );
666+ } else {
667+ printf_fn ("%s is already stopped\n" , g_main_ctx .program_name );
660668 }
661- hv_sleep (1 );
662- exit (0 );
669+ return true;
663670 } else {
664- printf ("Invalid signal: '%s'\n" , signal );
665- exit ( 0 ) ;
671+ printf_fn ("Invalid signal: '%s'\n" , signal );
672+ return true ;
666673 }
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;
668676}
669677
670678// master-workers processes
0 commit comments