chiark / gitweb /
tmpfiles: print error if basename lookup fails; document it in manpage
[elogind.git] / src / tmpfiles / tmpfiles.c
index 15913089ba44c576c6aec8f14da4b04c1b7a8bf7..12ec0b2f1bec9c1982f7e4f36f4f76d817d0590d 100644 (file)
 #include "log.h"
 #include "util.h"
 #include "mkdir.h"
+#include "path-util.h"
 #include "strv.h"
 #include "label.h"
 #include "set.h"
+#include "conf-files.h"
 
 /* This reads all files listed in /etc/tmpfiles.d/?*.conf and creates
  * them in the file system. This is intended to be used to create
@@ -98,6 +100,14 @@ static bool arg_remove = false;
 
 static const char *arg_prefix = NULL;
 
+static const char *conf_file_dirs[] = {
+        "/etc/tmpfiles.d",
+        "/run/tmpfiles.d",
+        "/usr/local/lib/tmpfiles.d",
+        "/usr/lib/tmpfiles.d",
+        NULL
+};
+
 #define MAX_DEPTH 256
 
 static bool needs_glob(ItemType t) {
@@ -562,7 +572,7 @@ static int glob_item(Item *i, int (*action)(Item *, const char *)) {
 }
 
 static int create_item(Item *i) {
-        int r;
+        int r, e;
         mode_t u;
         struct stat st;
 
@@ -584,8 +594,12 @@ static int create_item(Item *i) {
                         i->type == TRUNCATE_FILE ? O_CREAT|O_TRUNC : 0;
 
                 u = umask(0);
+                label_context_set(i->path, S_IFREG);
                 fd = open(i->path, flags|O_NDELAY|O_CLOEXEC|O_WRONLY|O_NOCTTY|O_NOFOLLOW, i->mode);
+                e = errno;
+                label_context_clear();
                 umask(u);
+                errno = e;
 
                 if (fd < 0) {
                         if (i->type == WRITE_FILE && errno == ENOENT)
@@ -611,8 +625,13 @@ static int create_item(Item *i) {
                         iovec[1].iov_len = 1;
 
                         n = writev(fd, iovec, 2);
-                        if (n < 0 || (size_t) n != l+1) {
-                                log_error("Failed to write file %s: %s", i->path, n < 0 ? strerror(-n) : "Short");
+
+                        /* It's OK if we don't write the trailing
+                         * newline, hence we check for l, instead of
+                         * l+1 here. Files in /sys often refuse
+                         * writing of the trailing newline. */
+                        if (n < 0 || (size_t) n < l) {
+                                log_error("Failed to write file %s: %s", i->path, n < 0 ? strerror(-n) : "Short write");
                                 close_nointr_nofail(fd);
                                 return n < 0 ? n : -EIO;
                         }
@@ -641,7 +660,7 @@ static int create_item(Item *i) {
         case CREATE_DIRECTORY:
 
                 u = umask(0);
-                mkdir_parents(i->path, 0755);
+                mkdir_parents_label(i->path, 0755);
                 r = mkdir(i->path, i->mode);
                 umask(u);
 
@@ -696,7 +715,12 @@ static int create_item(Item *i) {
         case CREATE_SYMLINK: {
                 char *x;
 
+                label_context_set(i->path, S_IFLNK);
                 r = symlink(i->argument, i->path);
+                e = errno;
+                label_context_clear();
+                errno = e;
+
                 if (r < 0 && errno != EEXIST) {
                         log_error("symlink(%s, %s) failed: %m", i->argument, i->path);
                         return -errno;
@@ -722,8 +746,12 @@ static int create_item(Item *i) {
         case CREATE_CHAR_DEVICE: {
 
                 u = umask(0);
+                label_context_set(i->path, CREATE_BLOCK_DEVICE ? S_IFBLK : S_IFCHR);
                 r = mknod(i->path, i->mode | (i->type == CREATE_BLOCK_DEVICE ? S_IFBLK : S_IFCHR), i->major_minor);
+                e = errno;
+                label_context_clear();
                 umask(u);
+                errno = e;
 
                 if (r < 0 && errno != EEXIST) {
                         log_error("Failed to create device node %s: %m", i->path);
@@ -1233,6 +1261,30 @@ static int read_config_file(const char *fn, bool ignore_enoent) {
         return r;
 }
 
+static char *resolve_fragment(const char *fragment, const char **search_paths) {
+        const char **p;
+        char *resolved_path;
+
+        if (is_path(fragment))
+                return strdup(fragment);
+
+        STRV_FOREACH(p, search_paths) {
+                resolved_path = join(*p, "/", fragment, NULL);
+                if (resolved_path == NULL) {
+                        log_error("Out of memory");
+                        return NULL;
+                }
+
+                if (access(resolved_path, F_OK) == 0)
+                        return resolved_path;
+
+                free(resolved_path);
+        }
+
+        errno = ENOENT;
+        return NULL;
+}
+
 int main(int argc, char *argv[]) {
         int r;
         Item *i;
@@ -1248,7 +1300,7 @@ int main(int argc, char *argv[]) {
 
         umask(0022);
 
-        label_init();
+        label_init(NULL);
 
         items = hashmap_new(string_hash_func, string_compare_func);
         globs = hashmap_new(string_hash_func, string_compare_func);
@@ -1264,22 +1316,28 @@ int main(int argc, char *argv[]) {
         if (optind < argc) {
                 int j;
 
-                for (j = optind; j < argc; j++)
-                        if (read_config_file(argv[j], false) < 0)
+                for (j = optind; j < argc; j++) {
+                        char *fragment;
+
+                        fragment = resolve_fragment(argv[j], conf_file_dirs);
+                        if (!fragment) {
+                                log_error("Failed to find any: %s file: %m", argv[j]);
                                 r = EXIT_FAILURE;
+                                goto finish;
+                        }
+                        if (read_config_file(fragment, false) < 0)
+                                r = EXIT_FAILURE;
+                        free(fragment);
+                }
 
         } else {
                 char **files, **f;
 
-                r = conf_files_list(&files, ".conf",
-                                    "/etc/tmpfiles.d",
-                                    "/run/tmpfiles.d",
-                                    "/usr/local/lib/tmpfiles.d",
-                                    "/usr/lib/tmpfiles.d",
-                                    NULL);
+                r = conf_files_list_strv(&files, ".conf",
+                                    (const char **)conf_file_dirs);
                 if (r < 0) {
-                        r = EXIT_FAILURE;
                         log_error("Failed to enumerate tmpfiles.d files: %s", strerror(-r));
+                        r = EXIT_FAILURE;
                         goto finish;
                 }