]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/generator.c
Merge pull request #2267 from gdamjan/dont-drop-critical
[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 <errno.h>
23 #include <unistd.h>
24
25 #include "alloc-util.h"
26 #include "dropin.h"
27 #include "escape.h"
28 #include "fd-util.h"
29 #include "fileio.h"
30 #include "fstab-util.h"
31 #include "generator.h"
32 #include "log.h"
33 #include "macro.h"
34 #include "mkdir.h"
35 #include "path-util.h"
36 #include "special.h"
37 #include "string-util.h"
38 #include "time-util.h"
39 #include "unit-name.h"
40 #include "util.h"
41
42 static int write_fsck_sysroot_service(const char *dir, const char *what) {
43 _cleanup_free_ char *device = NULL, *escaped = NULL;
44 _cleanup_fclose_ FILE *f = NULL;
45 const char *unit;
46 int r;
47
48 escaped = cescape(what);
49 if (!escaped)
50 return log_oom();
51
52 unit = strjoina(dir, "/systemd-fsck-root.service");
53 log_debug("Creating %s", unit);
54
55 r = unit_name_from_path(what, ".device", &device);
56 if (r < 0)
57 return log_error_errno(r, "Failed to convert device \"%s\" to unit name: %m", what);
58
59 f = fopen(unit, "wxe");
60 if (!f)
61 return log_error_errno(errno, "Failed to create unit file %s: %m", unit);
62
63 fprintf(f,
64 "# Automatically generated by %1$s\n\n"
65 "[Unit]\n"
66 "Documentation=man:systemd-fsck-root.service(8)\n"
67 "Description=File System Check on %2$s\n"
68 "DefaultDependencies=no\n"
69 "BindsTo=%3$s\n"
70 "After=%3$s local-fs-pre.target\n"
71 "Before=shutdown.target\n"
72 "\n"
73 "[Service]\n"
74 "Type=oneshot\n"
75 "RemainAfterExit=yes\n"
76 "ExecStart=" SYSTEMD_FSCK_PATH " %4$s\n"
77 "TimeoutSec=0\n",
78 program_invocation_short_name,
79 what,
80 device,
81 escaped);
82
83 r = fflush_and_check(f);
84 if (r < 0)
85 return log_error_errno(r, "Failed to write unit file %s: %m", unit);
86
87 return 0;
88 }
89
90 int generator_write_fsck_deps(
91 FILE *f,
92 const char *dir,
93 const char *what,
94 const char *where,
95 const char *fstype) {
96
97 int r;
98
99 assert(f);
100 assert(dir);
101 assert(what);
102 assert(where);
103
104 if (!is_device_path(what)) {
105 log_warning("Checking was requested for \"%s\", but it is not a device.", what);
106 return 0;
107 }
108
109 if (!isempty(fstype) && !streq(fstype, "auto")) {
110 r = fsck_exists(fstype);
111 if (r < 0)
112 log_warning_errno(r, "Checking was requested for %s, but couldn't detect if fsck.%s may be used, proceeding: %m", what, fstype);
113 else if (r == 0) {
114 /* treat missing check as essentially OK */
115 log_debug("Checking was requested for %s, but fsck.%s does not exist.", what, fstype);
116 return 0;
117 }
118 }
119
120 if (path_equal(where, "/")) {
121 const char *lnk;
122
123 lnk = strjoina(dir, "/" SPECIAL_LOCAL_FS_TARGET ".wants/systemd-fsck-root.service");
124
125 mkdir_parents(lnk, 0755);
126 if (symlink(SYSTEM_DATA_UNIT_PATH "/systemd-fsck-root.service", lnk) < 0)
127 return log_error_errno(errno, "Failed to create symlink %s: %m", lnk);
128
129 } else {
130 _cleanup_free_ char *_fsck = NULL;
131 const char *fsck;
132
133 if (in_initrd() && path_equal(where, "/sysroot")) {
134 r = write_fsck_sysroot_service(dir, what);
135 if (r < 0)
136 return r;
137
138 fsck = "systemd-fsck-root.service";
139 } else {
140 r = unit_name_from_path_instance("systemd-fsck", what, ".service", &_fsck);
141 if (r < 0)
142 return log_error_errno(r, "Failed to create fsck service name: %m");
143
144 fsck = _fsck;
145 }
146
147 fprintf(f,
148 "Requires=%1$s\n"
149 "After=%1$s\n",
150 fsck);
151 }
152
153 return 0;
154 }
155
156 int generator_write_timeouts(
157 const char *dir,
158 const char *what,
159 const char *where,
160 const char *opts,
161 char **filtered) {
162
163 /* Allow configuration how long we wait for a device that
164 * backs a mount point to show up. This is useful to support
165 * endless device timeouts for devices that show up only after
166 * user input, like crypto devices. */
167
168 _cleanup_free_ char *node = NULL, *unit = NULL, *timeout = NULL;
169 usec_t u;
170 int r;
171
172 r = fstab_filter_options(opts, "comment=systemd.device-timeout\0" "x-systemd.device-timeout\0",
173 NULL, &timeout, filtered);
174 if (r <= 0)
175 return r;
176
177 r = parse_sec(timeout, &u);
178 if (r < 0) {
179 log_warning("Failed to parse timeout for %s, ignoring: %s", where, timeout);
180 return 0;
181 }
182
183 node = fstab_node_to_udev_node(what);
184 if (!node)
185 return log_oom();
186
187 r = unit_name_from_path(node, ".device", &unit);
188 if (r < 0)
189 return log_error_errno(r, "Failed to make unit name from path: %m");
190
191 return write_drop_in_format(dir, unit, 50, "device-timeout",
192 "# Automatically generated by %s\n\n"
193 "[Unit]\nJobTimeoutSec=%s",
194 program_invocation_short_name, timeout);
195 }