]> mj.ucw.cz Git - libucw.git/commitdiff
Daemon: Added DAEMON_STATUS_STALE
authorMartin Mares <mj@ucw.cz>
Wed, 18 Jul 2012 11:41:20 +0000 (13:41 +0200)
committerMartin Mares <mj@ucw.cz>
Wed, 18 Jul 2012 11:41:20 +0000 (13:41 +0200)
ucw/daemon-ctrl.c
ucw/daemon.h
ucw/utils/daemon-control.c

index 2a7908aed0b9a895d41de71abca900c68c4a4a75..21132e3241a5f0d0b649b9bf632612a644b7f90b 100644 (file)
@@ -32,8 +32,10 @@ daemon_control_err(struct daemon_control_params *dc, char *msg, ...)
 }
 
 static int
-daemon_read_pid(struct daemon_control_params *dc, int will_wait)
+daemon_read_pid(struct daemon_control_params *dc, int will_wait, int *stalep)
 {
+  *stalep = 0;
+
   int pid_fd = open(dc->pid_file, O_RDONLY);
   if (pid_fd < 0)
     {
@@ -47,6 +49,7 @@ daemon_read_pid(struct daemon_control_params *dc, int will_wait)
     {
       // The lock file is stale
       close(pid_fd);
+      *stalep = 1;
       return 0;
     }
 
@@ -87,7 +90,7 @@ enum daemon_control_status
 daemon_control(struct daemon_control_params *dc)
 {
   enum daemon_control_status st = DAEMON_STATUS_ERROR;
-  int sig;
+  int sig, stale, stale2;
 
   int guard_fd = open(dc->guard_file, O_RDWR | O_CREAT, 0666);
   if (guard_fd < 0)
@@ -96,7 +99,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, 0);
+  int pid = daemon_read_pid(dc, 0, &stale);
   if (pid < 0)
     goto done;
 
@@ -105,6 +108,8 @@ daemon_control(struct daemon_control_params *dc)
     case DAEMON_CONTROL_CHECK:
       if (pid)
        st = DAEMON_STATUS_OK;
+      else if (stale)
+       st = DAEMON_STATUS_STALE;
       else
        st = DAEMON_STATUS_NOT_RUNNING;
       break;
@@ -144,23 +149,23 @@ daemon_control(struct daemon_control_params *dc)
              daemon_control_err(dc, "Daemon %s %s", dc->argv[0], ecmsg);
              goto done;
            }
-         pid = daemon_read_pid(dc, 0);
+         pid = daemon_read_pid(dc, 0, &stale2);
          if (!pid)
            daemon_control_err(dc, "Daemon %s failed to write the PID file `%s'", dc->argv[0], dc->pid_file);
          else
-           st = DAEMON_STATUS_OK;
+           st = stale ? DAEMON_STATUS_STALE : DAEMON_STATUS_OK;
        }
       break;
     case DAEMON_CONTROL_STOP:
       if (!pid)
-       return DAEMON_STATUS_ALREADY_DONE;
+       return stale ? DAEMON_STATUS_STALE : DAEMON_STATUS_ALREADY_DONE;
       sig = dc->signal ? : SIGTERM;
       if (kill(pid, sig) < 0)
        {
          daemon_control_err(dc, "Cannot send signal %d: %m", sig);
          goto done;
        }
-      pid = daemon_read_pid(dc, 1);
+      pid = daemon_read_pid(dc, 1, &stale2);
       ASSERT(pid <= 0);
       if (!pid)
        st = DAEMON_STATUS_OK;
index c99f8e0849f5f525efbdfe7b632421ed9327edfa..9cdfe05027fb65bcd414afc00a30db81ef90fed1 100644 (file)
@@ -86,6 +86,8 @@ enum daemon_control_action {
  * * `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,
  *   `dc->error_msg` contains a full error message)
+ * * `DAEMON_STATUS_STALE` if the daemon was in an undefined state (e.g., a stale PID file);
+ *   for `DAEMON_CONTROL_START`, it means success
  **/
 enum daemon_control_status daemon_control(struct daemon_control_params *dc);
 
@@ -95,6 +97,7 @@ enum daemon_control_status {
   DAEMON_STATUS_ALREADY_DONE = 100,
   DAEMON_STATUS_NOT_RUNNING = 101,
   DAEMON_STATUS_ERROR = 102,
+  DAEMON_STATUS_STALE = 103,
 };
 
 #endif
index 85fb450121fae750f75592c25024ec60876162f4..5174919bc1ce505d19c97e2c9e85abff7caba213 100644 (file)
@@ -56,6 +56,7 @@ Exit codes:\n\
 100                    The action was null (e.g., --stop on a stopped daemon)\n\
 101                    The daemon was not running (on --reload or --check)\n\
 102                    The action has failed (error message was printed to stderr)\n\
+103                    The daemon was in an undefined state (e.g., stale PID file)\n\
 ", stderr);
   exit(1);
 }