#include <stdlib.h>
#include <string.h>
#include <sys/sysinfo.h>
+#include <sys/inotify.h>
+#include <fcntl.h>
+#include <sys/mman.h>
+#include <unistd.h>
#include "log.h"
#include "readahead-common.h"
}
if (st->st_size <= 0 || st->st_size > file_size_max) {
- log_debug("Not preloading file %s with size out of bounds %zi", fn, st->st_size);
+ log_debug("Not preloading file %s with size out of bounds %llu", fn, (unsigned long long) st->st_size);
return 0;
}
* with at least 128MB
* memory */
}
+
+int open_inotify(void) {
+ int fd;
+
+ if ((fd = inotify_init1(IN_CLOEXEC|IN_NONBLOCK)) < 0) {
+ log_error("Failed to create inotify handle: %m");
+ return -errno;
+ }
+
+ mkdir("/dev/.systemd", 0755);
+ mkdir("/dev/.systemd/readahead", 0755);
+
+ if (inotify_add_watch(fd, "/dev/.systemd/readahead", IN_CREATE) < 0) {
+ log_error("Failed to watch /dev/.systemd/readahead: %m");
+ close_nointr_nofail(fd);
+ return -errno;
+ }
+
+ return fd;
+}
+
+ReadaheadShared *shared_get(void) {
+ int fd;
+ ReadaheadShared *m = NULL;
+
+ mkdir("/dev/.systemd", 0755);
+ mkdir("/dev/.systemd/readahead", 0755);
+
+ if ((fd = open("/dev/.systemd/readahead/shared", O_CREAT|O_RDWR|O_CLOEXEC, 0644)) < 0) {
+ log_error("Failed to create shared memory segment: %m");
+ goto finish;
+ }
+
+ if (ftruncate(fd, sizeof(ReadaheadShared)) < 0) {
+ log_error("Failed to truncate shared memory segment: %m");
+ goto finish;
+ }
+
+ if ((m = mmap(NULL, sizeof(ReadaheadShared), PROT_WRITE|PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED) {
+ log_error("Failed to mmap shared memory segment: %m");
+ m = NULL;
+ goto finish;
+ }
+
+finish:
+ if (fd >= 0)
+ close_nointr_nofail(fd);
+
+ return m;
+}