]> git.ozlabs.org Git - petitboot/blob - discover/device-handler.c
discover: Make device handler a little more testable
[petitboot] / discover / device-handler.c
1
2 #include <assert.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <string.h>
6 #include <errno.h>
7 #include <sys/stat.h>
8 #include <sys/wait.h>
9
10 #include <talloc/talloc.h>
11 #include <list/list.h>
12 #include <log/log.h>
13 #include <types/types.h>
14 #include <system/system.h>
15 #include <url/url.h>
16
17 #include "device-handler.h"
18 #include "discover-server.h"
19 #include "event.h"
20 #include "parser.h"
21 #include "resource.h"
22 #include "udev.h"
23 #include "paths.h"
24 #include "boot.h"
25
26 struct device_handler {
27         struct discover_server  *server;
28         int                     dry_run;
29
30         struct discover_device  **devices;
31         unsigned int            n_devices;
32
33         struct list             unresolved_boot_options;
34 };
35
36 void discover_context_add_boot_option(struct discover_context *ctx,
37                 struct discover_boot_option *boot_option)
38 {
39         boot_option->source = ctx->parser;
40         list_add_tail(&ctx->boot_options, &boot_option->list);
41         talloc_steal(ctx, boot_option);
42 }
43
44 /**
45  * device_handler_get_device_count - Get the count of current handler devices.
46  */
47
48 int device_handler_get_device_count(const struct device_handler *handler)
49 {
50         return handler->n_devices;
51 }
52
53 /**
54  * device_handler_get_device - Get a handler device by index.
55  */
56
57 const struct discover_device *device_handler_get_device(
58         const struct device_handler *handler, unsigned int index)
59 {
60         if (index >= handler->n_devices) {
61                 assert(0 && "bad index");
62                 return NULL;
63         }
64
65         return handler->devices[index];
66 }
67
68 struct discover_boot_option *discover_boot_option_create(
69                 struct discover_context *ctx,
70                 struct discover_device *device)
71 {
72         struct discover_boot_option *opt;
73
74         opt = talloc_zero(ctx, struct discover_boot_option);
75         opt->option = talloc_zero(opt, struct boot_option);
76         opt->device = device;
77
78         return opt;
79 }
80
81 static int device_match_path(struct discover_device *dev, const char *path)
82 {
83         return dev->device_path && !strcmp(dev->device_path, path);
84 }
85
86 static int device_match_uuid(struct discover_device *dev, const char *uuid)
87 {
88         return dev->uuid && !strcmp(dev->uuid, uuid);
89 }
90
91 static int device_match_label(struct discover_device *dev, const char *label)
92 {
93         return dev->label && !strcmp(dev->label, label);
94 }
95
96 static int device_match_id(struct discover_device *dev, const char *id)
97 {
98         return !strcmp(dev->device->id, id);
99 }
100
101 static struct discover_device *device_lookup(
102                 struct device_handler *device_handler,
103                 int (match_fn)(struct discover_device *, const char *),
104                 const char *str)
105 {
106         struct discover_device *dev;
107         unsigned int i;
108
109         if (!str)
110                 return NULL;
111
112         for (i = 0; i < device_handler->n_devices; i++) {
113                 dev = device_handler->devices[i];
114
115                 if (match_fn(dev, str))
116                         return dev;
117         }
118
119         return NULL;
120 }
121
122 struct discover_device *device_lookup_by_name(struct device_handler *handler,
123                 const char *name)
124 {
125         struct discover_device *dev;
126         char *path;
127
128         if (strncmp(name, "/dev/", strlen("/dev/")))
129                 path = talloc_asprintf(NULL, "/dev/%s", name);
130         else
131                 path = talloc_strdup(NULL, name);
132
133         dev = device_lookup_by_path(handler, path);
134
135         talloc_free(path);
136
137         return dev;
138 }
139
140 struct discover_device *device_lookup_by_path(
141                 struct device_handler *device_handler,
142                 const char *path)
143 {
144         return device_lookup(device_handler, device_match_path, path);
145 }
146
147 struct discover_device *device_lookup_by_uuid(
148                 struct device_handler *device_handler,
149                 const char *uuid)
150 {
151         return device_lookup(device_handler, device_match_uuid, uuid);
152 }
153
154 struct discover_device *device_lookup_by_label(
155                 struct device_handler *device_handler,
156                 const char *label)
157 {
158         return device_lookup(device_handler, device_match_label, label);
159 }
160
161 struct discover_device *device_lookup_by_id(
162                 struct device_handler *device_handler,
163                 const char *id)
164 {
165         return device_lookup(device_handler, device_match_id, id);
166 }
167
168 void device_handler_destroy(struct device_handler *handler)
169 {
170         talloc_free(handler);
171 }
172
173 #ifdef PETITBOOT_TEST
174
175 /* we have a simplified interface for petitboot testing, but still want
176  * to keep struct device_handler opaque. */
177 struct device_handler *device_handler_init(
178                 struct discover_server *server __attribute__((unused)),
179                 int dry_run __attribute__((unused)))
180 {
181         struct device_handler *handler;
182
183         handler = talloc_zero(NULL, struct device_handler);
184         list_init(&handler->unresolved_boot_options);
185
186         return handler;
187 }
188
189 void device_handler_add_device(struct device_handler *handler,
190                 struct discover_device *dev)
191 {
192         handler->n_devices++;
193         handler->devices = talloc_realloc(handler, handler->devices,
194                 struct discover_device *, handler->n_devices);
195         handler->devices[handler->n_devices - 1] = dev;
196 }
197
198 #else
199
200 static int mount_device(struct discover_device *dev)
201 {
202         const char *argv[6];
203
204         if (!dev->device_path)
205                 return -1;
206
207         if (!dev->mount_path)
208                 dev->mount_path = join_paths(dev, mount_base(),
209                                                 dev->device_path);
210
211         if (pb_mkdir_recursive(dev->mount_path))
212                 pb_log("couldn't create mount directory %s: %s\n",
213                                 dev->mount_path, strerror(errno));
214
215         argv[0] = pb_system_apps.mount;
216         argv[1] = dev->device_path;
217         argv[2] = dev->mount_path;
218         argv[3] = "-o";
219         argv[4] = "ro";
220         argv[5] = NULL;
221
222         if (pb_run_cmd(argv, 1, 0)) {
223
224                 /* Retry mount without ro option. */
225
226                 argv[0] = pb_system_apps.mount;
227                 argv[1] = dev->device_path;
228                 argv[2] = dev->mount_path;
229                 argv[3] = NULL;
230
231                 if (pb_run_cmd(argv, 1, 0))
232                         goto out_rmdir;
233         }
234
235         return 0;
236
237 out_rmdir:
238         pb_rmdir_recursive(mount_base(), dev->mount_path);
239         return -1;
240 }
241
242 static int umount_device(struct discover_device *dev)
243 {
244         int status;
245         pid_t pid;
246
247         if (!dev->mount_path)
248                 return 0;
249
250         pid = fork();
251         if (pid == -1) {
252                 pb_log("%s: fork failed: %s\n", __func__, strerror(errno));
253                 return -1;
254         }
255
256         if (pid == 0) {
257                 execl(pb_system_apps.umount, pb_system_apps.umount,
258                                                 dev->mount_path, NULL);
259                 exit(EXIT_FAILURE);
260         }
261
262         if (waitpid(pid, &status, 0) == -1) {
263                 pb_log("%s: waitpid failed: %s\n", __func__,
264                                 strerror(errno));
265                 return -1;
266         }
267
268         if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
269                 return -1;
270
271         pb_rmdir_recursive(mount_base(), dev->mount_path);
272
273         return 0;
274 }
275
276 struct device_handler *device_handler_init(struct discover_server *server,
277                 int dry_run)
278 {
279         struct device_handler *handler;
280
281         handler = talloc(NULL, struct device_handler);
282         handler->devices = NULL;
283         handler->n_devices = 0;
284         handler->server = server;
285         handler->dry_run = dry_run;
286         list_init(&handler->unresolved_boot_options);
287
288         /* set up our mount point base */
289         pb_mkdir_recursive(mount_base());
290
291         parser_init();
292
293         return handler;
294 }
295
296 static int destroy_device(void *arg)
297 {
298         struct discover_device *dev = arg;
299
300         umount_device(dev);
301
302         return 0;
303 }
304
305 static struct discover_device *find_device(struct device_handler *handler,
306                 const char *id)
307 {
308         struct discover_device *dev;
309         unsigned int i;
310
311         for (i = 0; i < handler->n_devices; i++) {
312                 dev = handler->devices[i];
313                 if (!strcmp(dev->device->id, id))
314                         return dev;
315         }
316
317         return NULL;
318 }
319
320 static struct discover_device *discover_device_create(
321                 struct device_handler *handler,
322                 struct discover_context *ctx,
323                 struct event *event)
324 {
325         struct discover_device *dev;
326         const char *devname;
327
328         dev = find_device(handler, event->device);
329         if (dev)
330                 return dev;
331
332         dev = talloc_zero(ctx, struct discover_device);
333         dev->device = talloc_zero(dev, struct device);
334         list_init(&dev->boot_options);
335
336         devname = event_get_param(ctx->event, "DEVNAME");
337         if (devname)
338                 dev->device_path = talloc_strdup(dev, devname);
339
340         dev->device->id = talloc_strdup(dev, event->device);
341
342         talloc_set_destructor(dev, destroy_device);
343
344         return dev;
345 }
346
347 /**
348  * device_handler_remove - Remove a device from the handler device array.
349  */
350
351 static void device_handler_remove(struct device_handler *handler,
352         struct discover_device *device)
353 {
354         unsigned int i;
355
356         for (i = 0; i < handler->n_devices; i++)
357                 if (handler->devices[i] == device)
358                         break;
359
360         if (i == handler->n_devices) {
361                 assert(0 && "unknown device");
362                 return;
363         }
364
365         handler->n_devices--;
366         memmove(&handler->devices[i], &handler->devices[i + 1],
367                 (handler->n_devices - i) * sizeof(handler->devices[0]));
368         handler->devices = talloc_realloc(handler, handler->devices,
369                 struct discover_device *, handler->n_devices);
370
371         discover_server_notify_device_remove(handler->server, device->device);
372
373         talloc_free(device);
374 }
375
376 static bool resource_is_resolved(struct resource *res)
377 {
378         return !res || res->resolved;
379 }
380
381 /* We only use this in an assert, which will disappear if we're compiling
382  * with NDEBUG, so we need the 'used' attribute for these builds */
383 static bool __attribute__((used)) boot_option_is_resolved(
384                 struct discover_boot_option *opt)
385 {
386         return resource_is_resolved(opt->boot_image) &&
387                 resource_is_resolved(opt->initrd) &&
388                 resource_is_resolved(opt->icon);
389 }
390
391 static bool resource_resolve(struct resource *res, struct parser *parser,
392                 struct device_handler *handler)
393 {
394         if (resource_is_resolved(res))
395                 return true;
396
397         parser->resolve_resource(handler, res);
398
399         return res->resolved;
400 }
401
402 static bool boot_option_resolve(struct discover_boot_option *opt,
403                 struct device_handler *handler)
404 {
405         return resource_resolve(opt->boot_image, opt->source, handler) &&
406                 resource_resolve(opt->initrd, opt->source, handler) &&
407                 resource_resolve(opt->icon, opt->source, handler);
408 }
409
410 static void boot_option_finalise(struct discover_boot_option *opt)
411 {
412         assert(boot_option_is_resolved(opt));
413
414         /* check that the parsers haven't set any of the final data */
415         assert(!opt->option->boot_image_file);
416         assert(!opt->option->initrd_file);
417         assert(!opt->option->icon_file);
418         assert(!opt->option->device_id);
419
420         if (opt->boot_image)
421                 opt->option->boot_image_file = opt->boot_image->url->full;
422         if (opt->initrd)
423                 opt->option->initrd_file = opt->initrd->url->full;
424         if (opt->icon)
425                 opt->option->icon_file = opt->icon->url->full;
426
427         opt->option->device_id = opt->device->device->id;
428 }
429
430 static void process_boot_option_queue(struct device_handler *handler)
431 {
432         struct discover_boot_option *opt, *tmp;
433
434         list_for_each_entry_safe(&handler->unresolved_boot_options,
435                         opt, tmp, list) {
436
437                 if (!boot_option_resolve(opt, handler))
438                         continue;
439
440                 list_remove(&opt->list);
441                 list_add(&opt->device->boot_options, &opt->list);
442                 talloc_steal(opt->device, opt);
443                 boot_option_finalise(opt);
444                 discover_server_notify_boot_option_add(handler->server,
445                                                         opt->option);
446         }
447 }
448
449 /**
450  * context_commit - Commit a temporary discovery context to the handler,
451  * and notify the clients about any new options / devices
452  */
453 static void context_commit(struct device_handler *handler,
454                 struct discover_context *ctx)
455 {
456         struct discover_device *dev = ctx->device;
457         struct discover_boot_option *opt, *tmp;
458         unsigned int i, existing_device = 0;
459
460         /* do we already have this device? */
461         for (i = 0; i < handler->n_devices; i++) {
462                 if (ctx->device == handler->devices[i]) {
463                         existing_device = 1;
464                         break;
465                 }
466         }
467
468         /* if not already present, add the device to the handler's array */
469         if (!existing_device) {
470                 handler->n_devices++;
471                 handler->devices = talloc_realloc(handler, handler->devices,
472                         struct discover_device *, handler->n_devices);
473                 handler->devices[handler->n_devices - 1] = dev;
474                 talloc_steal(handler, dev);
475
476                 discover_server_notify_device_add(handler->server, dev->device);
477
478                 /* this new device might be able to resolve existing boot
479                  * options */
480                 process_boot_option_queue(handler);
481         }
482
483
484         /* move boot options from the context to the device */
485         list_for_each_entry_safe(&ctx->boot_options, opt, tmp, list) {
486                 list_remove(&opt->list);
487
488                 if (boot_option_resolve(opt, handler)) {
489                         list_add(&dev->boot_options, &opt->list);
490                         talloc_steal(dev, opt);
491                         boot_option_finalise(opt);
492                         discover_server_notify_boot_option_add(handler->server,
493                                                                 opt->option);
494                 } else {
495                         if (!opt->source->resolve_resource) {
496                                 pb_log("parser %s gave us an unresolved "
497                                         "resource (%s), but no way to "
498                                         "resolve it\n",
499                                         opt->source->name, opt->option->id);
500                                 talloc_free(opt);
501                         } else {
502                                 pb_log("boot option %s is unresolved, "
503                                                 "adding to queue\n",
504                                                 opt->option->id);
505                                 list_add(&handler->unresolved_boot_options,
506                                                 &opt->list);
507                                 talloc_steal(handler, opt);
508                         }
509                 }
510         }
511 }
512
513 static int handle_add_udev_event(struct device_handler *handler,
514                 struct event *event)
515 {
516         struct discover_context *ctx;
517         struct discover_device *dev;
518         const char *param;
519         int rc;
520
521         /* create our context */
522         ctx = talloc(handler, struct discover_context);
523         ctx->event = event;
524         list_init(&ctx->boot_options);
525
526         /* create our top-level device */
527         dev = discover_device_create(handler, ctx, event);
528
529         ctx->device = dev;
530
531         /* try to parse UUID and labels */
532         param = event_get_param(ctx->event, "ID_FS_UUID");
533         if (param)
534                 dev->uuid = talloc_strdup(dev, param);
535
536         param = event_get_param(ctx->event, "ID_FS_LABEL");
537         if (param)
538                 dev->label = talloc_strdup(dev, param);
539
540         rc = mount_device(dev);
541         if (rc) {
542                 talloc_free(ctx);
543                 return 0;
544         }
545
546         /* run the parsers. This will populate the ctx's boot_option list. */
547         iterate_parsers(ctx, CONF_METHOD_LOCAL_FILE);
548
549         /* add discovered stuff to the handler */
550         context_commit(handler, ctx);
551
552         talloc_free(ctx);
553
554         return 0;
555 }
556
557 static int handle_remove_udev_event(struct device_handler *handler,
558                 struct event *event)
559 {
560         struct discover_device *dev;
561
562         dev = find_device(handler, event->device);
563         if (!dev)
564                 return 0;
565
566         /* remove device from handler device array */
567         device_handler_remove(handler, dev);
568
569         return 0;
570 }
571
572 static int handle_add_user_event(struct device_handler *handler,
573                 struct event *event)
574 {
575         struct discover_context *ctx;
576         struct discover_device *dev;
577         int rc;
578
579         assert(event->device);
580
581         ctx = talloc(handler, struct discover_context);
582         ctx->event = event;
583         list_init(&ctx->boot_options);
584
585         dev = discover_device_create(handler, ctx, event);
586         ctx->device = dev;
587
588         rc = parse_user_event(ctx, event);
589
590         if (!rc)
591                 context_commit(handler, ctx);
592
593         return rc;
594 }
595
596 static int handle_remove_user_event(struct device_handler *handler,
597                 struct event *event)
598 {
599         struct discover_device *dev = find_device(handler, event->device);
600
601         if (!dev)
602                 return 0;
603
604         /* remove device from handler device array */
605         device_handler_remove(handler, dev);
606
607         return 0;
608 }
609
610 static enum conf_method parse_conf_method(const char *str)
611 {
612
613         if (!strcasecmp(str, "dhcp")) {
614                 return CONF_METHOD_DHCP;
615         }
616         return CONF_METHOD_UNKNOWN;
617 }
618
619 static int handle_conf_user_event(struct device_handler *handler,
620                 struct event *event)
621 {
622         struct discover_context *ctx;
623         struct discover_device *dev;
624         enum conf_method method;
625         const char *val;
626
627         ctx = talloc(handler, struct discover_context);
628         ctx->event = event;
629         list_init(&ctx->boot_options);
630
631         val = event_get_param(event, "url");
632         if (!val) {
633                 talloc_free(ctx);
634                 return 0;
635         }
636
637         ctx->conf_url = pb_url_parse(ctx, val);
638         if (!ctx->conf_url) {
639                 talloc_free(ctx);
640                 return 0;
641         }
642
643         val = event_get_param(event, "method");
644         if (!val) {
645                 talloc_free(ctx);
646                 return 0;
647         }
648
649         method = parse_conf_method(val);
650         if (method == CONF_METHOD_UNKNOWN) {
651                 talloc_free(ctx);
652                 return 0;
653         }
654
655         dev = discover_device_create(handler, ctx, event);
656         ctx->device = dev;
657
658         iterate_parsers(ctx, method);
659
660         context_commit(handler, ctx);
661
662         return 0;
663 }
664
665 typedef int (*event_handler)(struct device_handler *, struct event *);
666
667 static event_handler handlers[EVENT_TYPE_MAX][EVENT_ACTION_MAX] = {
668         [EVENT_TYPE_UDEV] = {
669                 [EVENT_ACTION_ADD]      = handle_add_udev_event,
670                 [EVENT_ACTION_REMOVE]   = handle_remove_udev_event,
671         },
672         [EVENT_TYPE_USER] = {
673                 [EVENT_ACTION_ADD]      = handle_add_user_event,
674                 [EVENT_ACTION_REMOVE]   = handle_remove_user_event,
675                 [EVENT_ACTION_CONF]     = handle_conf_user_event,
676         }
677 };
678
679 int device_handler_event(struct device_handler *handler,
680                 struct event *event)
681 {
682         if (event->type >= EVENT_TYPE_MAX ||
683                         event->action >= EVENT_ACTION_MAX ||
684                         !handlers[event->type][event->action]) {
685                 pb_log("%s unknown type/action: %d/%d\n", __func__,
686                                 event->type, event->action);
687                 return 0;
688         }
689
690         return handlers[event->type][event->action](handler, event);
691 }
692
693 static void boot_status(void *arg, struct boot_status *status)
694 {
695         struct device_handler *handler = arg;
696
697         discover_server_notify_boot_status(handler->server, status);
698 }
699
700 static struct discover_boot_option *find_boot_option_by_id(
701                 struct device_handler *handler, const char *id)
702 {
703         unsigned int i;
704
705         for (i = 0; i < handler->n_devices; i++) {
706                 struct discover_device *dev = handler->devices[i];
707                 struct discover_boot_option *opt;
708
709                 list_for_each_entry(&dev->boot_options, opt, list)
710                         if (!strcmp(opt->option->id, id))
711                                 return opt;
712         }
713
714         return NULL;
715 }
716
717 void device_handler_boot(struct device_handler *handler,
718                 struct boot_command *cmd)
719 {
720         struct discover_boot_option *opt;
721
722         opt = find_boot_option_by_id(handler, cmd->option_id);
723
724         boot(handler, opt, cmd, handler->dry_run, boot_status, handler);
725 }
726 #endif