cpuplugd: Improve systemctl start error handling

Currently "systemctl start cpuplugd" does not report any errors in
case the startup fails.

Example:

 # mv /etc/cpuplugd.conf /etc/cpuplugd.conf.xxx
 # systemctl start cpuplugd

The reason is that for type=simple systemd forks/execs cpuplugd and if
that is successful immediately returns. There is no way to find out
if the initial startup fails.

Fix this by using type=fork and running cpuplugd in the background. In
this case systemd waits until the initial process returns.

In addition use PIDFile and ensure that the pid file is already available
when the initial cpuplugd process returns. To achieve this, replace the
daemon() function by our own implementation that introduces startup
synchronization via pipe. Without that systemd would print the following
warning:

systemd[1]: cpuplugd.service: PID file /var/run/cpuplugd.pid not readable
                              (yet?) after start: No such file or directory

With this patch, an early startup error like in the example above, is now
reported correctly in "systemctl start":

 # systemctl start cpuplugd
   Job for cpuplugd.service failed because the control process exited...
   See "systemctl status cpuplugd.service" and "journalctl -xe" for ...
 # journalctl -ex | grep cpuplugd
   Nov 16 15:52:27 ... cpuplugd[5096]: Opening configuration file failed:
                                       No such file or directory

Signed-off-by: Michael Holzheu <holzheu@linux.vnet.ibm.com>
Acked-by: Gerald Schaefer <gerald.schaefer@de.ibm.com>
This commit is contained in:
Michael Holzheu
2017-11-20 17:00:12 +01:00
parent c12c4af760
commit 82c8148983
4 changed files with 63 additions and 8 deletions

View File

@@ -197,10 +197,10 @@ int is_online(int cpuid);
long get_cmmpages_size();
void parse_options(int argc, char **argv);
void check_if_started_twice();
void store_pid(void);
void handle_signals(void);
void handle_sighup(void);
void reload_daemon(void);
int daemonize(void);
int check_cmmfiles(void);
void check_config();
void set_cmm_pages(long size);

View File

@@ -9,6 +9,10 @@
* it under the terms of the MIT license. See LICENSE for details.
*/
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "cpuplugd.h"
const char *name = NAME;
@@ -49,7 +53,7 @@ void print_version()
/*
* Store daemon's pid so it can be stopped
*/
void store_pid(void)
static int store_pid(void)
{
FILE *filp;
@@ -57,10 +61,62 @@ void store_pid(void)
if (!filp) {
cpuplugd_error("cannot open pid file %s: %s\n", pid_file,
strerror(errno));
exit(1);
return -1;
}
fprintf(filp, "%d\n", getpid());
fclose(filp);
return 0;
}
/*
* Run daemon in background and write pid file
*/
int daemonize(void)
{
int fd, pipe_fds[2], startup_rc = 1;
pid_t pid;
if (pipe(pipe_fds) == -1) {
cpuplugd_error("cannot create pipe\n");
return -1;
}
pid = fork();
if (pid < 0)
goto close_pipe;
if (pid != 0) {
/* Wait for startup return code from daemon */
if (read(pipe_fds[0], &startup_rc, sizeof(startup_rc)) == -1)
cpuplugd_error("cannot read from pipe\n");
/* On success daemon has written pid file at this point */
exit(startup_rc);
}
/* Create new session */
if (setsid() < 0)
goto notify_parent;
/* Redirect stdin/out/err to /dev/null */
fd = open("/dev/null", O_RDWR, 0);
if (fd == -1)
goto notify_parent;
if (dup2(fd, STDIN_FILENO) < 0)
goto notify_parent;
if (dup2(fd, STDOUT_FILENO) < 0)
goto notify_parent;
if (dup2(fd, STDERR_FILENO) < 0)
goto notify_parent;
/* Create pid file */
if (store_pid() < 0)
goto notify_parent;
startup_rc = 0;
notify_parent:
/* Inform waiting parent about startup return code */
if (write(pipe_fds[1], &startup_rc, sizeof(startup_rc)) == -1) {
cpuplugd_error("cannot write to pipe\n");
startup_rc = 1;
}
close_pipe:
close(pipe_fds[0]);
close(pipe_fds[1]);
return startup_rc ? -1 : 0;
}
/*

View File

@@ -418,13 +418,11 @@ int main(int argc, char *argv[])
check_config();
if (!foreground) {
rc = daemon(1, 0);
rc = daemonize();
if (rc < 0)
cpuplugd_exit("Detach from terminal failed: %s\n",
strerror(errno));
}
/* Store daemon pid */
store_pid();
/* Unlock lock file */
flock(fd, LOCK_UN);
close(fd);

View File

@@ -13,10 +13,11 @@ Documentation=man:cpuplugd(8) man:cpuplugd.conf(5)
After=remote-fs.target
[Service]
ExecStart=@usrsbin_path@/cpuplugd -f -c @sysconf_path@/cpuplugd.conf
ExecStart=@usrsbin_path@/cpuplugd -c @sysconf_path@/cpuplugd.conf
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Type=simple
Type=forking
PIDFile=/var/run/cpuplugd.pid
[Install]
WantedBy=multi-user.target