chiark / gitweb /
umount: unescape path from /proc/self/mountinfo first, then check against api mount...
[elogind.git] / src / umount.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 ProFUSION embedded systems
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 <errno.h>
23 #include <fcntl.h>
24 #include <string.h>
25 #include <sys/mount.h>
26 #include <sys/swap.h>
27 #include <unistd.h>
28 #include <linux/loop.h>
29 #include <libudev.h>
30
31 #include "list.h"
32 #include "mount-setup.h"
33 #include "umount.h"
34 #include "util.h"
35
36 typedef struct MountPoint {
37         char *path;
38         LIST_FIELDS (struct MountPoint, mount_point);
39 } MountPoint;
40
41 /* Takes over possession of path */
42 static MountPoint *mount_point_alloc(char *path) {
43         MountPoint *mp;
44
45         if (!(mp = new(MountPoint, 1)))
46                 return NULL;
47
48         mp->path = path;
49         return mp;
50 }
51
52 static void mount_point_remove_and_free(MountPoint *mount_point, MountPoint **mount_point_list_head) {
53         LIST_REMOVE(MountPoint, mount_point, *mount_point_list_head, mount_point);
54
55         free(mount_point->path);
56         free(mount_point);
57 }
58
59 static void mount_points_list_free(MountPoint **mount_point_list_head) {
60         while (*mount_point_list_head)
61                 mount_point_remove_and_free(*mount_point_list_head, mount_point_list_head);
62 }
63
64 static int mount_points_list_get(MountPoint **mount_point_list_head) {
65         FILE *proc_self_mountinfo;
66         char *path, *p;
67         unsigned int i;
68         int r;
69
70         if (!(proc_self_mountinfo = fopen("/proc/self/mountinfo", "re")))
71                 return -errno;
72
73         for (i = 1;; i++) {
74                 int k;
75                 MountPoint *mp;
76
77                 path = p = NULL;
78
79                 if ((k = fscanf(proc_self_mountinfo,
80                                 "%*s "       /* (1) mount id */
81                                 "%*s "       /* (2) parent id */
82                                 "%*s "       /* (3) major:minor */
83                                 "%*s "       /* (4) root */
84                                 "%ms "       /* (5) mount point */
85                                 "%*s"        /* (6) mount options */
86                                 "%*[^-]"     /* (7) optional fields */
87                                 "- "         /* (8) separator */
88                                 "%*s "       /* (9) file system type */
89                                 "%*s"        /* (10) mount source */
90                                 "%*s"        /* (11) mount options 2 */
91                                 "%*[^\n]",   /* some rubbish at the end */
92                                 &path)) != 1) {
93                         if (k == EOF)
94                                 break;
95
96                         log_warning("Failed to parse /proc/self/mountinfo:%u.", i);
97
98                         free(path);
99                         continue;
100                 }
101
102                 p = cunescape(path);
103                 free(path);
104
105                 if (!p) {
106                         r = -ENOMEM;
107                         goto finish;
108                 }
109
110                 if (mount_point_is_api(p)) {
111                         free(p);
112                         continue;
113                 }
114
115                 if (!(mp = mount_point_alloc(p))) {
116                         free(p);
117                         r = -ENOMEM;
118                         goto finish;
119                 }
120
121                 LIST_PREPEND(MountPoint, mount_point, *mount_point_list_head, mp);
122         }
123
124         r = 0;
125
126 finish:
127         fclose(proc_self_mountinfo);
128
129         return r;
130 }
131
132 static int swap_list_get(MountPoint **swap_list_head) {
133         FILE *proc_swaps;
134         unsigned int i;
135         int r;
136
137         if (!(proc_swaps = fopen("/proc/swaps", "re")))
138                 return -errno;
139
140         (void) fscanf(proc_swaps, "%*s %*s %*s %*s %*s\n");
141
142         for (i = 2;; i++) {
143                 MountPoint *swap;
144                 char *dev = NULL, *d;
145                 int k;
146
147                 if ((k = fscanf(proc_swaps,
148                                 "%ms " /* device/file */
149                                 "%*s " /* type of swap */
150                                 "%*s " /* swap size */
151                                 "%*s " /* used */
152                                 "%*s\n", /* priority */
153                                 &dev)) != 1) {
154
155                         if (k == EOF)
156                                 break;
157
158                         log_warning("Failed to parse /proc/swaps:%u.", i);
159
160                         free(dev);
161                         continue;
162                 }
163
164                 if (endswith(dev, "(deleted)")) {
165                         free(dev);
166                         continue;
167                 }
168
169                 d = cunescape(dev);
170                 free(dev);
171
172                 if (!d) {
173                         r = -ENOMEM;
174                         goto finish;
175                 }
176
177                 swap = mount_point_alloc(d);
178                 if (!swap) {
179                         free(d);
180                         r = -ENOMEM;
181                         goto finish;
182                 }
183
184                 LIST_PREPEND(MountPoint, mount_point, *swap_list_head, swap);
185         }
186
187         r = 0;
188
189 finish:
190         fclose(proc_swaps);
191
192         return r;
193 }
194
195 static int loopback_list_get(MountPoint **loopback_list_head) {
196         int r;
197         struct udev *udev;
198         struct udev_enumerate *e = NULL;
199         struct udev_list_entry *item = NULL, *first = NULL;
200
201         if (!(udev = udev_new())) {
202                 r = -ENOMEM;
203                 goto finish;
204         }
205
206         if (!(e = udev_enumerate_new(udev))) {
207                 r = -ENOMEM;
208                 goto finish;
209         }
210
211         if (udev_enumerate_add_match_subsystem(e, "block") < 0) {
212                 r = -EIO;
213                 goto finish;
214         }
215
216         if (udev_enumerate_add_match_sysname(e, "loop*") < 0) {
217                 r = -EIO;
218                 goto finish;
219         }
220
221         if (udev_enumerate_scan_devices(e) < 0) {
222                 r = -EIO;
223                 goto finish;
224         }
225
226         first = udev_enumerate_get_list_entry(e);
227
228         udev_list_entry_foreach(item, first) {
229                 MountPoint *lb;
230                 char *loop;
231
232                 loop = cunescape(udev_list_entry_get_name(item));
233                 if (!loop) {
234                         r = -ENOMEM;
235                         goto finish;
236                 }
237
238                 lb = mount_point_alloc(loop);
239                 if (!lb) {
240                         free(loop);
241                         r = -ENOMEM;
242                         goto finish;
243                 }
244
245                 LIST_PREPEND(MountPoint, mount_point, *loopback_list_head, lb);
246         }
247
248         r = 0;
249
250 finish:
251         if (e)
252                 udev_enumerate_unref(e);
253
254         free(udev);
255         return r;
256 }
257
258 static int delete_loopback(const char *device) {
259         int fd, r;
260
261         if ((fd = open(device, O_RDONLY|O_CLOEXEC)) < 0) {
262                 if (errno == ENOENT) {
263                         log_debug("Loop device %s does not exist.", device);
264                         errno = 0;
265                         return 0;
266                 }
267                 return -errno;
268         }
269
270         ioctl(fd, LOOP_CLR_FD, 0);
271         r = errno;
272         close_nointr_nofail(fd);
273
274         if (r == ENXIO) /* not bound, so no error */
275                 r = 0;
276         errno = r;
277         return -errno;
278 }
279
280 static int mount_points_list_umount(MountPoint **mount_point_list_head) {
281         MountPoint *mp, *mp_next;
282         int failed = 0;
283
284         LIST_FOREACH_SAFE(mount_point, mp, mp_next, *mount_point_list_head) {
285                 if (streq(mp->path, "/"))
286                         continue;
287
288                 /* Trying to umount. Forcing to umount if busy (only for NFS mounts) */
289                 if (umount2(mp->path, MNT_FORCE) == 0)
290                         mount_point_remove_and_free(mp, mount_point_list_head);
291                 else {
292                         log_debug("Could not unmount %s: %m", mp->path);
293                         failed++;
294                 }
295         }
296
297         return failed;
298 }
299
300 static int mount_points_list_remount_read_only(MountPoint **mount_point_list_head) {
301         MountPoint *mp, *mp_next;
302         int failed = 0;
303
304         LIST_FOREACH_SAFE(mount_point, mp, mp_next, *mount_point_list_head) {
305                 /* Trying to remount read-only */
306                 if (mount(NULL, mp->path, NULL, MS_MGC_VAL|MS_REMOUNT|MS_RDONLY, NULL) == 0)
307                         mount_point_remove_and_free(mp, mount_point_list_head);
308                 else {
309                         log_debug("Could not remount as read-only %s: %m", mp->path);
310                         failed++;
311                 }
312         }
313
314         return failed;
315 }
316
317 static int swap_points_list_off(MountPoint **swap_list_head) {
318         MountPoint *swap, *swap_next;
319         int failed = 0;
320
321         LIST_FOREACH_SAFE(mount_point, swap, swap_next, *swap_list_head) {
322                 if (swapoff(swap->path) == 0)
323                         mount_point_remove_and_free(swap, swap_list_head);
324                 else {
325                         log_debug("Could not swapoff %s: %m", swap->path);
326                         failed++;
327                 }
328         }
329
330         return failed;
331 }
332
333 static int loopback_points_list_detach(MountPoint **loopback_list_head) {
334         MountPoint *loopback, *loopback_next;
335         int failed = 0;
336
337         LIST_FOREACH_SAFE(mount_point, loopback, loopback_next, *loopback_list_head) {
338                 if (delete_loopback(loopback->path) == 0)
339                         mount_point_remove_and_free(loopback, loopback_list_head);
340                 else {
341                         log_debug("Could not delete loopback %s: %m", loopback->path);
342                         failed++;
343                 }
344         }
345
346         return failed;
347 }
348
349 int umount_all(void) {
350         int r;
351         LIST_HEAD(MountPoint, mp_list_head);
352
353         LIST_HEAD_INIT(MountPoint, mp_list_head);
354
355         r = mount_points_list_get(&mp_list_head);
356         if (r < 0)
357                 goto end;
358
359         r = mount_points_list_umount(&mp_list_head);
360         if (r <= 0)
361                 goto end;
362
363         r = mount_points_list_remount_read_only(&mp_list_head);
364
365   end:
366         mount_points_list_free(&mp_list_head);
367
368         return r;
369 }
370
371 int swapoff_all(void) {
372         int r;
373         LIST_HEAD(MountPoint, swap_list_head);
374
375         LIST_HEAD_INIT(MountPoint, swap_list_head);
376
377         r = swap_list_get(&swap_list_head);
378         if (r < 0)
379                 goto end;
380
381         r = swap_points_list_off(&swap_list_head);
382
383   end:
384         mount_points_list_free(&swap_list_head);
385
386         return r;
387 }
388
389 int loopback_detach_all(void) {
390         int r;
391         LIST_HEAD(MountPoint, loopback_list_head);
392
393         LIST_HEAD_INIT(MountPoint, loopback_list_head);
394
395         r = loopback_list_get(&loopback_list_head);
396         if (r < 0)
397                 goto end;
398
399         r = loopback_points_list_detach(&loopback_list_head);
400
401   end:
402         mount_points_list_free(&loopback_list_head);
403
404         return r;
405 }