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