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