@@ -479,7 +479,7 @@ static void setup_signals(void)
479479 */
480480void motion_remove_pid (void )
481481{
482- if ((cnt_list [0 ]-> daemon ) && ( cnt_list [ 0 ] -> conf .pid_file ) && (restart == 0 )) {
482+ if ((cnt_list [0 ]-> conf .pid_file ) && (restart == 0 )) {
483483 if (!unlink (cnt_list [0 ]-> conf .pid_file )) {
484484 MOTION_LOG (NTC , TYPE_ALL , NO_ERRNO , _ ("Removed process id file (pid file)." ));
485485 } else {
@@ -2876,7 +2876,6 @@ static void *motion_loop(void *arg)
28762876static void become_daemon (void )
28772877{
28782878 int i ;
2879- FILE * pidf = NULL ;
28802879 struct sigaction sig_ign_action ;
28812880
28822881 /* Setup sig_ign_action */
@@ -2894,29 +2893,6 @@ static void become_daemon(void)
28942893 exit (0 );
28952894 }
28962895
2897- /*
2898- * Create the pid file if defined, if failed exit
2899- * If we fail we report it. If we succeed we postpone the log entry till
2900- * later when we have closed stdout. Otherwise Motion hangs in the terminal waiting
2901- * for an enter.
2902- */
2903- if (cnt_list [0 ]-> conf .pid_file ) {
2904- pidf = myfopen (cnt_list [0 ]-> conf .pid_file , "w+e" );
2905-
2906- if (pidf ) {
2907- (void )fprintf (pidf , "%d\n" , getpid ());
2908- myfclose (pidf );
2909- } else {
2910- MOTION_LOG (EMG , TYPE_ALL , SHOW_ERRNO
2911- ,_ ("Exit motion, cannot create process"
2912- " id file (pid file) %s" ), cnt_list [0 ]-> conf .pid_file );
2913- if (ptr_logfile ) {
2914- myfclose (ptr_logfile );
2915- }
2916- exit (0 );
2917- }
2918- }
2919-
29202896 /*
29212897 * Changing dir to root enables people to unmount a disk
29222898 * without having to stop Motion
@@ -2925,7 +2901,6 @@ static void become_daemon(void)
29252901 MOTION_LOG (ERR , TYPE_ALL , SHOW_ERRNO , _ ("Could not change directory" ));
29262902 }
29272903
2928-
29292904 #if (defined(BSD ) && !defined(__APPLE__ ))
29302905 setpgrp (0 , getpid ());
29312906 #else
@@ -2954,18 +2929,32 @@ static void become_daemon(void)
29542929 close (i );
29552930 }
29562931
2957- /* Now it is safe to add the PID creation to the logs */
2958- if (pidf ) {
2959- MOTION_LOG (NTC , TYPE_ALL , NO_ERRNO
2960- ,_ ("Created process id file %s. Process ID is %d" )
2961- ,cnt_list [0 ]-> conf .pid_file , getpid ());
2962- }
2963-
29642932 sigaction (SIGTTOU , & sig_ign_action , NULL );
29652933 sigaction (SIGTTIN , & sig_ign_action , NULL );
29662934 sigaction (SIGTSTP , & sig_ign_action , NULL );
29672935}
29682936
2937+ static void pid_write (void )
2938+ {
2939+ FILE * pidf = NULL ;
2940+
2941+ if (cnt_list [0 ]-> conf .pid_file ) {
2942+ pidf = myfopen (cnt_list [0 ]-> conf .pid_file , "w+e" );
2943+
2944+ if (pidf ) {
2945+ (void )fprintf (pidf , "%d\n" , getpid ());
2946+ myfclose (pidf );
2947+ MOTION_LOG (NTC , TYPE_ALL , NO_ERRNO
2948+ ,_ ("Created process id file %s. Process ID is %d" )
2949+ ,cnt_list [0 ]-> conf .pid_file , getpid ());
2950+ } else {
2951+ MOTION_LOG (ERR , TYPE_ALL , SHOW_ERRNO
2952+ ,_ ("Cannot create process id file (pid file) %s" )
2953+ , cnt_list [0 ]-> conf .pid_file );
2954+ }
2955+ }
2956+ }
2957+
29692958static void cntlist_create (int argc , char * argv [])
29702959{
29712960 /*
@@ -3213,6 +3202,8 @@ static void motion_startup(int daemonize, int argc, char *argv[])
32133202 }
32143203 }
32153204
3205+ pid_write ();
3206+
32163207 if (cnt_list [0 ]-> conf .setup_mode ) {
32173208 MOTION_LOG (NTC , TYPE_ALL , NO_ERRNO ,_ ("Motion running in setup mode." ));
32183209 }
0 commit comments