/
e4rat-preload-lite.c
216 lines (179 loc) · 4.94 KB
/
e4rat-preload-lite.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Maintained by Simonas Kazlauskas, 2012.
//
// Original version written by John Lindgren11, 2011.
// It can be found on http://e4rat-l.bananarocker.org/
//
// Replacement for e4rat-preload, which was written by Andreas Rid, 2011.
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#define VERBOSE 0
#define LIST "/var/lib/e4rat/startup.log"
#define INIT "/bin/systemd"
#define MAX_EARLY 1000
#define BLOCK 100
#define BUF 1048576 // = 1 MiB
typedef struct {
int dev;
uint64_t inode;
char *path;
} FileDesc;
static FileDesc **list = NULL;
static FileDesc **sorted = NULL;
static int listlen = 0;
#ifndef strdup
char *strdup(const char *source);
#endif
static int sort_cb(const void *_a, const void *_b){
// qsort helper for file list entries. Sorts by device and inode.
FileDesc *a = *(FileDesc **)_a;
FileDesc *b = *(FileDesc **)_b;
if(a->dev < b->dev){
return -1;
}
if(a->dev > b->dev){
return 1;
}
if(a->inode < b->inode){
return -1;
}
if(a->inode > b->inode){
return 1;
}
return 0;
}
static void die(const char *msg){
printf("Error: %s.\n", msg);
exit(EXIT_FAILURE);
}
static void free_list(FileDesc **list){
for (int i = 0; i < listlen; i++) {
free(list[i]->path);
free(list[i]);
}
free(list);
list = NULL;
}
static FileDesc *parse_line(const char *line){
// Parses a line from the file list. Returns NULL in case of parse errors.
// Expected format of the line:
// (device) (inode) (path)
// Example:
// 2049 2223875 /bin/bash
int dev = 0;
while(*line >= '0' && *line <= '9'){
dev = dev * 10 + (*line++ - '0');
}
if(*line++ != ' '){
return NULL;
}
uint64_t inode = 0;
while(*line >= '0' && *line <= '9'){
inode = inode * 10 + ((*line++) - '0');
}
if(*line++ != ' '){
return NULL;
}
FileDesc *f = malloc(sizeof(FileDesc));
if(!f) die("Failed to allocate memory while parsing file list!");
f->dev = dev;
f->inode = inode;
f->path = strdup(line);
if(!f->path) die("Failed to allocate memory for file path");
return f;
}
static void load_list(void){
// Loads list and parses contents into FileDescs
#if VERBOSE > 0
printf("Loading %s.\n", LIST);
#endif
FILE *stream = fopen(LIST, "r");
if (!stream) die(strerror(errno));
int listsize = 0;
while(1){
char buf[512];
if(!fgets(buf, sizeof buf, stream)){
break;
}
if(buf[0] && buf[strlen(buf) - 1] == '\n'){
buf[strlen (buf) - 1] = 0;
}
FileDesc *f = parse_line(buf);
if(!f){
continue;
}
if(listlen >= listsize){
listsize = listsize ? listsize * 2 : 256;
list = realloc(list, sizeof(FileDesc *) * listsize);
}
list[listlen++] = f;
}
fclose(stream);
list = realloc(list, sizeof(FileDesc *) * listlen);
sorted = malloc(sizeof(FileDesc *) * listlen);
if(!list || !sorted) die("Could not allocate memory for lists");
memcpy(sorted, list, sizeof(FileDesc *) * listlen);
qsort(sorted, listlen, sizeof(FileDesc *), sort_cb);
}
static void load_inodes(const int a, const int b){
struct stat s;
for(int i = a; i < ((b < listlen) ? b : listlen); i++){
stat(sorted[i]->path, &s);
}
}
static void exec_init(char **argv){
#if VERBOSE > 0
printf("Executing %s.\n", INIT);
#endif
switch(fork()){
case -1:
die(strerror(errno));
case 0:
return;
default:
execv(INIT, argv);
die(strerror(errno));
}
}
static void load_files(int a, int b){
void *buf = malloc(BUF);
if(!buf) die("Failed to allocate preload buffer");
for(int i = a; i < b && i < listlen; i ++){
int handle = open(list[i]->path, O_RDONLY);
if(handle < 0){
continue;
}
while(read(handle, buf, BUF) > 0){}
close(handle);
}
free(buf);
}
int main(int argc, char **argv){
int early_load = 0;
load_list();
#if VERBOSE > 0
printf("Preloading %d files.\n", listlen);
#endif
early_load = listlen * 0.33;
if(early_load > MAX_EARLY) early_load = MAX_EARLY;
// Preload a third of the list or MAX_EARLY files (whichever is smaller),
// then start init.
load_inodes(0, early_load);
load_files(0, early_load);
exec_init(argv);
// And continue preloading files further in chunks of BLOCK files.
for(int i = early_load; i < listlen; i += BLOCK){
load_inodes(i, i + BLOCK);
load_files(i, i + BLOCK);
}
free_list(list);
// As sorted originally was a copy of list, it's contents are already
// freed by the time we are freeing sorted list
free(sorted);
exit(EXIT_SUCCESS);
}