Author: arkurth
Date: Tue Aug  1 20:28:01 2017
New Revision: 1803703

URL: http://svn.apache.org/viewvc?rev=1803703&view=rev
Log:
VCL-984
Updated systemd vcld.service file from Type = simple to forking. Added 
"StartLimitInterval=0" to prevent the service from being unable to start due to 
too many successive tries.

Modified vcld::daemonize so that the parent "service startup" process writes 
the PID file instead of the child. This is required or else systemd will not 
detect that the forked daemon process properly started and repeatedly try to 
start vcld (even though it is started).

Modified:
    vcl/trunk/managementnode/bin/vcld
    vcl/trunk/managementnode/etc/systemd/system/vcld.service

Modified: vcl/trunk/managementnode/bin/vcld
URL: 
http://svn.apache.org/viewvc/vcl/trunk/managementnode/bin/vcld?rev=1803703&r1=1803702&r2=1803703&view=diff
==============================================================================
--- vcl/trunk/managementnode/bin/vcld (original)
+++ vcl/trunk/managementnode/bin/vcld Tue Aug  1 20:28:01 2017
@@ -782,8 +782,8 @@ sub REAPER {
 
 =head2 daemonize
 
- Parameters  : 
- Returns     : 
+ Parameters  : none
+ Returns     : true or exits
  Description :
 
 =cut
@@ -793,35 +793,56 @@ sub daemonize {
        my $subsys_lock = "/var/lock/subsys/$PROCESSNAME";
        sysopen(LOCKFILE, $subsys_lock, O_RDONLY | O_CREAT ) or die "unable to 
open lock file: $PIDFILE \n";   
        unless(flock(LOCKFILE, LOCK_EX|LOCK_NB)) {
-               notify($ERRORS{'WARNING'}, $LOGFILE, " An process instance of 
$PROCESSNAME is already running ");
-               print STDOUT "\nFailed to start.\n\nAn instance of $PROCESSNAME 
is already running\n\n";
-               print STDERR "\nFailed to start.\n\nAn instance of $PROCESSNAME 
is already running\n\n";
+               notify($ERRORS{'WARNING'}, $LOGFILE, " an process instance of 
$PROCESSNAME is already running, failed to lock file: $subsys_lock");
+               print STDOUT "\nFailed to start. An instance of $PROCESSNAME is 
already running\n\n";
+               print STDERR "\nFailed to start. An instance of $PROCESSNAME is 
already running\n\n";
                exit(1); 
        }
-
-
-       chdir '/' or die "Can't chdir to /: $!";
-       defined(my $pid = fork) or die "Can't fork $!";
-       exit if $pid;
-
-
-       umask 0;
-       setsid or die "Can't start a new session: $!";
-
-       # write pid to pidfile
-       open(PIDFILE, ">" . $PIDFILE) or notify($ERRORS{'WARNING'}, $LOGFILE, 
"unable to open PID file: $PIDFILE, $!");   
-       print PIDFILE $$ ;
-       close(PIDFILE);
        
-       preplogfile();
-       
-       #Redirect STDIN,STDOUT,STDERR 
-       open STDIN,  '/dev/null'  or die "Can't read /dev/null $!";
-       open STDOUT, ">>$LOGFILE" or die "Can't write $LOGFILE $!";
-       open STDERR, ">>$LOGFILE" or die "Can't write $LOGFILE $!";
-
-       print "Created VCL daemon process: $$\n";
        
+       my $child_pid = fork();
+       if (!defined($child_pid)) {
+               die "Can't fork $!";
+       }
+       elsif ($child_pid) {
+               # If here, this is the process that called fork()
+               # This service starting process is only used to start the 
actual forked vcld daemon process, it must exit
+               
+               # Before exiting, the parent must create the pidfile
+               # This must be done before this parent exits can cannot be done 
by the forked child
+               # Otherwise, timing issues will occur with systemd
+               # As soon as this parent exits, systemd expects the pidfile to 
exist and checks it
+               # If the pidfile is missing or incorrect, systemd will try over 
and over again to start vcld
+               notify($ERRORS{'DEBUG'}, 0, "this is parent service startup 
process: $PID, forked child process which will be the main vcld daemon process: 
$child_pid, writing child PID to file: $PIDFILE");
+               open(PIDFILE, ">" . $PIDFILE) or notify($ERRORS{'WARNING'}, 
$LOGFILE, "unable to open PID file: $PIDFILE, $!");
+               print PIDFILE $child_pid ;
+               close(PIDFILE);
+               notify($ERRORS{'DEBUG'}, 0, "parent service startup process 
exiting: $PID");
+               exit;
+       }
+       else {
+               # If here, this is the child process created by the fork and 
will be the main vcld process
+               
+               # Detatch from controlling terminal
+               setsid;
+               
+               # Process umask is inherited to child from fork
+               umask 0;
+               
+               # Current working directory is inherited to child from fork
+               # The daemon will run from that directory and may prevent 
modification 
+               chdir '/';
+               
+               preplogfile();
+               
+               # Redirect STDIN,STDOUT,STDERR 
+               open STDIN,  '/dev/null'  or die "Can't read /dev/null $!";
+               open STDOUT, ">>$LOGFILE" or die "Can't write $LOGFILE $!";
+               open STDERR, ">>$LOGFILE" or die "Can't write $LOGFILE $!";
+               
+               print "Created VCL daemon process: $$\n";
+               return 1;
+       }
 } ## end sub daemonize
 
 #/////////////////////////////////////////////////////////////////////////////

Modified: vcl/trunk/managementnode/etc/systemd/system/vcld.service
URL: 
http://svn.apache.org/viewvc/vcl/trunk/managementnode/etc/systemd/system/vcld.service?rev=1803703&r1=1803702&r2=1803703&view=diff
==============================================================================
--- vcl/trunk/managementnode/etc/systemd/system/vcld.service (original)
+++ vcl/trunk/managementnode/etc/systemd/system/vcld.service Tue Aug  1 
20:28:01 2017
@@ -22,7 +22,7 @@ Description=VCL management node daemon
 After=network-online.target
 
 [Service]
-Type=simple
+Type=forking
 EnvironmentFile=/etc/sysconfig/vcld
 PIDFile=/var/run/vcld.pid
 ExecStart=/usr/local/vcl/bin/vcld $OPTIONS
@@ -30,6 +30,7 @@ ExecStop=/bin/kill ${MAINPID}
 KillMode=process
 Restart=always
 RestartSec=10s
+StartLimitInterval=0
 
 [Install]
 WantedBy=multi-user.target
\ No newline at end of file


Reply via email to