chiark / gitweb /
label: fix database check
[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         umask(0022);
56
57         if (!(f = setmntent("/etc/fstab", "r"))) {
58                 log_error("Failed to open /etc/fstab: %m");
59                 goto finish;
60         }
61
62         if (!(pids = hashmap_new(trivial_hash_func, trivial_compare_func))) {
63                 log_error("Failed to allocate set");
64                 goto finish;
65         }
66
67         ret = EXIT_SUCCESS;
68
69         while ((me = getmntent(f))) {
70                 pid_t pid;
71                 int k;
72                 char *s;
73
74                 if (!mount_point_is_api(me->mnt_dir))
75                         continue;
76
77                 log_debug("Remounting %s", me->mnt_dir);
78
79                 if ((pid = fork()) < 0) {
80                         log_error("Failed to fork: %m");
81                         ret = 1;
82                         continue;
83                 }
84
85                 if (pid == 0) {
86                         const char *arguments[5];
87                         /* Child */
88
89                         arguments[0] = "/bin/mount";
90                         arguments[1] = me->mnt_dir;
91                         arguments[2] = "-o";
92                         arguments[3] = "remount";
93                         arguments[4] = NULL;
94
95                         execv("/bin/mount", (char **) arguments);
96
97                         log_error("Failed to execute /bin/mount: %m");
98                         _exit(EXIT_FAILURE);
99                 }
100
101                 /* Parent */
102
103                 s = strdup(me->mnt_dir);
104
105                 if ((k = hashmap_put(pids, UINT_TO_PTR(pid), s)) < 0) {
106                         log_error("Failed to add PID to set: %s", strerror(-k));
107                         ret = EXIT_FAILURE;
108                         continue;
109                 }
110         }
111
112         while (!hashmap_isempty(pids)) {
113                 siginfo_t si;
114                 char *s;
115
116                 zero(si);
117                 if (waitid(P_ALL, 0, &si, WEXITED) < 0) {
118
119                         if (errno == EINTR)
120                                 continue;
121
122                         log_error("waitid() failed: %m");
123                         ret = EXIT_FAILURE;
124                         break;
125                 }
126
127                 if ((s = hashmap_remove(pids, UINT_TO_PTR(si.si_pid)))) {
128                         if (!is_clean_exit(si.si_code, si.si_status)) {
129                                 if (si.si_code == CLD_EXITED)
130                                         log_error("/bin/mount for %s exited with exit status %i.", s, si.si_status);
131                                 else
132                                         log_error("/bin/mount for %s terminated by signal %s.", s, signal_to_string(si.si_status));
133
134                                 ret = EXIT_FAILURE;
135                         }
136
137                         free(s);
138                 }
139         }
140
141 finish:
142
143         if (pids)
144                 hashmap_free_free(pids);
145
146         if (f)
147                 endmntent(f);
148
149         return ret;
150 }