diff options
author | Mateusz Kusiak <mateusz.kusiak@intel.com> | 2024-11-20 20:01:30 +0100 |
---|---|---|
committer | Mariusz Tkaczyk <mtkaczyk@kernel.org> | 2025-01-13 10:24:49 +0100 |
commit | 82ccad68d46d4b10a928bc860c0feedf26e483e3 (patch) | |
tree | 20ed74691826bdfcd1f32f3278eda0e483538d44 | |
parent | Better error messages for broken reshape (diff) | |
download | mdadm-82ccad68d46d4b10a928bc860c0feedf26e483e3.tar.xz mdadm-82ccad68d46d4b10a928bc860c0feedf26e483e3.zip |
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 <mateusz.kusiak@intel.com>
-rw-r--r-- | Grow.c | 2 | ||||
-rw-r--r-- | mdadm.h | 2 | ||||
-rw-r--r-- | util.c | 43 |
3 files changed, 26 insertions, 21 deletions
@@ -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()) { @@ -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); @@ -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) |