1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
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.
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.
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/>.
25 #include <linux/limits.h>
30 #include <sys/select.h>
32 #include <sys/types.h>
39 #include "sd-daemon.h"
41 #include "readahead-common.h"
43 static int unpack_file(FILE *pack) {
51 if (!fgets(fn, sizeof(fn), pack))
57 if ((fd = open(fn, O_RDONLY|O_CLOEXEC|O_NOATIME|O_NOCTTY|O_NOFOLLOW)) < 0)
58 log_warning("open(%s) failed: %m", fn);
59 else if (file_verify(fd, fn, &st) <= 0) {
60 close_nointr_nofail(fd);
67 if (fread(&b, sizeof(b), 1, pack) != 1 ||
68 fread(&c, sizeof(c), 1, pack) != 1) {
69 log_error("Premature end of pack file.");
78 log_error("Invalid pack file.");
83 log_debug("%s: page %u to %u", fn, b, c);
88 if (readahead(fd, b * PAGE_SIZE, (c - b) * PAGE_SIZE) < 0) {
89 log_warning("readahead() failed: %m");
94 if (!any && fd >= 0) {
95 /* if no range is encoded in the pack file this is
96 * intended to mean that the whole file shall be
99 if (readahead(fd, 0, st.st_size) < 0) {
100 log_warning("readahead() failed: %m");
107 close_nointr_nofail(fd);
112 static int replay(const char *root) {
116 char *pack_fn = NULL, c;
122 if (asprintf(&pack_fn, "%s/.readahead", root) < 0) {
123 log_error("Out of memory");
128 if ((!(pack = fopen(pack_fn, "re")))) {
129 if (errno == -ENOENT)
130 log_debug("No pack file found.");
132 log_error("Failed to open pack file: %m");
139 if (!(fgets(line, sizeof(line), pack))) {
140 log_error("Premature end of pack file.");
147 if (!streq(line, CANONICAL_HOST "\n")) {
148 log_debug("Pack file host type mismatch.");
152 if ((c = getc(pack)) == EOF) {
153 log_debug("Premature end of pack file.");
158 /* We do not retest SSD here, so that we can start replaying
159 * before udev is up.*/
161 log_debug("On SSD: %s", yes_no(on_ssd));
164 prio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0);
166 prio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_RT, 7);
168 if (ioprio_set(IOPRIO_WHO_PROCESS, getpid(), prio) < 0)
169 log_warning("Failed to set IDLE IO priority class: %m");
173 "STATUS=Replaying readahead data");
175 log_debug("Replaying...");
177 while (!feof(pack) && !ferror(pack)) {
180 if ((k = unpack_file(pack)) < 0) {
187 log_error("Failed to read pack file.");
203 int main(int argc, char*argv[]) {
204 /* log_set_target(LOG_TARGET_SYSLOG_OR_KMSG); */
205 log_parse_environment();
208 log_set_max_level(LOG_DEBUG);