]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/mount/mount-tool.c
Merge pull request #9783 from poettering/get-user-creds-flags
[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 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_(udev_device_unrefp) struct udev_device *d = NULL;
925 _cleanup_(udev_unrefp) struct udev *udev = NULL;
926 _cleanup_strv_free_ char **list = NULL;
927 struct stat st;
928 const char *v;
929 char **l;
930 int r, r2 = 0;
931
932 assert(what);
933
934 if (stat(what, &st) < 0)
935 return log_error_errno(errno, "Can't stat %s: %m", what);
936
937 if (!S_ISBLK(st.st_mode)) {
938 log_error("Not a block device: %s", what);
939 return -ENOTBLK;
940 }
941
942 udev = udev_new();
943 if (!udev)
944 return log_oom();
945
946 d = udev_device_new_from_devnum(udev, 'b', st.st_rdev);
947 if (!d)
948 return log_oom();
949
950 v = udev_device_get_property_value(d, "ID_FS_USAGE");
951 if (!streq_ptr(v, "filesystem")) {
952 log_error("%s does not contain a known file system.", what);
953 return -EINVAL;
954 }
955
956 v = udev_device_get_property_value(d, "SYSTEMD_MOUNT_WHERE");
957 if (!isempty(v))
958 r2 = stop_mounts(bus, v);
959
960 r = find_mount_points(what, &list);
961 if (r < 0)
962 return r;
963
964 for (l = list; *l; l++) {
965 r = stop_mounts(bus, *l);
966 if (r < 0)
967 r2 = r;
968 }
969
970 return r2;
971 }
972
973 static int umount_loop(sd_bus *bus, const char *backing_file) {
974 _cleanup_free_ char *loop_dev = NULL;
975 int r;
976
977 assert(backing_file);
978
979 r = find_loop_device(backing_file, &loop_dev);
980 if (r < 0)
981 return log_error_errno(r, r == -ENXIO ? "File %s is not mounted." : "Can't get loop device for %s: %m", backing_file);
982
983 return umount_by_device(bus, loop_dev);
984 }
985
986 static int action_umount(
987 sd_bus *bus,
988 int argc,
989 char **argv) {
990
991 int i, r, r2 = 0;
992
993 if (arg_transport != BUS_TRANSPORT_LOCAL) {
994 for (i = optind; i < argc; i++) {
995 _cleanup_free_ char *p = NULL;
996
997 p = strdup(argv[i]);
998 if (!p)
999 return log_oom();
1000
1001 path_simplify(p, false);
1002
1003 r = stop_mounts(bus, p);
1004 if (r < 0)
1005 r2 = r;
1006 }
1007 return r2;
1008 }
1009
1010 for (i = optind; i < argc; i++) {
1011 _cleanup_free_ char *u = NULL, *p = NULL;
1012 struct stat st;
1013
1014 u = fstab_node_to_udev_node(argv[i]);
1015 if (!u)
1016 return log_oom();
1017
1018 r = chase_symlinks(u, NULL, 0, &p);
1019 if (r < 0) {
1020 r2 = log_error_errno(r, "Failed to make path %s absolute: %m", argv[i]);
1021 continue;
1022 }
1023
1024 if (stat(p, &st) < 0)
1025 return log_error_errno(errno, "Can't stat %s (from %s): %m", p, argv[i]);
1026
1027 if (S_ISBLK(st.st_mode))
1028 r = umount_by_device(bus, p);
1029 else if (S_ISREG(st.st_mode))
1030 r = umount_loop(bus, p);
1031 else if (S_ISDIR(st.st_mode))
1032 r = stop_mounts(bus, p);
1033 else {
1034 log_error("Invalid file type: %s (from %s)", p, argv[i]);
1035 r = -EINVAL;
1036 }
1037
1038 if (r < 0)
1039 r2 = r;
1040 }
1041
1042 return r2;
1043 }
1044
1045 static int acquire_mount_type(struct udev_device *d) {
1046 const char *v;
1047
1048 assert(d);
1049
1050 if (arg_mount_type)
1051 return 0;
1052
1053 v = udev_device_get_property_value(d, "ID_FS_TYPE");
1054 if (isempty(v))
1055 return 0;
1056
1057 arg_mount_type = strdup(v);
1058 if (!arg_mount_type)
1059 return log_oom();
1060
1061 log_debug("Discovered type=%s", arg_mount_type);
1062 return 1;
1063 }
1064
1065 static int acquire_mount_options(struct udev_device *d) {
1066 const char *v;
1067
1068 if (arg_mount_options)
1069 return 0;
1070
1071 v = udev_device_get_property_value(d, "SYSTEMD_MOUNT_OPTIONS");
1072 if (isempty(v))
1073 return 0;
1074
1075 arg_mount_options = strdup(v);
1076 if (!arg_mount_options)
1077 return log_oom();
1078
1079 log_debug("Discovered options=%s", arg_mount_options);
1080 return 1;
1081 }
1082
1083 static const char *get_model(struct udev_device *d) {
1084 const char *model;
1085
1086 assert(d);
1087
1088 model = udev_device_get_property_value(d, "ID_MODEL_FROM_DATABASE");
1089 if (model)
1090 return model;
1091
1092 return udev_device_get_property_value(d, "ID_MODEL");
1093 }
1094
1095 static const char* get_label(struct udev_device *d) {
1096 const char *label;
1097
1098 assert(d);
1099
1100 label = udev_device_get_property_value(d, "ID_FS_LABEL");
1101 if (label)
1102 return label;
1103
1104 return udev_device_get_property_value(d, "ID_PART_ENTRY_NAME");
1105 }
1106
1107 static int acquire_mount_where(struct udev_device *d) {
1108 const char *v;
1109
1110 if (arg_mount_where)
1111 return 0;
1112
1113 v = udev_device_get_property_value(d, "SYSTEMD_MOUNT_WHERE");
1114 if (isempty(v)) {
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 dn = udev_device_get_devnode(d);
1125 if (!dn)
1126 return 0;
1127
1128 name = basename(dn);
1129 }
1130
1131 escaped = xescape(name, "\\");
1132 if (!escaped)
1133 return log_oom();
1134 if (!filename_is_valid(escaped))
1135 return 0;
1136
1137 arg_mount_where = strjoin("/run/media/system/", escaped);
1138 } else
1139 arg_mount_where = strdup(v);
1140
1141 if (!arg_mount_where)
1142 return log_oom();
1143
1144 log_debug("Discovered where=%s", arg_mount_where);
1145 return 1;
1146 }
1147
1148 static int acquire_mount_where_for_loop_dev(const char *loop_dev) {
1149 _cleanup_strv_free_ char **list = NULL;
1150 int r;
1151
1152 if (arg_mount_where)
1153 return 0;
1154
1155 r = find_mount_points(loop_dev, &list);
1156 if (r < 0)
1157 return r;
1158 else if (r == 0) {
1159 log_error("Can't find mount point of %s. It is expected that %s is already mounted on a place.", loop_dev, loop_dev);
1160 return -EINVAL;
1161 } else if (r >= 2) {
1162 log_error("%s is mounted on %d places. It is expected that %s is mounted on a place.", loop_dev, r, loop_dev);
1163 return -EINVAL;
1164 }
1165
1166 arg_mount_where = strdup(list[0]);
1167 if (!arg_mount_where)
1168 return log_oom();
1169
1170 log_debug("Discovered where=%s", arg_mount_where);
1171 return 1;
1172 }
1173
1174 static int acquire_description(struct udev_device *d) {
1175 const char *model, *label;
1176
1177 if (arg_description)
1178 return 0;
1179
1180 model = get_model(d);
1181
1182 label = get_label(d);
1183 if (!label)
1184 label = udev_device_get_property_value(d, "ID_PART_ENTRY_NUMBER");
1185
1186 if (model && label)
1187 arg_description = strjoin(model, " ", label);
1188 else if (label)
1189 arg_description = strdup(label);
1190 else if (model)
1191 arg_description = strdup(model);
1192 else
1193 return 0;
1194
1195 if (!arg_description)
1196 return log_oom();
1197
1198 log_debug("Discovered description=%s", arg_description);
1199 return 1;
1200 }
1201
1202 static int acquire_removable(struct udev_device *d) {
1203 const char *v;
1204
1205 /* Shortcut this if there's no reason to check it */
1206 if (arg_action != ACTION_DEFAULT && arg_timeout_idle_set && arg_bind_device >= 0)
1207 return 0;
1208
1209 for (;;) {
1210 v = udev_device_get_sysattr_value(d, "removable");
1211 if (v)
1212 break;
1213
1214 d = udev_device_get_parent(d);
1215 if (!d)
1216 return 0;
1217
1218 if (!streq_ptr(udev_device_get_subsystem(d), "block"))
1219 return 0;
1220 }
1221
1222 if (parse_boolean(v) <= 0)
1223 return 0;
1224
1225 log_debug("Discovered removable device.");
1226
1227 if (arg_action == ACTION_DEFAULT) {
1228 log_debug("Automatically turning on automount.");
1229 arg_action = ACTION_AUTOMOUNT;
1230 }
1231
1232 if (!arg_timeout_idle_set) {
1233 log_debug("Setting idle timeout to 1s.");
1234 arg_timeout_idle = USEC_PER_SEC;
1235 }
1236
1237 if (arg_bind_device < 0) {
1238 log_debug("Binding automount unit to device.");
1239 arg_bind_device = true;
1240 }
1241
1242 return 1;
1243 }
1244
1245 static int discover_loop_backing_file(void) {
1246 _cleanup_(udev_device_unrefp) struct udev_device *d = NULL;
1247 _cleanup_(udev_unrefp) struct udev *udev = NULL;
1248 _cleanup_free_ char *loop_dev = NULL;
1249 struct stat st;
1250 const char *v;
1251 int r;
1252
1253 r = find_loop_device(arg_mount_what, &loop_dev);
1254 if (r < 0 && r != -ENXIO)
1255 return log_error_errno(errno, "Can't get loop device for %s: %m", arg_mount_what);
1256
1257 if (r == -ENXIO) {
1258 _cleanup_free_ char *escaped = NULL;
1259
1260 if (arg_mount_where)
1261 return 0;
1262
1263 escaped = xescape(basename(arg_mount_what), "\\");
1264 if (!escaped)
1265 return log_oom();
1266 if (!filename_is_valid(escaped)) {
1267 log_error("Escaped name %s is not a valid filename.", escaped);
1268 return -EINVAL;
1269 }
1270
1271 arg_mount_where = strjoin("/run/media/system/", escaped);
1272 if (!arg_mount_where)
1273 return log_oom();
1274
1275 log_debug("Discovered where=%s", arg_mount_where);
1276 return 0;
1277 }
1278
1279 if (stat(loop_dev, &st) < 0)
1280 return log_error_errno(errno, "Can't stat %s: %m", loop_dev);
1281
1282 if (!S_ISBLK(st.st_mode)) {
1283 log_error("Invalid file type: %s", loop_dev);
1284 return -EINVAL;
1285 }
1286
1287 udev = udev_new();
1288 if (!udev)
1289 return log_oom();
1290
1291 d = udev_device_new_from_devnum(udev, 'b', st.st_rdev);
1292 if (!d)
1293 return log_oom();
1294
1295 v = udev_device_get_property_value(d, "ID_FS_USAGE");
1296 if (!streq_ptr(v, "filesystem")) {
1297 log_error("%s does not contain a known file system.", arg_mount_what);
1298 return -EINVAL;
1299 }
1300
1301 r = acquire_mount_type(d);
1302 if (r < 0)
1303 return r;
1304
1305 r = acquire_mount_options(d);
1306 if (r < 0)
1307 return r;
1308
1309 r = acquire_mount_where_for_loop_dev(loop_dev);
1310 if (r < 0)
1311 return r;
1312
1313 r = acquire_description(d);
1314 if (r < 0)
1315 return r;
1316
1317 return 0;
1318 }
1319
1320 static int discover_device(void) {
1321 _cleanup_(udev_device_unrefp) struct udev_device *d = NULL;
1322 _cleanup_(udev_unrefp) struct udev *udev = NULL;
1323 struct stat st;
1324 const char *v;
1325 int r;
1326
1327 if (stat(arg_mount_what, &st) < 0)
1328 return log_error_errno(errno, "Can't stat %s: %m", arg_mount_what);
1329
1330 if (S_ISREG(st.st_mode))
1331 return discover_loop_backing_file();
1332
1333 if (!S_ISBLK(st.st_mode)) {
1334 log_error("Invalid file type: %s", arg_mount_what);
1335 return -EINVAL;
1336 }
1337
1338 udev = udev_new();
1339 if (!udev)
1340 return log_oom();
1341
1342 d = udev_device_new_from_devnum(udev, 'b', st.st_rdev);
1343 if (!d)
1344 return log_oom();
1345
1346 v = udev_device_get_property_value(d, "ID_FS_USAGE");
1347 if (!streq_ptr(v, "filesystem")) {
1348 log_error("%s does not contain a known file system.", arg_mount_what);
1349 return -EINVAL;
1350 }
1351
1352 r = acquire_mount_type(d);
1353 if (r < 0)
1354 return r;
1355
1356 r = acquire_mount_options(d);
1357 if (r < 0)
1358 return r;
1359
1360 r = acquire_mount_where(d);
1361 if (r < 0)
1362 return r;
1363
1364 r = acquire_description(d);
1365 if (r < 0)
1366 return r;
1367
1368 r = acquire_removable(d);
1369 if (r < 0)
1370 return r;
1371
1372 return 0;
1373 }
1374
1375 enum {
1376 COLUMN_NODE,
1377 COLUMN_PATH,
1378 COLUMN_MODEL,
1379 COLUMN_WWN,
1380 COLUMN_FSTYPE,
1381 COLUMN_LABEL,
1382 COLUMN_UUID,
1383 _COLUMN_MAX,
1384 };
1385
1386 struct item {
1387 char* columns[_COLUMN_MAX];
1388 };
1389
1390 static int compare_item(const void *a, const void *b) {
1391 const struct item *x = a, *y = b;
1392
1393 if (x->columns[COLUMN_NODE] == y->columns[COLUMN_NODE])
1394 return 0;
1395 if (!x->columns[COLUMN_NODE])
1396 return 1;
1397 if (!y->columns[COLUMN_NODE])
1398 return -1;
1399
1400 return path_compare(x->columns[COLUMN_NODE], y->columns[COLUMN_NODE]);
1401 }
1402
1403 static int list_devices(void) {
1404
1405 static const char * const titles[_COLUMN_MAX] = {
1406 [COLUMN_NODE] = "NODE",
1407 [COLUMN_PATH] = "PATH",
1408 [COLUMN_MODEL] = "MODEL",
1409 [COLUMN_WWN] = "WWN",
1410 [COLUMN_FSTYPE] = "TYPE",
1411 [COLUMN_LABEL] = "LABEL",
1412 [COLUMN_UUID] = "UUID"
1413 };
1414
1415 _cleanup_(udev_enumerate_unrefp) struct udev_enumerate *e = NULL;
1416 _cleanup_(udev_unrefp) struct udev *udev = NULL;
1417 struct udev_list_entry *item = NULL, *first = NULL;
1418 size_t n_allocated = 0, n = 0, i;
1419 size_t column_width[_COLUMN_MAX];
1420 struct item *items = NULL;
1421 unsigned c;
1422 int r;
1423
1424 for (c = 0; c < _COLUMN_MAX; c++)
1425 column_width[c] = strlen(titles[c]);
1426
1427 udev = udev_new();
1428 if (!udev)
1429 return log_oom();
1430
1431 e = udev_enumerate_new(udev);
1432 if (!e)
1433 return log_oom();
1434
1435 r = udev_enumerate_add_match_subsystem(e, "block");
1436 if (r < 0)
1437 return log_error_errno(r, "Failed to add block match: %m");
1438
1439 r = udev_enumerate_add_match_property(e, "ID_FS_USAGE", "filesystem");
1440 if (r < 0)
1441 return log_error_errno(r, "Failed to add property match: %m");
1442
1443 r = udev_enumerate_scan_devices(e);
1444 if (r < 0)
1445 return log_error_errno(r, "Failed to scan devices: %m");
1446
1447 first = udev_enumerate_get_list_entry(e);
1448 udev_list_entry_foreach(item, first) {
1449 _cleanup_(udev_device_unrefp) struct udev_device *d;
1450 struct item *j;
1451
1452 d = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item));
1453 if (!d) {
1454 r = log_oom();
1455 goto finish;
1456 }
1457
1458 if (!GREEDY_REALLOC0(items, n_allocated, n+1)) {
1459 r = log_oom();
1460 goto finish;
1461 }
1462
1463 j = items + n++;
1464
1465 for (c = 0; c < _COLUMN_MAX; c++) {
1466 const char *x = NULL;
1467 size_t k;
1468
1469 switch (c) {
1470
1471 case COLUMN_NODE:
1472 x = udev_device_get_devnode(d);
1473 break;
1474
1475 case COLUMN_PATH:
1476 x = udev_device_get_property_value(d, "ID_PATH");
1477 break;
1478
1479 case COLUMN_MODEL:
1480 x = get_model(d);
1481 break;
1482
1483 case COLUMN_WWN:
1484 x = udev_device_get_property_value(d, "ID_WWN");
1485 break;
1486
1487 case COLUMN_FSTYPE:
1488 x = udev_device_get_property_value(d, "ID_FS_TYPE");
1489 break;
1490
1491 case COLUMN_LABEL:
1492 x = get_label(d);
1493 break;
1494
1495 case COLUMN_UUID:
1496 x = udev_device_get_property_value(d, "ID_FS_UUID");
1497 break;
1498 }
1499
1500 if (isempty(x))
1501 continue;
1502
1503 j->columns[c] = strdup(x);
1504 if (!j->columns[c]) {
1505 r = log_oom();
1506 goto finish;
1507 }
1508
1509 k = strlen(x);
1510 if (k > column_width[c])
1511 column_width[c] = k;
1512 }
1513 }
1514
1515 if (n == 0) {
1516 log_info("No devices found.");
1517 goto finish;
1518 }
1519
1520 qsort_safe(items, n, sizeof(struct item), compare_item);
1521
1522 (void) pager_open(arg_no_pager, false);
1523
1524 fputs(ansi_underline(), stdout);
1525 for (c = 0; c < _COLUMN_MAX; c++) {
1526 if (c > 0)
1527 fputc(' ', stdout);
1528
1529 printf("%-*s", (int) column_width[c], titles[c]);
1530 }
1531 fputs(ansi_normal(), stdout);
1532 fputc('\n', stdout);
1533
1534 for (i = 0; i < n; i++) {
1535 for (c = 0; c < _COLUMN_MAX; c++) {
1536 if (c > 0)
1537 fputc(' ', stdout);
1538
1539 printf("%-*s", (int) column_width[c], strna(items[i].columns[c]));
1540 }
1541 fputc('\n', stdout);
1542 }
1543
1544 r = 0;
1545
1546 finish:
1547 for (i = 0; i < n; i++)
1548 for (c = 0; c < _COLUMN_MAX; c++)
1549 free(items[i].columns[c]);
1550
1551 free(items);
1552 return r;
1553 }
1554
1555 int main(int argc, char* argv[]) {
1556 sd_bus *bus = NULL;
1557 int r;
1558
1559 log_parse_environment();
1560 log_open();
1561
1562 r = parse_argv(argc, argv);
1563 if (r <= 0)
1564 goto finish;
1565
1566 if (arg_action == ACTION_LIST) {
1567 r = list_devices();
1568 goto finish;
1569 }
1570
1571 r = bus_connect_transport_systemd(arg_transport, arg_host, arg_user, &bus);
1572 if (r < 0) {
1573 log_error_errno(r, "Failed to create bus connection: %m");
1574 goto finish;
1575 }
1576
1577 if (arg_action == ACTION_UMOUNT) {
1578 r = action_umount(bus, argc, argv);
1579 goto finish;
1580 }
1581
1582 if (!path_is_normalized(arg_mount_what)) {
1583 log_error("Path contains non-normalized components: %s", arg_mount_what);
1584 r = -EINVAL;
1585 goto finish;
1586 }
1587
1588 if (arg_discover) {
1589 r = discover_device();
1590 if (r < 0)
1591 goto finish;
1592 }
1593
1594 if (!arg_mount_where) {
1595 log_error("Can't figure out where to mount %s.", arg_mount_what);
1596 r = -EINVAL;
1597 goto finish;
1598 }
1599
1600 if (path_equal(arg_mount_where, "/")) {
1601 log_error("Refusing to operate on root directory.");
1602 r = -EINVAL;
1603 goto finish;
1604 }
1605
1606 if (!path_is_normalized(arg_mount_where)) {
1607 log_error("Path contains non-normalized components: %s", arg_mount_where);
1608 r = -EINVAL;
1609 goto finish;
1610 }
1611
1612 if (streq_ptr(arg_mount_type, "auto"))
1613 arg_mount_type = mfree(arg_mount_type);
1614 if (streq_ptr(arg_mount_options, "defaults"))
1615 arg_mount_options = mfree(arg_mount_options);
1616
1617 if (!is_device_path(arg_mount_what))
1618 arg_fsck = false;
1619
1620 if (arg_fsck && arg_mount_type && arg_transport == BUS_TRANSPORT_LOCAL) {
1621 r = fsck_exists(arg_mount_type);
1622 if (r < 0)
1623 log_warning_errno(r, "Couldn't determine whether fsck for %s exists, proceeding anyway.", arg_mount_type);
1624 else if (r == 0) {
1625 log_debug("Disabling file system check as fsck for %s doesn't exist.", arg_mount_type);
1626 arg_fsck = false; /* fsck doesn't exist, let's not attempt it */
1627 }
1628 }
1629
1630 /* The kernel (properly) refuses mounting file systems with unknown uid=,gid= options,
1631 * but not for all filesystem types. Let's try to catch the cases where the option
1632 * would be used if the file system does not support it. It is also possible to
1633 * autodetect the file system, but that's only possible with disk-based file systems
1634 * which incidentally seem to be implemented more carefully and reject unknown options,
1635 * so it's probably OK that we do the check only when the type is specified.
1636 */
1637 if (arg_mount_type &&
1638 !streq(arg_mount_type, "auto") &&
1639 arg_uid != UID_INVALID &&
1640 !fstype_can_uid_gid(arg_mount_type)) {
1641 log_error("File system type %s is not known to support uid=/gid=, refusing.",
1642 arg_mount_type);
1643 r = -EOPNOTSUPP;
1644 goto finish;
1645 }
1646
1647 switch (arg_action) {
1648
1649 case ACTION_MOUNT:
1650 case ACTION_DEFAULT:
1651 r = start_transient_mount(bus, argv + optind);
1652 break;
1653
1654 case ACTION_AUTOMOUNT:
1655 r = start_transient_automount(bus, argv + optind);
1656 break;
1657
1658 default:
1659 assert_not_reached("Unexpected action.");
1660 }
1661
1662 finish:
1663 /* make sure we terminate the bus connection first, and then close the
1664 * pager, see issue #3543 for the details. */
1665 bus = sd_bus_flush_close_unref(bus);
1666 pager_close();
1667
1668 free(arg_mount_what);
1669 free(arg_mount_where);
1670 free(arg_mount_type);
1671 free(arg_mount_options);
1672 free(arg_description);
1673 strv_free(arg_property);
1674 strv_free(arg_automount_property);
1675
1676 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
1677 }