chiark / gitweb /
2dc34bf738a55e23aeacf509e5506ddeb7b84f65
[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 <unistd.h>
23
24 #include "util.h"
25 #include "special.h"
26 #include "mkdir.h"
27 #include "unit-name.h"
28 #include "generator.h"
29 #include "path-util.h"
30 #include "fstab-util.h"
31 #include "dropin.h"
32
33 int generator_write_fsck_deps(
34                 FILE *f,
35                 const char *dir,
36                 const char *what,
37                 const char *where,
38                 const char *fstype) {
39
40         int r;
41
42         assert(f);
43         assert(dir);
44         assert(what);
45         assert(where);
46
47         if (!is_device_path(what)) {
48                 log_warning("Checking was requested for \"%s\", but it is not a device.", what);
49                 return 0;
50         }
51
52         if (!isempty(fstype) && !streq(fstype, "auto")) {
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 (path_equal(where, "/")) {
63                 char *lnk;
64
65                 lnk = strjoina(dir, "/" 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                 r = unit_name_from_path_instance("systemd-fsck", what, ".service", &fsck);
75                 if (r < 0)
76                         return log_error_errno(r, "Failed to create fsck service name: %m");
77
78                 fprintf(f,
79                         "RequiresOverridable=%1$s\n"
80                         "After=%1$s\n",
81                         fsck);
82         }
83
84         return 0;
85 }
86
87 int generator_write_timeouts(
88                 const char *dir,
89                 const char *what,
90                 const char *where,
91                 const char *opts,
92                 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, *timeout = NULL;
100         usec_t u;
101         int r;
102
103         r = fstab_filter_options(opts, "comment=systemd.device-timeout\0" "x-systemd.device-timeout\0",
104                                  NULL, &timeout, filtered);
105         if (r <= 0)
106                 return r;
107
108         r = parse_sec(timeout, &u);
109         if (r < 0) {
110                 log_warning("Failed to parse timeout for %s, ignoring: %s", where, timeout);
111                 return 0;
112         }
113
114         node = fstab_node_to_udev_node(what);
115         if (!node)
116                 return log_oom();
117
118         r = unit_name_from_path(node, ".device", &unit);
119         if (r < 0)
120                 return log_error_errno(r, "Failed to make unit name from path: %m");
121
122         return write_drop_in_format(dir, unit, 50, "device-timeout",
123                                     "# Automatically generated by %s\n\n"
124                                     "[Unit]\nJobTimeoutSec=" USEC_FMT,
125                                     program_invocation_short_name,
126                                     u / USEC_PER_SEC);
127 }