chiark / gitweb /
build-sys: ensure selinux configure check follows logic of other optional features
[elogind.git] / src / readahead-replay.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 <inttypes.h>
24 #include <fcntl.h>
25 #include <linux/limits.h>
26 #include <stdbool.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/select.h>
31 #include <sys/time.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <unistd.h>
35 #include <getopt.h>
36 #include <sys/inotify.h>
37
38 #include "missing.h"
39 #include "util.h"
40 #include "set.h"
41 #include "sd-daemon.h"
42 #include "ioprio.h"
43 #include "readahead-common.h"
44
45 static off_t arg_file_size_max = READAHEAD_FILE_SIZE_MAX;
46
47 static ReadaheadShared *shared = NULL;
48
49 static int unpack_file(FILE *pack) {
50         char fn[PATH_MAX];
51         int r = 0, fd = -1;
52         bool any = false;
53         struct stat st;
54
55         assert(pack);
56
57         if (!fgets(fn, sizeof(fn), pack))
58                 return 0;
59
60         char_array_0(fn);
61         truncate_nl(fn);
62
63         if ((fd = open(fn, O_RDONLY|O_CLOEXEC|O_NOATIME|O_NOCTTY|O_NOFOLLOW)) < 0) {
64
65                 if (errno != ENOENT)
66                         log_warning("open(%s) failed: %m", fn);
67
68         } else if (file_verify(fd, fn, arg_file_size_max, &st) <= 0) {
69                 close_nointr_nofail(fd);
70                 fd = -1;
71         }
72
73         for (;;) {
74                 uint32_t b, c;
75
76                 if (fread(&b, sizeof(b), 1, pack) != 1 ||
77                     fread(&c, sizeof(c), 1, pack) != 1) {
78                         log_error("Premature end of pack file.");
79                         r = -EIO;
80                         goto finish;
81                 }
82
83                 if (b == 0 && c == 0)
84                         break;
85
86                 if (c <= b) {
87                         log_error("Invalid pack file.");
88                         r = -EIO;
89                         goto finish;
90                 }
91
92                 log_debug("%s: page %u to %u", fn, b, c);
93
94                 any = true;
95
96                 if (fd >= 0)
97                         if (posix_fadvise(fd, b * PAGE_SIZE, (c - b) * PAGE_SIZE, POSIX_FADV_WILLNEED) < 0) {
98                                 log_warning("posix_fadvise() failed: %m");
99                                 goto finish;
100                         }
101         }
102
103         if (!any && fd >= 0) {
104                 /* if no range is encoded in the pack file this is
105                  * intended to mean that the whole file shall be
106                  * read */
107
108                 if (posix_fadvise(fd, 0, st.st_size, POSIX_FADV_WILLNEED) < 0) {
109                         log_warning("posix_fadvise() failed: %m");
110                         goto finish;
111                 }
112         }
113
114 finish:
115         if (fd >= 0)
116                 close_nointr_nofail(fd);
117
118         return r;
119 }
120
121 static int replay(const char *root) {
122         FILE *pack = NULL;
123         char line[LINE_MAX];
124         int r = 0;
125         char *pack_fn = NULL, c;
126         bool on_ssd, ready = false;
127         int prio;
128         int inotify_fd = -1;
129
130         assert(root);
131
132         write_one_line_file("/proc/self/oom_score_adj", "1000");
133         bump_request_nr(root);
134
135         if (asprintf(&pack_fn, "%s/.readahead", root) < 0) {
136                 log_error("Out of memory");
137                 r = -ENOMEM;
138                 goto finish;
139         }
140
141         if ((!(pack = fopen(pack_fn, "re")))) {
142                 if (errno == ENOENT)
143                         log_debug("No pack file found.");
144                 else {
145                         log_error("Failed to open pack file: %m");
146                         r = -errno;
147                 }
148
149                 goto finish;
150         }
151
152         posix_fadvise(fileno(pack), 0, 0, POSIX_FADV_WILLNEED);
153
154         if ((inotify_fd = open_inotify()) < 0) {
155                 r = inotify_fd;
156                 goto finish;
157         }
158
159         if (!(fgets(line, sizeof(line), pack))) {
160                 log_error("Premature end of pack file.");
161                 r = -EIO;
162                 goto finish;
163         }
164
165         char_array_0(line);
166
167         if (!streq(line, CANONICAL_HOST "\n")) {
168                 log_debug("Pack file host type mismatch.");
169                 goto finish;
170         }
171
172         if ((c = getc(pack)) == EOF) {
173                 log_debug("Premature end of pack file.");
174                 r = -EIO;
175                 goto finish;
176         }
177
178         /* We do not retest SSD here, so that we can start replaying
179          * before udev is up.*/
180         on_ssd = c == 'S';
181         log_debug("On SSD: %s", yes_no(on_ssd));
182
183         if (on_ssd)
184                 prio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0);
185         else
186                 prio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_RT, 7);
187
188         if (ioprio_set(IOPRIO_WHO_PROCESS, getpid(), prio) < 0)
189                 log_warning("Failed to set IDLE IO priority class: %m");
190
191         sd_notify(0, "STATUS=Replaying readahead data");
192
193         log_debug("Replaying...");
194
195         if (access("/dev/.systemd/readahead/noreplay", F_OK) >= 0) {
196                 log_debug("Got termination request");
197                 goto done;
198         }
199
200         while (!feof(pack) && !ferror(pack)) {
201                 uint8_t inotify_buffer[sizeof(struct inotify_event) + FILENAME_MAX];
202                 int k;
203                 ssize_t n;
204
205                 if ((n = read(inotify_fd, &inotify_buffer, sizeof(inotify_buffer))) < 0) {
206                         if (errno != EINTR && errno != EAGAIN) {
207                                 log_error("Failed to read inotify event: %m");
208                                 r = -errno;
209                                 goto finish;
210                         }
211                 } else {
212                         struct inotify_event *e = (struct inotify_event*) inotify_buffer;
213
214                         while (n > 0) {
215                                 size_t step;
216
217                                 if ((e->mask & IN_CREATE) && streq(e->name, "noreplay")) {
218                                         log_debug("Got termination request");
219                                         goto done;
220                                 }
221
222                                 step = sizeof(struct inotify_event) + e->len;
223                                 assert(step <= (size_t) n);
224
225                                 e = (struct inotify_event*) ((uint8_t*) e + step);
226                                 n -= step;
227                         }
228                 }
229
230                 if ((k = unpack_file(pack)) < 0) {
231                         r = k;
232                         goto finish;
233                 }
234
235                 if (!ready) {
236                         /* We delay the ready notification until we
237                          * queued at least one read */
238                         sd_notify(0, "READY=1");
239                         ready = true;
240                 }
241         }
242
243 done:
244         if (!ready)
245                 sd_notify(0, "READY=1");
246
247         if (ferror(pack)) {
248                 log_error("Failed to read pack file.");
249                 r = -EIO;
250                 goto finish;
251         }
252
253         log_debug("Done.");
254
255 finish:
256         if (pack)
257                 fclose(pack);
258
259         if (inotify_fd >= 0)
260                 close_nointr_nofail(inotify_fd);
261
262         free(pack_fn);
263
264         return r;
265 }
266
267
268 static int help(void) {
269
270         printf("%s [OPTIONS...] [DIRECTORY]\n\n"
271                "Replay collected read-ahead data on early boot.\n\n"
272                "  -h --help                 Show this help\n"
273                "     --max-file-size=BYTES  Maximum size of files to read ahead\n",
274                program_invocation_short_name);
275
276         return 0;
277 }
278
279 static int parse_argv(int argc, char *argv[]) {
280
281         enum {
282                 ARG_FILE_SIZE_MAX
283         };
284
285         static const struct option options[] = {
286                 { "help",          no_argument,       NULL, 'h'                },
287                 { "file-size-max", required_argument, NULL, ARG_FILE_SIZE_MAX  },
288                 { NULL,            0,                 NULL, 0                  }
289         };
290
291         int c;
292
293         assert(argc >= 0);
294         assert(argv);
295
296         while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
297
298                 switch (c) {
299
300                 case 'h':
301                         help();
302                         return 0;
303
304                 case ARG_FILE_SIZE_MAX: {
305                         unsigned long long ull;
306
307                         if (safe_atollu(optarg, &ull) < 0 || ull <= 0) {
308                                 log_error("Failed to parse maximum file size %s.", optarg);
309                                 return -EINVAL;
310                         }
311
312                         arg_file_size_max = (off_t) ull;
313                         break;
314                 }
315
316                 case '?':
317                         return -EINVAL;
318
319                 default:
320                         log_error("Unknown option code %c", c);
321                         return -EINVAL;
322                 }
323         }
324
325         if (optind != argc &&
326             optind != argc-1) {
327                 help();
328                 return -EINVAL;
329         }
330
331         return 1;
332 }
333
334 int main(int argc, char*argv[]) {
335         int r;
336
337         log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
338         log_parse_environment();
339         log_open();
340
341         if ((r = parse_argv(argc, argv)) <= 0)
342                 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
343
344         if (!enough_ram()) {
345                 log_info("Disabling readahead replay due to low memory.");
346                 return 0;
347         }
348
349         if (!(shared = shared_get()))
350                 return 1;
351
352         shared->replay = getpid();
353         __sync_synchronize();
354
355         if (replay(optind < argc ? argv[optind] : "/") < 0)
356                 return 1;
357
358         return 0;
359 }