chiark / gitweb /
util: rework strappenda(), and rename it strjoina()
[elogind.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 <string.h>
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"
30 #include "path-util.h"
31 #include "fstab-util.h"
32 #include "dropin.h"
33
34 int generator_write_fsck_deps(
35                 FILE *f,
36                 const char *dest,
37                 const char *what,
38                 const char *where,
39                 const char *fstype) {
40
41         assert(f);
42         assert(dest);
43         assert(what);
44         assert(where);
45
46         if (!is_device_path(what)) {
47                 log_warning("Checking was requested for \"%s\", but it is not a device.", what);
48                 return 0;
49         }
50
51         if (!isempty(fstype) && !streq(fstype, "auto")) {
52                 int r;
53                 r = fsck_exists(fstype);
54                 if (r == -ENOENT) {
55                         /* treat missing check as essentially OK */
56                         log_debug_errno(r, "Checking was requested for %s, but fsck.%s does not exist: %m", what, fstype);
57                         return 0;
58                 } else if (r < 0)
59                         return log_warning_errno(r, "Checking was requested for %s, but fsck.%s cannot be used: %m", what, fstype);
60         }
61
62         if (streq(where, "/")) {
63                 char *lnk;
64
65                 lnk = strjoina(dest, "/" SPECIAL_LOCAL_FS_TARGET ".wants/systemd-fsck-root.service");
66
67                 mkdir_parents(lnk, 0755);
68                 if (symlink(SYSTEM_DATA_UNIT_PATH "/systemd-fsck-root.service", lnk) < 0)
69                         return log_error_errno(errno, "Failed to create symlink %s: %m", lnk);
70
71         } else {
72                 _cleanup_free_ char *fsck = NULL;
73
74                 fsck = unit_name_from_path_instance("systemd-fsck", what, ".service");
75                 if (!fsck)
76                         return log_oom();
77
78                 fprintf(f,
79                         "RequiresOverridable=%s\n"
80                         "After=%s\n",
81                         fsck,
82                         fsck);
83         }
84
85         return 0;
86 }
87
88 int generator_write_timeouts(const char *dir, const char *what, const char *where,
89                              const char *opts, char **filtered) {
90
91         /* Allow configuration how long we wait for a device that
92          * backs a mount point to show up. This is useful to support
93          * endless device timeouts for devices that show up only after
94          * user input, like crypto devices. */
95
96         _cleanup_free_ char *node = NULL, *unit = NULL, *timeout = NULL;
97         usec_t u;
98         int r;
99
100         r = fstab_filter_options(opts, "comment=systemd.device-timeout\0" "x-systemd.device-timeout\0",
101                                  NULL, &timeout, filtered);
102         if (r <= 0)
103                 return r;
104
105         r = parse_sec(timeout, &u);
106         if (r < 0) {
107                 log_warning("Failed to parse timeout for %s, ignoring: %s",
108                             where, timeout);
109                 return 0;
110         }
111
112         node = fstab_node_to_udev_node(what);
113         if (!node)
114                 return log_oom();
115
116         unit = unit_name_from_path(node, ".device");
117         if (!unit)
118                 return log_oom();
119
120         return write_drop_in_format(dir, unit, 50, "device-timeout",
121                                     "# Automatically generated by %s\n\n"
122                                     "[Unit]\nJobTimeoutSec=" USEC_FMT,
123                                     program_invocation_short_name,
124                                     u / USEC_PER_SEC);
125 }