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