]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/mount/mount-tool.c
Merge pull request #10871 from keszybz/more-cleanup-2
[thirdparty/systemd.git] / src / mount / mount-tool.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <getopt.h>
4
5 #include "sd-bus.h"
6 #include "sd-device.h"
7
8 #include "bus-error.h"
9 #include "bus-unit-util.h"
10 #include "bus-util.h"
11 #include "device-util.h"
12 #include "dirent-util.h"
13 #include "escape.h"
14 #include "fd-util.h"
15 #include "fileio.h"
16 #include "fs-util.h"
17 #include "fstab-util.h"
18 #include "main-func.h"
19 #include "mount-util.h"
20 #include "pager.h"
21 #include "parse-util.h"
22 #include "path-util.h"
23 #include "pretty-print.h"
24 #include "spawn-polkit-agent.h"
25 #include "stat-util.h"
26 #include "strv.h"
27 #include "unit-def.h"
28 #include "unit-name.h"
29 #include "user-util.h"
30 #include "terminal-util.h"
31
32 enum {
33 ACTION_DEFAULT,
34 ACTION_MOUNT,
35 ACTION_AUTOMOUNT,
36 ACTION_UMOUNT,
37 ACTION_LIST,
38 } arg_action = ACTION_DEFAULT;
39
40 static bool arg_no_block = false;
41 static PagerFlags arg_pager_flags = 0;
42 static bool arg_ask_password = true;
43 static bool arg_quiet = false;
44 static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
45 static bool arg_user = false;
46 static const char *arg_host = NULL;
47 static bool arg_discover = false;
48 static char *arg_mount_what = NULL;
49 static char *arg_mount_where = NULL;
50 static char *arg_mount_type = NULL;
51 static char *arg_mount_options = NULL;
52 static char *arg_description = NULL;
53 static char **arg_property = NULL;
54 static usec_t arg_timeout_idle = USEC_INFINITY;
55 static bool arg_timeout_idle_set = false;
56 static char **arg_automount_property = NULL;
57 static int arg_bind_device = -1;
58 static uid_t arg_uid = UID_INVALID;
59 static gid_t arg_gid = GID_INVALID;
60 static bool arg_fsck = true;
61 static bool arg_aggressive_gc = false;
62
63 STATIC_DESTRUCTOR_REGISTER(arg_mount_what, freep);
64 STATIC_DESTRUCTOR_REGISTER(arg_mount_where, freep);
65 STATIC_DESTRUCTOR_REGISTER(arg_mount_type, freep);
66 STATIC_DESTRUCTOR_REGISTER(arg_mount_options, freep);
67 STATIC_DESTRUCTOR_REGISTER(arg_description, freep);
68 STATIC_DESTRUCTOR_REGISTER(arg_property, strv_freep);
69 STATIC_DESTRUCTOR_REGISTER(arg_automount_property, strv_freep);
70
71 static int help(void) {
72 _cleanup_free_ char *link = NULL;
73 int r;
74
75 r = terminal_urlify_man("systemd-mount", "1", &link);
76 if (r < 0)
77 return log_oom();
78
79 printf("systemd-mount [OPTIONS...] WHAT [WHERE]\n"
80 "systemd-mount [OPTIONS...] --list\n"
81 "%s [OPTIONS...] %sWHAT|WHERE...\n\n"
82 "Establish a mount or auto-mount point transiently.\n\n"
83 " -h --help Show this help\n"
84 " --version Show package version\n"
85 " --no-block Do not wait until operation finished\n"
86 " --no-pager Do not pipe output into a pager\n"
87 " --no-ask-password Do not prompt for password\n"
88 " -q --quiet Suppress information messages during runtime\n"
89 " --user Run as user unit\n"
90 " -H --host=[USER@]HOST Operate on remote host\n"
91 " -M --machine=CONTAINER Operate on local container\n"
92 " --discover Discover mount device metadata\n"
93 " -t --type=TYPE File system type\n"
94 " -o --options=OPTIONS Mount options\n"
95 " --owner=USER Add uid= and gid= options for USER\n"
96 " --fsck=no Don't run file system check before mount\n"
97 " --description=TEXT Description for unit\n"
98 " -p --property=NAME=VALUE Set mount unit property\n"
99 " -A --automount=BOOL Create an auto-mount point\n"
100 " --timeout-idle-sec=SEC Specify automount idle timeout\n"
101 " --automount-property=NAME=VALUE\n"
102 " Set automount unit property\n"
103 " --bind-device Bind automount unit to device\n"
104 " --list List mountable block devices\n"
105 " -u --umount Unmount mount points\n"
106 " -G --collect Unload unit after it stopped, even when failed\n"
107 "\nSee the %s for details.\n"
108 , program_invocation_short_name
109 , streq(program_invocation_short_name, "systemd-umount") ? "" : "--umount "
110 , link
111 );
112
113 return 0;
114 }
115
116 static int parse_argv(int argc, char *argv[]) {
117
118 enum {
119 ARG_VERSION = 0x100,
120 ARG_NO_BLOCK,
121 ARG_NO_PAGER,
122 ARG_NO_ASK_PASSWORD,
123 ARG_USER,
124 ARG_SYSTEM,
125 ARG_DISCOVER,
126 ARG_MOUNT_TYPE,
127 ARG_MOUNT_OPTIONS,
128 ARG_OWNER,
129 ARG_FSCK,
130 ARG_DESCRIPTION,
131 ARG_TIMEOUT_IDLE,
132 ARG_AUTOMOUNT,
133 ARG_AUTOMOUNT_PROPERTY,
134 ARG_BIND_DEVICE,
135 ARG_LIST,
136 };
137
138 static const struct option options[] = {
139 { "help", no_argument, NULL, 'h' },
140 { "version", no_argument, NULL, ARG_VERSION },
141 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
142 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
143 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
144 { "quiet", no_argument, NULL, 'q' },
145 { "user", no_argument, NULL, ARG_USER },
146 { "system", no_argument, NULL, ARG_SYSTEM },
147 { "host", required_argument, NULL, 'H' },
148 { "machine", required_argument, NULL, 'M' },
149 { "discover", no_argument, NULL, ARG_DISCOVER },
150 { "type", required_argument, NULL, 't' },
151 { "options", required_argument, NULL, 'o' },
152 { "owner", required_argument, NULL, ARG_OWNER },
153 { "fsck", required_argument, NULL, ARG_FSCK },
154 { "description", required_argument, NULL, ARG_DESCRIPTION },
155 { "property", required_argument, NULL, 'p' },
156 { "automount", required_argument, NULL, ARG_AUTOMOUNT },
157 { "timeout-idle-sec", required_argument, NULL, ARG_TIMEOUT_IDLE },
158 { "automount-property", required_argument, NULL, ARG_AUTOMOUNT_PROPERTY },
159 { "bind-device", no_argument, NULL, ARG_BIND_DEVICE },
160 { "list", no_argument, NULL, ARG_LIST },
161 { "umount", no_argument, NULL, 'u' },
162 { "unmount", no_argument, NULL, 'u' },
163 { "collect", no_argument, NULL, 'G' },
164 {},
165 };
166
167 int r, c;
168
169 assert(argc >= 0);
170 assert(argv);
171
172 if (strstr(program_invocation_short_name, "systemd-umount"))
173 arg_action = ACTION_UMOUNT;
174
175 while ((c = getopt_long(argc, argv, "hqH:M:t:o:p:AuG", options, NULL)) >= 0)
176
177 switch (c) {
178
179 case 'h':
180 return help();
181
182 case ARG_VERSION:
183 return version();
184
185 case ARG_NO_BLOCK:
186 arg_no_block = true;
187 break;
188
189 case ARG_NO_PAGER:
190 arg_pager_flags |= PAGER_DISABLE;
191 break;
192
193 case ARG_NO_ASK_PASSWORD:
194 arg_ask_password = false;
195 break;
196
197 case 'q':
198 arg_quiet = true;
199 break;
200
201 case ARG_USER:
202 arg_user = true;
203 break;
204
205 case ARG_SYSTEM:
206 arg_user = false;
207 break;
208
209 case 'H':
210 arg_transport = BUS_TRANSPORT_REMOTE;
211 arg_host = optarg;
212 break;
213
214 case 'M':
215 arg_transport = BUS_TRANSPORT_MACHINE;
216 arg_host = optarg;
217 break;
218
219 case ARG_DISCOVER:
220 arg_discover = true;
221 break;
222
223 case 't':
224 if (free_and_strdup(&arg_mount_type, optarg) < 0)
225 return log_oom();
226 break;
227
228 case 'o':
229 if (free_and_strdup(&arg_mount_options, optarg) < 0)
230 return log_oom();
231 break;
232
233 case ARG_OWNER: {
234 const char *user = optarg;
235
236 r = get_user_creds(&user, &arg_uid, &arg_gid, NULL, NULL, 0);
237 if (r < 0)
238 return log_error_errno(r,
239 r == -EBADMSG ? "UID or GID of user %s are invalid."
240 : "Cannot use \"%s\" as owner: %m",
241 optarg);
242 break;
243 }
244
245 case ARG_FSCK:
246 r = parse_boolean(optarg);
247 if (r < 0)
248 return log_error_errno(r, "Failed to parse --fsck= argument: %s", optarg);
249
250 arg_fsck = r;
251 break;
252
253 case ARG_DESCRIPTION:
254 if (free_and_strdup(&arg_description, optarg) < 0)
255 return log_oom();
256 break;
257
258 case 'p':
259 if (strv_extend(&arg_property, optarg) < 0)
260 return log_oom();
261
262 break;
263
264 case 'A':
265 arg_action = ACTION_AUTOMOUNT;
266 break;
267
268 case ARG_AUTOMOUNT:
269 r = parse_boolean(optarg);
270 if (r < 0)
271 return log_error_errno(r, "--automount= expects a valid boolean parameter: %s", optarg);
272
273 arg_action = r ? ACTION_AUTOMOUNT : ACTION_MOUNT;
274 break;
275
276 case ARG_TIMEOUT_IDLE:
277 r = parse_sec(optarg, &arg_timeout_idle);
278 if (r < 0)
279 return log_error_errno(r, "Failed to parse timeout: %s", optarg);
280
281 break;
282
283 case ARG_AUTOMOUNT_PROPERTY:
284 if (strv_extend(&arg_automount_property, optarg) < 0)
285 return log_oom();
286
287 break;
288
289 case ARG_BIND_DEVICE:
290 arg_bind_device = true;
291 break;
292
293 case ARG_LIST:
294 arg_action = ACTION_LIST;
295 break;
296
297 case 'u':
298 arg_action = ACTION_UMOUNT;
299 break;
300
301 case 'G':
302 arg_aggressive_gc = true;
303 break;
304
305 case '?':
306 return -EINVAL;
307
308 default:
309 assert_not_reached("Unhandled option");
310 }
311
312 if (arg_user && arg_transport != BUS_TRANSPORT_LOCAL)
313 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
314 "Execution in user context is not supported on non-local systems.");
315
316 if (arg_action == ACTION_LIST) {
317 if (optind < argc)
318 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
319 "Too many arguments.");
320
321 if (arg_transport != BUS_TRANSPORT_LOCAL)
322 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
323 "Listing devices only supported locally.");
324 } else if (arg_action == ACTION_UMOUNT) {
325 if (optind >= argc)
326 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
327 "At least one argument required.");
328
329 if (arg_transport != BUS_TRANSPORT_LOCAL) {
330 int i;
331
332 for (i = optind; i < argc; i++)
333 if (!path_is_absolute(argv[i]) )
334 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
335 "Only absolute path is supported: %s", argv[i]);
336 }
337 } else {
338 if (optind >= argc)
339 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
340 "At least one argument required.");
341
342 if (argc > optind+2)
343 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
344 "At most two arguments required.");
345
346 if (arg_mount_type && (fstype_is_api_vfs(arg_mount_type) || fstype_is_network(arg_mount_type))) {
347 arg_mount_what = strdup(argv[optind]);
348 if (!arg_mount_what)
349 return log_oom();
350
351 } else if (arg_transport == BUS_TRANSPORT_LOCAL) {
352 _cleanup_free_ char *u = NULL;
353
354 u = fstab_node_to_udev_node(argv[optind]);
355 if (!u)
356 return log_oom();
357
358 r = chase_symlinks(u, NULL, 0, &arg_mount_what);
359 if (r < 0)
360 return log_error_errno(r, "Failed to make path %s absolute: %m", u);
361 } else {
362 arg_mount_what = strdup(argv[optind]);
363 if (!arg_mount_what)
364 return log_oom();
365
366 path_simplify(arg_mount_what, false);
367
368 if (!path_is_absolute(arg_mount_what))
369 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
370 "Only absolute path is supported: %s", arg_mount_what);
371 }
372
373 if (argc > optind+1) {
374 if (arg_transport == BUS_TRANSPORT_LOCAL) {
375 r = chase_symlinks(argv[optind+1], NULL, CHASE_NONEXISTENT, &arg_mount_where);
376 if (r < 0)
377 return log_error_errno(r, "Failed to make path %s absolute: %m", argv[optind+1]);
378 } else {
379 arg_mount_where = strdup(argv[optind+1]);
380 if (!arg_mount_where)
381 return log_oom();
382
383 path_simplify(arg_mount_where, false);
384
385 if (!path_is_absolute(arg_mount_where))
386 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
387 "Only absolute path is supported: %s", arg_mount_where);
388 }
389 } else
390 arg_discover = true;
391
392 if (arg_discover && arg_transport != BUS_TRANSPORT_LOCAL)
393 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
394 "Automatic mount location discovery is only supported locally.");
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 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
904 "Refusing to operate on root directory: %s", where);
905
906 if (!path_is_normalized(where))
907 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
908 "Path contains non-normalized components: %s", where);
909
910 r = stop_mount(bus, where, ".mount");
911 if (r < 0)
912 return r;
913
914 r = stop_mount(bus, where, ".automount");
915 if (r < 0)
916 return r;
917
918 return 0;
919 }
920
921 static int umount_by_device(sd_bus *bus, const char *what) {
922 _cleanup_(sd_device_unrefp) sd_device *d = NULL;
923 _cleanup_strv_free_ char **list = NULL;
924 struct stat st;
925 const char *v;
926 char **l;
927 int r, r2 = 0;
928
929 assert(what);
930
931 if (stat(what, &st) < 0)
932 return log_error_errno(errno, "Can't stat %s: %m", what);
933
934 if (!S_ISBLK(st.st_mode)) {
935 log_error("Not a block device: %s", what);
936 return -ENOTBLK;
937 }
938
939 r = sd_device_new_from_devnum(&d, 'b', st.st_rdev);
940 if (r < 0)
941 return log_error_errno(r, "Failed to get device from device number: %m");
942
943 r = sd_device_get_property_value(d, "ID_FS_USAGE", &v);
944 if (r < 0)
945 return log_device_error_errno(d, r, "Failed to get device property: %m");
946
947 if (!streq(v, "filesystem")) {
948 log_device_error(d, "%s does not contain a known file system.", what);
949 return -EINVAL;
950 }
951
952 if (sd_device_get_property_value(d, "SYSTEMD_MOUNT_WHERE", &v) >= 0)
953 r2 = stop_mounts(bus, v);
954
955 r = find_mount_points(what, &list);
956 if (r < 0)
957 return r;
958
959 for (l = list; *l; l++) {
960 r = stop_mounts(bus, *l);
961 if (r < 0)
962 r2 = r;
963 }
964
965 return r2;
966 }
967
968 static int umount_loop(sd_bus *bus, const char *backing_file) {
969 _cleanup_free_ char *loop_dev = NULL;
970 int r;
971
972 assert(backing_file);
973
974 r = find_loop_device(backing_file, &loop_dev);
975 if (r < 0)
976 return log_error_errno(r, r == -ENXIO ? "File %s is not mounted." : "Can't get loop device for %s: %m", backing_file);
977
978 return umount_by_device(bus, loop_dev);
979 }
980
981 static int action_umount(
982 sd_bus *bus,
983 int argc,
984 char **argv) {
985
986 int i, r, r2 = 0;
987
988 if (arg_transport != BUS_TRANSPORT_LOCAL) {
989 for (i = optind; i < argc; i++) {
990 _cleanup_free_ char *p = NULL;
991
992 p = strdup(argv[i]);
993 if (!p)
994 return log_oom();
995
996 path_simplify(p, false);
997
998 r = stop_mounts(bus, p);
999 if (r < 0)
1000 r2 = r;
1001 }
1002 return r2;
1003 }
1004
1005 for (i = optind; i < argc; i++) {
1006 _cleanup_free_ char *u = NULL, *p = NULL;
1007 struct stat st;
1008
1009 u = fstab_node_to_udev_node(argv[i]);
1010 if (!u)
1011 return log_oom();
1012
1013 r = chase_symlinks(u, NULL, 0, &p);
1014 if (r < 0) {
1015 r2 = log_error_errno(r, "Failed to make path %s absolute: %m", argv[i]);
1016 continue;
1017 }
1018
1019 if (stat(p, &st) < 0)
1020 return log_error_errno(errno, "Can't stat %s (from %s): %m", p, argv[i]);
1021
1022 if (S_ISBLK(st.st_mode))
1023 r = umount_by_device(bus, p);
1024 else if (S_ISREG(st.st_mode))
1025 r = umount_loop(bus, p);
1026 else if (S_ISDIR(st.st_mode))
1027 r = stop_mounts(bus, p);
1028 else {
1029 log_error("Invalid file type: %s (from %s)", p, argv[i]);
1030 r = -EINVAL;
1031 }
1032
1033 if (r < 0)
1034 r2 = r;
1035 }
1036
1037 return r2;
1038 }
1039
1040 static int acquire_mount_type(sd_device *d) {
1041 const char *v;
1042
1043 assert(d);
1044
1045 if (arg_mount_type)
1046 return 0;
1047
1048 if (sd_device_get_property_value(d, "ID_FS_TYPE", &v) < 0)
1049 return 0;
1050
1051 arg_mount_type = strdup(v);
1052 if (!arg_mount_type)
1053 return log_oom();
1054
1055 log_debug("Discovered type=%s", arg_mount_type);
1056 return 1;
1057 }
1058
1059 static int acquire_mount_options(sd_device *d) {
1060 const char *v;
1061
1062 assert(d);
1063
1064 if (arg_mount_options)
1065 return 0;
1066
1067 if (sd_device_get_property_value(d, "SYSTEMD_MOUNT_OPTIONS", &v) < 0)
1068 return 0;
1069
1070 arg_mount_options = strdup(v);
1071 if (!arg_mount_options)
1072 return log_oom();
1073
1074 log_debug("Discovered options=%s", arg_mount_options);
1075 return 1;
1076 }
1077
1078 static const char *get_model(sd_device *d) {
1079 const char *model;
1080
1081 assert(d);
1082
1083 if (sd_device_get_property_value(d, "ID_MODEL_FROM_DATABASE", &model) >= 0)
1084 return model;
1085
1086 if (sd_device_get_property_value(d, "ID_MODEL", &model) >= 0)
1087 return model;
1088
1089 return NULL;
1090 }
1091
1092 static const char* get_label(sd_device *d) {
1093 const char *label;
1094
1095 assert(d);
1096
1097 if (sd_device_get_property_value(d, "ID_FS_LABEL", &label) >= 0)
1098 return label;
1099
1100 if (sd_device_get_property_value(d, "ID_PART_ENTRY_NAME", &label) >= 0)
1101 return label;
1102
1103 return NULL;
1104 }
1105
1106 static int acquire_mount_where(sd_device *d) {
1107 const char *v;
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;
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 name = basename(dn);
1126 }
1127
1128 escaped = xescape(name, "\\");
1129 if (!escaped)
1130 return log_oom();
1131 if (!filename_is_valid(escaped))
1132 return 0;
1133
1134 arg_mount_where = strjoin("/run/media/system/", escaped);
1135 } else
1136 arg_mount_where = strdup(v);
1137
1138 if (!arg_mount_where)
1139 return log_oom();
1140
1141 log_debug("Discovered where=%s", arg_mount_where);
1142 return 1;
1143 }
1144
1145 static int acquire_mount_where_for_loop_dev(const char *loop_dev) {
1146 _cleanup_strv_free_ char **list = NULL;
1147 int r;
1148
1149 if (arg_mount_where)
1150 return 0;
1151
1152 r = find_mount_points(loop_dev, &list);
1153 if (r < 0)
1154 return r;
1155 else if (r == 0)
1156 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1157 "Can't find mount point of %s. It is expected that %s is already mounted on a place.",
1158 loop_dev, loop_dev);
1159 else if (r >= 2)
1160 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1161 "%s is mounted on %d places. It is expected that %s is mounted on a place.",
1162 loop_dev, r, loop_dev);
1163
1164 arg_mount_where = strdup(list[0]);
1165 if (!arg_mount_where)
1166 return log_oom();
1167
1168 log_debug("Discovered where=%s", arg_mount_where);
1169 return 1;
1170 }
1171
1172 static int acquire_description(sd_device *d) {
1173 const char *model, *label;
1174
1175 if (arg_description)
1176 return 0;
1177
1178 model = get_model(d);
1179
1180 label = get_label(d);
1181 if (!label)
1182 (void) sd_device_get_property_value(d, "ID_PART_ENTRY_NUMBER", &label);
1183
1184 if (model && label)
1185 arg_description = strjoin(model, " ", label);
1186 else if (label)
1187 arg_description = strdup(label);
1188 else if (model)
1189 arg_description = strdup(model);
1190 else
1191 return 0;
1192
1193 if (!arg_description)
1194 return log_oom();
1195
1196 log_debug("Discovered description=%s", arg_description);
1197 return 1;
1198 }
1199
1200 static int acquire_removable(sd_device *d) {
1201 const char *v;
1202
1203 /* Shortcut this if there's no reason to check it */
1204 if (arg_action != ACTION_DEFAULT && arg_timeout_idle_set && arg_bind_device >= 0)
1205 return 0;
1206
1207 for (;;) {
1208 if (sd_device_get_sysattr_value(d, "removable", &v) > 0)
1209 break;
1210
1211 if (sd_device_get_parent(d, &d) < 0)
1212 return 0;
1213
1214 if (sd_device_get_subsystem(d, &v) < 0 || !streq(v, "block"))
1215 return 0;
1216 }
1217
1218 if (parse_boolean(v) <= 0)
1219 return 0;
1220
1221 log_debug("Discovered removable device.");
1222
1223 if (arg_action == ACTION_DEFAULT) {
1224 log_debug("Automatically turning on automount.");
1225 arg_action = ACTION_AUTOMOUNT;
1226 }
1227
1228 if (!arg_timeout_idle_set) {
1229 log_debug("Setting idle timeout to 1s.");
1230 arg_timeout_idle = USEC_PER_SEC;
1231 }
1232
1233 if (arg_bind_device < 0) {
1234 log_debug("Binding automount unit to device.");
1235 arg_bind_device = true;
1236 }
1237
1238 return 1;
1239 }
1240
1241 static int discover_loop_backing_file(void) {
1242 _cleanup_(sd_device_unrefp) sd_device *d = NULL;
1243 _cleanup_free_ char *loop_dev = NULL;
1244 struct stat st;
1245 const char *v;
1246 int r;
1247
1248 r = find_loop_device(arg_mount_what, &loop_dev);
1249 if (r < 0 && r != -ENXIO)
1250 return log_error_errno(errno, "Can't get loop device for %s: %m", arg_mount_what);
1251
1252 if (r == -ENXIO) {
1253 _cleanup_free_ char *escaped = NULL;
1254
1255 if (arg_mount_where)
1256 return 0;
1257
1258 escaped = xescape(basename(arg_mount_what), "\\");
1259 if (!escaped)
1260 return log_oom();
1261 if (!filename_is_valid(escaped)) {
1262 log_error("Escaped name %s is not a valid filename.", escaped);
1263 return -EINVAL;
1264 }
1265
1266 arg_mount_where = strjoin("/run/media/system/", escaped);
1267 if (!arg_mount_where)
1268 return log_oom();
1269
1270 log_debug("Discovered where=%s", arg_mount_where);
1271 return 0;
1272 }
1273
1274 if (stat(loop_dev, &st) < 0)
1275 return log_error_errno(errno, "Can't stat %s: %m", loop_dev);
1276
1277 if (!S_ISBLK(st.st_mode)) {
1278 log_error("Invalid file type: %s", loop_dev);
1279 return -EINVAL;
1280 }
1281
1282 r = sd_device_new_from_devnum(&d, 'b', st.st_rdev);
1283 if (r < 0)
1284 return log_error_errno(r, "Failed to get device from device number: %m");
1285
1286 if (sd_device_get_property_value(d, "ID_FS_USAGE", &v) < 0 || !streq(v, "filesystem")) {
1287 log_device_error(d, "%s does not contain a known file system.", arg_mount_what);
1288 return -EINVAL;
1289 }
1290
1291 r = acquire_mount_type(d);
1292 if (r < 0)
1293 return r;
1294
1295 r = acquire_mount_options(d);
1296 if (r < 0)
1297 return r;
1298
1299 r = acquire_mount_where_for_loop_dev(loop_dev);
1300 if (r < 0)
1301 return r;
1302
1303 r = acquire_description(d);
1304 if (r < 0)
1305 return r;
1306
1307 return 0;
1308 }
1309
1310 static int discover_device(void) {
1311 _cleanup_(sd_device_unrefp) sd_device *d = 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 r = sd_device_new_from_devnum(&d, 'b', st.st_rdev);
1328 if (r < 0)
1329 return log_error_errno(r, "Failed to get device from device number: %m");
1330
1331 if (sd_device_get_property_value(d, "ID_FS_USAGE", &v) < 0 || !streq(v, "filesystem")) {
1332 log_error("%s does not contain a known file system.", arg_mount_what);
1333 return -EINVAL;
1334 }
1335
1336 r = acquire_mount_type(d);
1337 if (r < 0)
1338 return r;
1339
1340 r = acquire_mount_options(d);
1341 if (r < 0)
1342 return r;
1343
1344 r = acquire_mount_where(d);
1345 if (r < 0)
1346 return r;
1347
1348 r = acquire_description(d);
1349 if (r < 0)
1350 return r;
1351
1352 r = acquire_removable(d);
1353 if (r < 0)
1354 return r;
1355
1356 return 0;
1357 }
1358
1359 enum {
1360 COLUMN_NODE,
1361 COLUMN_PATH,
1362 COLUMN_MODEL,
1363 COLUMN_WWN,
1364 COLUMN_FSTYPE,
1365 COLUMN_LABEL,
1366 COLUMN_UUID,
1367 _COLUMN_MAX,
1368 };
1369
1370 struct item {
1371 char* columns[_COLUMN_MAX];
1372 };
1373
1374 static int compare_item(const struct item *a, const struct item *b) {
1375 if (a->columns[COLUMN_NODE] == b->columns[COLUMN_NODE])
1376 return 0;
1377 if (!a->columns[COLUMN_NODE])
1378 return 1;
1379 if (!b->columns[COLUMN_NODE])
1380 return -1;
1381
1382 return path_compare(a->columns[COLUMN_NODE], b->columns[COLUMN_NODE]);
1383 }
1384
1385 static int list_devices(void) {
1386
1387 static const char * const titles[_COLUMN_MAX] = {
1388 [COLUMN_NODE] = "NODE",
1389 [COLUMN_PATH] = "PATH",
1390 [COLUMN_MODEL] = "MODEL",
1391 [COLUMN_WWN] = "WWN",
1392 [COLUMN_FSTYPE] = "TYPE",
1393 [COLUMN_LABEL] = "LABEL",
1394 [COLUMN_UUID] = "UUID"
1395 };
1396
1397 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
1398 size_t n_allocated = 0, n = 0, i;
1399 size_t column_width[_COLUMN_MAX];
1400 struct item *items = NULL;
1401 sd_device *d;
1402 unsigned c;
1403 int r;
1404
1405 for (c = 0; c < _COLUMN_MAX; c++)
1406 column_width[c] = strlen(titles[c]);
1407
1408 r = sd_device_enumerator_new(&e);
1409 if (r < 0)
1410 return log_oom();
1411
1412 r = sd_device_enumerator_add_match_subsystem(e, "block", true);
1413 if (r < 0)
1414 return log_error_errno(r, "Failed to add block match: %m");
1415
1416 r = sd_device_enumerator_add_match_property(e, "ID_FS_USAGE", "filesystem");
1417 if (r < 0)
1418 return log_error_errno(r, "Failed to add property match: %m");
1419
1420 FOREACH_DEVICE(e, d) {
1421 struct item *j;
1422
1423 if (!GREEDY_REALLOC0(items, n_allocated, n+1)) {
1424 r = log_oom();
1425 goto finish;
1426 }
1427
1428 j = items + n++;
1429
1430 for (c = 0; c < _COLUMN_MAX; c++) {
1431 const char *x = NULL;
1432 size_t k;
1433
1434 switch (c) {
1435
1436 case COLUMN_NODE:
1437 (void) sd_device_get_devname(d, &x);
1438 break;
1439
1440 case COLUMN_PATH:
1441 (void) sd_device_get_property_value(d, "ID_PATH", &x);
1442 break;
1443
1444 case COLUMN_MODEL:
1445 x = get_model(d);
1446 break;
1447
1448 case COLUMN_WWN:
1449 (void) sd_device_get_property_value(d, "ID_WWN", &x);
1450 break;
1451
1452 case COLUMN_FSTYPE:
1453 (void) sd_device_get_property_value(d, "ID_FS_TYPE", &x);
1454 break;
1455
1456 case COLUMN_LABEL:
1457 x = get_label(d);
1458 break;
1459
1460 case COLUMN_UUID:
1461 (void) sd_device_get_property_value(d, "ID_FS_UUID", &x);
1462 break;
1463 }
1464
1465 if (isempty(x))
1466 continue;
1467
1468 j->columns[c] = strdup(x);
1469 if (!j->columns[c]) {
1470 r = log_oom();
1471 goto finish;
1472 }
1473
1474 k = strlen(x);
1475 if (k > column_width[c])
1476 column_width[c] = k;
1477 }
1478 }
1479
1480 if (n == 0) {
1481 log_info("No devices found.");
1482 goto finish;
1483 }
1484
1485 typesafe_qsort(items, n, compare_item);
1486
1487 (void) pager_open(arg_pager_flags);
1488
1489 fputs(ansi_underline(), stdout);
1490 for (c = 0; c < _COLUMN_MAX; c++) {
1491 if (c > 0)
1492 fputc(' ', stdout);
1493
1494 printf("%-*s", (int) column_width[c], titles[c]);
1495 }
1496 fputs(ansi_normal(), stdout);
1497 fputc('\n', stdout);
1498
1499 for (i = 0; i < n; i++) {
1500 for (c = 0; c < _COLUMN_MAX; c++) {
1501 if (c > 0)
1502 fputc(' ', stdout);
1503
1504 printf("%-*s", (int) column_width[c], strna(items[i].columns[c]));
1505 }
1506 fputc('\n', stdout);
1507 }
1508
1509 r = 0;
1510
1511 finish:
1512 for (i = 0; i < n; i++)
1513 for (c = 0; c < _COLUMN_MAX; c++)
1514 free(items[i].columns[c]);
1515
1516 free(items);
1517 return r;
1518 }
1519
1520 static int run(int argc, char* argv[]) {
1521 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
1522 int r;
1523
1524 log_parse_environment();
1525 log_open();
1526
1527 r = parse_argv(argc, argv);
1528 if (r <= 0)
1529 return r;
1530
1531 if (arg_action == ACTION_LIST)
1532 return list_devices();
1533
1534 r = bus_connect_transport_systemd(arg_transport, arg_host, arg_user, &bus);
1535 if (r < 0)
1536 return log_error_errno(r, "Failed to create bus connection: %m");
1537
1538 if (arg_action == ACTION_UMOUNT)
1539 return action_umount(bus, argc, argv);
1540
1541 if (!path_is_normalized(arg_mount_what)) {
1542 log_error("Path contains non-normalized components: %s", arg_mount_what);
1543 return -EINVAL;
1544 }
1545
1546 if (arg_discover) {
1547 r = discover_device();
1548 if (r < 0)
1549 return r;
1550 }
1551
1552 if (!arg_mount_where) {
1553 log_error("Can't figure out where to mount %s.", arg_mount_what);
1554 return -EINVAL;
1555 }
1556
1557 if (path_equal(arg_mount_where, "/")) {
1558 log_error("Refusing to operate on root directory.");
1559 return -EINVAL;
1560 }
1561
1562 if (!path_is_normalized(arg_mount_where)) {
1563 log_error("Path contains non-normalized components: %s", arg_mount_where);
1564 return -EINVAL;
1565 }
1566
1567 if (streq_ptr(arg_mount_type, "auto"))
1568 arg_mount_type = mfree(arg_mount_type);
1569 if (streq_ptr(arg_mount_options, "defaults"))
1570 arg_mount_options = mfree(arg_mount_options);
1571
1572 if (!is_device_path(arg_mount_what))
1573 arg_fsck = false;
1574
1575 if (arg_fsck && arg_mount_type && arg_transport == BUS_TRANSPORT_LOCAL) {
1576 r = fsck_exists(arg_mount_type);
1577 if (r < 0)
1578 log_warning_errno(r, "Couldn't determine whether fsck for %s exists, proceeding anyway.", arg_mount_type);
1579 else if (r == 0) {
1580 log_debug("Disabling file system check as fsck for %s doesn't exist.", arg_mount_type);
1581 arg_fsck = false; /* fsck doesn't exist, let's not attempt it */
1582 }
1583 }
1584
1585 /* The kernel (properly) refuses mounting file systems with unknown uid=,gid= options,
1586 * but not for all filesystem types. Let's try to catch the cases where the option
1587 * would be used if the file system does not support it. It is also possible to
1588 * autodetect the file system, but that's only possible with disk-based file systems
1589 * which incidentally seem to be implemented more carefully and reject unknown options,
1590 * so it's probably OK that we do the check only when the type is specified.
1591 */
1592 if (arg_mount_type &&
1593 !streq(arg_mount_type, "auto") &&
1594 arg_uid != UID_INVALID &&
1595 !fstype_can_uid_gid(arg_mount_type)) {
1596 log_error("File system type %s is not known to support uid=/gid=, refusing.",
1597 arg_mount_type);
1598 return -EOPNOTSUPP;
1599 }
1600
1601 switch (arg_action) {
1602
1603 case ACTION_MOUNT:
1604 case ACTION_DEFAULT:
1605 r = start_transient_mount(bus, argv + optind);
1606 break;
1607
1608 case ACTION_AUTOMOUNT:
1609 r = start_transient_automount(bus, argv + optind);
1610 break;
1611
1612 default:
1613 assert_not_reached("Unexpected action.");
1614 }
1615
1616 return r;
1617 }
1618
1619 DEFINE_MAIN_FUNCTION(run);