discover/boot: Allow printf formatting in update_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
31 struct boot_task {
32         char *local_image;
33         char *local_initrd;
34         char *local_dtb;
35         char *args;
36
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->local_initrd) {
57                 s_initrd = talloc_asprintf(boot_task, "--initrd=%s",
58                                 boot_task->local_initrd);
59                 assert(s_initrd);
60                 *p++ = s_initrd;         /* 3 */
61         }
62
63         if (boot_task->local_dtb) {
64                 s_dtb = talloc_asprintf(boot_task, "--dtb=%s",
65                                                 boot_task->local_dtb);
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->local_image;  /* 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                 char **p;
149         } *param, params[] = {
150                 { "boot_image",         &task->local_image },
151                 { "boot_initrd",        &task->local_initrd },
152                 { "boot_dtb",           &task->local_dtb },
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->local_image, 1);
201         if (task->local_initrd)
202                 setenv("boot_initrd", task->local_initrd, 1);
203         if (task->local_dtb)
204                 setenv("boot_dtb", task->local_dtb, 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, boot_status_fn status_fn,
220                 void *status_arg)
221 {
222         struct dirent **hooks;
223         int i, n;
224
225         n = scandir(boot_hook_dir, &hooks, hook_filter, hook_cmp);
226         if (n < 1)
227                 return;
228
229         update_status(status_fn, status_arg, BOOT_STATUS_INFO,
230                         "running boot hooks");
231
232         boot_hook_setenv(task);
233
234         for (i = 0; i < n; i++) {
235                 const char *argv[2] = { NULL, NULL };
236                 struct process *process;
237                 char *path;
238                 int rc;
239
240                 path = join_paths(task, boot_hook_dir, hooks[i]->d_name);
241
242                 if (access(path, X_OK)) {
243                         talloc_free(path);
244                         continue;
245                 }
246
247                 process = process_create(task);
248
249                 argv[0] = path;
250                 process->path = path;
251                 process->argv = argv;
252                 process->keep_stdout = true;
253
254                 pb_log("running boot hook %s\n", hooks[i]->d_name);
255
256                 rc = process_run_sync(process);
257                 if (rc) {
258                         pb_log("boot hook exec failed!\n");
259
260                 } else if (WIFEXITED(process->exit_status) &&
261                            WEXITSTATUS(process->exit_status)
262                                 == BOOT_HOOK_EXIT_UPDATE) {
263                         /* if the hook returned with BOOT_HOOK_EXIT_UPDATE,
264                          * then we process stdout to look for updated params
265                          */
266                         if (rc == BOOT_HOOK_EXIT_UPDATE) {
267                                 boot_hook_update(task, hooks[i]->d_name,
268                                                 process->stdout_buf);
269                                 boot_hook_setenv(task);
270                         }
271                 }
272
273                 process_release(process);
274                 talloc_free(path);
275         }
276
277         free(hooks);
278 }
279
280 int boot(void *ctx, struct discover_boot_option *opt, struct boot_command *cmd,
281                 int dry_run, boot_status_fn status_fn, void *status_arg)
282 {
283         struct boot_task *boot_task;
284         struct pb_url *image, *initrd, *dtb;
285         unsigned int clean_image = 0;
286         unsigned int clean_initrd = 0;
287         unsigned int clean_dtb = 0;
288         int result;
289
290         image = NULL;
291         initrd = NULL;
292         dtb = NULL;
293
294         if (cmd && cmd->boot_image_file) {
295                 image = pb_url_parse(opt, cmd->boot_image_file);
296         } else if (opt && opt->boot_image) {
297                 image = opt->boot_image->url;
298         } else {
299                 pb_log("%s: no image specified\n", __func__);
300                 return -1;
301         }
302
303         boot_task = talloc_zero(ctx, struct boot_task);
304         boot_task->dry_run = dry_run;
305
306         if (cmd && cmd->initrd_file) {
307                 initrd = pb_url_parse(opt, cmd->initrd_file);
308         } else if (opt && opt->initrd) {
309                 initrd = opt->initrd->url;
310         }
311
312         if (cmd && cmd->dtb_file) {
313                 dtb = pb_url_parse(opt, cmd->dtb_file);
314         } else if (opt && opt->dtb) {
315                 dtb = opt->dtb->url;
316         }
317
318         if (cmd && cmd->boot_args) {
319                 boot_task->args = talloc_strdup(boot_task, cmd->boot_args);
320         } else if (opt && opt->option->boot_args) {
321                 boot_task->args = talloc_strdup(boot_task,
322                                                 opt->option->boot_args);
323         } else {
324                 boot_task->args = NULL;
325         }
326
327         result = -1;
328
329         update_status(status_fn, status_arg, BOOT_STATUS_INFO,
330                         "loading kernel");
331         boot_task->local_image = load_url(NULL, image, &clean_image);
332         if (!boot_task->local_image) {
333                 update_status(status_fn, status_arg, BOOT_STATUS_ERROR,
334                                 "Couldn't load kernel image");
335                 goto no_load;
336         }
337
338         if (initrd) {
339                 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
340                                 "loading initrd");
341                 boot_task->local_initrd = load_url(NULL, initrd, &clean_initrd);
342                 if (!boot_task->local_initrd) {
343                         update_status(status_fn, status_arg, BOOT_STATUS_ERROR,
344                                         "Couldn't load initrd image");
345                         goto no_load;
346                 }
347         }
348
349         if (dtb) {
350                 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
351                                 "loading device tree");
352                 boot_task->local_dtb = load_url(NULL, dtb, &clean_dtb);
353                 if (!boot_task->local_dtb) {
354                         update_status(status_fn, status_arg, BOOT_STATUS_ERROR,
355                                         "Couldn't load device tree");
356                         goto no_load;
357                 }
358         }
359
360         run_boot_hooks(boot_task, status_fn, status_arg);
361
362         update_status(status_fn, status_arg, BOOT_STATUS_INFO,
363                         "performing kexec_load");
364
365         result = kexec_load(boot_task);
366
367         if (result) {
368                 update_status(status_fn, status_arg, BOOT_STATUS_ERROR,
369                                 "kexec load failed");
370         }
371
372 no_load:
373         if (clean_image)
374                 unlink(boot_task->local_image);
375         if (clean_initrd)
376                 unlink(boot_task->local_initrd);
377         if (clean_dtb)
378                 unlink(boot_task->local_dtb);
379
380         if (!result) {
381                 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
382                                 "performing kexec reboot");
383
384                 result = kexec_reboot(boot_task);
385
386                 if (result) {
387                         update_status(status_fn, status_arg, BOOT_STATUS_ERROR,
388                                         "kexec reboot failed");
389                 }
390         }
391
392         talloc_free(boot_task);
393
394         return result;
395 }