]>
Commit | Line | Data |
---|---|---|
e3478379 FF |
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; | |
e3478379 FF |
38 | LIST_FIELDS (struct MountPoint, mount_point); |
39 | } MountPoint; | |
40 | ||
2054a5b8 | 41 | /* Takes over possession of path */ |
e3478379 FF |
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; | |
e3478379 FF |
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 | ||
2054a5b8 LP |
102 | p = cunescape(path); |
103 | free(path); | |
e3478379 | 104 | |
2054a5b8 | 105 | if (!p) { |
e3478379 FF |
106 | r = -ENOMEM; |
107 | goto finish; | |
108 | } | |
109 | ||
2054a5b8 LP |
110 | if (mount_point_is_api(p)) { |
111 | free(p); | |
112 | continue; | |
113 | } | |
114 | ||
e3478379 | 115 | if (!(mp = mount_point_alloc(p))) { |
2054a5b8 | 116 | free(p); |
e3478379 FF |
117 | r = -ENOMEM; |
118 | goto finish; | |
119 | } | |
e3478379 | 120 | |
2054a5b8 | 121 | LIST_PREPEND(MountPoint, mount_point, *mount_point_list_head, mp); |
e3478379 FF |
122 | } |
123 | ||
124 | r = 0; | |
125 | ||
126 | finish: | |
127 | fclose(proc_self_mountinfo); | |
128 | ||
e3478379 FF |
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; | |
b854a7e7 | 230 | struct udev_device *d; |
e3478379 | 231 | char *loop; |
b854a7e7 | 232 | const char *dn; |
e3478379 | 233 | |
b854a7e7 | 234 | if (!(d = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item)))) { |
e3478379 FF |
235 | r = -ENOMEM; |
236 | goto finish; | |
237 | } | |
238 | ||
b854a7e7 LP |
239 | if ((dn = udev_device_get_devnode(d))) { |
240 | loop = strdup(dn); | |
241 | udev_device_unref(d); | |
242 | ||
243 | if (!loop) { | |
244 | r = -ENOMEM; | |
245 | goto finish; | |
246 | } | |
247 | } else | |
248 | udev_device_unref(d); | |
249 | ||
250 | if (!(lb = mount_point_alloc(loop))) { | |
e3478379 FF |
251 | free(loop); |
252 | r = -ENOMEM; | |
253 | goto finish; | |
254 | } | |
255 | ||
256 | LIST_PREPEND(MountPoint, mount_point, *loopback_list_head, lb); | |
257 | } | |
258 | ||
259 | r = 0; | |
260 | ||
261 | finish: | |
262 | if (e) | |
263 | udev_enumerate_unref(e); | |
264 | ||
b854a7e7 LP |
265 | if (udev) |
266 | udev_unref(udev); | |
267 | ||
e3478379 FF |
268 | return r; |
269 | } | |
270 | ||
271 | static int delete_loopback(const char *device) { | |
272 | int fd, r; | |
273 | ||
ce726252 | 274 | if ((fd = open(device, O_RDONLY|O_CLOEXEC)) < 0) |
e3478379 | 275 | return -errno; |
e3478379 | 276 | |
ce726252 | 277 | r = ioctl(fd, LOOP_CLR_FD, 0); |
143b4e9b | 278 | close_nointr_nofail(fd); |
e3478379 | 279 | |
ce726252 LP |
280 | /* ENXIO: not bound, so no error */ |
281 | return (r >= 0 || errno == ENXIO) ? 0 : -errno; | |
e3478379 FF |
282 | } |
283 | ||
284 | static int mount_points_list_umount(MountPoint **mount_point_list_head) { | |
285 | MountPoint *mp, *mp_next; | |
286 | int failed = 0; | |
287 | ||
288 | LIST_FOREACH_SAFE(mount_point, mp, mp_next, *mount_point_list_head) { | |
289 | if (streq(mp->path, "/")) | |
290 | continue; | |
291 | ||
292 | /* Trying to umount. Forcing to umount if busy (only for NFS mounts) */ | |
293 | if (umount2(mp->path, MNT_FORCE) == 0) | |
294 | mount_point_remove_and_free(mp, mount_point_list_head); | |
295 | else { | |
7e23b34c | 296 | log_warning("Could not unmount %s: %m", mp->path); |
e3478379 FF |
297 | failed++; |
298 | } | |
299 | } | |
300 | ||
301 | return failed; | |
302 | } | |
303 | ||
304 | static int mount_points_list_remount_read_only(MountPoint **mount_point_list_head) { | |
305 | MountPoint *mp, *mp_next; | |
306 | int failed = 0; | |
307 | ||
308 | LIST_FOREACH_SAFE(mount_point, mp, mp_next, *mount_point_list_head) { | |
e3478379 | 309 | /* Trying to remount read-only */ |
143b4e9b | 310 | if (mount(NULL, mp->path, NULL, MS_MGC_VAL|MS_REMOUNT|MS_RDONLY, NULL) == 0) |
e3478379 | 311 | mount_point_remove_and_free(mp, mount_point_list_head); |
143b4e9b | 312 | else { |
7e23b34c | 313 | log_warning("Could not remount as read-only %s: %m", mp->path); |
e3478379 FF |
314 | failed++; |
315 | } | |
316 | } | |
317 | ||
318 | return failed; | |
319 | } | |
320 | ||
321 | static int swap_points_list_off(MountPoint **swap_list_head) { | |
322 | MountPoint *swap, *swap_next; | |
323 | int failed = 0; | |
324 | ||
325 | LIST_FOREACH_SAFE(mount_point, swap, swap_next, *swap_list_head) { | |
326 | if (swapoff(swap->path) == 0) | |
327 | mount_point_remove_and_free(swap, swap_list_head); | |
328 | else { | |
7e23b34c | 329 | log_warning("Could not deactivate swap %s: %m", swap->path); |
e3478379 FF |
330 | failed++; |
331 | } | |
332 | } | |
333 | ||
334 | return failed; | |
335 | } | |
336 | ||
337 | static int loopback_points_list_detach(MountPoint **loopback_list_head) { | |
338 | MountPoint *loopback, *loopback_next; | |
339 | int failed = 0; | |
340 | ||
341 | LIST_FOREACH_SAFE(mount_point, loopback, loopback_next, *loopback_list_head) { | |
342 | if (delete_loopback(loopback->path) == 0) | |
343 | mount_point_remove_and_free(loopback, loopback_list_head); | |
344 | else { | |
7e23b34c | 345 | log_warning("Could not delete loopback %s: %m", loopback->path); |
e3478379 FF |
346 | failed++; |
347 | } | |
348 | } | |
349 | ||
350 | return failed; | |
351 | } | |
352 | ||
353 | int umount_all(void) { | |
354 | int r; | |
355 | LIST_HEAD(MountPoint, mp_list_head); | |
356 | ||
357 | LIST_HEAD_INIT(MountPoint, mp_list_head); | |
358 | ||
359 | r = mount_points_list_get(&mp_list_head); | |
360 | if (r < 0) | |
361 | goto end; | |
362 | ||
363 | r = mount_points_list_umount(&mp_list_head); | |
364 | if (r <= 0) | |
365 | goto end; | |
366 | ||
367 | r = mount_points_list_remount_read_only(&mp_list_head); | |
368 | ||
369 | end: | |
370 | mount_points_list_free(&mp_list_head); | |
371 | ||
372 | return r; | |
373 | } | |
374 | ||
375 | int swapoff_all(void) { | |
376 | int r; | |
377 | LIST_HEAD(MountPoint, swap_list_head); | |
378 | ||
379 | LIST_HEAD_INIT(MountPoint, swap_list_head); | |
380 | ||
381 | r = swap_list_get(&swap_list_head); | |
382 | if (r < 0) | |
383 | goto end; | |
384 | ||
385 | r = swap_points_list_off(&swap_list_head); | |
386 | ||
387 | end: | |
388 | mount_points_list_free(&swap_list_head); | |
389 | ||
390 | return r; | |
391 | } | |
392 | ||
393 | int loopback_detach_all(void) { | |
394 | int r; | |
395 | LIST_HEAD(MountPoint, loopback_list_head); | |
396 | ||
397 | LIST_HEAD_INIT(MountPoint, loopback_list_head); | |
398 | ||
399 | r = loopback_list_get(&loopback_list_head); | |
400 | if (r < 0) | |
401 | goto end; | |
402 | ||
403 | r = loopback_points_list_detach(&loopback_list_head); | |
404 | ||
405 | end: | |
406 | mount_points_list_free(&loopback_list_head); | |
407 | ||
408 | return r; | |
409 | } |