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