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