chiark / gitweb /
dbus: return DBUS_ERROR_UNKNOWN_OBJECT when an object is unknown
[elogind.git] / src / remount-api-vfs.c
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 #include "exit-status.h"
35
36 /* Goes through /etc/fstab and remounts all API file systems, applying
37  * options that are in /etc/fstab that systemd might not have
38  * respected */
39
40 int main(int argc, char *argv[]) {
41         int ret = EXIT_FAILURE;
42         FILE *f = NULL;
43         struct mntent* me;
44         Hashmap *pids = NULL;
45
46         if (argc > 1) {
47                 log_error("This program takes no argument.");
48                 return EXIT_FAILURE;
49         }
50
51         log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
52         log_parse_environment();
53         log_open();
54
55         if (!(f = setmntent("/etc/fstab", "r"))) {
56                 log_error("Failed to open /etc/fstab: %m");
57                 goto finish;
58         }
59
60         if (!(pids = hashmap_new(trivial_hash_func, trivial_compare_func))) {
61                 log_error("Failed to allocate set");
62                 goto finish;
63         }
64
65         ret = EXIT_SUCCESS;
66
67         while ((me = getmntent(f))) {
68                 pid_t pid;
69                 int k;
70                 char *s;
71
72                 if (!mount_point_is_api(me->mnt_dir))
73                         continue;
74
75                 log_debug("Remounting %s", me->mnt_dir);
76
77                 if ((pid = fork()) < 0) {
78                         log_error("Failed to fork: %m");
79                         ret = 1;
80                         continue;
81                 }
82
83                 if (pid == 0) {
84                         const char *arguments[5];
85                         /* Child */
86
87                         arguments[0] = "/bin/mount";
88                         arguments[1] = me->mnt_dir;
89                         arguments[2] = "-o";
90                         arguments[3] = "remount";
91                         arguments[4] = NULL;
92
93                         execv("/bin/mount", (char **) arguments);
94
95                         log_error("Failed to execute /bin/mount: %m");
96                         _exit(EXIT_FAILURE);
97                 }
98
99                 /* Parent */
100
101                 s = strdup(me->mnt_dir);
102
103                 if ((k = hashmap_put(pids, UINT_TO_PTR(pid), s)) < 0) {
104                         log_error("Failed to add PID to set: %s", strerror(-k));
105                         ret = EXIT_FAILURE;
106                         continue;
107                 }
108         }
109
110         while (!hashmap_isempty(pids)) {
111                 siginfo_t si;
112                 char *s;
113
114                 zero(si);
115                 if (waitid(P_ALL, 0, &si, WEXITED) < 0) {
116
117                         if (errno == EINTR)
118                                 continue;
119
120                         log_error("waitid() failed: %m");
121                         ret = EXIT_FAILURE;
122                         break;
123                 }
124
125                 if ((s = hashmap_remove(pids, UINT_TO_PTR(si.si_pid)))) {
126                         if (!is_clean_exit(si.si_code, si.si_status)) {
127                                 if (si.si_code == CLD_EXITED)
128                                         log_error("/bin/mount for %s exited with exit status %i.", s, si.si_status);
129                                 else
130                                         log_error("/bin/mount for %s terminated by signal %s.", s, signal_to_string(si.si_status));
131
132                                 ret = EXIT_FAILURE;
133                         }
134
135                         free(s);
136                 }
137         }
138
139 finish:
140
141         if (pids)
142                 hashmap_free_free(pids);
143
144         if (f)
145                 endmntent(f);
146
147         return ret;
148 }