chiark / gitweb /
fstab-generator: allow x-systemd.device-timeout for swap units
[elogind.git] / src / shared / dropin.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 Zbigniew JÄ™drzejewski-Szmek
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 "dropin.h"
23 #include "util.h"
24 #include "mkdir.h"
25 #include "fileio-label.h"
26
27 int drop_in_file(const char *dir, const char *unit,
28                  const char *name, char **_p, char **_q) {
29
30         _cleanup_free_ char *b = NULL;
31         char *p, *q;
32
33         assert(unit);
34         assert(name);
35         assert(_p);
36         assert(_q);
37
38         b = xescape(name, "/.");
39         if (!b)
40                 return -ENOMEM;
41
42         if (!filename_is_safe(b))
43                 return -EINVAL;
44
45         p = strjoin(dir, "/", unit, ".d", NULL);
46         if (!p)
47                 return -ENOMEM;
48
49         q = strjoin(p, "/90-", b, ".conf", NULL);
50         if (!q) {
51                 free(p);
52                 return -ENOMEM;
53         }
54
55         *_p = p;
56         *_q = q;
57         return 0;
58 }
59
60 int write_drop_in(const char *dir, const char *unit,
61                   const char *name, const char *data) {
62
63         _cleanup_free_ char *p = NULL, *q = NULL;
64         int r;
65
66         assert(dir);
67         assert(unit);
68         assert(name);
69         assert(data);
70
71         r = drop_in_file(dir, unit, name, &p, &q);
72         if (r < 0)
73                 return r;
74
75         mkdir_p(p, 0755);
76         return write_string_file_atomic_label(q, data);
77 }
78
79 int write_drop_in_format(const char *dir, const char *unit,
80                          const char *name, const char *format, ...) {
81         _cleanup_free_ char *p = NULL;
82         va_list ap;
83         int r;
84
85         assert(dir);
86         assert(unit);
87         assert(name);
88         assert(format);
89
90         va_start(ap, format);
91         r = vasprintf(&p, format, ap);
92         va_end(ap);
93
94         if (r < 0)
95                 return -ENOMEM;
96
97         return write_drop_in(dir, unit, name, p);
98 }