lib/process: replace pb_run_cmd_pipe
[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 = pb_run_cmd(argv, 1, boot_task->dry_run);
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(bool dry_run)
95 {
96         int result = 0;
97         const char *argv[4];
98         const char **p;
99
100         /* First try running shutdown.  Init scripts should run 'exec -e' */
101
102         p = argv;
103         *p++ = pb_system_apps.shutdown; /* 1 */
104         *p++ =  "-r";                   /* 2 */
105         *p++ =  "now";                  /* 3 */
106         *p++ =  NULL;                   /* 4 */
107
108         result = pb_run_cmd(argv, 1, dry_run);
109
110         /* On error, force a kexec with the -e option */
111
112         if (result) {
113                 p = argv;
114                 *p++ = pb_system_apps.kexec;    /* 1 */
115                 *p++ = "-e";                    /* 2 */
116                 *p++ = NULL;                    /* 3 */
117
118                 result = pb_run_cmd(argv, 1, 0);
119         }
120
121         if (result)
122                 pb_log("%s: failed: (%d)\n", __func__, result);
123
124         /* okay, kexec -e -f */
125         if (result) {
126                 p = argv;
127                 *p++ = pb_system_apps.kexec;    /* 1 */
128                 *p++ = "-e";                    /* 2 */
129                 *p++ = "-f";                    /* 3 */
130                 *p++ = NULL;                    /* 4 */
131
132                 result = pb_run_cmd(argv, 1, 0);
133         }
134
135         if (result)
136                 pb_log("%s: failed: (%d)\n", __func__, result);
137
138
139         return result;
140 }
141
142 static void update_status(boot_status_fn fn, void *arg, int type,
143                 char *message)
144 {
145         struct boot_status status;
146
147         status.type = type;
148         status.message = message;
149         status.progress = -1;
150         status.detail = NULL;
151
152         fn(arg, &status);
153 }
154
155 static void boot_hook_update_param(void *ctx, struct boot_task *task,
156                 const char *name, const char *value)
157 {
158         struct p {
159                 const char *name;
160                 char **p;
161         } *param, params[] = {
162                 { "boot_image",         &task->local_image },
163                 { "boot_initrd",        &task->local_initrd },
164                 { "boot_dtb",           &task->local_dtb },
165                 { "boot_args",          &task->args },
166                 { NULL, NULL },
167         };
168
169         for (param = params; param->name; param++) {
170                 if (strcmp(param->name, name))
171                         continue;
172
173                 *param->p = talloc_strdup(ctx, value);
174                 return;
175         }
176 }
177
178 static void boot_hook_update(struct boot_task *task, const char *hookname,
179                 char *buf)
180 {
181         char *line, *name, *val, *sep;
182         char *saveptr;
183
184         for (;; buf = NULL) {
185
186                 line = strtok_r(buf, "\n", &saveptr);
187                 if (!line)
188                         break;
189
190                 sep = strchr(line, '=');
191                 if (!sep)
192                         continue;
193
194                 *sep = '\0';
195                 name = line;
196                 val = sep + 1;
197
198                 boot_hook_update_param(task, task, name, val);
199
200                 pb_log("boot hook %s specified %s=%s\n",
201                                 hookname, name, val);
202         }
203 }
204
205 static void boot_hook_setenv(struct boot_task *task)
206 {
207         unsetenv("boot_image");
208         unsetenv("boot_initrd");
209         unsetenv("boot_dtb");
210         unsetenv("boot_args");
211
212         setenv("boot_image", task->local_image, 1);
213         if (task->local_initrd)
214                 setenv("boot_initrd", task->local_initrd, 1);
215         if (task->local_dtb)
216                 setenv("boot_dtb", task->local_dtb, 1);
217         if (task->args)
218                 setenv("boot_args", task->args, 1);
219 }
220
221 static int hook_filter(const struct dirent *dirent)
222 {
223         return dirent->d_type == DT_REG || dirent->d_type == DT_LNK;
224 }
225
226 static int hook_cmp(const struct dirent **a, const struct dirent **b)
227 {
228         return strcmp((*a)->d_name, (*b)->d_name);
229 }
230
231 static void run_boot_hooks(struct boot_task *task, boot_status_fn status_fn,
232                 void *status_arg)
233 {
234         struct dirent **hooks;
235         int i, n;
236
237         n = scandir(boot_hook_dir, &hooks, hook_filter, hook_cmp);
238         if (n < 1)
239                 return;
240
241         update_status(status_fn, status_arg, BOOT_STATUS_INFO,
242                         "running boot hooks");
243
244         boot_hook_setenv(task);
245
246         for (i = 0; i < n; i++) {
247                 const char *argv[2] = { NULL, NULL };
248                 struct process *process;
249                 char *path;
250                 int rc;
251
252                 path = join_paths(task, boot_hook_dir, hooks[i]->d_name);
253
254                 if (access(path, X_OK)) {
255                         talloc_free(path);
256                         continue;
257                 }
258
259                 process = process_create(task);
260
261                 argv[0] = path;
262                 process->path = path;
263                 process->argv = argv;
264                 process->keep_stdout = true;
265
266                 pb_log("running boot hook %s\n", hooks[i]->d_name);
267
268                 rc = process_run_sync(process);
269                 if (rc) {
270                         pb_log("boot hook exec failed!\n");
271
272                 } else if (WIFEXITED(process->exit_status) &&
273                            WEXITSTATUS(process->exit_status)
274                                 == BOOT_HOOK_EXIT_UPDATE) {
275                         /* if the hook returned with BOOT_HOOK_EXIT_UPDATE,
276                          * then we process stdout to look for updated params
277                          */
278                         if (rc == BOOT_HOOK_EXIT_UPDATE) {
279                                 boot_hook_update(task, hooks[i]->d_name,
280                                                 process->stdout_buf);
281                                 boot_hook_setenv(task);
282                         }
283                 }
284
285                 process_release(process);
286                 talloc_free(path);
287         }
288
289         free(hooks);
290 }
291
292 int boot(void *ctx, struct discover_boot_option *opt, struct boot_command *cmd,
293                 int dry_run, boot_status_fn status_fn, void *status_arg)
294 {
295         struct boot_task *boot_task;
296         struct pb_url *image, *initrd, *dtb;
297         unsigned int clean_image = 0;
298         unsigned int clean_initrd = 0;
299         unsigned int clean_dtb = 0;
300         int result;
301
302         image = NULL;
303         initrd = NULL;
304         dtb = NULL;
305
306         if (cmd && cmd->boot_image_file) {
307                 image = pb_url_parse(opt, cmd->boot_image_file);
308         } else if (opt && opt->boot_image) {
309                 image = opt->boot_image->url;
310         } else {
311                 pb_log("%s: no image specified", __func__);
312                 return -1;
313         }
314
315         boot_task = talloc_zero(ctx, struct boot_task);
316         boot_task->dry_run = dry_run;
317
318         if (cmd && cmd->initrd_file) {
319                 initrd = pb_url_parse(opt, cmd->initrd_file);
320         } else if (opt && opt->initrd) {
321                 initrd = opt->initrd->url;
322         }
323
324         if (cmd && cmd->dtb_file) {
325                 dtb = pb_url_parse(opt, cmd->dtb_file);
326         } else if (opt && opt->dtb) {
327                 dtb = opt->dtb->url;
328         }
329
330         if (cmd && cmd->boot_args) {
331                 boot_task->args = talloc_strdup(boot_task, cmd->boot_args);
332         } else if (opt && opt->option->boot_args) {
333                 boot_task->args = talloc_strdup(boot_task,
334                                                 opt->option->boot_args);
335         } else {
336                 boot_task->args = NULL;
337         }
338
339         result = -1;
340
341         update_status(status_fn, status_arg, BOOT_STATUS_INFO,
342                         "loading kernel");
343         boot_task->local_image = load_url(NULL, image, &clean_image);
344         if (!boot_task->local_image) {
345                 update_status(status_fn, status_arg, BOOT_STATUS_ERROR,
346                                 "Couldn't load kernel image");
347                 goto no_load;
348         }
349
350         if (initrd) {
351                 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
352                                 "loading initrd");
353                 boot_task->local_initrd = load_url(NULL, initrd, &clean_initrd);
354                 if (!boot_task->local_initrd) {
355                         update_status(status_fn, status_arg, BOOT_STATUS_ERROR,
356                                         "Couldn't load initrd image");
357                         goto no_load;
358                 }
359         }
360
361         if (dtb) {
362                 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
363                                 "loading device tree");
364                 boot_task->local_dtb = load_url(NULL, dtb, &clean_dtb);
365                 if (!boot_task->local_dtb) {
366                         update_status(status_fn, status_arg, BOOT_STATUS_ERROR,
367                                         "Couldn't load device tree");
368                         goto no_load;
369                 }
370         }
371
372         run_boot_hooks(boot_task, status_fn, status_arg);
373
374         update_status(status_fn, status_arg, BOOT_STATUS_INFO,
375                         "performing kexec_load");
376
377         result = kexec_load(boot_task);
378
379         if (result) {
380                 update_status(status_fn, status_arg, BOOT_STATUS_ERROR,
381                                 "kexec load failed");
382         }
383
384 no_load:
385         if (clean_image)
386                 unlink(boot_task->local_image);
387         if (clean_initrd)
388                 unlink(boot_task->local_initrd);
389         if (clean_dtb)
390                 unlink(boot_task->local_dtb);
391
392         talloc_free(boot_task);
393
394         if (!result) {
395                 update_status(status_fn, status_arg, BOOT_STATUS_INFO,
396                                 "performing kexec reboot");
397
398                 result = kexec_reboot(boot_task->dry_run);
399
400                 if (result) {
401                         update_status(status_fn, status_arg, BOOT_STATUS_ERROR,
402                                         "kexec reboot failed");
403                 }
404         }
405
406         return result;
407 }