]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/generator.c
treewide: use log_*_errno whenever %m is in the format string
[thirdparty/systemd.git] / src / shared / generator.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2014 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <string.h>
23 #include <unistd.h>
24
25 #include "util.h"
26 #include "special.h"
27 #include "mkdir.h"
28 #include "unit-name.h"
29 #include "generator.h"
30 #include "path-util.h"
31 #include "dropin.h"
32
33 int generator_write_fsck_deps(
34 FILE *f,
35 const char *dest,
36 const char *what,
37 const char *where,
38 const char *fstype) {
39
40 assert(f);
41 assert(dest);
42 assert(what);
43 assert(where);
44
45 if (!is_device_path(what)) {
46 log_warning("Checking was requested for \"%s\", but it is not a device.", what);
47 return 0;
48 }
49
50 if (!isempty(fstype) && !streq(fstype, "auto")) {
51 int r;
52 r = fsck_exists(fstype);
53 if (r == -ENOENT) {
54 /* treat missing check as essentially OK */
55 log_debug_errno(r, "Checking was requested for %s, but fsck.%s does not exist: %m", what, fstype);
56 return 0;
57 } else if (r < 0)
58 return log_warning_errno(r, "Checking was requested for %s, but fsck.%s cannot be used: %m", what, fstype);
59 }
60
61 if (streq(where, "/")) {
62 char *lnk;
63
64 lnk = strappenda(dest, "/" SPECIAL_LOCAL_FS_TARGET ".wants/systemd-fsck-root.service");
65
66 mkdir_parents(lnk, 0755);
67 if (symlink(SYSTEM_DATA_UNIT_PATH "/systemd-fsck-root.service", lnk) < 0) {
68 log_error_errno(errno, "Failed to create symlink %s: %m", lnk);
69 return -errno;
70 }
71
72 } else {
73 _cleanup_free_ char *fsck = NULL;
74
75 fsck = unit_name_from_path_instance("systemd-fsck", what, ".service");
76 if (!fsck)
77 return log_oom();
78
79 fprintf(f,
80 "RequiresOverridable=%s\n"
81 "After=%s\n",
82 fsck,
83 fsck);
84 }
85
86 return 0;
87 }
88
89 int generator_write_timeouts(const char *dir, const char *what, const char *where,
90 const char *opts, char **filtered) {
91
92 /* Allow configuration how long we wait for a device that
93 * backs a mount point to show up. This is useful to support
94 * endless device timeouts for devices that show up only after
95 * user input, like crypto devices. */
96
97 _cleanup_free_ char *node = NULL, *unit = NULL, *t = NULL;
98 char *start, *timeout;
99 usec_t u;
100 int r;
101 size_t len;
102
103 if ((start = mount_test_option(opts, "comment=systemd.device-timeout")))
104 timeout = start + 31;
105 else if ((start = mount_test_option(opts, "x-systemd.device-timeout")))
106 timeout = start + 25;
107 else {
108 if (filtered) {
109 *filtered = strdup(opts ?: "");
110 if (!*filtered)
111 return log_oom();
112 }
113
114 return 0;
115 }
116
117 len = strcspn(timeout, ",;" WHITESPACE);
118 t = strndup(timeout, len);
119 if (!t)
120 return -ENOMEM;
121
122 if (filtered) {
123 char *prefix, *postfix;
124
125 prefix = strndupa(opts, start - opts - (start != opts));
126 postfix = timeout + len + (start == opts && timeout[len] != '\0');
127 *filtered = strjoin(prefix, *postfix ? postfix : NULL, NULL);
128 if (!*filtered)
129 return log_oom();
130 }
131
132 r = parse_sec(t, &u);
133 if (r < 0) {
134 log_warning("Failed to parse timeout for %s, ignoring: %s",
135 where, timeout);
136 return 0;
137 }
138
139 node = fstab_node_to_udev_node(what);
140 if (!node)
141 return log_oom();
142
143 unit = unit_name_from_path(node, ".device");
144 if (!unit)
145 return -ENOMEM;
146
147 return write_drop_in_format(dir, unit, 50, "device-timeout",
148 "# Automatically generated by %s\n\n"
149 "[Unit]\nJobTimeoutSec=" USEC_FMT,
150 program_invocation_short_name,
151 u / USEC_PER_SEC);
152 }