From 82ccad68d46d4b10a928bc860c0feedf26e483e3 Mon Sep 17 00:00:00 2001 From: Mateusz Kusiak Date: Wed, 20 Nov 2024 19:01:30 +0000 Subject: Refactor continue_via_systemd() Refactor continue_via_systemd() and it's calls to make it more readable. No functional changes. Signed-off-by: Mateusz Kusiak --- Grow.c | 2 +- mdadm.h | 2 +- util.c | 43 ++++++++++++++++++++++++------------------- 3 files changed, 26 insertions(+), 21 deletions(-) diff --git a/Grow.c b/Grow.c index 818eb6a4..53b0b387 100644 --- a/Grow.c +++ b/Grow.c @@ -3013,7 +3013,7 @@ static mdadm_status_t handle_forking(bool forked, char *devname) if (forked) return MDADM_STATUS_FORKED; - if (devname && continue_via_systemd(devname, GROW_SERVICE, NULL)) + if (devname && continue_via_systemd(devname, GROW_SERVICE, NULL) == MDADM_STATUS_SUCCESS) return MDADM_STATUS_SUCCESS; switch (fork()) { diff --git a/mdadm.h b/mdadm.h index 6062e167..e84c341c 100644 --- a/mdadm.h +++ b/mdadm.h @@ -1694,7 +1694,7 @@ extern int same_dev(char *one, char *two); extern int compare_paths (char* path1,char* path2); extern void enable_fds(int devices); extern void manage_fork_fds(int close_all); -extern int continue_via_systemd(char *devnm, char *service_name, char *prefix); +extern mdadm_status_t continue_via_systemd(char *devnm, char *service_name, char *prefix); extern void ident_init(struct mddev_ident *ident); extern mdadm_status_t ident_set_devname(struct mddev_ident *ident, const char *devname); diff --git a/util.c b/util.c index 8099852f..8c45f0e1 100644 --- a/util.c +++ b/util.c @@ -1982,7 +1982,7 @@ int start_mdmon(char *devnm) if (check_env("MDADM_NO_MDMON")) return 0; - if (continue_via_systemd(devnm, MDMON_SERVICE, prefix)) + if (continue_via_systemd(devnm, MDMON_SERVICE, prefix) == MDADM_STATUS_SUCCESS) return 0; /* That failed, try running mdmon directly */ @@ -2299,36 +2299,41 @@ void manage_fork_fds(int close_all) /* In a systemd/udev world, it is best to get systemd to * run daemon rather than running in the background. * Returns: - * 1- if systemd service has been started - * 0- otherwise + * MDADM_STATUS_SUCCESS - if systemd service has been started. + * MDADM_STATUS_ERROR - otherwise. */ -int continue_via_systemd(char *devnm, char *service_name, char *prefix) +mdadm_status_t continue_via_systemd(char *devnm, char *service_name, char *prefix) { int pid, status; - char pathbuf[1024]; + char pathbuf[PATH_MAX]; dprintf("Start %s service\n", service_name); /* Simply return that service cannot be started */ if (check_env("MDADM_NO_SYSTEMCTL")) - return 0; + return MDADM_STATUS_SUCCESS; + + /* Fork in attempt to start services */ switch (fork()) { - case 0: - manage_fork_fds(1); - snprintf(pathbuf, sizeof(pathbuf), - "%s@%s%s.service", service_name, prefix ?: "", devnm); - status = execl("/usr/bin/systemctl", "systemctl", "restart", - pathbuf, NULL); - status = execl("/bin/systemctl", "systemctl", "restart", - pathbuf, NULL); - exit(1); - case -1: /* Just do it ourselves. */ + case -1: /* Fork failed, just do it ourselves. */ break; - default: /* parent - good */ + case 0: /* child */ + manage_fork_fds(1); + snprintf(pathbuf, sizeof(pathbuf), "%s@%s%s.service", + service_name, prefix ? prefix : "", devnm); + + /* Attempt to start service. + * On success execl() will "kill" the fork, and return status of systemctl call. + */ + execl("/usr/bin/systemctl", "systemctl", "restart", pathbuf, NULL); + execl("/bin/systemctl", "systemctl", "restart", pathbuf, NULL); + exit(MDADM_STATUS_ERROR); + default: /* parent */ + /* Check if forked process successfully trigered service */ pid = wait(&status); if (pid >= 0 && status == 0) - return 1; + return MDADM_STATUS_SUCCESS; } - return 0; + return MDADM_STATUS_ERROR; } int in_initrd(void) -- cgit v1.2.3