]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/mount/mount-tool.c
Merge pull request #9928 from yuwata/libudev-cleanups
[thirdparty/systemd.git] / src / mount / mount-tool.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <getopt.h>
4
5 #include "sd-bus.h"
6 #include "sd-device.h"
7
8 #include "bus-error.h"
9 #include "bus-unit-util.h"
10 #include "bus-util.h"
11 #include "device-util.h"
12 #include "dirent-util.h"
13 #include "escape.h"
14 #include "fd-util.h"
15 #include "fileio.h"
16 #include "fs-util.h"
17 #include "fstab-util.h"
18 #include "mount-util.h"
19 #include "pager.h"
20 #include "parse-util.h"
21 #include "path-util.h"
22 #include "spawn-polkit-agent.h"
23 #include "stat-util.h"
24 #include "strv.h"
25 #include "unit-def.h"
26 #include "unit-name.h"
27 #include "user-util.h"
28 #include "terminal-util.h"
29
30 enum {
31 ACTION_DEFAULT,
32 ACTION_MOUNT,
33 ACTION_AUTOMOUNT,
34 ACTION_UMOUNT,
35 ACTION_LIST,
36 } arg_action = ACTION_DEFAULT;
37
38 static bool arg_no_block = false;
39 static bool arg_no_pager = false;
40 static bool arg_ask_password = true;
41 static bool arg_quiet = false;
42 static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
43 static bool arg_user = false;
44 static const char *arg_host = NULL;
45 static bool arg_discover = false;
46 static char *arg_mount_what = NULL;
47 static char *arg_mount_where = NULL;
48 static char *arg_mount_type = NULL;
49 static char *arg_mount_options = NULL;
50 static char *arg_description = NULL;
51 static char **arg_property = NULL;
52 static usec_t arg_timeout_idle = USEC_INFINITY;
53 static bool arg_timeout_idle_set = false;
54 static char **arg_automount_property = NULL;
55 static int arg_bind_device = -1;
56 static uid_t arg_uid = UID_INVALID;
57 static gid_t arg_gid = GID_INVALID;
58 static bool arg_fsck = true;
59 static bool arg_aggressive_gc = false;
60
61 static int help(void) {
62 _cleanup_free_ char *link = NULL;
63 int r;
64
65 r = terminal_urlify_man("systemd-mount", "1", &link);
66 if (r < 0)
67 return log_oom();
68
69 printf("systemd-mount [OPTIONS...] WHAT [WHERE]\n"
70 "systemd-mount [OPTIONS...] --list\n"
71 "%s [OPTIONS...] %sWHAT|WHERE...\n\n"
72 "Establish a mount or auto-mount point transiently.\n\n"
73 " -h --help Show this help\n"
74 " --version Show package version\n"
75 " --no-block Do not wait until operation finished\n"
76 " --no-pager Do not pipe output into a pager\n"
77 " --no-ask-password Do not prompt for password\n"
78 " -q --quiet Suppress information messages during runtime\n"
79 " --user Run as user unit\n"
80 " -H --host=[USER@]HOST Operate on remote host\n"
81 " -M --machine=CONTAINER Operate on local container\n"
82 " --discover Discover mount device metadata\n"
83 " -t --type=TYPE File system type\n"
84 " -o --options=OPTIONS Mount options\n"
85 " --owner=USER Add uid= and gid= options for USER\n"
86 " --fsck=no Don't run file system check before mount\n"
87 " --description=TEXT Description for unit\n"
88 " -p --property=NAME=VALUE Set mount unit property\n"
89 " -A --automount=BOOL Create an auto-mount point\n"
90 " --timeout-idle-sec=SEC Specify automount idle timeout\n"
91 " --automount-property=NAME=VALUE\n"
92 " Set automount unit property\n"
93 " --bind-device Bind automount unit to device\n"
94 " --list List mountable block devices\n"
95 " -u --umount Unmount mount points\n"
96 " -G --collect Unload unit after it stopped, even when failed\n"
97 "\nSee the %s for details.\n"
98 , program_invocation_short_name
99 , streq(program_invocation_short_name, "systemd-umount") ? "" : "--umount "
100 , link
101 );
102
103 return 0;
104 }
105
106 static int parse_argv(int argc, char *argv[]) {
107
108 enum {
109 ARG_VERSION = 0x100,
110 ARG_NO_BLOCK,
111 ARG_NO_PAGER,
112 ARG_NO_ASK_PASSWORD,
113 ARG_USER,
114 ARG_SYSTEM,
115 ARG_DISCOVER,
116 ARG_MOUNT_TYPE,
117 ARG_MOUNT_OPTIONS,
118 ARG_OWNER,
119 ARG_FSCK,
120 ARG_DESCRIPTION,
121 ARG_TIMEOUT_IDLE,
122 ARG_AUTOMOUNT,
123 ARG_AUTOMOUNT_PROPERTY,
124 ARG_BIND_DEVICE,
125 ARG_LIST,
126 };
127
128 static const struct option options[] = {
129 { "help", no_argument, NULL, 'h' },
130 { "version", no_argument, NULL, ARG_VERSION },
131 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
132 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
133 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
134 { "quiet", no_argument, NULL, 'q' },
135 { "user", no_argument, NULL, ARG_USER },
136 { "system", no_argument, NULL, ARG_SYSTEM },
137 { "host", required_argument, NULL, 'H' },
138 { "machine", required_argument, NULL, 'M' },
139 { "discover", no_argument, NULL, ARG_DISCOVER },
140 { "type", required_argument, NULL, 't' },
141 { "options", required_argument, NULL, 'o' },
142 { "owner", required_argument, NULL, ARG_OWNER },
143 { "fsck", required_argument, NULL, ARG_FSCK },
144 { "description", required_argument, NULL, ARG_DESCRIPTION },
145 { "property", required_argument, NULL, 'p' },
146 { "automount", required_argument, NULL, ARG_AUTOMOUNT },
147 { "timeout-idle-sec", required_argument, NULL, ARG_TIMEOUT_IDLE },
148 { "automount-property", required_argument, NULL, ARG_AUTOMOUNT_PROPERTY },
149 { "bind-device", no_argument, NULL, ARG_BIND_DEVICE },
150 { "list", no_argument, NULL, ARG_LIST },
151 { "umount", no_argument, NULL, 'u' },
152 { "unmount", no_argument, NULL, 'u' },
153 { "collect", no_argument, NULL, 'G' },
154 {},
155 };
156
157 int r, c;
158
159 assert(argc >= 0);
160 assert(argv);
161
162 if (strstr(program_invocation_short_name, "systemd-umount"))
163 arg_action = ACTION_UMOUNT;
164
165 while ((c = getopt_long(argc, argv, "hqH:M:t:o:p:AuG", options, NULL)) >= 0)
166
167 switch (c) {
168
169 case 'h':
170 return help();
171
172 case ARG_VERSION:
173 return version();
174
175 case ARG_NO_BLOCK:
176 arg_no_block = true;
177 break;
178
179 case ARG_NO_PAGER:
180 arg_no_pager = true;
181 break;
182
183 case ARG_NO_ASK_PASSWORD:
184 arg_ask_password = false;
185 break;
186
187 case 'q':
188 arg_quiet = true;
189 break;
190
191 case ARG_USER:
192 arg_user = true;
193 break;
194
195 case ARG_SYSTEM:
196 arg_user = false;
197 break;
198
199 case 'H':
200 arg_transport = BUS_TRANSPORT_REMOTE;
201 arg_host = optarg;
202 break;
203
204 case 'M':
205 arg_transport = BUS_TRANSPORT_MACHINE;
206 arg_host = optarg;
207 break;
208
209 case ARG_DISCOVER:
210 arg_discover = true;
211 break;
212
213 case 't':
214 if (free_and_strdup(&arg_mount_type, optarg) < 0)
215 return log_oom();
216 break;
217
218 case 'o':
219 if (free_and_strdup(&arg_mount_options, optarg) < 0)
220 return log_oom();
221 break;
222
223 case ARG_OWNER: {
224 const char *user = optarg;
225
226 r = get_user_creds(&user, &arg_uid, &arg_gid, NULL, NULL, 0);
227 if (r < 0)
228 return log_error_errno(r,
229 r == -EBADMSG ? "UID or GID of user %s are invalid."
230 : "Cannot use \"%s\" as owner: %m",
231 optarg);
232 break;
233 }
234
235 case ARG_FSCK:
236 r = parse_boolean(optarg);
237 if (r < 0)
238 return log_error_errno(r, "Failed to parse --fsck= argument: %s", optarg);
239
240 arg_fsck = r;
241 break;
242
243 case ARG_DESCRIPTION:
244 if (free_and_strdup(&arg_description, optarg) < 0)
245 return log_oom();
246 break;
247
248 case 'p':
249 if (strv_extend(&arg_property, optarg) < 0)
250 return log_oom();
251
252 break;
253
254 case 'A':
255 arg_action = ACTION_AUTOMOUNT;
256 break;
257
258 case ARG_AUTOMOUNT:
259 r = parse_boolean(optarg);
260 if (r < 0)
261 return log_error_errno(r, "--automount= expects a valid boolean parameter: %s", optarg);
262
263 arg_action = r ? ACTION_AUTOMOUNT : ACTION_MOUNT;
264 break;
265
266 case ARG_TIMEOUT_IDLE:
267 r = parse_sec(optarg, &arg_timeout_idle);
268 if (r < 0)
269 return log_error_errno(r, "Failed to parse timeout: %s", optarg);
270
271 break;
272
273 case ARG_AUTOMOUNT_PROPERTY:
274 if (strv_extend(&arg_automount_property, optarg) < 0)
275 return log_oom();
276
277 break;
278
279 case ARG_BIND_DEVICE:
280 arg_bind_device = true;
281 break;
282
283 case ARG_LIST:
284 arg_action = ACTION_LIST;
285 break;
286
287 case 'u':
288 arg_action = ACTION_UMOUNT;
289 break;
290
291 case 'G':
292 arg_aggressive_gc = true;
293 break;
294
295 case '?':
296 return -EINVAL;
297
298 default:
299 assert_not_reached("Unhandled option");
300 }
301
302 if (arg_user && arg_transport != BUS_TRANSPORT_LOCAL) {
303 log_error("Execution in user context is not supported on non-local systems.");
304 return -EINVAL;
305 }
306
307 if (arg_action == ACTION_LIST) {
308 if (optind < argc) {
309 log_error("Too many arguments.");
310 return -EINVAL;
311 }
312
313 if (arg_transport != BUS_TRANSPORT_LOCAL) {
314 log_error("Listing devices only supported locally.");
315 return -EOPNOTSUPP;
316 }
317 } else if (arg_action == ACTION_UMOUNT) {
318 if (optind >= argc) {
319 log_error("At least one argument required.");
320 return -EINVAL;
321 }
322
323 if (arg_transport != BUS_TRANSPORT_LOCAL) {
324 int i;
325
326 for (i = optind; i < argc; i++)
327 if (!path_is_absolute(argv[i]) ) {
328 log_error("Only absolute path is supported: %s", argv[i]);
329 return -EINVAL;
330 }
331 }
332 } else {
333 if (optind >= argc) {
334 log_error("At least one argument required.");
335 return -EINVAL;
336 }
337
338 if (argc > optind+2) {
339 log_error("At most two arguments required.");
340 return -EINVAL;
341 }
342
343 if (arg_mount_type && (fstype_is_api_vfs(arg_mount_type) || fstype_is_network(arg_mount_type))) {
344 arg_mount_what = strdup(argv[optind]);
345 if (!arg_mount_what)
346 return log_oom();
347
348 } else if (arg_transport == BUS_TRANSPORT_LOCAL) {
349 _cleanup_free_ char *u = NULL;
350
351 u = fstab_node_to_udev_node(argv[optind]);
352 if (!u)
353 return log_oom();
354
355 r = chase_symlinks(u, NULL, 0, &arg_mount_what);
356 if (r < 0)
357 return log_error_errno(r, "Failed to make path %s absolute: %m", u);
358 } else {
359 arg_mount_what = strdup(argv[optind]);
360 if (!arg_mount_what)
361 return log_oom();
362
363 path_simplify(arg_mount_what, false);
364
365 if (!path_is_absolute(arg_mount_what)) {
366 log_error("Only absolute path is supported: %s", arg_mount_what);
367 return -EINVAL;
368 }
369 }
370
371 if (argc > optind+1) {
372 if (arg_transport == BUS_TRANSPORT_LOCAL) {
373 r = chase_symlinks(argv[optind+1], NULL, CHASE_NONEXISTENT, &arg_mount_where);
374 if (r < 0)
375 return log_error_errno(r, "Failed to make path %s absolute: %m", argv[optind+1]);
376 } else {
377 arg_mount_where = strdup(argv[optind+1]);
378 if (!arg_mount_where)
379 return log_oom();
380
381 path_simplify(arg_mount_where, false);
382
383 if (!path_is_absolute(arg_mount_where)) {
384 log_error("Only absolute path is supported: %s", arg_mount_where);
385 return -EINVAL;
386 }
387 }
388 } else
389 arg_discover = true;
390
391 if (arg_discover && arg_transport != BUS_TRANSPORT_LOCAL) {
392 log_error("Automatic mount location discovery is only supported locally.");
393 return -EOPNOTSUPP;
394 }
395 }
396
397 return 1;
398 }
399
400 static int transient_unit_set_properties(sd_bus_message *m, UnitType t, char **properties) {
401 int r;
402
403 if (!isempty(arg_description)) {
404 r = sd_bus_message_append(m, "(sv)", "Description", "s", arg_description);
405 if (r < 0)
406 return r;
407 }
408
409 if (arg_bind_device && is_device_path(arg_mount_what)) {
410 _cleanup_free_ char *device_unit = NULL;
411
412 r = unit_name_from_path(arg_mount_what, ".device", &device_unit);
413 if (r < 0)
414 return r;
415
416 r = sd_bus_message_append(m, "(sv)(sv)",
417 "After", "as", 1, device_unit,
418 "BindsTo", "as", 1, device_unit);
419 if (r < 0)
420 return r;
421 }
422
423 if (arg_aggressive_gc) {
424 r = sd_bus_message_append(m, "(sv)", "CollectMode", "s", "inactive-or-failed");
425 if (r < 0)
426 return r;
427 }
428
429 r = bus_append_unit_property_assignment_many(m, t, properties);
430 if (r < 0)
431 return r;
432
433 return 0;
434 }
435
436 static int transient_mount_set_properties(sd_bus_message *m) {
437 _cleanup_free_ char *options = NULL;
438 int r;
439
440 assert(m);
441
442 r = transient_unit_set_properties(m, UNIT_MOUNT, arg_property);
443 if (r < 0)
444 return r;
445
446 if (arg_mount_what) {
447 r = sd_bus_message_append(m, "(sv)", "What", "s", arg_mount_what);
448 if (r < 0)
449 return r;
450 }
451
452 if (arg_mount_type) {
453 r = sd_bus_message_append(m, "(sv)", "Type", "s", arg_mount_type);
454 if (r < 0)
455 return r;
456 }
457
458 /* Prepend uid=…,gid=… if arg_uid is set */
459 if (arg_uid != UID_INVALID) {
460 r = asprintf(&options,
461 "uid=" UID_FMT ",gid=" GID_FMT "%s%s",
462 arg_uid, arg_gid,
463 arg_mount_options ? "," : "", strempty(arg_mount_options));
464 if (r < 0)
465 return -ENOMEM;
466 }
467
468 if (options || arg_mount_options) {
469 log_debug("Using mount options: %s", options ?: arg_mount_options);
470
471 r = sd_bus_message_append(m, "(sv)", "Options", "s", options ?: arg_mount_options);
472 if (r < 0)
473 return r;
474 } else
475 log_debug("Not using any mount options");
476
477 if (arg_fsck) {
478 _cleanup_free_ char *fsck = NULL;
479
480 r = unit_name_from_path_instance("systemd-fsck", arg_mount_what, ".service", &fsck);
481 if (r < 0)
482 return r;
483
484 r = sd_bus_message_append(m,
485 "(sv)(sv)",
486 "Requires", "as", 1, fsck,
487 "After", "as", 1, fsck);
488 if (r < 0)
489 return r;
490 }
491
492 return 0;
493 }
494
495 static int transient_automount_set_properties(sd_bus_message *m) {
496 int r;
497
498 assert(m);
499
500 r = transient_unit_set_properties(m, UNIT_AUTOMOUNT, arg_automount_property);
501 if (r < 0)
502 return r;
503
504 if (arg_timeout_idle != USEC_INFINITY) {
505 r = sd_bus_message_append(m, "(sv)", "TimeoutIdleUSec", "t", arg_timeout_idle);
506 if (r < 0)
507 return r;
508 }
509
510 return 0;
511 }
512
513 static int start_transient_mount(
514 sd_bus *bus,
515 char **argv) {
516
517 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
518 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
519 _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL;
520 _cleanup_free_ char *mount_unit = NULL;
521 int r;
522
523 if (!arg_no_block) {
524 r = bus_wait_for_jobs_new(bus, &w);
525 if (r < 0)
526 return log_error_errno(r, "Could not watch jobs: %m");
527 }
528
529 r = unit_name_from_path(arg_mount_where, ".mount", &mount_unit);
530 if (r < 0)
531 return log_error_errno(r, "Failed to make mount unit name: %m");
532
533 r = sd_bus_message_new_method_call(
534 bus,
535 &m,
536 "org.freedesktop.systemd1",
537 "/org/freedesktop/systemd1",
538 "org.freedesktop.systemd1.Manager",
539 "StartTransientUnit");
540 if (r < 0)
541 return bus_log_create_error(r);
542
543 r = sd_bus_message_set_allow_interactive_authorization(m, arg_ask_password);
544 if (r < 0)
545 return bus_log_create_error(r);
546
547 /* Name and mode */
548 r = sd_bus_message_append(m, "ss", mount_unit, "fail");
549 if (r < 0)
550 return bus_log_create_error(r);
551
552 /* Properties */
553 r = sd_bus_message_open_container(m, 'a', "(sv)");
554 if (r < 0)
555 return bus_log_create_error(r);
556
557 r = transient_mount_set_properties(m);
558 if (r < 0)
559 return bus_log_create_error(r);
560
561 r = sd_bus_message_close_container(m);
562 if (r < 0)
563 return bus_log_create_error(r);
564
565 /* Auxiliary units */
566 r = sd_bus_message_append(m, "a(sa(sv))", 0);
567 if (r < 0)
568 return bus_log_create_error(r);
569
570 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
571
572 r = sd_bus_call(bus, m, 0, &error, &reply);
573 if (r < 0)
574 return log_error_errno(r, "Failed to start transient mount unit: %s", bus_error_message(&error, r));
575
576 if (w) {
577 const char *object;
578
579 r = sd_bus_message_read(reply, "o", &object);
580 if (r < 0)
581 return bus_log_parse_error(r);
582
583 r = bus_wait_for_jobs_one(w, object, arg_quiet);
584 if (r < 0)
585 return r;
586 }
587
588 if (!arg_quiet)
589 log_info("Started unit %s%s%s for mount point: %s%s%s",
590 ansi_highlight(), mount_unit, ansi_normal(),
591 ansi_highlight(), arg_mount_where, ansi_normal());
592
593 return 0;
594 }
595
596 static int start_transient_automount(
597 sd_bus *bus,
598 char **argv) {
599
600 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
601 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
602 _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL;
603 _cleanup_free_ char *automount_unit = NULL, *mount_unit = NULL;
604 int r;
605
606 if (!arg_no_block) {
607 r = bus_wait_for_jobs_new(bus, &w);
608 if (r < 0)
609 return log_error_errno(r, "Could not watch jobs: %m");
610 }
611
612 r = unit_name_from_path(arg_mount_where, ".automount", &automount_unit);
613 if (r < 0)
614 return log_error_errno(r, "Failed to make automount unit name: %m");
615
616 r = unit_name_from_path(arg_mount_where, ".mount", &mount_unit);
617 if (r < 0)
618 return log_error_errno(r, "Failed to make mount unit name: %m");
619
620 r = sd_bus_message_new_method_call(
621 bus,
622 &m,
623 "org.freedesktop.systemd1",
624 "/org/freedesktop/systemd1",
625 "org.freedesktop.systemd1.Manager",
626 "StartTransientUnit");
627 if (r < 0)
628 return bus_log_create_error(r);
629
630 r = sd_bus_message_set_allow_interactive_authorization(m, arg_ask_password);
631 if (r < 0)
632 return bus_log_create_error(r);
633
634 /* Name and mode */
635 r = sd_bus_message_append(m, "ss", automount_unit, "fail");
636 if (r < 0)
637 return bus_log_create_error(r);
638
639 /* Properties */
640 r = sd_bus_message_open_container(m, 'a', "(sv)");
641 if (r < 0)
642 return bus_log_create_error(r);
643
644 r = transient_automount_set_properties(m);
645 if (r < 0)
646 return bus_log_create_error(r);
647
648 r = sd_bus_message_close_container(m);
649 if (r < 0)
650 return bus_log_create_error(r);
651
652 /* Auxiliary units */
653 r = sd_bus_message_open_container(m, 'a', "(sa(sv))");
654 if (r < 0)
655 return bus_log_create_error(r);
656
657 r = sd_bus_message_open_container(m, 'r', "sa(sv)");
658 if (r < 0)
659 return bus_log_create_error(r);
660
661 r = sd_bus_message_append(m, "s", mount_unit);
662 if (r < 0)
663 return bus_log_create_error(r);
664
665 r = sd_bus_message_open_container(m, 'a', "(sv)");
666 if (r < 0)
667 return bus_log_create_error(r);
668
669 r = transient_mount_set_properties(m);
670 if (r < 0)
671 return bus_log_create_error(r);
672
673 r = sd_bus_message_close_container(m);
674 if (r < 0)
675 return bus_log_create_error(r);
676
677 r = sd_bus_message_close_container(m);
678 if (r < 0)
679 return bus_log_create_error(r);
680
681 r = sd_bus_message_close_container(m);
682 if (r < 0)
683 return bus_log_create_error(r);
684
685 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
686
687 r = sd_bus_call(bus, m, 0, &error, &reply);
688 if (r < 0)
689 return log_error_errno(r, "Failed to start transient automount unit: %s", bus_error_message(&error, r));
690
691 if (w) {
692 const char *object;
693
694 r = sd_bus_message_read(reply, "o", &object);
695 if (r < 0)
696 return bus_log_parse_error(r);
697
698 r = bus_wait_for_jobs_one(w, object, arg_quiet);
699 if (r < 0)
700 return r;
701 }
702
703 if (!arg_quiet)
704 log_info("Started unit %s%s%s for mount point: %s%s%s",
705 ansi_highlight(), automount_unit, ansi_normal(),
706 ansi_highlight(), arg_mount_where, ansi_normal());
707
708 return 0;
709 }
710
711 static int find_mount_points(const char *what, char ***list) {
712 _cleanup_fclose_ FILE *proc_self_mountinfo = NULL;
713 _cleanup_strv_free_ char **l = NULL;
714 size_t bufsize = 0, n = 0;
715
716 assert(what);
717 assert(list);
718
719 /* Returns all mount points obtained from /proc/self/mountinfo in *list,
720 * and the number of mount points as return value. */
721
722 proc_self_mountinfo = fopen("/proc/self/mountinfo", "re");
723 if (!proc_self_mountinfo)
724 return log_error_errno(errno, "Can't open /proc/self/mountinfo: %m");
725
726 for (;;) {
727 _cleanup_free_ char *path = NULL, *where = NULL, *dev = NULL;
728 int r;
729
730 r = fscanf(proc_self_mountinfo,
731 "%*s " /* (1) mount id */
732 "%*s " /* (2) parent id */
733 "%*s " /* (3) major:minor */
734 "%*s " /* (4) root */
735 "%ms " /* (5) mount point */
736 "%*s" /* (6) mount options */
737 "%*[^-]" /* (7) optional fields */
738 "- " /* (8) separator */
739 "%*s " /* (9) file system type */
740 "%ms" /* (10) mount source */
741 "%*s" /* (11) mount options 2 */
742 "%*[^\n]", /* some rubbish at the end */
743 &path, &dev);
744 if (r != 2) {
745 if (r == EOF)
746 break;
747
748 continue;
749 }
750
751 if (!streq(what, dev))
752 continue;
753
754 r = cunescape(path, UNESCAPE_RELAX, &where);
755 if (r < 0)
756 continue;
757
758 /* one extra slot is needed for the terminating NULL */
759 if (!GREEDY_REALLOC(l, bufsize, n + 2))
760 return log_oom();
761
762 l[n++] = TAKE_PTR(where);
763 }
764
765 if (!GREEDY_REALLOC(l, bufsize, n + 1))
766 return log_oom();
767
768 l[n] = NULL;
769 *list = TAKE_PTR(l);
770
771 return n;
772 }
773
774 static int find_loop_device(const char *backing_file, char **loop_dev) {
775 _cleanup_closedir_ DIR *d = NULL;
776 struct dirent *de;
777 _cleanup_free_ char *l = NULL;
778
779 assert(backing_file);
780 assert(loop_dev);
781
782 d = opendir("/sys/devices/virtual/block");
783 if (!d)
784 return -errno;
785
786 FOREACH_DIRENT(de, d, return -errno) {
787 _cleanup_free_ char *sys = NULL, *fname = NULL;
788 int r;
789
790 dirent_ensure_type(d, de);
791
792 if (de->d_type != DT_DIR)
793 continue;
794
795 if (!startswith(de->d_name, "loop"))
796 continue;
797
798 sys = strjoin("/sys/devices/virtual/block/", de->d_name, "/loop/backing_file");
799 if (!sys)
800 return -ENOMEM;
801
802 r = read_one_line_file(sys, &fname);
803 if (r < 0) {
804 log_debug_errno(r, "Failed to read %s, ignoring: %m", sys);
805 continue;
806 }
807
808 if (files_same(fname, backing_file, 0) <= 0)
809 continue;
810
811 l = strjoin("/dev/", de->d_name);
812 if (!l)
813 return -ENOMEM;
814
815 break;
816 }
817
818 if (!l)
819 return -ENXIO;
820
821 *loop_dev = TAKE_PTR(l);
822
823 return 0;
824 }
825
826 static int stop_mount(
827 sd_bus *bus,
828 const char *where,
829 const char *suffix) {
830
831 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
832 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
833 _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL;
834 _cleanup_free_ char *mount_unit = NULL;
835 int r;
836
837 if (!arg_no_block) {
838 r = bus_wait_for_jobs_new(bus, &w);
839 if (r < 0)
840 return log_error_errno(r, "Could not watch jobs: %m");
841 }
842
843 r = unit_name_from_path(where, suffix, &mount_unit);
844 if (r < 0)
845 return log_error_errno(r, "Failed to make %s unit name from path %s: %m", suffix + 1, where);
846
847 r = sd_bus_message_new_method_call(
848 bus,
849 &m,
850 "org.freedesktop.systemd1",
851 "/org/freedesktop/systemd1",
852 "org.freedesktop.systemd1.Manager",
853 "StopUnit");
854 if (r < 0)
855 return bus_log_create_error(r);
856
857 r = sd_bus_message_set_allow_interactive_authorization(m, arg_ask_password);
858 if (r < 0)
859 return bus_log_create_error(r);
860
861 /* Name and mode */
862 r = sd_bus_message_append(m, "ss", mount_unit, "fail");
863 if (r < 0)
864 return bus_log_create_error(r);
865
866 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
867
868 r = sd_bus_call(bus, m, 0, &error, &reply);
869 if (r < 0) {
870 if (streq(suffix, ".automount") &&
871 sd_bus_error_has_name(&error, "org.freedesktop.systemd1.NoSuchUnit"))
872 return 0;
873 return log_error_errno(r, "Failed to stop %s unit: %s", suffix + 1, bus_error_message(&error, r));
874 }
875
876 if (w) {
877 const char *object;
878
879 r = sd_bus_message_read(reply, "o", &object);
880 if (r < 0)
881 return bus_log_parse_error(r);
882
883 r = bus_wait_for_jobs_one(w, object, arg_quiet);
884 if (r < 0)
885 return r;
886 }
887
888 if (!arg_quiet)
889 log_info("Stopped unit %s%s%s for mount point: %s%s%s",
890 ansi_highlight(), mount_unit, ansi_normal(),
891 ansi_highlight(), where, ansi_normal());
892
893 return 0;
894 }
895
896 static int stop_mounts(
897 sd_bus *bus,
898 const char *where) {
899
900 int r;
901
902 if (path_equal(where, "/")) {
903 log_error("Refusing to operate on root directory: %s", where);
904 return -EINVAL;
905 }
906
907 if (!path_is_normalized(where)) {
908 log_error("Path contains non-normalized components: %s", where);
909 return -EINVAL;
910 }
911
912 r = stop_mount(bus, where, ".mount");
913 if (r < 0)
914 return r;
915
916 r = stop_mount(bus, where, ".automount");
917 if (r < 0)
918 return r;
919
920 return 0;
921 }
922
923 static int umount_by_device(sd_bus *bus, const char *what) {
924 _cleanup_(sd_device_unrefp) sd_device *d = NULL;
925 _cleanup_strv_free_ char **list = NULL;
926 struct stat st;
927 const char *v;
928 char **l;
929 int r, r2 = 0;
930
931 assert(what);
932
933 if (stat(what, &st) < 0)
934 return log_error_errno(errno, "Can't stat %s: %m", what);
935
936 if (!S_ISBLK(st.st_mode)) {
937 log_error("Not a block device: %s", what);
938 return -ENOTBLK;
939 }
940
941 r = sd_device_new_from_devnum(&d, 'b', st.st_rdev);
942 if (r < 0)
943 return log_error_errno(r, "Failed to get device from device number: %m");
944
945 r = sd_device_get_property_value(d, "ID_FS_USAGE", &v);
946 if (r < 0)
947 return log_error_errno(r, "Failed to get device property: %m");
948
949 if (!streq(v, "filesystem")) {
950 log_error("%s does not contain a known file system.", what);
951 return -EINVAL;
952 }
953
954 if (sd_device_get_property_value(d, "SYSTEMD_MOUNT_WHERE", &v) >= 0)
955 r2 = stop_mounts(bus, v);
956
957 r = find_mount_points(what, &list);
958 if (r < 0)
959 return r;
960
961 for (l = list; *l; l++) {
962 r = stop_mounts(bus, *l);
963 if (r < 0)
964 r2 = r;
965 }
966
967 return r2;
968 }
969
970 static int umount_loop(sd_bus *bus, const char *backing_file) {
971 _cleanup_free_ char *loop_dev = NULL;
972 int r;
973
974 assert(backing_file);
975
976 r = find_loop_device(backing_file, &loop_dev);
977 if (r < 0)
978 return log_error_errno(r, r == -ENXIO ? "File %s is not mounted." : "Can't get loop device for %s: %m", backing_file);
979
980 return umount_by_device(bus, loop_dev);
981 }
982
983 static int action_umount(
984 sd_bus *bus,
985 int argc,
986 char **argv) {
987
988 int i, r, r2 = 0;
989
990 if (arg_transport != BUS_TRANSPORT_LOCAL) {
991 for (i = optind; i < argc; i++) {
992 _cleanup_free_ char *p = NULL;
993
994 p = strdup(argv[i]);
995 if (!p)
996 return log_oom();
997
998 path_simplify(p, false);
999
1000 r = stop_mounts(bus, p);
1001 if (r < 0)
1002 r2 = r;
1003 }
1004 return r2;
1005 }
1006
1007 for (i = optind; i < argc; i++) {
1008 _cleanup_free_ char *u = NULL, *p = NULL;
1009 struct stat st;
1010
1011 u = fstab_node_to_udev_node(argv[i]);
1012 if (!u)
1013 return log_oom();
1014
1015 r = chase_symlinks(u, NULL, 0, &p);
1016 if (r < 0) {
1017 r2 = log_error_errno(r, "Failed to make path %s absolute: %m", argv[i]);
1018 continue;
1019 }
1020
1021 if (stat(p, &st) < 0)
1022 return log_error_errno(errno, "Can't stat %s (from %s): %m", p, argv[i]);
1023
1024 if (S_ISBLK(st.st_mode))
1025 r = umount_by_device(bus, p);
1026 else if (S_ISREG(st.st_mode))
1027 r = umount_loop(bus, p);
1028 else if (S_ISDIR(st.st_mode))
1029 r = stop_mounts(bus, p);
1030 else {
1031 log_error("Invalid file type: %s (from %s)", p, argv[i]);
1032 r = -EINVAL;
1033 }
1034
1035 if (r < 0)
1036 r2 = r;
1037 }
1038
1039 return r2;
1040 }
1041
1042 static int acquire_mount_type(sd_device *d) {
1043 const char *v;
1044
1045 assert(d);
1046
1047 if (arg_mount_type)
1048 return 0;
1049
1050 if (sd_device_get_property_value(d, "ID_FS_TYPE", &v) < 0)
1051 return 0;
1052
1053 arg_mount_type = strdup(v);
1054 if (!arg_mount_type)
1055 return log_oom();
1056
1057 log_debug("Discovered type=%s", arg_mount_type);
1058 return 1;
1059 }
1060
1061 static int acquire_mount_options(sd_device *d) {
1062 const char *v;
1063
1064 assert(d);
1065
1066 if (arg_mount_options)
1067 return 0;
1068
1069 if (sd_device_get_property_value(d, "SYSTEMD_MOUNT_OPTIONS", &v) < 0)
1070 return 0;
1071
1072 arg_mount_options = strdup(v);
1073 if (!arg_mount_options)
1074 return log_oom();
1075
1076 log_debug("Discovered options=%s", arg_mount_options);
1077 return 1;
1078 }
1079
1080 static const char *get_model(sd_device *d) {
1081 const char *model;
1082
1083 assert(d);
1084
1085 if (sd_device_get_property_value(d, "ID_MODEL_FROM_DATABASE", &model) >= 0)
1086 return model;
1087
1088 if (sd_device_get_property_value(d, "ID_MODEL", &model) >= 0)
1089 return model;
1090
1091 return NULL;
1092 }
1093
1094 static const char* get_label(sd_device *d) {
1095 const char *label;
1096
1097 assert(d);
1098
1099 if (sd_device_get_property_value(d, "ID_FS_LABEL", &label) >= 0)
1100 return label;
1101
1102 if (sd_device_get_property_value(d, "ID_PART_ENTRY_NAME", &label) >= 0)
1103 return label;
1104
1105 return NULL;
1106 }
1107
1108 static int acquire_mount_where(sd_device *d) {
1109 const char *v;
1110
1111 if (arg_mount_where)
1112 return 0;
1113
1114 if (sd_device_get_property_value(d, "SYSTEMD_MOUNT_WHERE", &v) < 0) {
1115 _cleanup_free_ char *escaped = NULL;
1116 const char *name;
1117
1118 name = get_label(d);
1119 if (!name)
1120 name = get_model(d);
1121 if (!name) {
1122 const char *dn;
1123
1124 if (sd_device_get_devname(d, &dn) < 0)
1125 return 0;
1126
1127 name = basename(dn);
1128 }
1129
1130 escaped = xescape(name, "\\");
1131 if (!escaped)
1132 return log_oom();
1133 if (!filename_is_valid(escaped))
1134 return 0;
1135
1136 arg_mount_where = strjoin("/run/media/system/", escaped);
1137 } else
1138 arg_mount_where = strdup(v);
1139
1140 if (!arg_mount_where)
1141 return log_oom();
1142
1143 log_debug("Discovered where=%s", arg_mount_where);
1144 return 1;
1145 }
1146
1147 static int acquire_mount_where_for_loop_dev(const char *loop_dev) {
1148 _cleanup_strv_free_ char **list = NULL;
1149 int r;
1150
1151 if (arg_mount_where)
1152 return 0;
1153
1154 r = find_mount_points(loop_dev, &list);
1155 if (r < 0)
1156 return r;
1157 else if (r == 0) {
1158 log_error("Can't find mount point of %s. It is expected that %s is already mounted on a place.", loop_dev, loop_dev);
1159 return -EINVAL;
1160 } else if (r >= 2) {
1161 log_error("%s is mounted on %d places. It is expected that %s is mounted on a place.", loop_dev, r, loop_dev);
1162 return -EINVAL;
1163 }
1164
1165 arg_mount_where = strdup(list[0]);
1166 if (!arg_mount_where)
1167 return log_oom();
1168
1169 log_debug("Discovered where=%s", arg_mount_where);
1170 return 1;
1171 }
1172
1173 static int acquire_description(sd_device *d) {
1174 const char *model, *label;
1175
1176 if (arg_description)
1177 return 0;
1178
1179 model = get_model(d);
1180
1181 label = get_label(d);
1182 if (!label)
1183 (void) sd_device_get_property_value(d, "ID_PART_ENTRY_NUMBER", &label);
1184
1185 if (model && label)
1186 arg_description = strjoin(model, " ", label);
1187 else if (label)
1188 arg_description = strdup(label);
1189 else if (model)
1190 arg_description = strdup(model);
1191 else
1192 return 0;
1193
1194 if (!arg_description)
1195 return log_oom();
1196
1197 log_debug("Discovered description=%s", arg_description);
1198 return 1;
1199 }
1200
1201 static int acquire_removable(sd_device *d) {
1202 const char *v;
1203
1204 /* Shortcut this if there's no reason to check it */
1205 if (arg_action != ACTION_DEFAULT && arg_timeout_idle_set && arg_bind_device >= 0)
1206 return 0;
1207
1208 for (;;) {
1209 if (sd_device_get_sysattr_value(d, "removable", &v) > 0)
1210 break;
1211
1212 if (sd_device_get_parent(d, &d) < 0)
1213 return 0;
1214
1215 if (sd_device_get_subsystem(d, &v) < 0 || !streq(v, "block"))
1216 return 0;
1217 }
1218
1219 if (parse_boolean(v) <= 0)
1220 return 0;
1221
1222 log_debug("Discovered removable device.");
1223
1224 if (arg_action == ACTION_DEFAULT) {
1225 log_debug("Automatically turning on automount.");
1226 arg_action = ACTION_AUTOMOUNT;
1227 }
1228
1229 if (!arg_timeout_idle_set) {
1230 log_debug("Setting idle timeout to 1s.");
1231 arg_timeout_idle = USEC_PER_SEC;
1232 }
1233
1234 if (arg_bind_device < 0) {
1235 log_debug("Binding automount unit to device.");
1236 arg_bind_device = true;
1237 }
1238
1239 return 1;
1240 }
1241
1242 static int discover_loop_backing_file(void) {
1243 _cleanup_(sd_device_unrefp) sd_device *d = NULL;
1244 _cleanup_free_ char *loop_dev = NULL;
1245 struct stat st;
1246 const char *v;
1247 int r;
1248
1249 r = find_loop_device(arg_mount_what, &loop_dev);
1250 if (r < 0 && r != -ENXIO)
1251 return log_error_errno(errno, "Can't get loop device for %s: %m", arg_mount_what);
1252
1253 if (r == -ENXIO) {
1254 _cleanup_free_ char *escaped = NULL;
1255
1256 if (arg_mount_where)
1257 return 0;
1258
1259 escaped = xescape(basename(arg_mount_what), "\\");
1260 if (!escaped)
1261 return log_oom();
1262 if (!filename_is_valid(escaped)) {
1263 log_error("Escaped name %s is not a valid filename.", escaped);
1264 return -EINVAL;
1265 }
1266
1267 arg_mount_where = strjoin("/run/media/system/", escaped);
1268 if (!arg_mount_where)
1269 return log_oom();
1270
1271 log_debug("Discovered where=%s", arg_mount_where);
1272 return 0;
1273 }
1274
1275 if (stat(loop_dev, &st) < 0)
1276 return log_error_errno(errno, "Can't stat %s: %m", loop_dev);
1277
1278 if (!S_ISBLK(st.st_mode)) {
1279 log_error("Invalid file type: %s", loop_dev);
1280 return -EINVAL;
1281 }
1282
1283 r = sd_device_new_from_devnum(&d, 'b', st.st_rdev);
1284 if (r < 0)
1285 return log_error_errno(r, "Failed to get device from device number: %m");
1286
1287 if (sd_device_get_property_value(d, "ID_FS_USAGE", &v) < 0 || !streq(v, "filesystem")) {
1288 log_error("%s does not contain a known file system.", arg_mount_what);
1289 return -EINVAL;
1290 }
1291
1292 r = acquire_mount_type(d);
1293 if (r < 0)
1294 return r;
1295
1296 r = acquire_mount_options(d);
1297 if (r < 0)
1298 return r;
1299
1300 r = acquire_mount_where_for_loop_dev(loop_dev);
1301 if (r < 0)
1302 return r;
1303
1304 r = acquire_description(d);
1305 if (r < 0)
1306 return r;
1307
1308 return 0;
1309 }
1310
1311 static int discover_device(void) {
1312 _cleanup_(sd_device_unrefp) sd_device *d = NULL;
1313 struct stat st;
1314 const char *v;
1315 int r;
1316
1317 if (stat(arg_mount_what, &st) < 0)
1318 return log_error_errno(errno, "Can't stat %s: %m", arg_mount_what);
1319
1320 if (S_ISREG(st.st_mode))
1321 return discover_loop_backing_file();
1322
1323 if (!S_ISBLK(st.st_mode)) {
1324 log_error("Invalid file type: %s", arg_mount_what);
1325 return -EINVAL;
1326 }
1327
1328 r = sd_device_new_from_devnum(&d, 'b', st.st_rdev);
1329 if (r < 0)
1330 return log_error_errno(r, "Failed to get device from device number: %m");
1331
1332 if (sd_device_get_property_value(d, "ID_FS_USAGE", &v) < 0 || !streq(v, "filesystem")) {
1333 log_error("%s does not contain a known file system.", arg_mount_what);
1334 return -EINVAL;
1335 }
1336
1337 r = acquire_mount_type(d);
1338 if (r < 0)
1339 return r;
1340
1341 r = acquire_mount_options(d);
1342 if (r < 0)
1343 return r;
1344
1345 r = acquire_mount_where(d);
1346 if (r < 0)
1347 return r;
1348
1349 r = acquire_description(d);
1350 if (r < 0)
1351 return r;
1352
1353 r = acquire_removable(d);
1354 if (r < 0)
1355 return r;
1356
1357 return 0;
1358 }
1359
1360 enum {
1361 COLUMN_NODE,
1362 COLUMN_PATH,
1363 COLUMN_MODEL,
1364 COLUMN_WWN,
1365 COLUMN_FSTYPE,
1366 COLUMN_LABEL,
1367 COLUMN_UUID,
1368 _COLUMN_MAX,
1369 };
1370
1371 struct item {
1372 char* columns[_COLUMN_MAX];
1373 };
1374
1375 static int compare_item(const void *a, const void *b) {
1376 const struct item *x = a, *y = b;
1377
1378 if (x->columns[COLUMN_NODE] == y->columns[COLUMN_NODE])
1379 return 0;
1380 if (!x->columns[COLUMN_NODE])
1381 return 1;
1382 if (!y->columns[COLUMN_NODE])
1383 return -1;
1384
1385 return path_compare(x->columns[COLUMN_NODE], y->columns[COLUMN_NODE]);
1386 }
1387
1388 static int list_devices(void) {
1389
1390 static const char * const titles[_COLUMN_MAX] = {
1391 [COLUMN_NODE] = "NODE",
1392 [COLUMN_PATH] = "PATH",
1393 [COLUMN_MODEL] = "MODEL",
1394 [COLUMN_WWN] = "WWN",
1395 [COLUMN_FSTYPE] = "TYPE",
1396 [COLUMN_LABEL] = "LABEL",
1397 [COLUMN_UUID] = "UUID"
1398 };
1399
1400 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
1401 size_t n_allocated = 0, n = 0, i;
1402 size_t column_width[_COLUMN_MAX];
1403 struct item *items = NULL;
1404 sd_device *d;
1405 unsigned c;
1406 int r;
1407
1408 for (c = 0; c < _COLUMN_MAX; c++)
1409 column_width[c] = strlen(titles[c]);
1410
1411 r = sd_device_enumerator_new(&e);
1412 if (r < 0)
1413 return log_oom();
1414
1415 r = sd_device_enumerator_add_match_subsystem(e, "block", true);
1416 if (r < 0)
1417 return log_error_errno(r, "Failed to add block match: %m");
1418
1419 r = sd_device_enumerator_add_match_property(e, "ID_FS_USAGE", "filesystem");
1420 if (r < 0)
1421 return log_error_errno(r, "Failed to add property match: %m");
1422
1423 FOREACH_DEVICE(e, d) {
1424 struct item *j;
1425
1426 if (!GREEDY_REALLOC0(items, n_allocated, n+1)) {
1427 r = log_oom();
1428 goto finish;
1429 }
1430
1431 j = items + n++;
1432
1433 for (c = 0; c < _COLUMN_MAX; c++) {
1434 const char *x = NULL;
1435 size_t k;
1436
1437 switch (c) {
1438
1439 case COLUMN_NODE:
1440 (void) sd_device_get_devname(d, &x);
1441 break;
1442
1443 case COLUMN_PATH:
1444 (void) sd_device_get_property_value(d, "ID_PATH", &x);
1445 break;
1446
1447 case COLUMN_MODEL:
1448 x = get_model(d);
1449 break;
1450
1451 case COLUMN_WWN:
1452 (void) sd_device_get_property_value(d, "ID_WWN", &x);
1453 break;
1454
1455 case COLUMN_FSTYPE:
1456 (void) sd_device_get_property_value(d, "ID_FS_TYPE", &x);
1457 break;
1458
1459 case COLUMN_LABEL:
1460 x = get_label(d);
1461 break;
1462
1463 case COLUMN_UUID:
1464 (void) sd_device_get_property_value(d, "ID_FS_UUID", &x);
1465 break;
1466 }
1467
1468 if (isempty(x))
1469 continue;
1470
1471 j->columns[c] = strdup(x);
1472 if (!j->columns[c]) {
1473 r = log_oom();
1474 goto finish;
1475 }
1476
1477 k = strlen(x);
1478 if (k > column_width[c])
1479 column_width[c] = k;
1480 }
1481 }
1482
1483 if (n == 0) {
1484 log_info("No devices found.");
1485 goto finish;
1486 }
1487
1488 qsort_safe(items, n, sizeof(struct item), compare_item);
1489
1490 (void) pager_open(arg_no_pager, false);
1491
1492 fputs(ansi_underline(), stdout);
1493 for (c = 0; c < _COLUMN_MAX; c++) {
1494 if (c > 0)
1495 fputc(' ', stdout);
1496
1497 printf("%-*s", (int) column_width[c], titles[c]);
1498 }
1499 fputs(ansi_normal(), stdout);
1500 fputc('\n', stdout);
1501
1502 for (i = 0; i < n; i++) {
1503 for (c = 0; c < _COLUMN_MAX; c++) {
1504 if (c > 0)
1505 fputc(' ', stdout);
1506
1507 printf("%-*s", (int) column_width[c], strna(items[i].columns[c]));
1508 }
1509 fputc('\n', stdout);
1510 }
1511
1512 r = 0;
1513
1514 finish:
1515 for (i = 0; i < n; i++)
1516 for (c = 0; c < _COLUMN_MAX; c++)
1517 free(items[i].columns[c]);
1518
1519 free(items);
1520 return r;
1521 }
1522
1523 int main(int argc, char* argv[]) {
1524 sd_bus *bus = NULL;
1525 int r;
1526
1527 log_parse_environment();
1528 log_open();
1529
1530 r = parse_argv(argc, argv);
1531 if (r <= 0)
1532 goto finish;
1533
1534 if (arg_action == ACTION_LIST) {
1535 r = list_devices();
1536 goto finish;
1537 }
1538
1539 r = bus_connect_transport_systemd(arg_transport, arg_host, arg_user, &bus);
1540 if (r < 0) {
1541 log_error_errno(r, "Failed to create bus connection: %m");
1542 goto finish;
1543 }
1544
1545 if (arg_action == ACTION_UMOUNT) {
1546 r = action_umount(bus, argc, argv);
1547 goto finish;
1548 }
1549
1550 if (!path_is_normalized(arg_mount_what)) {
1551 log_error("Path contains non-normalized components: %s", arg_mount_what);
1552 r = -EINVAL;
1553 goto finish;
1554 }
1555
1556 if (arg_discover) {
1557 r = discover_device();
1558 if (r < 0)
1559 goto finish;
1560 }
1561
1562 if (!arg_mount_where) {
1563 log_error("Can't figure out where to mount %s.", arg_mount_what);
1564 r = -EINVAL;
1565 goto finish;
1566 }
1567
1568 if (path_equal(arg_mount_where, "/")) {
1569 log_error("Refusing to operate on root directory.");
1570 r = -EINVAL;
1571 goto finish;
1572 }
1573
1574 if (!path_is_normalized(arg_mount_where)) {
1575 log_error("Path contains non-normalized components: %s", arg_mount_where);
1576 r = -EINVAL;
1577 goto finish;
1578 }
1579
1580 if (streq_ptr(arg_mount_type, "auto"))
1581 arg_mount_type = mfree(arg_mount_type);
1582 if (streq_ptr(arg_mount_options, "defaults"))
1583 arg_mount_options = mfree(arg_mount_options);
1584
1585 if (!is_device_path(arg_mount_what))
1586 arg_fsck = false;
1587
1588 if (arg_fsck && arg_mount_type && arg_transport == BUS_TRANSPORT_LOCAL) {
1589 r = fsck_exists(arg_mount_type);
1590 if (r < 0)
1591 log_warning_errno(r, "Couldn't determine whether fsck for %s exists, proceeding anyway.", arg_mount_type);
1592 else if (r == 0) {
1593 log_debug("Disabling file system check as fsck for %s doesn't exist.", arg_mount_type);
1594 arg_fsck = false; /* fsck doesn't exist, let's not attempt it */
1595 }
1596 }
1597
1598 /* The kernel (properly) refuses mounting file systems with unknown uid=,gid= options,
1599 * but not for all filesystem types. Let's try to catch the cases where the option
1600 * would be used if the file system does not support it. It is also possible to
1601 * autodetect the file system, but that's only possible with disk-based file systems
1602 * which incidentally seem to be implemented more carefully and reject unknown options,
1603 * so it's probably OK that we do the check only when the type is specified.
1604 */
1605 if (arg_mount_type &&
1606 !streq(arg_mount_type, "auto") &&
1607 arg_uid != UID_INVALID &&
1608 !fstype_can_uid_gid(arg_mount_type)) {
1609 log_error("File system type %s is not known to support uid=/gid=, refusing.",
1610 arg_mount_type);
1611 r = -EOPNOTSUPP;
1612 goto finish;
1613 }
1614
1615 switch (arg_action) {
1616
1617 case ACTION_MOUNT:
1618 case ACTION_DEFAULT:
1619 r = start_transient_mount(bus, argv + optind);
1620 break;
1621
1622 case ACTION_AUTOMOUNT:
1623 r = start_transient_automount(bus, argv + optind);
1624 break;
1625
1626 default:
1627 assert_not_reached("Unexpected action.");
1628 }
1629
1630 finish:
1631 /* make sure we terminate the bus connection first, and then close the
1632 * pager, see issue #3543 for the details. */
1633 bus = sd_bus_flush_close_unref(bus);
1634 pager_close();
1635
1636 free(arg_mount_what);
1637 free(arg_mount_where);
1638 free(arg_mount_type);
1639 free(arg_mount_options);
1640 free(arg_description);
1641 strv_free(arg_property);
1642 strv_free(arg_automount_property);
1643
1644 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
1645 }