1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
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 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
37 /* This reads all files listed in /etc/tempfiles.d/?*.conf and creates
38 * them in the file system. This is intended to be used to create
39 * properly owned directories beneath /tmp, /var/tmp, /var/run and
40 * /var/lock which are volatile and hence need to be recreated on
43 static void process_line(const char *fname, unsigned line, const char *buffer, const char *prefix) {
47 char *user = NULL, *group = NULL;
50 bool uid_set = false, gid_set = false;
57 if ((n = sscanf(buffer,
68 log_error("[%s:%u] Syntax error.", fname, line);
72 if (type != 'f' && type != 'd') {
73 log_error("[%s:%u] Unknown file type '%c'.", fname, line, type);
77 if (prefix && !path_startswith(path, prefix))
80 if (user && !streq(user, "-")) {
84 if (streq(user, "root") || streq(user, "0"))
86 else if (safe_atolu(user, &lu) >= 0)
88 else if ((p = getpwnam(user)))
91 log_error("[%s:%u] Unknown user '%s'.", fname, line, user);
98 if (group && !streq(group, "-")) {
102 if (streq(group, "root") || streq(group, "0"))
104 else if (safe_atolu(group, &lu) >= 0)
106 else if ((g = getgrnam(group)))
109 log_error("[%s:%u] Unknown group '%s'.", fname, line, group);
117 mode = type == 'f' ? 0644 : 0755;
124 fd = open(path, O_CREAT|O_NDELAY|O_CLOEXEC|O_WRONLY|O_NOCTTY|O_NOFOLLOW, mode);
128 log_error("Failed to create file %s: %m", path);
132 if (fstat(fd, &st) < 0) {
133 log_error("stat(%s) failed: %m", path);
137 if (!S_ISREG(st.st_mode)) {
138 log_error("%s is not a file.", path);
142 if (fchmod(fd, mode) < 0) {
143 log_error("chmod(%s) failed: %m", path);
147 if (uid_set || gid_set) {
150 uid_set ? uid : (uid_t) -1,
151 gid_set ? gid : (gid_t) -1) < 0) {
152 log_error("chown(%s) failed: %m", path);
157 } else if (type == 'd') {
163 r = mkdir(path, mode);
166 if (r < 0 && errno != EEXIST) {
167 log_error("Failed to create directory %s: %m", path);
171 if (stat(path, &st) < 0) {
172 log_error("stat(%s) failed: %m", path);
176 if (!S_ISDIR(st.st_mode)) {
177 log_error("%s is not a directory.", path);
181 if (chmod(path, mode) < 0) {
182 log_error("chmod(%s) failed: %m", path);
186 if (uid_set || gid_set) {
189 uid_set ? uid : (uid_t) -1,
190 gid_set ? gid : (gid_t) -1) < 0) {
191 log_error("chown(%s) failed: %m", path);
199 log_debug("%s created successfully.", path);
207 close_nointr_nofail(fd);
210 static int scandir_filter(const struct dirent *d) {
213 if (ignore_file(d->d_name))
216 if (d->d_type != DT_REG &&
220 return endswith(d->d_name, ".conf");
223 int main(int argc, char *argv[]) {
224 struct dirent **de = NULL;
225 int r = EXIT_FAILURE, n, i;
226 const char *prefix = NULL;
229 log_error("This program takes no more than one argument.");
236 log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
237 log_parse_environment();
242 if ((n = scandir("/etc/tempfiles.d/", &de, scandir_filter, alphasort)) < 0) {
247 log_error("Failed to enumerate /etc/tempfiles.d/ files: %m");
254 for (i = 0; i < n; i++) {
260 k = asprintf(&fn, "/etc/tempfiles.d/%s", de[i]->d_name);
264 log_error("Failed to allocate file name.");
273 log_error("Failed to open %s: %m", fn);
280 char line[LINE_MAX], *l;
282 if (!(fgets(line, sizeof(line), f)))
288 if (*l == '#' || *l == 0)
291 process_line(de[i]->d_name, j, l, prefix);
296 log_error("Failed to read from file: %m");