]> mj.ucw.cz Git - libucw.git/commitdiff
Daemon: daemon_control() finished
authorMartin Mares <mj@ucw.cz>
Wed, 18 Jul 2012 08:36:07 +0000 (10:36 +0200)
committerMartin Mares <mj@ucw.cz>
Wed, 18 Jul 2012 08:36:07 +0000 (10:36 +0200)
ucw/daemon.c
ucw/daemon.h

index b0ea47672d8cc0c14dbc2a74bb0400d99b174879..9f3273df73b6d438e4116d75f0c90464b99d9aa6 100644 (file)
@@ -191,7 +191,7 @@ daemon_control_err(struct daemon_control_params *dc, char *msg, ...)
 }
 
 static int
-daemon_read_pid(struct daemon_control_params *dc)
+daemon_read_pid(struct daemon_control_params *dc, int will_wait)
 {
   int pid_fd = open(dc->pid_file, O_RDONLY);
   if (pid_fd < 0)
@@ -202,7 +202,7 @@ daemon_read_pid(struct daemon_control_params *dc)
       return -1;
     }
 
-  if (flock(pid_fd, LOCK_EX | LOCK_NB) >= 0)
+  if (flock(pid_fd, LOCK_EX | (will_wait ? 0 : LOCK_NB)) >= 0)
     {
       // The lock file is stale
       close(pid_fd);
@@ -254,7 +254,7 @@ daemon_control(struct daemon_control_params *dc)
     return daemon_control_err(dc, "Cannot lock guard file `%s': %m", dc->guard_file);
 
   // Read the PID file
-  int pid = daemon_read_pid(dc);
+  int pid = daemon_read_pid(dc, 0);
   if (pid < 0)
     goto done;
 
@@ -302,7 +302,7 @@ daemon_control(struct daemon_control_params *dc)
              daemon_control_err(dc, "Daemon %s", ecmsg);
              goto done;
            }
-         pid = daemon_read_pid(dc);
+         pid = daemon_read_pid(dc, 0);
          if (!pid)
            daemon_control_err(dc, "Daemon failed to write the PID file `%s'", dc->pid_file);
          else
@@ -318,7 +318,10 @@ daemon_control(struct daemon_control_params *dc)
          daemon_control_err(dc, "Cannot send signal %d: %m", dc->signal);
          goto done;
        }
-      // FIXME: Wait for the daemon to exit, possibly with a timeout
+      pid = daemon_read_pid(dc, 1);
+      ASSERT(pid <= 0);
+      if (!pid)
+       st = DAEMON_STATUS_OK;
       break;
     case DAEMON_CONTROL_SIGNAL:
       if (!pid)
index 561a401577e9bbecc5614a932be298c71475ea7b..dc2fd746eb39342287293041b31c76f9a15f67be 100644 (file)
@@ -85,7 +85,7 @@ enum daemon_control_action {
  * * `DAEMON_STATUS_ALREADY_DONE` if the daemon is already in the requested state
  * * `DAEMON_STATUS_NOT_RUNNING` if the action failed, because the daemon is not running
  * * `DAEMON_STATUS_ERROR` if the action failed for some other reason (in this case,
- *   the error_msg field contains a full error message)
+ *   `dc->error_msg` contains a full error message)
  **/
 enum daemon_control_status daemon_control(struct daemon_control_params *dc);