chiark / gitweb /
tempfiles: add little utility for creating volatile files/dirs in tmpfs hierarchies
[elogind.git] / src / readahead-common.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2010 Lennart Poettering
7
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.
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   General Public License for more details.
17
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/>.
20 ***/
21
22 #include <errno.h>
23 #include <libudev.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/sysinfo.h>
27 #include <sys/inotify.h>
28
29 #include "log.h"
30 #include "readahead-common.h"
31 #include "util.h"
32
33 int file_verify(int fd, const char *fn, off_t file_size_max, struct stat *st) {
34         assert(fd >= 0);
35         assert(fn);
36         assert(st);
37
38         if (fstat(fd, st) < 0) {
39                 log_warning("fstat(%s) failed: %m", fn);
40                 return -errno;
41         }
42
43         if (!S_ISREG(st->st_mode)) {
44                 log_debug("Not preloading special file %s", fn);
45                 return 0;
46         }
47
48         if (st->st_size <= 0 || st->st_size > file_size_max) {
49                 log_debug("Not preloading file %s with size out of bounds %zi", fn, st->st_size);
50                 return 0;
51         }
52
53         return 1;
54 }
55
56 int fs_on_ssd(const char *p) {
57         struct stat st;
58         struct udev *udev = NULL;
59         struct udev_device *udev_device = NULL, *look_at = NULL;
60         bool b = false;
61         const char *devtype, *rotational, *model, *id;
62
63         assert(p);
64
65         if (stat(p, &st) < 0)
66                 return -errno;
67
68         if (!(udev = udev_new()))
69                 return -ENOMEM;
70
71         if (!(udev_device = udev_device_new_from_devnum(udev, 'b', st.st_dev)))
72                 goto finish;
73
74         if ((devtype = udev_device_get_property_value(udev_device, "DEVTYPE")) &&
75             streq(devtype, "partition"))
76                 look_at = udev_device_get_parent(udev_device);
77         else
78                 look_at = udev_device;
79
80         if (!look_at)
81                 goto finish;
82
83         /* First, try high-level property */
84         if ((id = udev_device_get_property_value(look_at, "ID_SSD"))) {
85                 b = streq(id, "1");
86                 goto finish;
87         }
88
89         /* Second, try kernel attribute */
90         if ((rotational = udev_device_get_sysattr_value(look_at, "queue/rotational")))
91                 if ((b = streq(rotational, "0")))
92                         goto finish;
93
94         /* Finally, fallback to heuristics */
95         if (!(look_at = udev_device_get_parent(look_at)))
96                 goto finish;
97
98         if ((model = udev_device_get_sysattr_value(look_at, "model")))
99                 b = !!strstr(model, "SSD");
100
101 finish:
102         if (udev_device)
103                 udev_device_unref(udev_device);
104
105         if (udev)
106                 udev_unref(udev);
107
108         return b;
109 }
110
111 bool enough_ram(void) {
112         struct sysinfo si;
113
114         assert_se(sysinfo(&si) >= 0);
115
116         return si.totalram > 127 * 1024*1024; /* Enable readahead only
117                                                * with at least 128MB
118                                                * memory */
119 }
120
121 int open_inotify(void) {
122         int fd;
123
124         if ((fd = inotify_init1(IN_CLOEXEC|IN_NONBLOCK)) < 0) {
125                 log_error("Failed to create inotify handle: %m");
126                 return -errno;
127         }
128
129         mkdir("/dev/.systemd", 0755);
130         mkdir("/dev/.systemd/readahead", 0755);
131
132         if (inotify_add_watch(fd, "/dev/.systemd/readahead", IN_CREATE) < 0) {
133                 log_error("Failed to watch /dev/.systemd/readahead: %m");
134                 close_nointr_nofail(fd);
135                 return -errno;
136         }
137
138         return fd;
139 }