]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/generator.c
Move x-systemd-device.timeout handling from core to fstab-generator
[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
eb66db55 22#include <string.h>
e48fdd84
LP
23#include <unistd.h>
24
25#include "util.h"
26#include "special.h"
27#include "mkdir.h"
28#include "unit-name.h"
29#include "generator.h"
eb66db55 30#include "path-util.h"
29686440 31#include "dropin.h"
e48fdd84
LP
32
33int generator_write_fsck_deps(
34 FILE *f,
35 const char *dest,
36 const char *what,
37 const char *where,
6db615c1 38 const char *fstype) {
e48fdd84
LP
39
40 assert(f);
6db615c1
LP
41 assert(dest);
42 assert(what);
43 assert(where);
e48fdd84
LP
44
45 if (!is_device_path(what)) {
46 log_warning("Checking was requested for \"%s\", but it is not a device.", what);
47 return 0;
48 }
49
6db615c1 50 if (!isempty(fstype) && !streq(fstype, "auto")) {
e48fdd84 51 int r;
eb66db55 52 r = fsck_exists(fstype);
571d0134 53 if (r == -ENOENT) {
e48fdd84 54 /* treat missing check as essentially OK */
571d0134
LP
55 log_debug("Checking was requested for %s, but fsck.%s does not exist: %s", what, fstype, strerror(-r));
56 return 0;
57 } else if (r < 0) {
58 log_warning("Checking was requested for %s, but fsck.%s cannot be used: %s", what, fstype, strerror(-r));
59 return r;
e48fdd84
LP
60 }
61 }
62
63 if (streq(where, "/")) {
64 char *lnk;
65
66 lnk = strappenda(dest, "/" SPECIAL_LOCAL_FS_TARGET ".wants/systemd-fsck-root.service");
67
68 mkdir_parents(lnk, 0755);
69 if (symlink(SYSTEM_DATA_UNIT_PATH "/systemd-fsck-root.service", lnk) < 0) {
70 log_error("Failed to create symlink %s: %m", lnk);
71 return -errno;
72 }
73
74 } else {
75 _cleanup_free_ char *fsck = NULL;
76
77 fsck = unit_name_from_path_instance("systemd-fsck", what, ".service");
78 if (!fsck)
79 return log_oom();
80
81 fprintf(f,
82 "RequiresOverridable=%s\n"
83 "After=%s\n",
84 fsck,
85 fsck);
86 }
87
88 return 0;
89}
29686440
ZJS
90
91int generator_write_timeouts(const char *dir, const char *what, const char *where,
92 const char *opts, char **filtered) {
93
94 /* Allow configuration how long we wait for a device that
95 * backs a mount point to show up. This is useful to support
96 * endless device timeouts for devices that show up only after
97 * user input, like crypto devices. */
98
99 _cleanup_free_ char *node = NULL, *unit = NULL, *t = NULL;
100 char *prefix, *start, *timeout, *postfix;
101 usec_t u;
102 int r;
103 size_t len;
104
105 if ((start = mount_test_option(opts, "comment=systemd.device-timeout")))
106 timeout = start + 31;
107 else if ((start = mount_test_option(opts, "x-systemd.device-timeout")))
108 timeout = start + 25;
109 else {
110 *filtered = strdup(opts);
111 if (!*filtered)
112 return log_oom();
113
114 return 0;
115 }
116
117 len = strcspn(timeout, ",;" WHITESPACE);
118 t = strndup(timeout, len);
119 if (!t)
120 return -ENOMEM;
121
122 prefix = strndupa(opts, start - opts - (start != opts));
123 postfix = timeout + len + (timeout[len] != '\0');
124 *filtered = strjoin(prefix, *postfix ? postfix : NULL, NULL);
125 if (!*filtered)
126 return log_oom();
127
128 r = parse_sec(t, &u);
129 if (r < 0) {
130 log_warning("Failed to parse timeout for %s, ignoring: %s",
131 where, timeout);
132 return 0;
133 }
134
135 node = fstab_node_to_udev_node(what);
136 if (!node)
137 return log_oom();
138
139 unit = unit_name_from_path(node, ".device");
140 if (!unit)
141 return -ENOMEM;
142
143 return write_drop_in_format(dir, unit, "device-timeout",
144 "[Unit]\nJobTimeoutSec=%u", u / USEC_PER_SEC);
145}