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