chiark / gitweb /
readahead: be more verbose about creation failures
[elogind.git] / src / readahead / readahead-common.c
index 1599ac15a476b628bc27b86a0176a446bf84bd86..a234a89954de129413631212d7ee59a190d713c2 100644 (file)
@@ -33,6 +33,7 @@
 #include "readahead-common.h"
 #include "util.h"
 #include "missing.h"
+#include "fileio.h"
 
 int file_verify(int fd, const char *fn, off_t file_size_max, struct stat *st) {
         assert(fd >= 0);
@@ -215,19 +216,26 @@ bool enough_ram(void) {
         return si.totalram > 127 * 1024*1024 / si.mem_unit;
 }
 
+static void mkdirs(void) {
+        if (mkdir("/run/systemd", 0755) && errno != EEXIST)
+                log_warning("Failed to create /run/systemd: %m");
+        if (mkdir("/run/systemd/readahead", 0755) && errno != EEXIST)
+                log_warning("Failed to create /run/systemd: %m");
+}
+
 int open_inotify(void) {
         int fd;
 
-        if ((fd = inotify_init1(IN_CLOEXEC|IN_NONBLOCK)) < 0) {
+        fd = inotify_init1(IN_CLOEXEC|IN_NONBLOCK);
+        if (fd < 0) {
                 log_error("Failed to create inotify handle: %m");
                 return -errno;
         }
 
-        mkdir("/run/systemd", 0755);
-        mkdir("/run/systemd/readahead", 0755);
+        mkdirs();
 
         if (inotify_add_watch(fd, "/run/systemd/readahead", IN_CREATE) < 0) {
-                log_error("Failed to add watch on /run/systemd/readahead: %m");
+                log_error("Failed to watch /run/systemd/readahead: %m");
                 close_nointr_nofail(fd);
                 return -errno;
         }
@@ -236,32 +244,28 @@ int open_inotify(void) {
 }
 
 ReadaheadShared *shared_get(void) {
-        int fd;
+        int _cleanup_close_ fd = -1;
         ReadaheadShared *m = NULL;
 
-        mkdir("/run/systemd", 0755);
-        mkdir("/run/systemd/readahead", 0755);
+        mkdirs();
 
-        if ((fd = open("/run/systemd/readahead/shared", O_CREAT|O_RDWR|O_CLOEXEC, 0644)) < 0) {
+        fd = open("/run/systemd/readahead/shared", O_CREAT|O_RDWR|O_CLOEXEC, 0644);
+        if (fd < 0) {
                 log_error("Failed to create shared memory segment: %m");
-                goto finish;
+                return NULL;
         }
 
         if (ftruncate(fd, sizeof(ReadaheadShared)) < 0) {
                 log_error("Failed to truncate shared memory segment: %m");
-                goto finish;
+                return NULL;
         }
 
-        if ((m = mmap(NULL, sizeof(ReadaheadShared), PROT_WRITE|PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED) {
+        m = mmap(NULL, sizeof(ReadaheadShared), PROT_WRITE|PROT_READ, MAP_SHARED, fd, 0);
+        if (m == MAP_FAILED) {
                 log_error("Failed to mmap shared memory segment: %m");
-                m = NULL;
-                goto finish;
+                return NULL;
         }
 
-finish:
-        if (fd >= 0)
-                close_nointr_nofail(fd);
-
         return m;
 }
 
@@ -315,7 +319,7 @@ int block_bump_request_nr(const char *p) {
                 goto finish;
         }
 
-        r = write_one_line_file(ap, line);
+        r = write_string_file(ap, line);
         if (r < 0)
                 goto finish;
 
@@ -398,7 +402,7 @@ int block_set_readahead(const char *p, uint64_t bytes) {
                 goto finish;
         }
 
-        r = write_one_line_file(ap, line);
+        r = write_string_file(ap, line);
         if (r < 0)
                 goto finish;