Debian Codegraph @

src/shared/generator.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <stdlib.h>
4 #include <sys/stat.h>
5 #include <unistd.h>
6
7 #include "alloc-util.h"
8 #include "argv-util.h"
9 #include "dropin.h"
10 #include "escape.h"
11 #include "fd-util.h"
12 #include "fileio.h"
13 #include "fstab-util.h"
14 #include "generator.h"
15 #include "initrd-util.h"
16 #include "log.h"
17 #include "mkdir.h"
18 #include "mountpoint-util.h"
19 #include "parse-util.h"
20 #include "path-util.h"
21 #include "special.h"
22 #include "specifier.h"
23 #include "string-util.h"
24 #include "time-util.h"
25 #include "tmpfile-util.h"
26 #include "unit-name.h"
27
28 static int symlink_unless_exists(const char *target, const char *linkpath) {
29 (void) mkdir_parents(linkpath, 0755);
30
31 if (symlink(target, linkpath) < 0 && errno != EEXIST)
32 return log_error_errno(errno, "Failed to create symlink %s: %m", linkpath);
33 return 0;
34 }
35
36 int generator_open_unit_file_full(
37 const char *dir,
38 const char *source,
39 const char *filename,
40 FILE **ret_file,
41 char **ret_final_path,
42 char **ret_temp_path) {
43
44 _cleanup_free_ char *p = NULL;
45 FILE *f;
46 int r;
47
48 assert(dir);
49 assert(ret_file);
50
51 /* If <ret_temp_path> is specified, it creates a temporary unit file and also returns its
52 * temporary path. */
53
54 if (ret_temp_path) {
55 r = fopen_temporary(dir, &f, &p);
56 if (r < 0)
57 return log_error_errno(r, "Failed to create temporary unit file in '%s': %m", dir);
58
59 (void) fchmod(fileno(f), 0644);
60
61 *ret_temp_path = TAKE_PTR(p);
62 } else {
63 assert(filename);
64
65 p = path_join(dir, filename);
66 if (!p)
67 return log_oom();
68
69 r = fopen_unlocked(p, "wxe", &f);
70 if (r < 0) {
71 if (source && r == -EEXIST)
72 return log_error_errno(r,
73 "Failed to create unit file '%s', as it already exists. Duplicate entry in '%s'?",
74 p, source);
75
76 return log_error_errno(r, "Failed to create unit file '%s': %m", p);
77 }
78 }
79
80 fprintf(f,
81 "# Automatically generated by %s\n\n",
82 program_invocation_short_name);
83
84 *ret_file = f;
85
86 if (ret_final_path)
87 *ret_final_path = TAKE_PTR(p);
88
89 return 0;
90 }
91
92 int generator_add_symlink_full(
93 const char *dir,
94 const char *dst,
95 const char *dep_type,
96 const char *src,
97 const char *instance) {
98
99 _cleanup_free_ char *dn = NULL, *fn = NULL, *instantiated = NULL, *to = NULL, *from = NULL;
100 int r;
101
102 assert(dir);
103 assert(dst);
104 assert(src);
105
106 /* If 'dep_type' is specified adds a symlink from <dst>.<dep_type>/ to <src> (if src is absolute) or ../<src> (otherwise).
107 *
108 * If 'dep_type' is NULL, it will create a symlink to <src> (i.e. create an alias.
109 *
110 * If <instance> is specified, then <src> must be a template unit name, and we'll instantiate it. */
111
112 r = path_split_prefix_filename(src, &dn, &fn);
113 if (r < 0)
114 return log_error_errno(r, "Failed to split '%s' into directory prefix and filename: %m", src);
115 if (r == O_DIRECTORY)
116 return log_error_errno(SYNTHETIC_ERRNO(EISDIR), "Expected path to regular file name, but got '%s', refusing.", src);
117
118 if (instance) {
119 r = unit_name_replace_instance(fn, instance, &instantiated);
120 if (r < 0)
121 return log_error_errno(r, "Failed to instantiate '%s' for '%s': %m", fn, instance);
122 }
123
124 if (dep_type) { /* Create a .wants/ style dep */
125 from = path_join(dn ?: "..", fn);
126 if (!from)
127 return log_oom();
128
129 to = strjoin(dir, "/", dst, ".", dep_type, "/", instantiated ?: fn);
130 } else { /* or create an alias */
131 from = dn ? path_join(dn, fn) : strdup(fn);
132 if (!from)
133 return log_oom();
134
135 to = strjoin(dir, "/", dst);
136 }
137 if (!to)
138 return log_oom();
139
140 return symlink_unless_exists(from, to);
141 }
142
143 static int generator_add_ordering(
144 const char *dir,
145 const char *src,
146 const char *order,
147 const char *dst,
148 const char *instance) {
149
150 _cleanup_free_ char *instantiated = NULL, *p = NULL, *fn = NULL;
151 _cleanup_fclose_ FILE *f = NULL;
152 const char *to;
153 int r;
154
155 assert(dir);
156 assert(src);
157 assert(order);
158 assert(dst);
159
160 /* Adds an explicit ordering dependency of type <order> from <src> to <dst>. If <instance> is
161 * specified, it is inserted into <dst>. */
162
163 if (instance) {
164 r = unit_name_replace_instance(dst, instance, &instantiated);
165 if (r < 0)
166 return log_error_errno(r, "Failed to instantiate '%s' for '%s': %m", dst, instance);
167
168 to = instantiated;
169 } else
170 to = dst;
171
172 fn = strjoin(src, ".d/50-order-", to, ".conf");
173 if (!fn)
174 return log_oom();
175
176 p = path_join(dir, fn);
177 if (!p)
178 return log_oom();
179
180 (void) mkdir_parents_label(p, 0755);
181
182 r = fopen_unlocked(p, "wxe", &f);
183 if (r < 0)
184 return log_error_errno(r, "Failed to create '%s': %m", p);
185
186 fprintf(f,
187 "# Automatically generated by %s\n\n"
188 "[Unit]\n"
189 "%s=%s\n",
190 program_invocation_short_name,
191 order,
192 to);
193
194 r = fflush_and_check(f);
195 if (r < 0)
196 return log_error_errno(r, "Failed to write drop-in '%s': %m", p);
197
198 return 0;
199 }
200
201 static int write_fsck_sysroot_service(
202 const char *unit, /* Either SPECIAL_FSCK_ROOT_SERVICE or SPECIAL_FSCK_USR_SERVICE */
203 const char *dir,
204 const char *what,
205 const char *extra_after) {
206
207 _cleanup_free_ char *device = NULL, *escaped = NULL, *escaped2 = NULL;
208 _cleanup_fclose_ FILE *f = NULL;
209 int r;
210
211 assert(unit);
212 assert(dir);
213 assert(what);
214
215 /* Writes out special versions of systemd-fsck-root.service and systemd-fsck-usr.service for use in
216 * the initrd. The regular statically shipped versions of these unit files use / and /usr for as
217 * paths, which doesn't match what we need for the initrd (where the dirs are /sysroot/ +
218 * /sysusr/usr/), hence we overwrite those versions here. */
219
220 escaped = specifier_escape(what);
221 if (!escaped)
222 return log_oom();
223
224 escaped2 = cescape(escaped);
225 if (!escaped2)
226 return log_oom();
227
228 r = unit_name_from_path(what, ".device", &device);
229 if (r < 0)
230 return log_error_errno(r, "Failed to convert device \"%s\" to unit name: %m", what);
231
232 r = generator_open_unit_file(dir, /* source= */ NULL, unit, &f);
233 if (r < 0)
234 return r;
235
236 fprintf(f,
237 "[Unit]\n"
238 "Description=File System Check on %1$s\n"
239 "Documentation=man:%2$s(8)\n"
240 "\n"
241 "DefaultDependencies=no\n"
242 "BindsTo=%3$s\n"
243 "Conflicts=shutdown.target\n"
244 "After=%4$s%5$slocal-fs-pre.target %3$s\n"
245 "Before=shutdown.target\n"
246 "\n"
247 "[Service]\n"
248 "Type=oneshot\n"
249 "RemainAfterExit=yes\n"
250 "ExecStart=" SYSTEMD_FSCK_PATH " %6$s\n"
251 "TimeoutSec=infinity\n"
252 "ImportCredential=fsck.*\n",
253 escaped,
254 unit,
255 device,
256 strempty(extra_after),
257 isempty(extra_after) ? "" : " ",
258 escaped2);
259
260 r = fflush_and_check(f);
261 if (r < 0)
262 return log_error_errno(r, "Failed to write unit %s: %m", unit);
263
264 return 0;
265 }
266
267 int generator_write_fsck_deps(
268 FILE *f,
269 const char *dir,
270 const char *what,
271 const char *where,
272 const char *fstype,
273 const char *options) {
274
275 int r;
276
277 assert(f);
278 assert(dir);
279 assert(what);
280 assert(where);
281
282 /* Let's do an early exit if we are invoked for the root and /usr/ trees in the initrd, to avoid
283 * generating confusing log messages. */
284 if (in_initrd() && PATH_IN_SET(where, "/", "/usr")) {
285 log_debug("Skipping fsck for %s in initrd.", where);
286 return 0;
287 }
288
289 if (fstype) {
290 if (!fstype_is_blockdev_backed(fstype)) {
291 log_debug("Skipping file system check for non-block based file system '%s'.", what);
292 return 0;
293 }
294
295 if (fstype_is_ro(fstype)) {
296 log_debug("Skipping file system check for read-only file system '%s'.", what);
297 return 0;
298 }
299 }
300
301 if (fstab_test_option(options, "bind\0rbind\0")) {
302 log_debug("Skipping file system check for bind mount of '%s'.", what);
303 return 0;
304 }
305
306 if (!is_device_path(what)) {
307 log_debug("Checking was requested for \"%s\", but it is not a device.", what);
308 return 0;
309 }
310
311 if (!isempty(fstype) && !streq(fstype, "auto")) {
312 r = fsck_exists_for_fstype(fstype);
313 if (r < 0)
314 log_warning_errno(r, "Checking was requested for %s, but couldn't detect if fsck.%s may be used, proceeding: %m", what, fstype);
315 else if (r == 0) {
316 /* treat missing check as essentially OK */
317 log_debug("Checking was requested for %s, but fsck.%s does not exist.", what, fstype);
318 return 0;
319 }
320 } else {
321 r = fsck_exists();
322 if (r < 0)
323 log_warning_errno(r, "Checking was requested for %s, but couldn't detect if the fsck command may be used, proceeding: %m", what);
324 else if (r == 0) {
325 /* treat missing fsck as essentially OK */
326 log_debug("Checking was requested for %s, but the fsck command does not exist.", what);
327 return 0;
328 }
329 }
330
331 if (path_equal(where, "/")) {
332 /* We support running the fsck instance for the root fs while it is already mounted, for
333 * compatibility with non-initrd boots. It's ugly, but it is how it is. Since – unlike for
334 * regular file systems – this means the ordering is reversed (i.e. mount *before* fsck) we
335 * have a separate fsck unit for this, independent of [email protected]. */
336
337 const char *lnk = strjoina(dir, "/" SPECIAL_LOCAL_FS_TARGET ".wants/" SPECIAL_FSCK_ROOT_SERVICE);
338
339 r = symlink_unless_exists(SYSTEM_DATA_UNIT_DIR "/" SPECIAL_FSCK_ROOT_SERVICE, lnk);
340 if (r < 0)
341 return r;
342 } else {
343 _cleanup_free_ char *_fsck = NULL;
344 const char *fsck, *dep;
345
346 if (in_initrd() && path_equal(where, "/sysroot")) {
347 r = write_fsck_sysroot_service(SPECIAL_FSCK_ROOT_SERVICE, dir, what, SPECIAL_INITRD_ROOT_DEVICE_TARGET);
348 if (r < 0)
349 return r;
350
351 fsck = SPECIAL_FSCK_ROOT_SERVICE;
352 dep = "Requires";
353
354 } else if (in_initrd() && path_equal(where, "/sysusr/usr")) {
355 r = write_fsck_sysroot_service(SPECIAL_FSCK_USR_SERVICE, dir, what, NULL);
356 if (r < 0)
357 return r;
358
359 fsck = SPECIAL_FSCK_USR_SERVICE;
360 dep = "Requires";
361 } else {
362 /* When this is /usr, then let's add a Wants= dependency, otherwise a Requires=
363 * dependency. Why? We can't possibly unmount /usr during shutdown, but if we have a
364 * Requires= from /usr onto a [email protected] unit and that unit is shut down, then
365 * we'd have to unmount /usr too. */
366
367 dep = path_equal(where, "/usr") ? "Wants" : "Requires";
368
369 r = unit_name_from_path_instance("systemd-fsck", what, ".service", &_fsck);
370 if (r < 0)
371 return log_error_errno(r, "Failed to create fsck service name: %m");
372
373 fsck = _fsck;
374 }
375
376 fprintf(f,
377 "%1$s=%2$s\n"
378 "After=%2$s\n",
379 dep, fsck);
380 }
381
382 return 0;
383 }
384
385 int generator_write_device_timeout(
386 const char *dir,
387 const char *what,
388 const char *opts,
389 char **filtered) {
390
391 /* Configure how long we wait for a device that backs a mount point or a
392 * swap partition to show up. This is useful to support endless device timeouts
393 * for devices that show up only after user input, like crypto devices. */
394
395 _cleanup_free_ char *node = NULL, *unit = NULL, *timeout = NULL;
396 usec_t u;
397 int r;
398
399 assert(dir);
400 assert(what);
401
402 r = fstab_filter_options(opts, "comment=systemd.device-timeout\0"
403 "x-systemd.device-timeout\0",
404 NULL, &timeout, NULL, filtered);
405 if (r < 0) {
406 log_warning_errno(r, "Failed to parse fstab options, ignoring: %m");
407 return 0;
408 }
409 if (r == 0)
410 return 0;
411
412 r = parse_sec_fix_0(timeout, &u);
413 if (r < 0) {
414 log_warning("Failed to parse timeout for device '%s', ignoring: %s", what, timeout);
415 return 0;
416 }
417
418 node = fstab_node_to_udev_node(what);
419 if (!node)
420 return log_oom();
421 if (!is_device_path(node)) {
422 log_warning("'%s' is not a device path, ignoring x-systemd.device-timeout= option.", node);
423 return 0;
424 }
425
426 r = unit_name_from_path(node, ".device", &unit);
427 if (r < 0)
428 return log_error_errno(r, "Failed to make unit name from device path '%s': %m", node);
429
430 r = write_drop_in_format(dir, unit, 50, "device-timeout",
431 "# Automatically generated by %s\n"
432 "# from supplied options \"%s\"\n\n"
433 "[Unit]\n"
434 "JobRunningTimeoutSec=%s",
435 program_invocation_short_name,
436 opts,
437 timeout);
438 if (r < 0)
439 return r;
440
441 return 1;
442 }
443
444 int generator_write_unit_timeout(
445 FILE *f,
446 const char *where,
447 const char *opts,
448 const char *filter,
449 const char *unit_setting) {
450
451 _cleanup_free_ char *timeout = NULL;
452 usec_t u;
453 int r;
454
455 assert(f);
456 assert(where);
457 assert(filter);
458 assert(unit_setting);
459
460 r = fstab_filter_options(opts, filter, NULL, &timeout, NULL, NULL);
461 if (r < 0)
462 return log_error_errno(r, "Failed to parse options for '%s': %m", where);
463 if (r == 0)
464 return 0;
465
466 r = parse_sec_fix_0(timeout, &u);
467 if (r < 0) {
468 log_warning_errno(r, "Failed to parse timeout '%s' for '%s', ignoring: %m", timeout, where);
469 return 0;
470 }
471
472 fprintf(f, "%s=%s\n", unit_setting, FORMAT_TIMESPAN(u, 0));
473
474 return 0;
475 }
476
477 int generator_write_network_device_deps(
478 const char *dir,
479 const char *what,
480 const char *where,
481 const char *opts) {
482
483 /* fstab records that specify _netdev option should apply the network
484 * ordering on the actual device depending on network connection. If we
485 * are not mounting real device (NFS, CIFS), we rely on _netdev effect
486 * on the mount unit itself. */
487
488 _cleanup_free_ char *node = NULL, *unit = NULL;
489 int r;
490
491 assert(dir);
492 assert(what);
493
494 if (where && fstab_is_extrinsic(where, opts))
495 return 0;
496
497 if (!fstab_test_option(opts, "_netdev\0"))
498 return 0;
499
500 node = fstab_node_to_udev_node(what);
501 if (!node)
502 return log_oom();
503
504 /* Nothing to apply dependencies to. */
505 if (!is_device_path(node))
506 return 0;
507
508 r = unit_name_from_path(node, ".device", &unit);
509 if (r < 0)
510 return log_error_errno(r, "Failed to make unit name from path \"%s\": %m", node);
511
512 /* See mount_add_default_dependencies for explanation why we create such
513 * dependencies. */
514 return write_drop_in_format(dir, unit, 50, "netdev-dependencies",
515 "# Automatically generated by %s\n\n"
516 "[Unit]\n"
517 "After=" SPECIAL_NETWORK_ONLINE_TARGET " " SPECIAL_NETWORK_TARGET "\n"
518 "Wants=" SPECIAL_NETWORK_ONLINE_TARGET "\n",
519 program_invocation_short_name);
520 }
521
522 int generator_write_initrd_root_device_deps(const char *dir, const char *what) {
523 _cleanup_free_ char *unit = NULL;
524 int r;
525
526 r = unit_name_from_path(what, ".device", &unit);
527 if (r < 0)
528 return log_error_errno(r, "Failed to make unit name from path \"%s\": %m",
529 what);
530
531 return write_drop_in_format(dir, SPECIAL_INITRD_ROOT_DEVICE_TARGET, 50, "root-device",
532 "# Automatically generated by %s\n\n"
533 "[Unit]\n"
534 "Requires=%s\n"
535 "After=%s",
536 program_invocation_short_name,
537 unit,
538 unit);
539 }
540
541 int generator_hook_up_mkswap(
542 const char *dir,
543 const char *what) {
544
545 _cleanup_free_ char *node = NULL, *unit = NULL, *escaped = NULL, *where_unit = NULL;
546 _cleanup_fclose_ FILE *f = NULL;
547 int r;
548
549 assert(dir);
550 assert(what);
551
552 node = fstab_node_to_udev_node(what);
553 if (!node)
554 return log_oom();
555
556 /* Nothing to work on. */
557 if (!is_device_path(node))
558 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
559 "Cannot format something that is not a device node: %s",
560 node);
561
562 r = unit_name_from_path_instance("systemd-mkswap", node, ".service", &unit);
563 if (r < 0)
564 return log_error_errno(r, "Failed to make unit instance name from path \"%s\": %m",
565 node);
566
567 escaped = cescape(node);
568 if (!escaped)
569 return log_oom();
570
571 r = unit_name_from_path(what, ".swap", &where_unit);
572 if (r < 0)
573 return log_error_errno(r, "Failed to make unit name from path \"%s\": %m",
574 what);
575
576 r = generator_open_unit_file(dir, /* source= */ NULL, unit, &f);
577 if (r < 0)
578 return r;
579
580 fprintf(f,
581 "[Unit]\n"
582 "Description=Make Swap on %%f\n"
583 "Documentation=man:[email protected](8)\n"
584 "\n"
585 "DefaultDependencies=no\n"
586 "BindsTo=%%i.device\n"
587 "After=%%i.device\n"
588 "Before=%s\n"
589 "Conflicts=shutdown.target\n"
590 "Before=shutdown.target\n"
591 "\n"
592 "[Service]\n"
593 "Type=oneshot\n"
594 "RemainAfterExit=yes\n"
595 "ExecStart="SYSTEMD_MAKEFS_PATH " swap %s\n"
596 "TimeoutSec=infinity\n",
597 where_unit,
598 escaped);
599
600 r = fflush_and_check(f);
601 if (r < 0)
602 return log_error_errno(r, "Failed to write unit %s: %m", unit);
603
604 return generator_add_symlink(dir, where_unit, "requires", unit);
605 }
606
607 int generator_hook_up_mkfs(
608 const char *dir,
609 const char *what,
610 const char *where,
611 const char *type) {
612
613 _cleanup_free_ char *node = NULL, *unit = NULL, *escaped = NULL, *where_unit = NULL;
614 _cleanup_fclose_ FILE *f = NULL;
615 const char *fsck_unit;
616 int r;
617
618 assert(dir);
619 assert(what);
620 assert(where);
621
622 node = fstab_node_to_udev_node(what);
623 if (!node)
624 return log_oom();
625
626 /* Nothing to work on. */
627 if (!is_device_path(node))
628 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
629 "Cannot format something that is not a device node: %s",
630 node);
631
632 if (!type || streq(type, "auto"))
633 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
634 "Cannot format partition %s, filesystem type is not specified",
635 node);
636
637 r = unit_name_from_path_instance("systemd-makefs", node, ".service", &unit);
638 if (r < 0)
639 return log_error_errno(r, "Failed to make unit instance name from path \"%s\": %m",
640 node);
641
642 if (in_initrd() && path_equal(where, "/sysroot"))
643 fsck_unit = SPECIAL_FSCK_ROOT_SERVICE;
644 else if (in_initrd() && path_equal(where, "/sysusr/usr"))
645 fsck_unit = SPECIAL_FSCK_USR_SERVICE;
646 else
647 fsck_unit = "systemd-fsck@%i.service";
648
649 escaped = cescape(node);
650 if (!escaped)
651 return log_oom();
652
653 r = unit_name_from_path(where, ".mount", &where_unit);
654 if (r < 0)
655 return log_error_errno(r, "Failed to make unit name from path \"%s\": %m",
656 where);
657
658 r = generator_open_unit_file(dir, /* source= */ NULL, unit, &f);
659 if (r < 0)
660 return r;
661
662 fprintf(f,
663 "[Unit]\n"
664 "Description=Make File System on %%f\n"
665 "Documentation=man:[email protected](8)\n"
666 "\n"
667 "DefaultDependencies=no\n"
668 "BindsTo=%%i.device\n"
669 "After=%%i.device\n"
670 /* fsck might or might not be used, so let's be safe and order
671 * ourselves before both [email protected] and the mount unit. */
672 "Before=%s %s\n"
673 "Conflicts=shutdown.target\n"
674 "Before=shutdown.target\n"
675 "\n"
676 "[Service]\n"
677 "Type=oneshot\n"
678 "RemainAfterExit=yes\n"
679 "ExecStart="SYSTEMD_MAKEFS_PATH " %s %s\n"
680 "TimeoutSec=infinity\n",
681 fsck_unit,
682 where_unit,
683 type,
684 escaped);
685 // XXX: what about local-fs-pre.target?
686
687 r = fflush_and_check(f);
688 if (r < 0)
689 return log_error_errno(r, "Failed to write unit %s: %m", unit);
690
691 return generator_add_symlink(dir, where_unit, "requires", unit);
692 }
693
694 int generator_hook_up_growfs(
695 const char *dir,
696 const char *where,
697 const char *target) {
698
699 const char *growfs_unit, *growfs_unit_path;
700 _cleanup_free_ char *where_unit = NULL, *instance = NULL;
701 int r;
702
703 assert(dir);
704 assert(where);
705
706 r = unit_name_from_path(where, ".mount", &where_unit);
707 if (r < 0)
708 return log_error_errno(r, "Failed to make unit name from path '%s': %m", where);
709
710 if (empty_or_root(where)) {
711 growfs_unit = SPECIAL_GROWFS_ROOT_SERVICE;
712 growfs_unit_path = SYSTEM_DATA_UNIT_DIR "/" SPECIAL_GROWFS_ROOT_SERVICE;
713 } else {
714 growfs_unit = SPECIAL_GROWFS_SERVICE;
715 growfs_unit_path = SYSTEM_DATA_UNIT_DIR "/" SPECIAL_GROWFS_SERVICE;
716
717 r = unit_name_path_escape(where, &instance);
718 if (r < 0)
719 return log_error_errno(r, "Failed to escape path '%s': %m", where);
720 }
721
722 if (target) {
723 r = generator_add_ordering(dir, target, "After", growfs_unit, instance);
724 if (r < 0)
725 return r;
726 }
727
728 return generator_add_symlink_full(dir, where_unit, "wants", growfs_unit_path, instance);
729 }
730
731 int generator_hook_up_pcrfs(
732 const char *dir,
733 const char *where,
734 const char *target) {
735
736 const char *pcrfs_unit, *pcrfs_unit_path;
737 _cleanup_free_ char *where_unit = NULL, *instance = NULL;
738 int r;
739
740 assert(dir);
741 assert(where);
742
743 r = unit_name_from_path(where, ".mount", &where_unit);
744 if (r < 0)
745 return log_error_errno(r, "Failed to make unit name from path '%s': %m", where);
746
747 if (empty_or_root(where)) {
748 pcrfs_unit = SPECIAL_PCRFS_ROOT_SERVICE;
749 pcrfs_unit_path = SYSTEM_DATA_UNIT_DIR "/" SPECIAL_PCRFS_ROOT_SERVICE;
750 } else {
751 pcrfs_unit = SPECIAL_PCRFS_SERVICE;
752 pcrfs_unit_path = SYSTEM_DATA_UNIT_DIR "/" SPECIAL_PCRFS_SERVICE;
753
754 r = unit_name_path_escape(where, &instance);
755 if (r < 0)
756 return log_error_errno(r, "Failed to escape path '%s': %m", where);
757 }
758
759 if (target) {
760 r = generator_add_ordering(dir, target, "After", pcrfs_unit, instance);
761 if (r < 0)
762 return r;
763 }
764
765 return generator_add_symlink_full(dir, where_unit, "wants", pcrfs_unit_path, instance);
766 }
767
768 int generator_hook_up_validatefs(
769 const char *dir,
770 const char *where,
771 const char *target) {
772
773 _cleanup_free_ char *where_unit = NULL, *instance = NULL;
774 const char *validatefs_unit, *validatefs_unit_path;
775 int r;
776
777 assert(dir);
778 assert(where);
779
780 /* never hook this in for the actual root fs, because it's too late then, we already are running from
781 * the root fs, it makes no sense to validate it anymore */
782 if (empty_or_root(where))
783 return 0;
784
785 r = unit_name_from_path(where, ".mount", &where_unit);
786 if (r < 0)
787 return log_error_errno(r, "Failed to make unit name from path '%s': %m", where);
788
789 validatefs_unit = SPECIAL_VALIDATEFS_SERVICE;
790 validatefs_unit_path = SYSTEM_DATA_UNIT_DIR "/" SPECIAL_VALIDATEFS_SERVICE;
791
792 r = unit_name_path_escape(where, &instance);
793 if (r < 0)
794 return log_error_errno(r, "Failed to escape path '%s': %m", where);
795
796 if (target) {
797 r = generator_add_ordering(dir, target, "After", validatefs_unit, instance);
798 if (r < 0)
799 return r;
800 }
801
802 return generator_add_symlink_full(dir, where_unit, "wants", validatefs_unit_path, instance);
803 }
804
805 int generator_hook_up_quotacheck(
806 const char *dir,
807 const char *what,
808 const char *where,
809 const char *target,
810 const char *fstype) {
811
812 _cleanup_free_ char *where_unit = NULL, *instance = NULL;
813 int r;
814
815 assert(dir);
816 assert(where);
817
818 if (isempty(fstype) || streq(fstype, "auto"))
819 return log_warning_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Couldn't determine filesystem type for %s, quota cannot be activated", what);
820 if (fstype_has_internal_quota(fstype)) {
821 log_debug("%s handles quotas internally, skipping quotacheck/quotaon setup for %s", fstype, what);
822 return 0;
823 }
824 if (!fstype_needs_quota(fstype))
825 return log_warning_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Quota was requested for %s, but not supported, ignoring: %s", what, fstype);
826
827 /* quotacheck unit for system root */
828 if (path_equal(where, "/")) {
829 r = generator_add_symlink(dir, SPECIAL_LOCAL_FS_TARGET, "wants", SYSTEM_DATA_UNIT_DIR "/" SPECIAL_QUOTACHECK_ROOT_SERVICE);
830 return r < 0 ? r : 1;
831 }
832
833 r = unit_name_path_escape(where, &instance);
834 if (r < 0)
835 return log_error_errno(r, "Failed to escape path '%s': %m", where);
836
837 if (target) {
838 r = generator_add_ordering(dir, target, "After", SPECIAL_QUOTACHECK_SERVICE, instance);
839 if (r < 0)
840 return r;
841 }
842
843 r = unit_name_from_path(where, ".mount", &where_unit);
844 if (r < 0)
845 return log_error_errno(r, "Failed to make unit name from path '%s': %m", where);
846
847 r = generator_add_symlink_full(dir, where_unit, "wants", SYSTEM_DATA_UNIT_DIR "/" SPECIAL_QUOTACHECK_SERVICE, instance);
848 return r < 0 ? r : 1;
849 }
850
851 int generator_hook_up_quotaon(
852 const char *dir,
853 const char *where,
854 const char *target) {
855
856 _cleanup_free_ char *where_unit = NULL, *instance = NULL;
857 int r;
858
859 assert(dir);
860 assert(where);
861
862 /* quotaon unit for system root is not instantiated */
863 if (path_equal(where, "/"))
864 return generator_add_symlink(dir, SPECIAL_LOCAL_FS_TARGET, "wants", SYSTEM_DATA_UNIT_DIR "/" SPECIAL_QUOTAON_ROOT_SERVICE);
865
866 r = unit_name_path_escape(where, &instance);
867 if (r < 0)
868 return log_error_errno(r, "Failed to escape path '%s': %m", where);
869
870 if (target) {
871 r = generator_add_ordering(dir, target, "After", SPECIAL_QUOTAON_SERVICE, instance);
872 if (r < 0)
873 return r;
874 }
875
876 r = unit_name_from_path(where, ".mount", &where_unit);
877 if (r < 0)
878 return log_error_errno(r, "Failed to make unit name from path '%s': %m", where);
879
880 return generator_add_symlink_full(dir, where_unit, "wants", SYSTEM_DATA_UNIT_DIR "/" SPECIAL_QUOTAON_SERVICE, instance);
881 }
882
883 int generator_enable_remount_fs_service(const char *dir) {
884 /* Pull in systemd-remount-fs.service */
885 return generator_add_symlink(dir, SPECIAL_LOCAL_FS_TARGET, "wants",
886 SYSTEM_DATA_UNIT_DIR "/" SPECIAL_REMOUNT_FS_SERVICE);
887 }
888
889 int generator_write_blockdev_dependency(FILE *f, const char *what) {
890 _cleanup_free_ char *escaped = NULL;
891 int r;
892
893 assert(f);
894 assert(what);
895
896 if (!path_startswith(what, "/dev/"))
897 return 0;
898
899 r = unit_name_path_escape(what, &escaped);
900 if (r < 0)
901 return log_error_errno(r, "Failed to escape device node path %s: %m", what);
902
903 fprintf(f,
904 "After=blockdev@%s.target\n",
905 escaped);
906
907 return 0;
908 }
909
910 int generator_write_cryptsetup_unit_section(FILE *f, const char *source) {
911 assert(f);
912
913 fprintf(f,
914 "[Unit]\n"
915 "Description=Cryptography Setup for %%I\n"
916 "Documentation=man:crypttab(5) man:systemd-cryptsetup-generator(8) man:[email protected](8)\n");
917
918 if (source)
919 fprintf(f, "SourcePath=%s\n", source);
920
921 fprintf(f,
922 "\n"
923 "DefaultDependencies=no\n"
924 "After=cryptsetup-pre.target systemd-udevd-kernel.socket systemd-tpm2-setup-early.service\n"
925 "Before=blockdev@dev-mapper-%%i.target\n"
926 "Wants=blockdev@dev-mapper-%%i.target\n"
927 "IgnoreOnIsolate=true\n");
928
929 return 0;
930 }
931
932 int generator_write_cryptsetup_service_section(
933 FILE *f,
934 const char *name,
935 const char *what,
936 const char *key_file,
937 const char *options) {
938
939 _cleanup_free_ char *name_escaped = NULL, *what_escaped = NULL, *key_file_escaped = NULL, *options_escaped = NULL;
940
941 assert(f);
942 assert(name);
943 assert(what);
944
945 name_escaped = specifier_escape(name);
946 if (!name_escaped)
947 return log_oom();
948
949 what_escaped = specifier_escape(what);
950 if (!what_escaped)
951 return log_oom();
952
953 if (key_file) {
954 key_file_escaped = specifier_escape(key_file);
955 if (!key_file_escaped)
956 return log_oom();
957 }
958
959 if (options) {
960 options_escaped = specifier_escape(options);
961 if (!options_escaped)
962 return log_oom();
963 }
964
965 fprintf(f,
966 "\n"
967 "[Service]\n"
968 "Type=oneshot\n"
969 "RemainAfterExit=yes\n"
970 "TimeoutSec=infinity\n" /* The binary handles timeouts on its own */
971 "KeyringMode=shared\n" /* Make sure we can share cached keys among instances */
972 "OOMScoreAdjust=500\n" /* Unlocking can allocate a lot of memory if Argon2 is used */
973 "ImportCredential=cryptsetup.*\n"
974 "ExecStart=" SYSTEMD_CRYPTSETUP_PATH " attach '%s' '%s' '%s' '%s'\n"
975 "ExecStop=" SYSTEMD_CRYPTSETUP_PATH " detach '%s'\n",
976 name_escaped, what_escaped, strempty(key_file_escaped), strempty(options_escaped),
977 name_escaped);
978
979 return 0;
980 }
981
982 int generator_write_veritysetup_unit_section(FILE *f, const char *source) {
983 assert(f);
984
985 fprintf(f,
986 "[Unit]\n"
987 "Description=Integrity Protection Setup for %%I\n"
988 "Documentation=man:veritytab(5) man:systemd-veritysetup-generator(8) man:[email protected](8)\n");
989
990 if (source)
991 fprintf(f, "SourcePath=%s\n", source);
992
993 fprintf(f,
994 "DefaultDependencies=no\n"
995 "IgnoreOnIsolate=true\n"
996 "After=veritysetup-pre.target systemd-udevd-kernel.socket\n"
997 "Before=blockdev@dev-mapper-%%i.target\n"
998 "Wants=blockdev@dev-mapper-%%i.target\n");
999
1000 return 0;
1001 }
1002
1003 int generator_write_veritysetup_service_section(
1004 FILE *f,
1005 const char *name,
1006 const char *data_what,
1007 const char *hash_what,
1008 const char *roothash,
1009 const char *options) {
1010
1011 _cleanup_free_ char *name_escaped = NULL, *data_what_escaped = NULL, *hash_what_escaped = NULL,
1012 *roothash_escaped = NULL, *options_escaped = NULL;
1013
1014 assert(f);
1015 assert(name);
1016 assert(data_what);
1017 assert(hash_what);
1018
1019 name_escaped = specifier_escape(name);
1020 if (!name_escaped)
1021 return log_oom();
1022
1023 data_what_escaped = specifier_escape(data_what);
1024 if (!data_what_escaped)
1025 return log_oom();
1026
1027 hash_what_escaped = specifier_escape(hash_what);
1028 if (!hash_what_escaped)
1029 return log_oom();
1030
1031 if (roothash) {
1032 roothash_escaped = specifier_escape(roothash);
1033 if (!roothash_escaped)
1034 return log_oom();
1035 }
1036
1037 if (options) {
1038 options_escaped = specifier_escape(options);
1039 if (!options_escaped)
1040 return log_oom();
1041 }
1042
1043 fprintf(f,
1044 "\n"
1045 "[Service]\n"
1046 "Type=oneshot\n"
1047 "RemainAfterExit=yes\n"
1048 "ExecStart=" SYSTEMD_VERITYSETUP_PATH " attach '%s' '%s' '%s' '%s' '%s'\n"
1049 "ExecStop=" SYSTEMD_VERITYSETUP_PATH " detach '%s'\n",
1050 name_escaped, data_what_escaped, hash_what_escaped, empty_to_dash(roothash_escaped), strempty(options_escaped),
1051 name_escaped);
1052
1053 return 0;
1054 }
1055
1056 void log_setup_generator(void) {
1057 if (invoked_by_systemd()) {
1058 /* Disable talking to syslog/journal (i.e. the two IPC-based loggers) if we run in system context. */
1059 if (streq_ptr(getenv("SYSTEMD_SCOPE"), "system"))
1060 log_set_prohibit_ipc(true);
1061
1062 /* This effectively means: journal for per-user service manager generators, kmsg for per-system service manager generators */
1063 log_set_target(LOG_TARGET_JOURNAL_OR_KMSG);
1064 } else
1065 log_set_target(LOG_TARGET_AUTO);
1066
1067 log_parse_environment();
1068 log_open();
1069 }
1070
1071 bool generator_soft_rebooted(void) {
1072 static int cached = -1;
1073 int r;
1074
1075 if (cached >= 0)
1076 return cached;
1077
1078 const char *e = secure_getenv("SYSTEMD_SOFT_REBOOTS_COUNT");
1079 if (!e)
1080 return (cached = false);
1081
1082 unsigned u;
1083
1084 r = safe_atou(e, &u);
1085 if (r < 0) {
1086 log_debug_errno(r, "Failed to parse $SYSTEMD_SOFT_REBOOTS_COUNT, assuming the system hasn't soft-rebooted: %m");
1087 return (cached = false);
1088 }
1089
1090 return (cached = (u > 0));
1091 }
1092
1093 GptAutoRoot parse_gpt_auto_root(const char *switch_name, const char *value) {
1094 assert(switch_name);
1095 assert(value);
1096
1097 /* Parses the 'gpt-auto'/'gpt-auto-root'/'dissect'/'dissect-force' parameters to root=
1098 *
1099 * note that we are not using a regular string table here, because the mode names don't fully match
1100 * the parameter names. And root= being something else is not an error. */
1101
1102 if (streq(value, "gpt-auto")) {
1103 log_debug("Enabling partition auto-detection (respecting factory reset mode), %s is explicitly set to 'gpt-auto'.", switch_name);
1104 return GPT_AUTO_ROOT_ON;
1105 }
1106
1107 if (streq(value, "gpt-auto-force")) {
1108 log_debug("Enabling partition auto-detection (ignoring factory reset mode), %s is explicitly set to 'gpt-auto-force'.", switch_name);
1109 return GPT_AUTO_ROOT_FORCE;
1110 }
1111
1112 if (streq(value, "dissect")) {
1113 log_debug("Enabling partition auto-detection via full image dissection (respecting factory reset mode), %s is explicitly set to 'dissect'.", switch_name);
1114 return GPT_AUTO_ROOT_DISSECT;
1115 }
1116
1117 if (streq(value, "dissect-force")) {
1118 log_debug("Enabling partition auto-detection via full image dissection (ignoring factory reset mode), %s is explicitly set to 'dissect-force'.", switch_name);
1119 return GPT_AUTO_ROOT_DISSECT_FORCE;
1120 }
1121
1122 if (streq(value, "off"))
1123 log_debug("Disabling partition auto-detection, %s handling is explicitly turned off.", switch_name);
1124 else
1125 log_debug("Disabling partition auto-detection, %s is neither unset, nor set to 'gpt-auto', 'gpt-auto-force', 'dissect' or 'dissect-force'.", switch_name);
1126
1127 return GPT_AUTO_ROOT_OFF;
1128 }