]>
Commit | Line | Data |
---|---|---|
db216e68 KZ |
1 | /* |
2 | * umount(8) -- mount a filesystem | |
3 | * | |
4 | * Copyright (C) 2011 Red Hat, Inc. All rights reserved. | |
5 | * Written by Karel Zak <kzak@redhat.com> | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it would be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
7cebf0bb SK |
17 | * You should have received a copy of the GNU General Public License along |
18 | * with this program; if not, write to the Free Software Foundation, Inc., | |
19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
db216e68 KZ |
20 | */ |
21 | ||
22 | #include <stdio.h> | |
23 | #include <stdlib.h> | |
24 | #include <errno.h> | |
25 | #include <string.h> | |
26 | #include <getopt.h> | |
27 | #include <unistd.h> | |
28 | #include <sys/types.h> | |
29 | ||
30 | #include <libmount.h> | |
31 | ||
32 | #include "nls.h" | |
33 | #include "c.h" | |
34 | #include "env.h" | |
35 | #include "optutils.h" | |
73f9a114 | 36 | #include "exitcodes.h" |
efb8854f | 37 | #include "closestream.h" |
db216e68 KZ |
38 | |
39 | static int table_parser_errcb(struct libmnt_table *tb __attribute__((__unused__)), | |
40 | const char *filename, int line) | |
41 | { | |
42 | if (filename) | |
43 | warnx(_("%s: parse error: ignore entry at line %d."), | |
44 | filename, line); | |
45 | return 0; | |
46 | } | |
47 | ||
ac8ecab4 | 48 | |
db216e68 KZ |
49 | static void __attribute__((__noreturn__)) print_version(void) |
50 | { | |
51 | const char *ver = NULL; | |
ac8ecab4 | 52 | const char **features = NULL, **p; |
db216e68 KZ |
53 | |
54 | mnt_get_library_version(&ver); | |
ac8ecab4 | 55 | mnt_get_library_features(&features); |
db216e68 | 56 | |
ac8ecab4 KZ |
57 | printf(_("%s from %s (libmount %s"), |
58 | program_invocation_short_name, | |
59 | PACKAGE_STRING, | |
60 | ver); | |
61 | p = features; | |
62 | while (p && *p) { | |
63 | fputs(p == features ? ": " : ", ", stdout); | |
64 | fputs(*p++, stdout); | |
65 | } | |
66 | fputs(")\n", stdout); | |
73f9a114 | 67 | exit(MOUNT_EX_SUCCESS); |
db216e68 | 68 | } |
db216e68 KZ |
69 | static void __attribute__((__noreturn__)) usage(FILE *out) |
70 | { | |
71 | fputs(USAGE_HEADER, out); | |
72 | fprintf(out, _( | |
73 | " %1$s [-hV]\n" | |
74 | " %1$s -a [options]\n" | |
75 | " %1$s [options] <source> | <directory>\n"), | |
76 | program_invocation_short_name); | |
77 | ||
78 | fputs(USAGE_OPTIONS, out); | |
79 | fprintf(out, _( | |
ac8ecab4 | 80 | " -a, --all umount all filesystems\n" |
db216e68 KZ |
81 | " -c, --no-canonicalize don't canonicalize paths\n" |
82 | " -d, --detach-loop if mounted loop device, also free this loop device\n" | |
83 | " --fake dry run; skip the umount(2) syscall\n" | |
84 | " -f, --force force unmount (in case of an unreachable NFS system)\n")); | |
85 | fprintf(out, _( | |
86 | " -i, --internal-only don't call the umount.<type> helpers\n" | |
87 | " -n, --no-mtab don't write to /etc/mtab\n" | |
88 | " -l, --lazy detach the filesystem now, and cleanup all later\n")); | |
89 | fprintf(out, _( | |
90 | " -O, --test-opts <list> limit the set of filesystems (use with -a)\n" | |
91 | " -r, --read-only In case unmounting fails, try to remount read-only\n" | |
92 | " -t, --types <list> limit the set of filesystem types\n" | |
93 | " -v, --verbose say what is being done\n")); | |
94 | ||
95 | fputs(USAGE_SEPARATOR, out); | |
96 | fputs(USAGE_HELP, out); | |
97 | fputs(USAGE_VERSION, out); | |
98 | fprintf(out, USAGE_MAN_TAIL("umount(8)")); | |
99 | ||
73f9a114 | 100 | exit(out == stderr ? MOUNT_EX_USAGE : MOUNT_EX_SUCCESS); |
db216e68 KZ |
101 | } |
102 | ||
103 | static void __attribute__((__noreturn__)) exit_non_root(const char *option) | |
104 | { | |
105 | const uid_t ruid = getuid(); | |
106 | const uid_t euid = geteuid(); | |
107 | ||
108 | if (ruid == 0 && euid != 0) { | |
109 | /* user is root, but setuid to non-root */ | |
110 | if (option) | |
73f9a114 | 111 | errx(MOUNT_EX_USAGE, |
db216e68 KZ |
112 | _("only root can use \"--%s\" option " |
113 | "(effective UID is %u)"), | |
114 | option, euid); | |
73f9a114 | 115 | errx(MOUNT_EX_USAGE, _("only root can do that " |
db216e68 KZ |
116 | "(effective UID is %u)"), euid); |
117 | } | |
118 | if (option) | |
73f9a114 KZ |
119 | errx(MOUNT_EX_USAGE, _("only root can use \"--%s\" option"), option); |
120 | errx(MOUNT_EX_USAGE, _("only root can do that")); | |
121 | } | |
122 | ||
123 | /* | |
124 | * Handles generic errors like ENOMEM, ... | |
125 | * | |
126 | * rc = 0 success | |
127 | * <0 error (usually -errno) | |
128 | * | |
129 | * Returns exit status (MOUNT_EX_*) and prints error message. | |
130 | */ | |
131 | static int handle_generic_errors(int rc, const char *msg, ...) | |
132 | { | |
133 | va_list va; | |
134 | ||
135 | va_start(va, msg); | |
136 | errno = -rc; | |
137 | ||
138 | switch(errno) { | |
139 | case EINVAL: | |
140 | case EPERM: | |
141 | vwarn(msg, va); | |
142 | rc = MOUNT_EX_USAGE; | |
143 | break; | |
144 | case ENOMEM: | |
145 | vwarn(msg, va); | |
146 | rc = MOUNT_EX_SYSERR; | |
147 | break; | |
148 | default: | |
149 | vwarn(msg, va); | |
150 | rc = MOUNT_EX_FAIL; | |
151 | break; | |
152 | } | |
153 | va_end(va); | |
154 | return rc; | |
155 | } | |
156 | ||
157 | static int mk_exit_code(struct libmnt_context *cxt, int rc) | |
158 | { | |
159 | int syserr; | |
160 | const char *tgt = mnt_context_get_target(cxt); | |
161 | ||
162 | if (mnt_context_helper_executed(cxt)) | |
163 | /* | |
164 | * /sbin/umount.<type> called, return status | |
165 | */ | |
166 | return mnt_context_get_helper_status(cxt); | |
167 | ||
168 | if (rc == 0 && mnt_context_get_status(cxt) == 1) | |
169 | /* | |
170 | * Libmount success && syscall success. | |
171 | */ | |
172 | return MOUNT_EX_SUCCESS; | |
173 | ||
174 | ||
175 | if (!mnt_context_syscall_called(cxt)) { | |
176 | /* | |
177 | * libmount errors (extra library checks) | |
178 | */ | |
179 | return handle_generic_errors(rc, _("%s: umount failed"), tgt); | |
180 | ||
181 | } else if (mnt_context_get_syscall_errno(cxt) == 0) { | |
182 | /* | |
183 | * umount(2) syscall success, but something else failed | |
184 | * (probably error in mtab processing). | |
185 | */ | |
186 | if (rc < 0) | |
187 | return handle_generic_errors(rc, | |
188 | _("%s: filesystem umounted, but mount(8) failed"), | |
189 | tgt); | |
190 | ||
191 | return MOUNT_EX_SOFTWARE; /* internal error */ | |
192 | ||
193 | } | |
194 | ||
195 | /* | |
196 | * umount(2) errors | |
197 | */ | |
198 | syserr = mnt_context_get_syscall_errno(cxt); | |
199 | ||
200 | switch(syserr) { | |
201 | case ENXIO: | |
202 | warnx(_("%s: invalid block device"), tgt); /* ??? */ | |
203 | break; | |
204 | case EINVAL: | |
205 | warnx(_("%s: not mounted"), tgt); | |
206 | break; | |
207 | case EIO: | |
208 | warnx(_("%s: can't write superblock"), tgt); | |
209 | break; | |
210 | case EBUSY: | |
211 | warnx(_("%s: target is busy.\n" | |
212 | " (In some cases useful info about processes that use\n" | |
213 | " the device is found by lsof(8) or fuser(1))"), | |
214 | tgt); | |
7e1b1446 | 215 | break; |
73f9a114 KZ |
216 | case ENOENT: |
217 | warnx(_("%s: not found"), tgt); | |
218 | break; | |
219 | case EPERM: | |
220 | warnx(_("%s: must be superuser to umount"), tgt); | |
221 | break; | |
222 | case EACCES: | |
223 | warnx(_("%s: block devices not permitted on fs"), tgt); | |
224 | break; | |
225 | default: | |
226 | errno = syserr; | |
227 | warn(_("%s"), tgt); | |
228 | break; | |
229 | } | |
230 | return MOUNT_EX_FAIL; | |
db216e68 KZ |
231 | } |
232 | ||
190c342a | 233 | static int umount_all(struct libmnt_context *cxt) |
db216e68 | 234 | { |
190c342a KZ |
235 | struct libmnt_iter *itr; |
236 | struct libmnt_fs *fs; | |
237 | int mntrc, ignored, rc = 0; | |
238 | ||
239 | itr = mnt_new_iter(MNT_ITER_BACKWARD); | |
240 | if (!itr) { | |
241 | warn(_("failed to initialize libmount iterator")); | |
242 | return -ENOMEM; | |
243 | } | |
244 | ||
245 | while (mnt_context_next_umount(cxt, itr, &fs, &mntrc, &ignored) == 0) { | |
246 | ||
247 | const char *tgt = mnt_fs_get_target(fs); | |
248 | ||
249 | if (ignored) { | |
250 | if (mnt_context_is_verbose(cxt)) | |
251 | printf(_("%-25s: ignored\n"), tgt); | |
190c342a | 252 | } else { |
73f9a114 KZ |
253 | rc |= mk_exit_code(cxt, mntrc); |
254 | ||
190c342a KZ |
255 | if (mnt_context_is_verbose(cxt)) |
256 | printf("%-25s: successfully umounted\n", tgt); | |
190c342a KZ |
257 | } |
258 | } | |
259 | ||
0f2d6476 | 260 | mnt_free_iter(itr); |
190c342a | 261 | return rc; |
db216e68 KZ |
262 | } |
263 | ||
264 | static int umount_one(struct libmnt_context *cxt, const char *spec) | |
265 | { | |
266 | int rc; | |
267 | ||
268 | if (!spec) | |
269 | return -EINVAL; | |
270 | ||
271 | if (mnt_context_set_target(cxt, spec)) | |
73f9a114 | 272 | err(MOUNT_EX_SYSERR, _("failed to set umount target")); |
db216e68 KZ |
273 | |
274 | rc = mnt_context_umount(cxt); | |
73f9a114 | 275 | rc = mk_exit_code(cxt, rc); |
db216e68 KZ |
276 | |
277 | mnt_reset_context(cxt); | |
278 | return rc; | |
279 | } | |
280 | ||
281 | int main(int argc, char **argv) | |
282 | { | |
190c342a | 283 | int c, rc = 0, all = 0; |
db216e68 KZ |
284 | struct libmnt_context *cxt; |
285 | char *types = NULL; | |
286 | ||
287 | enum { | |
288 | UMOUNT_OPT_FAKE = CHAR_MAX + 1, | |
289 | }; | |
290 | ||
291 | static const struct option longopts[] = { | |
292 | { "all", 0, 0, 'a' }, | |
293 | { "detach-loop", 0, 0, 'd' }, | |
294 | { "fake", 0, 0, UMOUNT_OPT_FAKE }, | |
295 | { "force", 0, 0, 'f' }, | |
296 | { "help", 0, 0, 'h' }, | |
297 | { "internal-only", 0, 0, 'i' }, | |
298 | { "lazy", 0, 0, 'l' }, | |
299 | { "no-canonicalize", 0, 0, 'c' }, | |
300 | { "no-mtab", 0, 0, 'n' }, | |
301 | { "read-only", 0, 0, 'r' }, | |
302 | { "test-opts", 1, 0, 'O' }, | |
303 | { "types", 1, 0, 't' }, | |
304 | { "verbose", 0, 0, 'v' }, | |
305 | { "version", 0, 0, 'V' }, | |
306 | { NULL, 0, 0, 0 } | |
307 | }; | |
308 | ||
309 | sanitize_env(); | |
310 | setlocale(LC_ALL, ""); | |
311 | bindtextdomain(PACKAGE, LOCALEDIR); | |
312 | textdomain(PACKAGE); | |
efb8854f | 313 | atexit(close_stdout); |
db216e68 KZ |
314 | |
315 | mnt_init_debug(0); | |
316 | cxt = mnt_new_context(); | |
317 | if (!cxt) | |
73f9a114 | 318 | err(MOUNT_EX_SYSERR, _("libmount context allocation failed")); |
db216e68 KZ |
319 | |
320 | mnt_context_set_tables_errcb(cxt, table_parser_errcb); | |
321 | ||
322 | while ((c = getopt_long(argc, argv, "acdfhilnrO:t:vV", | |
323 | longopts, NULL)) != -1) { | |
324 | ||
325 | ||
326 | /* only few options are allowed for non-root users */ | |
327 | if (mnt_context_is_restricted(cxt) && !strchr("hdilVv", c)) | |
328 | exit_non_root(option_to_longopt(c, longopts)); | |
329 | ||
330 | switch(c) { | |
331 | case 'a': | |
332 | all = 1; | |
333 | break; | |
334 | case 'c': | |
335 | mnt_context_disable_canonicalize(cxt, TRUE); | |
336 | break; | |
337 | case 'd': | |
338 | mnt_context_enable_loopdel(cxt, TRUE); | |
339 | break; | |
340 | case UMOUNT_OPT_FAKE: | |
341 | mnt_context_enable_fake(cxt, TRUE); | |
342 | break; | |
343 | case 'f': | |
344 | mnt_context_enable_force(cxt, TRUE); | |
345 | break; | |
346 | case 'h': | |
347 | usage(stdout); | |
348 | break; | |
349 | case 'i': | |
350 | mnt_context_disable_helpers(cxt, TRUE); | |
351 | break; | |
352 | case 'l': | |
353 | mnt_context_enable_lazy(cxt, TRUE); | |
354 | break; | |
355 | case 'n': | |
356 | mnt_context_disable_mtab(cxt, TRUE); | |
357 | break; | |
358 | case 'r': | |
359 | mnt_context_enable_rdonly_umount(cxt, TRUE); | |
360 | break; | |
361 | case 'O': | |
362 | if (mnt_context_set_options_pattern(cxt, optarg)) | |
73f9a114 | 363 | err(MOUNT_EX_SYSERR, _("failed to set options pattern")); |
db216e68 KZ |
364 | break; |
365 | case 't': | |
366 | types = optarg; | |
367 | break; | |
368 | case 'v': | |
369 | mnt_context_enable_verbose(cxt, TRUE); | |
370 | break; | |
371 | case 'V': | |
372 | print_version(); | |
373 | break; | |
374 | default: | |
375 | usage(stderr); | |
376 | break; | |
377 | } | |
378 | } | |
379 | ||
380 | argc -= optind; | |
381 | argv += optind; | |
382 | ||
383 | if (all) { | |
384 | if (!types) | |
385 | types = "noproc,nodevfs,nodevpts,nosysfs,norpc_pipefs,nonfsd"; | |
386 | ||
387 | mnt_context_set_fstype_pattern(cxt, types); | |
388 | rc = umount_all(cxt); | |
389 | ||
390 | } else if (argc < 1) { | |
391 | usage(stderr); | |
392 | ||
73f9a114 | 393 | } else while (argc--) |
db216e68 | 394 | rc += umount_one(cxt, *argv++); |
db216e68 KZ |
395 | |
396 | mnt_free_context(cxt); | |
73f9a114 | 397 | return rc; |
db216e68 KZ |
398 | } |
399 |