]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
450442cf LP |
2 | |
3 | #include <getopt.h> | |
4 | ||
450442cf | 5 | #include "sd-bus.h" |
dcd26523 | 6 | #include "sd-device.h" |
450442cf LP |
7 | |
8 | #include "bus-error.h" | |
9 | #include "bus-unit-util.h" | |
10 | #include "bus-util.h" | |
8437c059 | 11 | #include "device-util.h" |
6f6165bf | 12 | #include "dirent-util.h" |
450442cf | 13 | #include "escape.h" |
9017f5d8 | 14 | #include "fd-util.h" |
6f6165bf | 15 | #include "fileio.h" |
f4938c2e | 16 | #include "fs-util.h" |
450442cf | 17 | #include "fstab-util.h" |
e2be442e | 18 | #include "mount-util.h" |
450442cf LP |
19 | #include "pager.h" |
20 | #include "parse-util.h" | |
21 | #include "path-util.h" | |
22 | #include "spawn-polkit-agent.h" | |
6f6165bf | 23 | #include "stat-util.h" |
450442cf | 24 | #include "strv.h" |
89ada3ba | 25 | #include "unit-def.h" |
450442cf | 26 | #include "unit-name.h" |
e09fc884 | 27 | #include "user-util.h" |
450442cf LP |
28 | #include "terminal-util.h" |
29 | ||
30 | enum { | |
31 | ACTION_DEFAULT, | |
32 | ACTION_MOUNT, | |
33 | ACTION_AUTOMOUNT, | |
c37fb55b | 34 | ACTION_UMOUNT, |
450442cf LP |
35 | ACTION_LIST, |
36 | } arg_action = ACTION_DEFAULT; | |
37 | ||
38 | static bool arg_no_block = false; | |
0221d68a | 39 | static PagerFlags arg_pager_flags = 0; |
450442cf LP |
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; | |
e09fc884 ZJS |
56 | static uid_t arg_uid = UID_INVALID; |
57 | static gid_t arg_gid = GID_INVALID; | |
450442cf | 58 | static bool arg_fsck = true; |
dc336483 | 59 | static bool arg_aggressive_gc = false; |
450442cf | 60 | |
37ec0fdd LP |
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 | ||
9017f5d8 YW |
69 | printf("systemd-mount [OPTIONS...] WHAT [WHERE]\n" |
70 | "systemd-mount [OPTIONS...] --list\n" | |
71 | "%s [OPTIONS...] %sWHAT|WHERE...\n\n" | |
450442cf LP |
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" | |
e09fc884 | 85 | " --owner=USER Add uid= and gid= options for USER\n" |
450442cf LP |
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" | |
dc336483 | 95 | " -u --umount Unmount mount points\n" |
37ec0fdd LP |
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; | |
450442cf LP |
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, | |
e09fc884 | 118 | ARG_OWNER, |
450442cf LP |
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' }, | |
e09fc884 | 142 | { "owner", required_argument, NULL, ARG_OWNER }, |
0f923832 | 143 | { "fsck", required_argument, NULL, ARG_FSCK }, |
450442cf LP |
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 }, | |
c37fb55b LR |
151 | { "umount", no_argument, NULL, 'u' }, |
152 | { "unmount", no_argument, NULL, 'u' }, | |
dc336483 | 153 | { "collect", no_argument, NULL, 'G' }, |
450442cf LP |
154 | {}, |
155 | }; | |
156 | ||
157 | int r, c; | |
158 | ||
159 | assert(argc >= 0); | |
160 | assert(argv); | |
161 | ||
c37fb55b LR |
162 | if (strstr(program_invocation_short_name, "systemd-umount")) |
163 | arg_action = ACTION_UMOUNT; | |
164 | ||
dc336483 | 165 | while ((c = getopt_long(argc, argv, "hqH:M:t:o:p:AuG", options, NULL)) >= 0) |
450442cf LP |
166 | |
167 | switch (c) { | |
168 | ||
169 | case 'h': | |
37ec0fdd | 170 | return help(); |
450442cf LP |
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: | |
0221d68a | 180 | arg_pager_flags |= PAGER_DISABLE; |
450442cf LP |
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 | ||
e09fc884 ZJS |
223 | case ARG_OWNER: { |
224 | const char *user = optarg; | |
225 | ||
fafff8f1 | 226 | r = get_user_creds(&user, &arg_uid, &arg_gid, NULL, NULL, 0); |
e09fc884 ZJS |
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 | ||
450442cf LP |
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 | ||
c37fb55b LR |
287 | case 'u': |
288 | arg_action = ACTION_UMOUNT; | |
289 | break; | |
290 | ||
dc336483 YW |
291 | case 'G': |
292 | arg_aggressive_gc = true; | |
293 | break; | |
294 | ||
450442cf LP |
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 | } | |
9017f5d8 YW |
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) { | |
3747daa2 YW |
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 | } | |
9017f5d8 | 331 | } |
450442cf LP |
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 | ||
e2be442e YW |
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) { | |
5bc9f949 | 349 | _cleanup_free_ char *u = NULL; |
450442cf | 350 | |
afde5b16 YW |
351 | u = fstab_node_to_udev_node(argv[optind]); |
352 | if (!u) | |
353 | return log_oom(); | |
354 | ||
5bc9f949 | 355 | r = chase_symlinks(u, NULL, 0, &arg_mount_what); |
450442cf | 356 | if (r < 0) |
de345d34 | 357 | return log_error_errno(r, "Failed to make path %s absolute: %m", u); |
afde5b16 | 358 | } else { |
4185da7c | 359 | arg_mount_what = strdup(argv[optind]); |
afde5b16 YW |
360 | if (!arg_mount_what) |
361 | return log_oom(); | |
362 | ||
858d36c1 | 363 | path_simplify(arg_mount_what, false); |
afde5b16 YW |
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) { | |
f4938c2e | 373 | r = chase_symlinks(argv[optind+1], NULL, CHASE_NONEXISTENT, &arg_mount_where); |
afde5b16 | 374 | if (r < 0) |
de345d34 | 375 | return log_error_errno(r, "Failed to make path %s absolute: %m", argv[optind+1]); |
afde5b16 YW |
376 | } else { |
377 | arg_mount_where = strdup(argv[optind+1]); | |
378 | if (!arg_mount_where) | |
379 | return log_oom(); | |
380 | ||
858d36c1 | 381 | path_simplify(arg_mount_where, false); |
afde5b16 YW |
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 | } | |
450442cf LP |
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 | ||
89ada3ba | 400 | static int transient_unit_set_properties(sd_bus_message *m, UnitType t, char **properties) { |
450442cf LP |
401 | int r; |
402 | ||
05b4d3b5 AK |
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 | } | |
450442cf LP |
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 | ||
dc336483 YW |
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 | ||
89ada3ba | 429 | r = bus_append_unit_property_assignment_many(m, t, properties); |
450442cf LP |
430 | if (r < 0) |
431 | return r; | |
432 | ||
433 | return 0; | |
434 | } | |
435 | ||
436 | static int transient_mount_set_properties(sd_bus_message *m) { | |
e09fc884 | 437 | _cleanup_free_ char *options = NULL; |
450442cf LP |
438 | int r; |
439 | ||
440 | assert(m); | |
441 | ||
89ada3ba | 442 | r = transient_unit_set_properties(m, UNIT_MOUNT, arg_property); |
450442cf LP |
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 | ||
e09fc884 ZJS |
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, | |
980982da | 463 | arg_mount_options ? "," : "", strempty(arg_mount_options)); |
450442cf | 464 | if (r < 0) |
e09fc884 | 465 | return -ENOMEM; |
450442cf LP |
466 | } |
467 | ||
e09fc884 ZJS |
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 | ||
450442cf LP |
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 | ||
89ada3ba | 500 | r = transient_unit_set_properties(m, UNIT_AUTOMOUNT, arg_automount_property); |
450442cf LP |
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 | ||
8a4b13c5 | 570 | polkit_agent_open_if_enabled(arg_transport, arg_ask_password); |
450442cf LP |
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 | ||
8a4b13c5 | 685 | polkit_agent_open_if_enabled(arg_transport, arg_ask_password); |
450442cf LP |
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 | ||
f0aac575 YW |
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 | ||
ae2a15bc | 762 | l[n++] = TAKE_PTR(where); |
f0aac575 YW |
763 | } |
764 | ||
290843c3 LP |
765 | if (!GREEDY_REALLOC(l, bufsize, n + 1)) |
766 | return log_oom(); | |
767 | ||
f0aac575 | 768 | l[n] = NULL; |
ae2a15bc | 769 | *list = TAKE_PTR(l); |
f0aac575 YW |
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"); | |
5c6803f5 LP |
783 | if (!d) |
784 | return -errno; | |
f0aac575 YW |
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) | |
5c6803f5 | 800 | return -ENOMEM; |
f0aac575 YW |
801 | |
802 | r = read_one_line_file(sys, &fname); | |
a53dceb7 LP |
803 | if (r < 0) { |
804 | log_debug_errno(r, "Failed to read %s, ignoring: %m", sys); | |
f0aac575 | 805 | continue; |
a53dceb7 | 806 | } |
f0aac575 YW |
807 | |
808 | if (files_same(fname, backing_file, 0) <= 0) | |
809 | continue; | |
810 | ||
811 | l = strjoin("/dev/", de->d_name); | |
812 | if (!l) | |
5c6803f5 | 813 | return -ENOMEM; |
f0aac575 YW |
814 | |
815 | break; | |
816 | } | |
817 | ||
818 | if (!l) | |
aa46fa64 | 819 | return -ENXIO; |
f0aac575 | 820 | |
ae2a15bc | 821 | *loop_dev = TAKE_PTR(l); |
f0aac575 YW |
822 | |
823 | return 0; | |
824 | } | |
825 | ||
c37fb55b LR |
826 | static int stop_mount( |
827 | sd_bus *bus, | |
9017f5d8 | 828 | const char *where, |
6f6165bf | 829 | const char *suffix) { |
c37fb55b LR |
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 | ||
9017f5d8 | 843 | r = unit_name_from_path(where, suffix, &mount_unit); |
c37fb55b | 844 | if (r < 0) |
6442c210 | 845 | return log_error_errno(r, "Failed to make %s unit name from path %s: %m", suffix + 1, where); |
c37fb55b LR |
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 | ||
8a4b13c5 | 866 | polkit_agent_open_if_enabled(arg_transport, arg_ask_password); |
c37fb55b LR |
867 | |
868 | r = sd_bus_call(bus, m, 0, &error, &reply); | |
6442c210 YW |
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 | } | |
c37fb55b LR |
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(), | |
9017f5d8 | 891 | ansi_highlight(), where, ansi_normal()); |
c37fb55b LR |
892 | |
893 | return 0; | |
894 | } | |
895 | ||
896 | static int stop_mounts( | |
897 | sd_bus *bus, | |
6f6165bf | 898 | const char *where) { |
c37fb55b LR |
899 | |
900 | int r; | |
901 | ||
9017f5d8 YW |
902 | if (path_equal(where, "/")) { |
903 | log_error("Refusing to operate on root directory: %s", where); | |
904 | return -EINVAL; | |
905 | } | |
906 | ||
99be45a4 LP |
907 | if (!path_is_normalized(where)) { |
908 | log_error("Path contains non-normalized components: %s", where); | |
9017f5d8 YW |
909 | return -EINVAL; |
910 | } | |
911 | ||
6f6165bf | 912 | r = stop_mount(bus, where, ".mount"); |
c37fb55b LR |
913 | if (r < 0) |
914 | return r; | |
915 | ||
6f6165bf | 916 | r = stop_mount(bus, where, ".automount"); |
c37fb55b LR |
917 | if (r < 0) |
918 | return r; | |
919 | ||
920 | return 0; | |
921 | } | |
922 | ||
9017f5d8 | 923 | static int umount_by_device(sd_bus *bus, const char *what) { |
dcd26523 | 924 | _cleanup_(sd_device_unrefp) sd_device *d = NULL; |
f0aac575 | 925 | _cleanup_strv_free_ char **list = NULL; |
9017f5d8 YW |
926 | struct stat st; |
927 | const char *v; | |
f0aac575 | 928 | char **l; |
6f6165bf | 929 | int r, r2 = 0; |
9017f5d8 YW |
930 | |
931 | assert(what); | |
932 | ||
933 | if (stat(what, &st) < 0) | |
934 | return log_error_errno(errno, "Can't stat %s: %m", what); | |
935 | ||
936 | if (!S_ISBLK(st.st_mode)) { | |
6f6165bf | 937 | log_error("Not a block device: %s", what); |
9017f5d8 YW |
938 | return -ENOTBLK; |
939 | } | |
940 | ||
dcd26523 YW |
941 | r = sd_device_new_from_devnum(&d, 'b', st.st_rdev); |
942 | if (r < 0) | |
943 | return log_error_errno(r, "Failed to get device from device number: %m"); | |
9017f5d8 | 944 | |
dcd26523 YW |
945 | r = sd_device_get_property_value(d, "ID_FS_USAGE", &v); |
946 | if (r < 0) | |
3d0ac9d0 | 947 | return log_device_error_errno(d, r, "Failed to get device property: %m"); |
9017f5d8 | 948 | |
dcd26523 | 949 | if (!streq(v, "filesystem")) { |
3d0ac9d0 | 950 | log_device_error(d, "%s does not contain a known file system.", what); |
9017f5d8 YW |
951 | return -EINVAL; |
952 | } | |
953 | ||
dcd26523 | 954 | if (sd_device_get_property_value(d, "SYSTEMD_MOUNT_WHERE", &v) >= 0) |
f0aac575 | 955 | r2 = stop_mounts(bus, v); |
9017f5d8 | 956 | |
f0aac575 YW |
957 | r = find_mount_points(what, &list); |
958 | if (r < 0) | |
959 | return r; | |
960 | ||
961 | for (l = list; *l; l++) { | |
962 | r = stop_mounts(bus, *l); | |
6f6165bf | 963 | if (r < 0) |
f0aac575 | 964 | r2 = r; |
9017f5d8 YW |
965 | } |
966 | ||
6f6165bf YW |
967 | return r2; |
968 | } | |
969 | ||
970 | static int umount_loop(sd_bus *bus, const char *backing_file) { | |
f0aac575 YW |
971 | _cleanup_free_ char *loop_dev = NULL; |
972 | int r; | |
6f6165bf YW |
973 | |
974 | assert(backing_file); | |
975 | ||
f0aac575 YW |
976 | r = find_loop_device(backing_file, &loop_dev); |
977 | if (r < 0) | |
aa46fa64 | 978 | return log_error_errno(r, r == -ENXIO ? "File %s is not mounted." : "Can't get loop device for %s: %m", backing_file); |
6f6165bf | 979 | |
f0aac575 | 980 | return umount_by_device(bus, loop_dev); |
9017f5d8 YW |
981 | } |
982 | ||
983 | static int action_umount( | |
984 | sd_bus *bus, | |
985 | int argc, | |
986 | char **argv) { | |
987 | ||
988 | int i, r, r2 = 0; | |
989 | ||
3747daa2 YW |
990 | if (arg_transport != BUS_TRANSPORT_LOCAL) { |
991 | for (i = optind; i < argc; i++) { | |
992 | _cleanup_free_ char *p = NULL; | |
993 | ||
994 | p = strdup(argv[i]); | |
995 | if (!p) | |
996 | return log_oom(); | |
997 | ||
858d36c1 | 998 | path_simplify(p, false); |
3747daa2 YW |
999 | |
1000 | r = stop_mounts(bus, p); | |
1001 | if (r < 0) | |
1002 | r2 = r; | |
1003 | } | |
1004 | return r2; | |
1005 | } | |
1006 | ||
9017f5d8 | 1007 | for (i = optind; i < argc; i++) { |
5bc9f949 | 1008 | _cleanup_free_ char *u = NULL, *p = NULL; |
6f6165bf | 1009 | struct stat st; |
9017f5d8 YW |
1010 | |
1011 | u = fstab_node_to_udev_node(argv[i]); | |
1012 | if (!u) | |
1013 | return log_oom(); | |
9017f5d8 | 1014 | |
5bc9f949 | 1015 | r = chase_symlinks(u, NULL, 0, &p); |
9017f5d8 | 1016 | if (r < 0) { |
02a8bd65 | 1017 | r2 = log_error_errno(r, "Failed to make path %s absolute: %m", argv[i]); |
9017f5d8 YW |
1018 | continue; |
1019 | } | |
1020 | ||
6f6165bf | 1021 | if (stat(p, &st) < 0) |
02a8bd65 | 1022 | return log_error_errno(errno, "Can't stat %s (from %s): %m", p, argv[i]); |
9017f5d8 | 1023 | |
6f6165bf YW |
1024 | if (S_ISBLK(st.st_mode)) |
1025 | r = umount_by_device(bus, p); | |
1026 | else if (S_ISREG(st.st_mode)) | |
1027 | r = umount_loop(bus, p); | |
1028 | else if (S_ISDIR(st.st_mode)) | |
1029 | r = stop_mounts(bus, p); | |
1030 | else { | |
02a8bd65 | 1031 | log_error("Invalid file type: %s (from %s)", p, argv[i]); |
6f6165bf YW |
1032 | r = -EINVAL; |
1033 | } | |
1034 | ||
1035 | if (r < 0) | |
9017f5d8 YW |
1036 | r2 = r; |
1037 | } | |
1038 | ||
1039 | return r2; | |
1040 | } | |
1041 | ||
dcd26523 | 1042 | static int acquire_mount_type(sd_device *d) { |
450442cf LP |
1043 | const char *v; |
1044 | ||
1045 | assert(d); | |
1046 | ||
1047 | if (arg_mount_type) | |
1048 | return 0; | |
1049 | ||
dcd26523 | 1050 | if (sd_device_get_property_value(d, "ID_FS_TYPE", &v) < 0) |
450442cf LP |
1051 | return 0; |
1052 | ||
1053 | arg_mount_type = strdup(v); | |
1054 | if (!arg_mount_type) | |
1055 | return log_oom(); | |
1056 | ||
1057 | log_debug("Discovered type=%s", arg_mount_type); | |
1058 | return 1; | |
1059 | } | |
1060 | ||
dcd26523 | 1061 | static int acquire_mount_options(sd_device *d) { |
450442cf LP |
1062 | const char *v; |
1063 | ||
dcd26523 YW |
1064 | assert(d); |
1065 | ||
450442cf LP |
1066 | if (arg_mount_options) |
1067 | return 0; | |
1068 | ||
dcd26523 | 1069 | if (sd_device_get_property_value(d, "SYSTEMD_MOUNT_OPTIONS", &v) < 0) |
450442cf LP |
1070 | return 0; |
1071 | ||
1072 | arg_mount_options = strdup(v); | |
1073 | if (!arg_mount_options) | |
1074 | return log_oom(); | |
1075 | ||
1076 | log_debug("Discovered options=%s", arg_mount_options); | |
1077 | return 1; | |
1078 | } | |
1079 | ||
dcd26523 | 1080 | static const char *get_model(sd_device *d) { |
450442cf LP |
1081 | const char *model; |
1082 | ||
1083 | assert(d); | |
1084 | ||
dcd26523 | 1085 | if (sd_device_get_property_value(d, "ID_MODEL_FROM_DATABASE", &model) >= 0) |
450442cf LP |
1086 | return model; |
1087 | ||
dcd26523 YW |
1088 | if (sd_device_get_property_value(d, "ID_MODEL", &model) >= 0) |
1089 | return model; | |
1090 | ||
1091 | return NULL; | |
450442cf LP |
1092 | } |
1093 | ||
dcd26523 | 1094 | static const char* get_label(sd_device *d) { |
450442cf LP |
1095 | const char *label; |
1096 | ||
1097 | assert(d); | |
1098 | ||
dcd26523 YW |
1099 | if (sd_device_get_property_value(d, "ID_FS_LABEL", &label) >= 0) |
1100 | return label; | |
1101 | ||
1102 | if (sd_device_get_property_value(d, "ID_PART_ENTRY_NAME", &label) >= 0) | |
450442cf LP |
1103 | return label; |
1104 | ||
dcd26523 | 1105 | return NULL; |
450442cf LP |
1106 | } |
1107 | ||
dcd26523 | 1108 | static int acquire_mount_where(sd_device *d) { |
450442cf LP |
1109 | const char *v; |
1110 | ||
1111 | if (arg_mount_where) | |
1112 | return 0; | |
1113 | ||
dcd26523 | 1114 | if (sd_device_get_property_value(d, "SYSTEMD_MOUNT_WHERE", &v) < 0) { |
450442cf LP |
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 | ||
dcd26523 | 1124 | if (sd_device_get_devname(d, &dn) < 0) |
450442cf LP |
1125 | return 0; |
1126 | ||
1127 | name = basename(dn); | |
1128 | } | |
1129 | ||
1130 | escaped = xescape(name, "\\"); | |
f0aac575 YW |
1131 | if (!escaped) |
1132 | return log_oom(); | |
450442cf LP |
1133 | if (!filename_is_valid(escaped)) |
1134 | return 0; | |
1135 | ||
605405c6 | 1136 | arg_mount_where = strjoin("/run/media/system/", escaped); |
450442cf LP |
1137 | } else |
1138 | arg_mount_where = strdup(v); | |
1139 | ||
1140 | if (!arg_mount_where) | |
1141 | return log_oom(); | |
1142 | ||
1143 | log_debug("Discovered where=%s", arg_mount_where); | |
1144 | return 1; | |
1145 | } | |
1146 | ||
f0aac575 YW |
1147 | static int acquire_mount_where_for_loop_dev(const char *loop_dev) { |
1148 | _cleanup_strv_free_ char **list = NULL; | |
1149 | int r; | |
1150 | ||
1151 | if (arg_mount_where) | |
1152 | return 0; | |
1153 | ||
1154 | r = find_mount_points(loop_dev, &list); | |
1155 | if (r < 0) | |
1156 | return r; | |
1157 | else if (r == 0) { | |
1158 | log_error("Can't find mount point of %s. It is expected that %s is already mounted on a place.", loop_dev, loop_dev); | |
1159 | return -EINVAL; | |
1160 | } else if (r >= 2) { | |
1161 | log_error("%s is mounted on %d places. It is expected that %s is mounted on a place.", loop_dev, r, loop_dev); | |
1162 | return -EINVAL; | |
1163 | } | |
1164 | ||
1165 | arg_mount_where = strdup(list[0]); | |
1166 | if (!arg_mount_where) | |
1167 | return log_oom(); | |
1168 | ||
1169 | log_debug("Discovered where=%s", arg_mount_where); | |
1170 | return 1; | |
1171 | } | |
1172 | ||
dcd26523 | 1173 | static int acquire_description(sd_device *d) { |
450442cf LP |
1174 | const char *model, *label; |
1175 | ||
1176 | if (arg_description) | |
1177 | return 0; | |
1178 | ||
1179 | model = get_model(d); | |
1180 | ||
1181 | label = get_label(d); | |
1182 | if (!label) | |
dcd26523 | 1183 | (void) sd_device_get_property_value(d, "ID_PART_ENTRY_NUMBER", &label); |
450442cf LP |
1184 | |
1185 | if (model && label) | |
605405c6 | 1186 | arg_description = strjoin(model, " ", label); |
450442cf LP |
1187 | else if (label) |
1188 | arg_description = strdup(label); | |
1189 | else if (model) | |
1190 | arg_description = strdup(model); | |
1191 | else | |
05b4d3b5 | 1192 | return 0; |
450442cf LP |
1193 | |
1194 | if (!arg_description) | |
1195 | return log_oom(); | |
1196 | ||
1197 | log_debug("Discovered description=%s", arg_description); | |
1198 | return 1; | |
1199 | } | |
1200 | ||
dcd26523 | 1201 | static int acquire_removable(sd_device *d) { |
450442cf LP |
1202 | const char *v; |
1203 | ||
1204 | /* Shortcut this if there's no reason to check it */ | |
1205 | if (arg_action != ACTION_DEFAULT && arg_timeout_idle_set && arg_bind_device >= 0) | |
1206 | return 0; | |
1207 | ||
1208 | for (;;) { | |
dcd26523 | 1209 | if (sd_device_get_sysattr_value(d, "removable", &v) > 0) |
450442cf LP |
1210 | break; |
1211 | ||
dcd26523 | 1212 | if (sd_device_get_parent(d, &d) < 0) |
450442cf LP |
1213 | return 0; |
1214 | ||
dcd26523 | 1215 | if (sd_device_get_subsystem(d, &v) < 0 || !streq(v, "block")) |
450442cf LP |
1216 | return 0; |
1217 | } | |
1218 | ||
1219 | if (parse_boolean(v) <= 0) | |
1220 | return 0; | |
1221 | ||
1222 | log_debug("Discovered removable device."); | |
1223 | ||
1224 | if (arg_action == ACTION_DEFAULT) { | |
1225 | log_debug("Automatically turning on automount."); | |
1226 | arg_action = ACTION_AUTOMOUNT; | |
1227 | } | |
1228 | ||
1229 | if (!arg_timeout_idle_set) { | |
1230 | log_debug("Setting idle timeout to 1s."); | |
1231 | arg_timeout_idle = USEC_PER_SEC; | |
1232 | } | |
1233 | ||
1234 | if (arg_bind_device < 0) { | |
1235 | log_debug("Binding automount unit to device."); | |
1236 | arg_bind_device = true; | |
1237 | } | |
1238 | ||
1239 | return 1; | |
1240 | } | |
1241 | ||
f0aac575 | 1242 | static int discover_loop_backing_file(void) { |
dcd26523 | 1243 | _cleanup_(sd_device_unrefp) sd_device *d = NULL; |
f0aac575 | 1244 | _cleanup_free_ char *loop_dev = NULL; |
450442cf LP |
1245 | struct stat st; |
1246 | const char *v; | |
1247 | int r; | |
1248 | ||
f0aac575 | 1249 | r = find_loop_device(arg_mount_what, &loop_dev); |
aa46fa64 | 1250 | if (r < 0 && r != -ENXIO) |
f0aac575 YW |
1251 | return log_error_errno(errno, "Can't get loop device for %s: %m", arg_mount_what); |
1252 | ||
aa46fa64 | 1253 | if (r == -ENXIO) { |
f0aac575 YW |
1254 | _cleanup_free_ char *escaped = NULL; |
1255 | ||
1256 | if (arg_mount_where) | |
1257 | return 0; | |
1258 | ||
1259 | escaped = xescape(basename(arg_mount_what), "\\"); | |
1260 | if (!escaped) | |
1261 | return log_oom(); | |
514368ad LP |
1262 | if (!filename_is_valid(escaped)) { |
1263 | log_error("Escaped name %s is not a valid filename.", escaped); | |
f0aac575 | 1264 | return -EINVAL; |
514368ad | 1265 | } |
f0aac575 YW |
1266 | |
1267 | arg_mount_where = strjoin("/run/media/system/", escaped); | |
1268 | if (!arg_mount_where) | |
1269 | return log_oom(); | |
1270 | ||
1271 | log_debug("Discovered where=%s", arg_mount_where); | |
450442cf | 1272 | return 0; |
f0aac575 YW |
1273 | } |
1274 | ||
1275 | if (stat(loop_dev, &st) < 0) | |
1276 | return log_error_errno(errno, "Can't stat %s: %m", loop_dev); | |
450442cf | 1277 | |
f0aac575 YW |
1278 | if (!S_ISBLK(st.st_mode)) { |
1279 | log_error("Invalid file type: %s", loop_dev); | |
450442cf LP |
1280 | return -EINVAL; |
1281 | } | |
1282 | ||
dcd26523 YW |
1283 | r = sd_device_new_from_devnum(&d, 'b', st.st_rdev); |
1284 | if (r < 0) | |
1285 | return log_error_errno(r, "Failed to get device from device number: %m"); | |
f0aac575 | 1286 | |
dcd26523 | 1287 | if (sd_device_get_property_value(d, "ID_FS_USAGE", &v) < 0 || !streq(v, "filesystem")) { |
3d0ac9d0 | 1288 | log_device_error(d, "%s does not contain a known file system.", arg_mount_what); |
f0aac575 YW |
1289 | return -EINVAL; |
1290 | } | |
1291 | ||
1292 | r = acquire_mount_type(d); | |
1293 | if (r < 0) | |
1294 | return r; | |
1295 | ||
1296 | r = acquire_mount_options(d); | |
1297 | if (r < 0) | |
1298 | return r; | |
1299 | ||
1300 | r = acquire_mount_where_for_loop_dev(loop_dev); | |
1301 | if (r < 0) | |
1302 | return r; | |
1303 | ||
1304 | r = acquire_description(d); | |
1305 | if (r < 0) | |
1306 | return r; | |
1307 | ||
1308 | return 0; | |
1309 | } | |
1310 | ||
1311 | static int discover_device(void) { | |
dcd26523 | 1312 | _cleanup_(sd_device_unrefp) sd_device *d = NULL; |
f0aac575 YW |
1313 | struct stat st; |
1314 | const char *v; | |
1315 | int r; | |
1316 | ||
450442cf LP |
1317 | if (stat(arg_mount_what, &st) < 0) |
1318 | return log_error_errno(errno, "Can't stat %s: %m", arg_mount_what); | |
1319 | ||
f0aac575 YW |
1320 | if (S_ISREG(st.st_mode)) |
1321 | return discover_loop_backing_file(); | |
1322 | ||
450442cf | 1323 | if (!S_ISBLK(st.st_mode)) { |
f0aac575 YW |
1324 | log_error("Invalid file type: %s", arg_mount_what); |
1325 | return -EINVAL; | |
450442cf LP |
1326 | } |
1327 | ||
dcd26523 YW |
1328 | r = sd_device_new_from_devnum(&d, 'b', st.st_rdev); |
1329 | if (r < 0) | |
1330 | return log_error_errno(r, "Failed to get device from device number: %m"); | |
450442cf | 1331 | |
dcd26523 | 1332 | if (sd_device_get_property_value(d, "ID_FS_USAGE", &v) < 0 || !streq(v, "filesystem")) { |
9017f5d8 | 1333 | log_error("%s does not contain a known file system.", arg_mount_what); |
450442cf LP |
1334 | return -EINVAL; |
1335 | } | |
1336 | ||
1337 | r = acquire_mount_type(d); | |
1338 | if (r < 0) | |
1339 | return r; | |
1340 | ||
1341 | r = acquire_mount_options(d); | |
1342 | if (r < 0) | |
1343 | return r; | |
1344 | ||
1345 | r = acquire_mount_where(d); | |
1346 | if (r < 0) | |
1347 | return r; | |
1348 | ||
1349 | r = acquire_description(d); | |
1350 | if (r < 0) | |
1351 | return r; | |
1352 | ||
1353 | r = acquire_removable(d); | |
1354 | if (r < 0) | |
1355 | return r; | |
1356 | ||
1357 | return 0; | |
1358 | } | |
1359 | ||
1360 | enum { | |
1361 | COLUMN_NODE, | |
1362 | COLUMN_PATH, | |
1363 | COLUMN_MODEL, | |
1364 | COLUMN_WWN, | |
1365 | COLUMN_FSTYPE, | |
1366 | COLUMN_LABEL, | |
1367 | COLUMN_UUID, | |
1368 | _COLUMN_MAX, | |
1369 | }; | |
1370 | ||
1371 | struct item { | |
1372 | char* columns[_COLUMN_MAX]; | |
1373 | }; | |
1374 | ||
93bab288 YW |
1375 | static int compare_item(const struct item *a, const struct item *b) { |
1376 | if (a->columns[COLUMN_NODE] == b->columns[COLUMN_NODE]) | |
450442cf | 1377 | return 0; |
93bab288 | 1378 | if (!a->columns[COLUMN_NODE]) |
450442cf | 1379 | return 1; |
93bab288 | 1380 | if (!b->columns[COLUMN_NODE]) |
450442cf LP |
1381 | return -1; |
1382 | ||
93bab288 | 1383 | return path_compare(a->columns[COLUMN_NODE], b->columns[COLUMN_NODE]); |
450442cf LP |
1384 | } |
1385 | ||
1386 | static int list_devices(void) { | |
1387 | ||
1388 | static const char * const titles[_COLUMN_MAX] = { | |
1389 | [COLUMN_NODE] = "NODE", | |
1390 | [COLUMN_PATH] = "PATH", | |
1391 | [COLUMN_MODEL] = "MODEL", | |
1392 | [COLUMN_WWN] = "WWN", | |
1393 | [COLUMN_FSTYPE] = "TYPE", | |
1394 | [COLUMN_LABEL] = "LABEL", | |
1395 | [COLUMN_UUID] = "UUID" | |
1396 | }; | |
1397 | ||
dcd26523 | 1398 | _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; |
450442cf LP |
1399 | size_t n_allocated = 0, n = 0, i; |
1400 | size_t column_width[_COLUMN_MAX]; | |
1401 | struct item *items = NULL; | |
dcd26523 | 1402 | sd_device *d; |
450442cf LP |
1403 | unsigned c; |
1404 | int r; | |
1405 | ||
1406 | for (c = 0; c < _COLUMN_MAX; c++) | |
1407 | column_width[c] = strlen(titles[c]); | |
1408 | ||
dcd26523 YW |
1409 | r = sd_device_enumerator_new(&e); |
1410 | if (r < 0) | |
450442cf LP |
1411 | return log_oom(); |
1412 | ||
dcd26523 | 1413 | r = sd_device_enumerator_add_match_subsystem(e, "block", true); |
450442cf LP |
1414 | if (r < 0) |
1415 | return log_error_errno(r, "Failed to add block match: %m"); | |
1416 | ||
dcd26523 | 1417 | r = sd_device_enumerator_add_match_property(e, "ID_FS_USAGE", "filesystem"); |
450442cf LP |
1418 | if (r < 0) |
1419 | return log_error_errno(r, "Failed to add property match: %m"); | |
1420 | ||
8437c059 | 1421 | FOREACH_DEVICE(e, d) { |
450442cf LP |
1422 | struct item *j; |
1423 | ||
450442cf LP |
1424 | if (!GREEDY_REALLOC0(items, n_allocated, n+1)) { |
1425 | r = log_oom(); | |
1426 | goto finish; | |
1427 | } | |
1428 | ||
1429 | j = items + n++; | |
1430 | ||
1431 | for (c = 0; c < _COLUMN_MAX; c++) { | |
6fd667e5 | 1432 | const char *x = NULL; |
450442cf LP |
1433 | size_t k; |
1434 | ||
1435 | switch (c) { | |
1436 | ||
1437 | case COLUMN_NODE: | |
dcd26523 | 1438 | (void) sd_device_get_devname(d, &x); |
450442cf LP |
1439 | break; |
1440 | ||
1441 | case COLUMN_PATH: | |
dcd26523 | 1442 | (void) sd_device_get_property_value(d, "ID_PATH", &x); |
450442cf LP |
1443 | break; |
1444 | ||
1445 | case COLUMN_MODEL: | |
1446 | x = get_model(d); | |
1447 | break; | |
1448 | ||
1449 | case COLUMN_WWN: | |
dcd26523 | 1450 | (void) sd_device_get_property_value(d, "ID_WWN", &x); |
450442cf LP |
1451 | break; |
1452 | ||
1453 | case COLUMN_FSTYPE: | |
dcd26523 | 1454 | (void) sd_device_get_property_value(d, "ID_FS_TYPE", &x); |
450442cf LP |
1455 | break; |
1456 | ||
1457 | case COLUMN_LABEL: | |
1458 | x = get_label(d); | |
1459 | break; | |
1460 | ||
1461 | case COLUMN_UUID: | |
dcd26523 | 1462 | (void) sd_device_get_property_value(d, "ID_FS_UUID", &x); |
450442cf LP |
1463 | break; |
1464 | } | |
1465 | ||
1466 | if (isempty(x)) | |
1467 | continue; | |
1468 | ||
1469 | j->columns[c] = strdup(x); | |
1470 | if (!j->columns[c]) { | |
1471 | r = log_oom(); | |
1472 | goto finish; | |
1473 | } | |
1474 | ||
1475 | k = strlen(x); | |
1476 | if (k > column_width[c]) | |
1477 | column_width[c] = k; | |
1478 | } | |
1479 | } | |
1480 | ||
1481 | if (n == 0) { | |
1482 | log_info("No devices found."); | |
1483 | goto finish; | |
1484 | } | |
1485 | ||
93bab288 | 1486 | typesafe_qsort(items, n, compare_item); |
450442cf | 1487 | |
0221d68a | 1488 | (void) pager_open(arg_pager_flags); |
450442cf LP |
1489 | |
1490 | fputs(ansi_underline(), stdout); | |
1491 | for (c = 0; c < _COLUMN_MAX; c++) { | |
1492 | if (c > 0) | |
1493 | fputc(' ', stdout); | |
1494 | ||
1495 | printf("%-*s", (int) column_width[c], titles[c]); | |
1496 | } | |
1497 | fputs(ansi_normal(), stdout); | |
1498 | fputc('\n', stdout); | |
1499 | ||
1500 | for (i = 0; i < n; i++) { | |
1501 | for (c = 0; c < _COLUMN_MAX; c++) { | |
1502 | if (c > 0) | |
1503 | fputc(' ', stdout); | |
1504 | ||
1505 | printf("%-*s", (int) column_width[c], strna(items[i].columns[c])); | |
1506 | } | |
1507 | fputc('\n', stdout); | |
1508 | } | |
1509 | ||
1510 | r = 0; | |
1511 | ||
1512 | finish: | |
1513 | for (i = 0; i < n; i++) | |
1514 | for (c = 0; c < _COLUMN_MAX; c++) | |
1515 | free(items[i].columns[c]); | |
1516 | ||
1517 | free(items); | |
1518 | return r; | |
1519 | } | |
1520 | ||
1521 | int main(int argc, char* argv[]) { | |
3b2f3033 | 1522 | sd_bus *bus = NULL; |
450442cf LP |
1523 | int r; |
1524 | ||
1525 | log_parse_environment(); | |
1526 | log_open(); | |
1527 | ||
1528 | r = parse_argv(argc, argv); | |
1529 | if (r <= 0) | |
1530 | goto finish; | |
1531 | ||
1532 | if (arg_action == ACTION_LIST) { | |
1533 | r = list_devices(); | |
1534 | goto finish; | |
1535 | } | |
1536 | ||
9017f5d8 YW |
1537 | r = bus_connect_transport_systemd(arg_transport, arg_host, arg_user, &bus); |
1538 | if (r < 0) { | |
1539 | log_error_errno(r, "Failed to create bus connection: %m"); | |
1540 | goto finish; | |
1541 | } | |
1542 | ||
1543 | if (arg_action == ACTION_UMOUNT) { | |
1544 | r = action_umount(bus, argc, argv); | |
1545 | goto finish; | |
1546 | } | |
1547 | ||
99be45a4 LP |
1548 | if (!path_is_normalized(arg_mount_what)) { |
1549 | log_error("Path contains non-normalized components: %s", arg_mount_what); | |
f0aac575 | 1550 | r = -EINVAL; |
450442cf | 1551 | goto finish; |
f0aac575 YW |
1552 | } |
1553 | ||
1554 | if (arg_discover) { | |
1555 | r = discover_device(); | |
1556 | if (r < 0) | |
1557 | goto finish; | |
1558 | } | |
1559 | ||
450442cf LP |
1560 | if (!arg_mount_where) { |
1561 | log_error("Can't figure out where to mount %s.", arg_mount_what); | |
1562 | r = -EINVAL; | |
1563 | goto finish; | |
1564 | } | |
1565 | ||
450442cf LP |
1566 | if (path_equal(arg_mount_where, "/")) { |
1567 | log_error("Refusing to operate on root directory."); | |
1568 | r = -EINVAL; | |
1569 | goto finish; | |
1570 | } | |
1571 | ||
99be45a4 LP |
1572 | if (!path_is_normalized(arg_mount_where)) { |
1573 | log_error("Path contains non-normalized components: %s", arg_mount_where); | |
450442cf LP |
1574 | r = -EINVAL; |
1575 | goto finish; | |
1576 | } | |
1577 | ||
1578 | if (streq_ptr(arg_mount_type, "auto")) | |
1579 | arg_mount_type = mfree(arg_mount_type); | |
1580 | if (streq_ptr(arg_mount_options, "defaults")) | |
1581 | arg_mount_options = mfree(arg_mount_options); | |
1582 | ||
1583 | if (!is_device_path(arg_mount_what)) | |
1584 | arg_fsck = false; | |
1585 | ||
1586 | if (arg_fsck && arg_mount_type && arg_transport == BUS_TRANSPORT_LOCAL) { | |
1587 | r = fsck_exists(arg_mount_type); | |
1588 | if (r < 0) | |
1589 | log_warning_errno(r, "Couldn't determine whether fsck for %s exists, proceeding anyway.", arg_mount_type); | |
1590 | else if (r == 0) { | |
1591 | log_debug("Disabling file system check as fsck for %s doesn't exist.", arg_mount_type); | |
1592 | arg_fsck = false; /* fsck doesn't exist, let's not attempt it */ | |
1593 | } | |
1594 | } | |
1595 | ||
e09fc884 ZJS |
1596 | /* The kernel (properly) refuses mounting file systems with unknown uid=,gid= options, |
1597 | * but not for all filesystem types. Let's try to catch the cases where the option | |
1598 | * would be used if the file system does not support it. It is also possible to | |
1599 | * autodetect the file system, but that's only possible with disk-based file systems | |
1600 | * which incidentally seem to be implemented more carefully and reject unknown options, | |
1601 | * so it's probably OK that we do the check only when the type is specified. | |
1602 | */ | |
1603 | if (arg_mount_type && | |
1604 | !streq(arg_mount_type, "auto") && | |
1605 | arg_uid != UID_INVALID && | |
1606 | !fstype_can_uid_gid(arg_mount_type)) { | |
1607 | log_error("File system type %s is not known to support uid=/gid=, refusing.", | |
1608 | arg_mount_type); | |
1609 | r = -EOPNOTSUPP; | |
1610 | goto finish; | |
1611 | } | |
1612 | ||
450442cf LP |
1613 | switch (arg_action) { |
1614 | ||
1615 | case ACTION_MOUNT: | |
1616 | case ACTION_DEFAULT: | |
1617 | r = start_transient_mount(bus, argv + optind); | |
1618 | break; | |
1619 | ||
1620 | case ACTION_AUTOMOUNT: | |
1621 | r = start_transient_automount(bus, argv + optind); | |
1622 | break; | |
1623 | ||
1624 | default: | |
1625 | assert_not_reached("Unexpected action."); | |
1626 | } | |
1627 | ||
1628 | finish: | |
0a84daa5 FB |
1629 | /* make sure we terminate the bus connection first, and then close the |
1630 | * pager, see issue #3543 for the details. */ | |
a3c56345 | 1631 | bus = sd_bus_flush_close_unref(bus); |
450442cf LP |
1632 | pager_close(); |
1633 | ||
1634 | free(arg_mount_what); | |
1635 | free(arg_mount_where); | |
1636 | free(arg_mount_type); | |
1637 | free(arg_mount_options); | |
1638 | free(arg_description); | |
1639 | strv_free(arg_property); | |
1640 | strv_free(arg_automount_property); | |
1641 | ||
1642 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; | |
1643 | } |