}
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)
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);
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;
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
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)
* * `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);