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