chiark / gitweb /
readahead: parse command line arguments
[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
37 #include "missing.h"
38 #include "util.h"
39 #include "set.h"
40 #include "sd-daemon.h"
41 #include "ioprio.h"
42 #include "readahead-common.h"
43
44 static off_t arg_file_size_max = READAHEAD_FILE_SIZE_MAX;
45
46 static int unpack_file(FILE *pack) {
47         char fn[PATH_MAX];
48         int r = 0, fd = -1;
49         bool any = false;
50         struct stat st;
51
52         assert(pack);
53
54         if (!fgets(fn, sizeof(fn), pack))
55                 return 0;
56
57         char_array_0(fn);
58         truncate_nl(fn);
59
60         if ((fd = open(fn, O_RDONLY|O_CLOEXEC|O_NOATIME|O_NOCTTY|O_NOFOLLOW)) < 0)
61                 log_warning("open(%s) failed: %m", fn);
62         else if (file_verify(fd, fn, arg_file_size_max, &st) <= 0) {
63                 close_nointr_nofail(fd);
64                 fd = -1;
65         }
66
67         for (;;) {
68                 uint32_t b, c;
69
70                 if (fread(&b, sizeof(b), 1, pack) != 1 ||
71                     fread(&c, sizeof(c), 1, pack) != 1) {
72                         log_error("Premature end of pack file.");
73                         r = -EIO;
74                         goto finish;
75                 }
76
77                 if (b == 0 && c == 0)
78                         break;
79
80                 if (c <= b) {
81                         log_error("Invalid pack file.");
82                         r = -EIO;
83                         goto finish;
84                 }
85
86                 log_debug("%s: page %u to %u", fn, b, c);
87
88                 any = true;
89
90                 if (fd >= 0)
91                         if (readahead(fd, b * PAGE_SIZE, (c - b) * PAGE_SIZE) < 0) {
92                                 log_warning("readahead() failed: %m");
93                                 goto finish;
94                         }
95         }
96
97         if (!any && fd >= 0) {
98                 /* if no range is encoded in the pack file this is
99                  * intended to mean that the whole file shall be
100                  * read */
101
102                 if (readahead(fd, 0, st.st_size) < 0) {
103                         log_warning("readahead() failed: %m");
104                         goto finish;
105                 }
106         }
107
108 finish:
109         if (fd >= 0)
110                 close_nointr_nofail(fd);
111
112         return r;
113 }
114
115 static int replay(const char *root) {
116         FILE *pack;
117         char line[LINE_MAX];
118         int r = 0;
119         char *pack_fn = NULL, c;
120         bool on_ssd, ready = false;
121         int prio;
122
123         assert(root);
124
125         write_one_line_file("/proc/self/oom_score_adj", "1000");
126
127         if (asprintf(&pack_fn, "%s/.readahead", root) < 0) {
128                 log_error("Out of memory");
129                 r = -ENOMEM;
130                 goto finish;
131         }
132
133         if ((!(pack = fopen(pack_fn, "re")))) {
134                 if (errno == -ENOENT)
135                         log_debug("No pack file found.");
136                 else {
137                         log_error("Failed to open pack file: %m");
138                         r = -errno;
139                 }
140
141                 goto finish;
142         }
143
144         if (!(fgets(line, sizeof(line), pack))) {
145                 log_error("Premature end of pack file.");
146                 r = -EIO;
147                 goto finish;
148         }
149
150         char_array_0(line);
151
152         if (!streq(line, CANONICAL_HOST "\n")) {
153                 log_debug("Pack file host type mismatch.");
154                 goto finish;
155         }
156
157         if ((c = getc(pack)) == EOF) {
158                 log_debug("Premature end of pack file.");
159                 r = -EIO;
160                 goto finish;
161         }
162
163         /* We do not retest SSD here, so that we can start replaying
164          * before udev is up.*/
165         on_ssd = c == 'S';
166         log_debug("On SSD: %s", yes_no(on_ssd));
167
168         if (on_ssd)
169                 prio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0);
170         else
171                 prio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_RT, 7);
172
173         if (ioprio_set(IOPRIO_WHO_PROCESS, getpid(), prio) < 0)
174                 log_warning("Failed to set IDLE IO priority class: %m");
175
176         sd_notify(0, "STATUS=Replaying readahead data");
177
178         log_debug("Replaying...");
179
180         while (!feof(pack) && !ferror(pack)) {
181                 int k;
182
183                 if ((k = unpack_file(pack)) < 0) {
184                         r = k;
185                         goto finish;
186                 }
187
188                 if (!ready) {
189                         /* We delay the ready notification until we
190                          * queued at least one read */
191                         sd_notify(0, "READY=1");
192                         ready = true;
193                 }
194         }
195
196         if (!ready)
197                 sd_notify(0, "READY=1");
198
199         if (ferror(pack)) {
200                 log_error("Failed to read pack file.");
201                 r = -EIO;
202                 goto finish;
203         }
204
205         log_debug("Done.");
206
207 finish:
208         if (pack)
209                 fclose(pack);
210
211         free(pack_fn);
212
213         return r;
214 }
215
216
217 static int help(void) {
218
219         printf("%s [OPTIONS...] [DIRECTORY]\n\n"
220                "Replay collected read-ahead data on early boot.\n\n"
221                "  -h --help                 Show this help\n"
222                "     --max-file-size=BYTES  Maximum size of files to read ahead\n",
223                program_invocation_short_name);
224
225         return 0;
226 }
227
228 static int parse_argv(int argc, char *argv[]) {
229
230         enum {
231                 ARG_FILE_SIZE_MAX
232         };
233
234         static const struct option options[] = {
235                 { "help",          no_argument,       NULL, 'h'                },
236                 { "file-size-max", required_argument, NULL, ARG_FILE_SIZE_MAX  },
237                 { NULL,            0,                 NULL, 0                  }
238         };
239
240         int c;
241
242         assert(argc >= 0);
243         assert(argv);
244
245         while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
246
247                 switch (c) {
248
249                 case 'h':
250                         help();
251                         return 0;
252
253                 case ARG_FILE_SIZE_MAX: {
254                         unsigned long long ull;
255
256                         if (safe_atollu(optarg, &ull) < 0 || ull <= 0) {
257                                 log_error("Failed to parse maximum file size %s.", optarg);
258                                 return -EINVAL;
259                         }
260
261                         arg_file_size_max = (off_t) ull;
262                         break;
263                 }
264
265                 case '?':
266                         return -EINVAL;
267
268                 default:
269                         log_error("Unknown option code %c", c);
270                         return -EINVAL;
271                 }
272         }
273
274         if (optind != argc &&
275             optind != argc-1) {
276                 help();
277                 return -EINVAL;
278         }
279
280         return 1;
281 }
282
283 int main(int argc, char*argv[]) {
284         int r;
285
286         log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
287         log_parse_environment();
288         log_open();
289
290         if ((r = parse_argv(argc, argv)) <= 0)
291                 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
292
293         if (!enough_ram()) {
294                 log_info("Disabling readahead replay due to low memory.");
295                 return 0;
296         }
297
298         if (replay(optind < argc ? argv[optind] : "/") < 0)
299                 return 1;
300
301         return 0;
302 }