discover/boot: Download resources in parallel
[petitboot] / discover / boot.c
1
2 #define _GNU_SOURCE
3
4 #include <stdbool.h>
5 #include <stdlib.h>
6 #include <assert.h>
7 #include <dirent.h>
8 #include <string.h>
9 #include <fcntl.h>
10 #include <sys/types.h>
11
12 #include <log/log.h>
13 #include <pb-protocol/pb-protocol.h>
14 #include <process/process.h>
15 #include <system/system.h>
16 #include <talloc/talloc.h>
17 #include <url/url.h>
18
19 #include "device-handler.h"
20 #include "boot.h"
21 #include "paths.h"
22 #include "resource.h"
23
24 static const char *boot_hook_dir = PKG_SYSCONF_DIR "/boot.d";
25 enum {
26         BOOT_HOOK_EXIT_OK       = 0,
27         BOOT_HOOK_EXIT_UPDATE   = 2,
28 };
29
30 struct boot_task {
31         struct load_url_result *image;
32         struct load_url_result *initrd;
33         struct load_url_result *dtb;
34         const char *args;
35         boot_status_fn status_fn;
36         void *status_arg;
37         bool dry_run;
38 };
39
40 /**
41  * kexec_load - kexec load helper.
42  */
43 static int kexec_load(struct boot_task *boot_task)
44 {
45         int result;
46         const char *argv[7];
47         const char **p;
48         char *s_initrd = NULL;
49         char *s_dtb = NULL;
50         char *s_args = NULL;
51
52         p = argv;
53         *p++ = pb_system_apps.kexec;    /* 1 */
54         *p++ = "-l";                    /* 2 */
55
56         if (boot_task->initrd) {
57                 s_initrd = talloc_asprintf(boot_task, "--initrd=%s",
58                                 boot_task->initrd->local);
59                 assert(s_initrd);
60                 *p++ = s_initrd;         /* 3 */
61         }
62
63         if (boot_task->dtb) {
64                 s_dtb = talloc_asprintf(boot_task, "--dtb=%s",
65                                                 boot_task->dtb->local);
66                 assert(s_dtb);
67                 *p++ = s_dtb;            /* 4 */
68         }
69
70         if (boot_task->args) {
71                 s_args = talloc_asprintf(boot_task, "--append=%s",
72                                                 boot_task->args);
73                 assert(s_args);
74                 *p++ = s_args;          /* 5 */
75         }
76
77         *p++ = boot_task->image->local; /* 6 */
78         *p++ = NULL;                    /* 7 */
79
80         result = process_run_simple_argv(boot_task, argv);
81
82         if (result)
83                 pb_log("%s: failed: (%d)\n", __func__, result);
84
85         return result;
86 }
87
88 /**
89  * kexec_reboot - Helper to boot the new kernel.
90  *
91  * Must only be called after a successful call to kexec_load().
92  */
93
94 static int kexec_reboot(struct boot_task *task)
95 {
96         int result;
97
98         /* First try running shutdown.  Init scripts should run 'exec -e' */
99         result = process_run_simple(task, pb_system_apps.shutdown, "-r",
100                         "now", NULL);
101
102         /* On error, force a kexec with the -e option */
103         if (result) {
104                 result = process_run_simple(task, pb_system_apps.kexec,
105                                                 "-e", NULL);
106         }
107
108         if (result)
109                 pb_log("%s: failed: (%d)\n", __func__, result);
110
111         /* okay, kexec -e -f */
112         if (result) {
113                 result = process_run_simple(task, pb_system_apps.kexec,
114                                                 "-e", "-f", NULL);
115         }
116
117         if (result)
118                 pb_log("%s: failed: (%d)\n", __func__, result);
119
120
121         return result;
122 }
123
124 static void __attribute__((format(__printf__, 4, 5))) update_status(
125                 boot_status_fn fn, void *arg, int type, char *fmt, ...)
126 {
127         struct boot_status status;
128         va_list ap;
129
130         va_start(ap, fmt);
131         status.message = talloc_vasprintf(NULL, fmt, ap);
132         va_end(ap);
133
134         status.type = type;
135         status.progress = -1;
136         status.detail = NULL;
137
138         fn(arg, &status);
139
140         talloc_free(status.message);
141 }
142
143 static void boot_hook_update_param(void *ctx, struct boot_task *task,
144                 const char *name, const char *value)
145 {
146         struct p {
147                 const char *name;
148                 const char **p;
149         } *param, params[] = {
150                 { "boot_image",         &task->image->local },
151                 { "boot_initrd",        &task->initrd->local },
152                 { "boot_dtb",           &task->dtb->local },
153                 { "boot_args",          &task->args },
154                 { NULL, NULL },
155         };
156
157         for (param = params; param->name; param++) {
158                 if (strcmp(param->name, name))
159                         continue;
160
161                 *param->p = talloc_strdup(ctx, value);
162                 return;
163         }
164 }
165
166 static void boot_hook_update(struct boot_task *task, const char *hookname,
167                 char *buf)
168 {
169         char *line, *name, *val, *sep;
170         char *saveptr;
171
172         for (;; buf = NULL) {
173
174                 line = strtok_r(buf, "\n", &saveptr);
175                 if (!line)
176                         break;
177
178                 sep = strchr(line, '=');
179                 if (!sep)
180                         continue;
181
182                 *sep = '\0';
183                 name = line;
184                 val = sep + 1;
185
186                 boot_hook_update_param(task, task, name, val);
187
188                 pb_log("boot hook %s specified %s=%s\n",
189                                 hookname, name, val);
190         }
191 }
192
193 static void boot_hook_setenv(struct boot_task *task)
194 {
195         unsetenv("boot_image");
196         unsetenv("boot_initrd");
197         unsetenv("boot_dtb");
198         unsetenv("boot_args");
199
200         setenv("boot_image", task->image->local, 1);
201         if (task->initrd)
202                 setenv("boot_initrd", task->initrd->local, 1);
203         if (task->dtb)
204                 setenv("boot_dtb", task->dtb->local, 1);
205         if (task->args)
206                 setenv("boot_args", task->args, 1);
207 }
208
209 static int hook_filter(const struct dirent *dirent)
210 {
211         return dirent->d_type == DT_REG || dirent->d_type == DT_LNK;
212 }
213
214 static int hook_cmp(const struct dirent **a, const struct dirent **b)
215 {
216         return strcmp((*a)->d_name, (*b)->d_name);
217 }
218
219 static void run_boot_hooks(struct boot_task *task)
220 {
221         struct dirent **hooks;
222         int i, n;
223
224         n = scandir(boot_hook_dir, &hooks, hook_filter, hook_cmp);
225         if (n < 1)
226                 return;
227
228         update_status(task->status_fn, task->status_arg, BOOT_STATUS_INFO,
229                         "running boot hooks");
230
231         boot_hook_setenv(task);
232
233         for (i = 0; i < n; i++) {
234                 const char *argv[2] = { NULL, NULL };
235                 struct process *process;
236                 char *path;
237                 int rc;
238
239                 path = join_paths(task, boot_hook_dir, hooks[i]->d_name);
240
241                 if (access(path, X_OK)) {
242                         talloc_free(path);
243                         continue;
244                 }
245
246                 process = process_create(task);
247
248                 argv[0] = path;
249                 process->path = path;
250                 process->argv = argv;
251                 process->keep_stdout = true;
252
253                 pb_log("running boot hook %s\n", hooks[i]->d_name);
254
255                 rc = process_run_sync(process);
256                 if (rc) {
257                         pb_log("boot hook exec failed!\n");
258
259                 } else if (WIFEXITED(process->exit_status) &&
260                            WEXITSTATUS(process->exit_status)
261                                 == BOOT_HOOK_EXIT_UPDATE) {
262                         /* if the hook returned with BOOT_HOOK_EXIT_UPDATE,
263                          * then we process stdout to look for updated params
264                          */
265                         if (rc == BOOT_HOOK_EXIT_UPDATE) {
266                                 boot_hook_update(task, hooks[i]->d_name,
267                                                 process->stdout_buf);
268                                 boot_hook_setenv(task);
269                         }
270                 }
271
272                 process_release(process);
273                 talloc_free(path);
274         }
275
276         free(hooks);
277 }
278
279 static bool load_pending(struct load_url_result *result)
280 {
281         return result && result->status == LOAD_ASYNC;
282 }
283
284 static int check_load(struct boot_task *task, const char *name,
285                 struct load_url_result *result)
286 {
287         if (!result)
288                 return 0;
289         if (result->status != LOAD_ERROR)
290                 return 0;
291
292         update_status(task->status_fn, task->status_arg,
293                         BOOT_STATUS_ERROR,
294                         "Couldn't load %s", name);
295         return -1;
296 }
297
298 static void cleanup_load(struct load_url_result *result)
299 {
300         if (!result)
301                 return;
302         if (result->status != LOAD_OK)
303                 return;
304         if (!result->cleanup_local)
305                 return;
306         unlink(result->local);
307 }
308
309 static void boot_process(struct load_url_result *result __attribute__((unused)),
310                 void *data)
311 {
312         struct boot_task *task = data;
313         int rc = -1;
314
315         if (load_pending(task->image) ||
316                         load_pending(task->initrd) ||
317                         load_pending(task->dtb))
318                 return;
319
320         if (check_load(task, "kernel image", task->image) ||
321                         check_load(task, "initrd", task->initrd) ||
322                         check_load(task, "dtb", task->dtb))
323                 goto no_load;
324
325         run_boot_hooks(task);
326
327         update_status(task->status_fn, task->status_arg, BOOT_STATUS_INFO,
328                         "performing kexec_load");
329
330         rc = kexec_load(task);
331         if (rc) {
332                 update_status(task->status_fn, task->status_arg,
333                                 BOOT_STATUS_ERROR, "kexec load failed");
334         }
335
336 no_load:
337         cleanup_load(task->image);
338         cleanup_load(task->initrd);
339         cleanup_load(task->dtb);
340
341         if (!rc) {
342                 update_status(task->status_fn, task->status_arg,
343                                 BOOT_STATUS_INFO,
344                                 "performing kexec reboot");
345
346                 rc = kexec_reboot(task);
347                 if (rc) {
348                         update_status(task->status_fn, task->status_arg,
349                                         BOOT_STATUS_ERROR,
350                                         "kexec reboot failed");
351                 }
352         }
353
354         talloc_free(task);
355 }
356
357 static int start_url_load(struct boot_task *task, const char *name,
358                 struct pb_url *url, struct load_url_result **result)
359 {
360         if (!url)
361                 return 0;
362
363         *result = load_url_async(task, url, boot_process, task);
364         if (!*result) {
365                 update_status(task->status_fn, task->status_arg,
366                                 BOOT_STATUS_ERROR,
367                                 "Error loading %s", name);
368                 return -1;
369         }
370         return 0;
371 }
372
373 int boot(void *ctx, struct discover_boot_option *opt, struct boot_command *cmd,
374                 int dry_run, boot_status_fn status_fn, void *status_arg)
375 {
376         struct pb_url *image = NULL, *initrd = NULL, *dtb = NULL;
377         struct boot_task *boot_task;
378         const char *boot_desc;
379         int rc;
380
381         if (opt && opt->option->name)
382                 boot_desc = opt->option->name;
383         else if (cmd && cmd->boot_image_file)
384                 boot_desc = cmd->boot_image_file;
385         else
386                 boot_desc = "(unknown)";
387
388         update_status(status_fn, status_arg, BOOT_STATUS_INFO,
389                         "Booting %s.", boot_desc);
390
391         if (cmd && cmd->boot_image_file) {
392                 image = pb_url_parse(opt, cmd->boot_image_file);
393         } else if (opt && opt->boot_image) {
394                 image = opt->boot_image->url;
395         } else {
396                 pb_log("%s: no image specified\n", __func__);
397                 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
398                                 "Boot failed: no image specified");
399                 return -1;
400         }
401
402         if (cmd && cmd->initrd_file) {
403                 initrd = pb_url_parse(opt, cmd->initrd_file);
404         } else if (opt && opt->initrd) {
405                 initrd = opt->initrd->url;
406         }
407
408         if (cmd && cmd->dtb_file) {
409                 dtb = pb_url_parse(opt, cmd->dtb_file);
410         } else if (opt && opt->dtb) {
411                 dtb = opt->dtb->url;
412         }
413
414         boot_task = talloc_zero(ctx, struct boot_task);
415         boot_task->dry_run = dry_run;
416         boot_task->status_fn = status_fn;
417         boot_task->status_arg = status_arg;
418
419         if (cmd && cmd->boot_args) {
420                 boot_task->args = talloc_strdup(boot_task, cmd->boot_args);
421         } else if (opt && opt->option->boot_args) {
422                 boot_task->args = talloc_strdup(boot_task,
423                                                 opt->option->boot_args);
424         } else {
425                 boot_task->args = NULL;
426         }
427
428         /* start async loads for boot resources */
429         rc = start_url_load(boot_task, "kernel image", image, &boot_task->image)
430           || start_url_load(boot_task, "initrd", initrd, &boot_task->initrd)
431           || start_url_load(boot_task, "dtb", dtb, &boot_task->dtb);
432
433         /* If all URLs are local, we may be done. */
434         if (!rc)
435                 boot_process(NULL, boot_task);
436
437         return rc;
438 }