]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/remount-api-vfs.c
service: rework killing logic so that we always kill the main process, even if it...
[thirdparty/systemd.git] / src / remount-api-vfs.c
CommitLineData
449ddb2d
LP
1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3/***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
22#include <unistd.h>
23#include <fcntl.h>
24#include <errno.h>
25#include <string.h>
26#include <sys/stat.h>
27#include <sys/wait.h>
28#include <mntent.h>
29
30#include "log.h"
31#include "util.h"
32#include "set.h"
33#include "mount-setup.h"
34
35/* Goes through /etc/fstab and remounts all API file systems, applying
36 * options that are in /etc/fstab that systemd might not have
37 * respected */
38
39int main(int argc, char *argv[]) {
22f4096c 40 int ret = EXIT_FAILURE;
449ddb2d
LP
41 FILE *f = NULL;
42 struct mntent* me;
43 Hashmap *pids = NULL;
44
45 if (argc > 1) {
46 log_error("This program takes no argument.");
22f4096c 47 return EXIT_FAILURE;
449ddb2d
LP
48 }
49
50 log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
51 log_parse_environment();
52 log_open();
53
54 if (!(f = setmntent("/etc/fstab", "r"))) {
55 log_error("Failed to open /etc/fstab: %m");
56 goto finish;
57 }
58
59 if (!(pids = hashmap_new(trivial_hash_func, trivial_compare_func))) {
60 log_error("Failed to allocate set");
61 goto finish;
62 }
63
22f4096c 64 ret = EXIT_SUCCESS;
449ddb2d
LP
65
66 while ((me = getmntent(f))) {
67 pid_t pid;
68 int k;
69 char *s;
70
71 if (!mount_point_is_api(me->mnt_dir))
72 continue;
73
74 log_debug("Remounting %s", me->mnt_dir);
75
76 if ((pid = fork()) < 0) {
77 log_error("Failed to fork: %m");
78 ret = 1;
79 continue;
80 }
81
82 if (pid == 0) {
83 const char *arguments[5];
84 /* Child */
85
86 arguments[0] = "/bin/mount";
87 arguments[1] = me->mnt_dir;
88 arguments[2] = "-o";
89 arguments[3] = "remount";
90 arguments[4] = NULL;
91
92 execv("/bin/mount", (char **) arguments);
93
94 log_error("Failed to execute /bin/mount: %m");
22f4096c 95 _exit(EXIT_FAILURE);
449ddb2d
LP
96 }
97
98 /* Parent */
99
100 s = strdup(me->mnt_dir);
101
102 if ((k = hashmap_put(pids, UINT_TO_PTR(pid), s)) < 0) {
103 log_error("Failed to add PID to set: %s", strerror(-k));
22f4096c 104 ret = EXIT_FAILURE;
449ddb2d
LP
105 continue;
106 }
107 }
108
109 while (!hashmap_isempty(pids)) {
110 siginfo_t si;
111 char *s;
112
113 zero(si);
114 if (waitid(P_ALL, 0, &si, WEXITED) < 0) {
115
116 if (errno == EINTR)
117 continue;
118
119 log_error("waitid() failed: %m");
22f4096c 120 ret = EXIT_FAILURE;
449ddb2d
LP
121 break;
122 }
123
124 if ((s = hashmap_remove(pids, UINT_TO_PTR(si.si_pid)))) {
125 if (!is_clean_exit(si.si_code, si.si_status)) {
126 if (si.si_code == CLD_EXITED)
127 log_error("/bin/mount for %s exited with exit status %i.", s, si.si_status);
128 else
129 log_error("/bin/mount for %s terminated by signal %s.", s, signal_to_string(si.si_status));
130
22f4096c 131 ret = EXIT_FAILURE;
449ddb2d
LP
132 }
133
134 free(s);
135 }
136 }
137
138finish:
139
140 if (pids)
141 hashmap_free_free(pids);
142
143 if (f)
144 endmntent(f);
145
146 return ret;
147}