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