remount-fs: configure logging before use and define main through macro

This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2018-11-17 09:13:35 +01:00
parent 72d0d7a628
commit 6e61c701f2
+30 -43
View File
@@ -22,44 +22,39 @@
* options that are in /etc/fstab that systemd might not have
* respected */
int main(int argc, char *argv[]) {
static int run(int argc, char *argv[]) {
_cleanup_hashmap_free_free_ Hashmap *pids = NULL;
_cleanup_endmntent_ FILE *f = NULL;
struct mntent* me;
int r;
if (argc > 1) {
log_error("This program takes no argument.");
return EXIT_FAILURE;
}
log_set_target(LOG_TARGET_AUTO);
log_parse_environment();
log_open();
if (argc > 1) {
log_error("This program takes no arguments.");
return -EINVAL;
}
umask(0022);
f = setmntent("/etc/fstab", "re");
if (!f) {
if (errno == ENOENT) {
r = 0;
goto finish;
}
if (errno == ENOENT)
return 0;
r = log_error_errno(errno, "Failed to open /etc/fstab: %m");
goto finish;
return log_error_errno(errno, "Failed to open /etc/fstab: %m");
}
pids = hashmap_new(NULL);
if (!pids) {
r = log_oom();
goto finish;
}
if (!pids)
return log_oom();
while ((me = getmntent(f))) {
_cleanup_free_ char *s = NULL;
pid_t pid;
int k;
char *s;
/* Remount the root fs, /usr and all API VFS */
if (!mount_point_is_api(me->mnt_dir) &&
@@ -71,7 +66,7 @@ int main(int argc, char *argv[]) {
r = safe_fork("(remount)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_LOG, &pid);
if (r < 0)
goto finish;
return r;
if (r == 0) {
/* Child */
@@ -84,48 +79,40 @@ int main(int argc, char *argv[]) {
/* Parent */
s = strdup(me->mnt_dir);
if (!s) {
r = log_oom();
goto finish;
}
if (!s)
return log_oom();
k = hashmap_put(pids, PID_TO_PTR(pid), s);
if (k < 0) {
free(s);
r = log_oom();
goto finish;
}
if (k < 0)
return log_oom();
TAKE_PTR(s);
}
r = 0;
while (!hashmap_isempty(pids)) {
siginfo_t si = {};
char *s;
_cleanup_free_ char *s = NULL;
if (waitid(P_ALL, 0, &si, WEXITED) < 0) {
if (errno == EINTR)
continue;
r = log_error_errno(errno, "waitid() failed: %m");
goto finish;
return log_error_errno(errno, "waitid() failed: %m");
}
s = hashmap_remove(pids, PID_TO_PTR(si.si_pid));
if (s) {
if (!is_clean_exit(si.si_code, si.si_status, EXIT_CLEAN_COMMAND, NULL)) {
if (si.si_code == CLD_EXITED)
log_error(MOUNT_PATH " for %s exited with exit status %i.", s, si.si_status);
else
log_error(MOUNT_PATH " for %s terminated by signal %s.", s, signal_to_string(si.si_status));
if (s &&
!is_clean_exit(si.si_code, si.si_status, EXIT_CLEAN_COMMAND, NULL)) {
if (si.si_code == CLD_EXITED)
log_error(MOUNT_PATH " for %s exited with exit status %i.", s, si.si_status);
else
log_error(MOUNT_PATH " for %s terminated by signal %s.", s, signal_to_string(si.si_status));
r = -ENOEXEC;
}
free(s);
r = -ENOEXEC;
}
}
finish:
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
return r;
}
DEFINE_MAIN_FUNCTION(run);