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