Skip to content

Commit

Permalink
Refactor continue_via_systemd()
Browse files Browse the repository at this point in the history
Refactor continue_via_systemd() and it's calls to make it more readable.
No functional changes.

Signed-off-by: Mateusz Kusiak <[email protected]>
  • Loading branch information
dancesWithMachines authored and mtkaczyk committed Jan 13, 2025
1 parent 70cba6b commit 82ccad6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Grow.c
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
2 changes: 1 addition & 1 deletion mdadm.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
43 changes: 24 additions & 19 deletions util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 82ccad6

Please sign in to comment.