Skip to content

Commit 82ccad6

Browse files
dancesWithMachinesmtkaczyk
authored andcommitted
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 <[email protected]>
1 parent 70cba6b commit 82ccad6

File tree

3 files changed

+26
-21
lines changed

3 files changed

+26
-21
lines changed

Grow.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3013,7 +3013,7 @@ static mdadm_status_t handle_forking(bool forked, char *devname)
30133013
if (forked)
30143014
return MDADM_STATUS_FORKED;
30153015

3016-
if (devname && continue_via_systemd(devname, GROW_SERVICE, NULL))
3016+
if (devname && continue_via_systemd(devname, GROW_SERVICE, NULL) == MDADM_STATUS_SUCCESS)
30173017
return MDADM_STATUS_SUCCESS;
30183018

30193019
switch (fork()) {

mdadm.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1694,7 +1694,7 @@ extern int same_dev(char *one, char *two);
16941694
extern int compare_paths (char* path1,char* path2);
16951695
extern void enable_fds(int devices);
16961696
extern void manage_fork_fds(int close_all);
1697-
extern int continue_via_systemd(char *devnm, char *service_name, char *prefix);
1697+
extern mdadm_status_t continue_via_systemd(char *devnm, char *service_name, char *prefix);
16981698

16991699
extern void ident_init(struct mddev_ident *ident);
17001700
extern mdadm_status_t ident_set_devname(struct mddev_ident *ident, const char *devname);

util.c

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1982,7 +1982,7 @@ int start_mdmon(char *devnm)
19821982

19831983
if (check_env("MDADM_NO_MDMON"))
19841984
return 0;
1985-
if (continue_via_systemd(devnm, MDMON_SERVICE, prefix))
1985+
if (continue_via_systemd(devnm, MDMON_SERVICE, prefix) == MDADM_STATUS_SUCCESS)
19861986
return 0;
19871987

19881988
/* That failed, try running mdmon directly */
@@ -2299,36 +2299,41 @@ void manage_fork_fds(int close_all)
22992299
/* In a systemd/udev world, it is best to get systemd to
23002300
* run daemon rather than running in the background.
23012301
* Returns:
2302-
* 1- if systemd service has been started
2303-
* 0- otherwise
2302+
* MDADM_STATUS_SUCCESS - if systemd service has been started.
2303+
* MDADM_STATUS_ERROR - otherwise.
23042304
*/
2305-
int continue_via_systemd(char *devnm, char *service_name, char *prefix)
2305+
mdadm_status_t continue_via_systemd(char *devnm, char *service_name, char *prefix)
23062306
{
23072307
int pid, status;
2308-
char pathbuf[1024];
2308+
char pathbuf[PATH_MAX];
23092309

23102310
dprintf("Start %s service\n", service_name);
23112311
/* Simply return that service cannot be started */
23122312
if (check_env("MDADM_NO_SYSTEMCTL"))
2313-
return 0;
2313+
return MDADM_STATUS_SUCCESS;
2314+
2315+
/* Fork in attempt to start services */
23142316
switch (fork()) {
2315-
case 0:
2316-
manage_fork_fds(1);
2317-
snprintf(pathbuf, sizeof(pathbuf),
2318-
"%s@%s%s.service", service_name, prefix ?: "", devnm);
2319-
status = execl("/usr/bin/systemctl", "systemctl", "restart",
2320-
pathbuf, NULL);
2321-
status = execl("/bin/systemctl", "systemctl", "restart",
2322-
pathbuf, NULL);
2323-
exit(1);
2324-
case -1: /* Just do it ourselves. */
2317+
case -1: /* Fork failed, just do it ourselves. */
23252318
break;
2326-
default: /* parent - good */
2319+
case 0: /* child */
2320+
manage_fork_fds(1);
2321+
snprintf(pathbuf, sizeof(pathbuf), "%s@%s%s.service",
2322+
service_name, prefix ? prefix : "", devnm);
2323+
2324+
/* Attempt to start service.
2325+
* On success execl() will "kill" the fork, and return status of systemctl call.
2326+
*/
2327+
execl("/usr/bin/systemctl", "systemctl", "restart", pathbuf, NULL);
2328+
execl("/bin/systemctl", "systemctl", "restart", pathbuf, NULL);
2329+
exit(MDADM_STATUS_ERROR);
2330+
default: /* parent */
2331+
/* Check if forked process successfully trigered service */
23272332
pid = wait(&status);
23282333
if (pid >= 0 && status == 0)
2329-
return 1;
2334+
return MDADM_STATUS_SUCCESS;
23302335
}
2331-
return 0;
2336+
return MDADM_STATUS_ERROR;
23322337
}
23332338

23342339
int in_initrd(void)

0 commit comments

Comments
 (0)