2b16b2ea2f7a9c937b864b66d28996cca9bae0cf
[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
16 #include "device-handler.h"
17 #include "discover-server.h"
18 #include "event.h"
19 #include "parser.h"
20 #include "udev.h"
21 #include "paths.h"
22 #include "boot.h"
23
24 struct device_handler {
25         struct discover_server  *server;
26         int                     dry_run;
27
28         struct discover_device  **devices;
29         unsigned int            n_devices;
30 };
31
32 /**
33  * context_commit - Commit a temporary discovery context to the handler,
34  * and notify the clients about any new options / devices
35  */
36 static void context_commit(struct device_handler *handler,
37                 struct discover_context *ctx)
38 {
39         struct discover_device *dev = ctx->device;
40         struct discover_boot_option *opt, *tmp;
41         unsigned int i, existing_device;
42
43         /* do we already have this device? */
44         for (i = 0; i < handler->n_devices; i++) {
45                 if (ctx->device == handler->devices[i]) {
46                         existing_device = 1;
47                         break;
48                 }
49         }
50
51         /* if not already present, add the device to the handler's array */
52         if (!existing_device) {
53                 handler->n_devices++;
54                 handler->devices = talloc_realloc(handler, handler->devices,
55                         struct discover_device *, handler->n_devices);
56                 handler->devices[handler->n_devices - 1] = dev;
57                 talloc_steal(handler, dev);
58
59                 discover_server_notify_device_add(handler->server, dev->device);
60         }
61
62
63         /* move boot options from the context to the device */
64         list_for_each_entry_safe(&ctx->boot_options, opt, tmp, list) {
65                 list_remove(&opt->list);
66                 list_add(&dev->boot_options, &opt->list);
67                 talloc_steal(dev, opt);
68                 discover_server_notify_boot_option_add(handler->server,
69                                                         opt->option);
70         }
71 }
72
73 void discover_context_add_boot_option(struct discover_context *ctx,
74                 struct discover_boot_option *boot_option)
75 {
76         list_add(&ctx->boot_options, &boot_option->list);
77         talloc_steal(ctx, boot_option);
78 }
79
80 /**
81  * device_handler_remove - Remove a device from the handler device array.
82  */
83
84 static void device_handler_remove(struct device_handler *handler,
85         struct discover_device *device)
86 {
87         unsigned int i;
88
89         for (i = 0; i < handler->n_devices; i++)
90                 if (handler->devices[i] == device)
91                         break;
92
93         if (i == handler->n_devices) {
94                 assert(0 && "unknown device");
95                 return;
96         }
97
98         handler->n_devices--;
99         memmove(&handler->devices[i], &handler->devices[i + 1],
100                 (handler->n_devices - i) * sizeof(handler->devices[0]));
101         handler->devices = talloc_realloc(handler, handler->devices,
102                 struct discover_device *, handler->n_devices);
103
104         discover_server_notify_device_remove(handler->server, device->device);
105
106         talloc_free(device);
107 }
108
109 /**
110  * device_handler_get_device_count - Get the count of current handler devices.
111  */
112
113 int device_handler_get_device_count(const struct device_handler *handler)
114 {
115         return handler->n_devices;
116 }
117
118 /**
119  * device_handler_get_device - Get a handler device by index.
120  */
121
122 const struct discover_device *device_handler_get_device(
123         const struct device_handler *handler, unsigned int index)
124 {
125         if (index >= handler->n_devices) {
126                 assert(0 && "bad index");
127                 return NULL;
128         }
129
130         return handler->devices[index];
131 }
132
133 static void setup_device_links(struct discover_device *dev)
134 {
135         struct link {
136                 const char *dir, *val;
137         } *link, links[] = {
138                 {
139                         .dir = "disk/by-uuid",
140                         .val = dev->uuid,
141                 },
142                 {
143                         .dir = "disk/by-label",
144                         .val = dev->label,
145                 },
146                 {
147                         .dir = NULL
148                 }
149         };
150
151         for (link = links; link->dir; link++) {
152                 char *enc, *dir, *path;
153
154                 if (!link->val || !*link->val)
155                         continue;
156
157                 enc = encode_label(dev, link->val);
158                 dir = join_paths(dev, mount_base(), link->dir);
159                 path = join_paths(dev, dir, enc);
160
161                 if (!pb_mkdir_recursive(dir)) {
162                         unlink(path);
163                         if (symlink(dev->mount_path, path)) {
164                                 pb_log("symlink(%s,%s): %s\n",
165                                                 dev->mount_path, path,
166                                                 strerror(errno));
167                                 talloc_free(path);
168                         } else {
169                                 int i = dev->n_links++;
170                                 dev->links = talloc_realloc(dev,
171                                                 dev->links, char *,
172                                                 dev->n_links);
173                                 dev->links[i] = path;
174                         }
175
176                 }
177
178                 talloc_free(dir);
179                 talloc_free(enc);
180         }
181 }
182
183 static void remove_device_links(struct discover_device *dev)
184 {
185         int i;
186
187         for (i = 0; i < dev->n_links; i++)
188                 unlink(dev->links[i]);
189 }
190
191 static int mount_device(struct discover_device *dev)
192 {
193         const char *mountpoint;
194         const char *argv[6];
195
196         if (!dev->mount_path) {
197                 mountpoint = mountpoint_for_device(dev->device_path);
198                 dev->mount_path = talloc_strdup(dev, mountpoint);
199         }
200
201         if (pb_mkdir_recursive(dev->mount_path))
202                 pb_log("couldn't create mount directory %s: %s\n",
203                                 dev->mount_path, strerror(errno));
204
205         argv[0] = pb_system_apps.mount;
206         argv[1] = dev->device_path;
207         argv[2] = dev->mount_path;
208         argv[3] = "-o";
209         argv[4] = "ro";
210         argv[5] = NULL;
211
212         if (pb_run_cmd(argv, 1, 0)) {
213
214                 /* Retry mount without ro option. */
215
216                 argv[0] = pb_system_apps.mount;
217                 argv[1] = dev->device_path;
218                 argv[2] = dev->mount_path;
219                 argv[3] = NULL;
220
221                 if (pb_run_cmd(argv, 1, 0))
222                         goto out_rmdir;
223         }
224
225         setup_device_links(dev);
226         return 0;
227
228 out_rmdir:
229         pb_rmdir_recursive(mount_base(), dev->mount_path);
230         return -1;
231 }
232
233 static int umount_device(struct discover_device *dev)
234 {
235         int status;
236         pid_t pid;
237
238         remove_device_links(dev);
239
240         if (!dev->mount_path)
241                 return 0;
242
243         pid = fork();
244         if (pid == -1) {
245                 pb_log("%s: fork failed: %s\n", __func__, strerror(errno));
246                 return -1;
247         }
248
249         if (pid == 0) {
250                 execl(pb_system_apps.umount, pb_system_apps.umount,
251                                                 dev->mount_path, NULL);
252                 exit(EXIT_FAILURE);
253         }
254
255         if (waitpid(pid, &status, 0) == -1) {
256                 pb_log("%s: waitpid failed: %s\n", __func__,
257                                 strerror(errno));
258                 return -1;
259         }
260
261         if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
262                 return -1;
263
264         pb_rmdir_recursive(mount_base(), dev->mount_path);
265
266         return 0;
267 }
268
269 static struct discover_device *find_device(struct device_handler *handler,
270                 const char *id)
271 {
272         struct discover_device *dev;
273         unsigned int i;
274
275         for (i = 0; i < handler->n_devices; i++) {
276                 dev = handler->devices[i];
277                 if (!strcmp(dev->device->id, id))
278                         return dev;
279         }
280
281         return NULL;
282 }
283
284 static int destroy_device(void *arg)
285 {
286         struct discover_device *dev = arg;
287
288         umount_device(dev);
289
290         return 0;
291 }
292
293 static struct discover_device *discover_device_create(
294                 struct device_handler *handler,
295                 struct discover_context *ctx,
296                 struct event *event)
297 {
298         struct discover_device *dev;
299         const char *devname;
300
301         dev = find_device(handler, event->device);
302         if (dev)
303                 return dev;
304
305         dev = talloc_zero(ctx, struct discover_device);
306         dev->device = talloc_zero(dev, struct device);
307         list_init(&dev->boot_options);
308
309         devname = event_get_param(ctx->event, "DEVNAME");
310         if (devname)
311                 dev->device_path = talloc_strdup(dev, devname);
312
313         dev->device->id = talloc_strdup(dev, event->device);
314
315         talloc_set_destructor(dev, destroy_device);
316
317         return dev;
318 }
319
320 struct discover_boot_option *discover_boot_option_create(
321                 struct discover_context *ctx,
322                 struct discover_device *device)
323 {
324         struct discover_boot_option *opt;
325
326         opt = talloc_zero(ctx, struct discover_boot_option);
327         opt->option = talloc_zero(opt, struct boot_option);
328         opt->device = device;
329
330         return opt;
331 }
332
333
334 static int handle_add_udev_event(struct device_handler *handler,
335                 struct event *event)
336 {
337         struct discover_context *ctx;
338         struct discover_device *dev;
339         const char *param;
340         int rc;
341
342         /* create our context */
343         ctx = talloc(handler, struct discover_context);
344         ctx->event = event;
345         list_init(&ctx->boot_options);
346
347         /* create our top-level device */
348         dev = discover_device_create(handler, ctx, event);
349
350         ctx->device = dev;
351
352         /* try to parse UUID and labels */
353         param = event_get_param(ctx->event, "ID_FS_UUID");
354         if (param)
355                 dev->uuid = talloc_strdup(dev, param);
356
357         param = event_get_param(ctx->event, "ID_FS_LABEL");
358         if (param)
359                 dev->label = talloc_strdup(dev, param);
360
361         rc = mount_device(dev);
362         if (rc) {
363                 talloc_free(ctx);
364                 return 0;
365         }
366
367         /* run the parsers. This will populate the ctx's boot_option list. */
368         iterate_parsers(ctx);
369
370         /* add discovered stuff to the handler */
371         context_commit(handler, ctx);
372
373         talloc_free(ctx);
374
375         return 0;
376 }
377
378 static int handle_remove_udev_event(struct device_handler *handler,
379                 struct event *event)
380 {
381         struct discover_device *dev;
382
383         dev = find_device(handler, event->device);
384         if (!dev)
385                 return 0;
386
387         /* remove device from handler device array */
388         device_handler_remove(handler, dev);
389
390         return 0;
391 }
392
393 static int handle_add_user_event(struct device_handler *handler,
394                 struct event *event)
395 {
396         struct discover_context *ctx;
397         struct discover_device *dev;
398         int rc;
399
400         assert(event->device);
401
402         ctx = talloc(handler, struct discover_context);
403         ctx->event = event;
404         list_init(&ctx->boot_options);
405
406         dev = discover_device_create(handler, ctx, event);
407         ctx->device = dev;
408
409         rc = parse_user_event(ctx, event);
410
411         if (!rc)
412                 context_commit(handler, ctx);
413
414         return rc;
415 }
416
417 static int handle_remove_user_event(struct device_handler *handler,
418                 struct event *event)
419 {
420         struct discover_device *dev = find_device(handler, event->device);
421
422         if (!dev)
423                 return 0;
424
425         /* remove device from handler device array */
426         device_handler_remove(handler, dev);
427
428         return 0;
429 }
430
431 typedef int (*event_handler)(struct device_handler *, struct event *);
432
433 static event_handler handlers[EVENT_TYPE_MAX][EVENT_ACTION_MAX] = {
434         [EVENT_TYPE_UDEV] = {
435                 [EVENT_ACTION_ADD]      = handle_add_udev_event,
436                 [EVENT_ACTION_REMOVE]   = handle_remove_udev_event,
437         },
438         [EVENT_TYPE_USER] = {
439                 [EVENT_ACTION_ADD]      = handle_add_user_event,
440                 [EVENT_ACTION_REMOVE]   = handle_remove_user_event,
441         }
442 };
443
444 int device_handler_event(struct device_handler *handler,
445                 struct event *event)
446 {
447         if (event->type >= EVENT_TYPE_MAX ||
448                         event->action >= EVENT_ACTION_MAX ||
449                         !handlers[event->type][event->action]) {
450                 pb_log("%s unknown type/action: %d/%d\n", __func__,
451                                 event->type, event->action);
452                 return 0;
453         }
454
455         return handlers[event->type][event->action](handler, event);
456 }
457
458 struct device_handler *device_handler_init(struct discover_server *server,
459                 int dry_run)
460 {
461         struct device_handler *handler;
462
463         handler = talloc(NULL, struct device_handler);
464         handler->devices = NULL;
465         handler->n_devices = 0;
466         handler->server = server;
467         handler->dry_run = dry_run;
468
469         /* set up our mount point base */
470         pb_mkdir_recursive(mount_base());
471
472         parser_init();
473
474         return handler;
475 }
476
477 void device_handler_destroy(struct device_handler *handler)
478 {
479         talloc_free(handler);
480 }
481
482 static int device_match_path(struct discover_device *dev, const char *path)
483 {
484         return !strcmp(dev->device_path, path);
485 }
486
487 static int device_match_uuid(struct discover_device *dev, const char *uuid)
488 {
489         return dev->uuid && !strcmp(dev->uuid, uuid);
490 }
491
492 static int device_match_label(struct discover_device *dev, const char *label)
493 {
494         return dev->label && !strcmp(dev->label, label);
495 }
496
497 static int device_match_id(struct discover_device *dev, const char *id)
498 {
499         return !strcmp(dev->device->id, id);
500 }
501
502 static struct discover_device *device_lookup(
503                 struct device_handler *device_handler,
504                 int (match_fn)(struct discover_device *, const char *),
505                 const char *str)
506 {
507         struct discover_device *dev;
508         unsigned int i;
509
510         if (!str)
511                 return NULL;
512
513         for (i = 0; i < device_handler->n_devices; i++) {
514                 dev = device_handler->devices[i];
515
516                 if (match_fn(dev, str))
517                         return dev;
518         }
519
520         return NULL;
521 }
522
523 struct discover_device *device_lookup_by_name(struct device_handler *handler,
524                 const char *name)
525 {
526         struct discover_device *dev;
527         char *path;
528
529         if (strncmp(name, "/dev/", strlen("/dev/")))
530                 path = talloc_asprintf(NULL, "/dev/%s", name);
531         else
532                 path = talloc_strdup(NULL, name);
533
534         dev = device_lookup_by_path(handler, path);
535
536         talloc_free(path);
537
538         return dev;
539 }
540
541 struct discover_device *device_lookup_by_path(
542                 struct device_handler *device_handler,
543                 const char *path)
544 {
545         return device_lookup(device_handler, device_match_path, path);
546 }
547
548 struct discover_device *device_lookup_by_uuid(
549                 struct device_handler *device_handler,
550                 const char *uuid)
551 {
552         return device_lookup(device_handler, device_match_uuid, uuid);
553 }
554
555 struct discover_device *device_lookup_by_label(
556                 struct device_handler *device_handler,
557                 const char *label)
558 {
559         return device_lookup(device_handler, device_match_label, label);
560 }
561
562 struct discover_device *device_lookup_by_id(
563                 struct device_handler *device_handler,
564                 const char *id)
565 {
566         return device_lookup(device_handler, device_match_id, id);
567 }
568
569 static struct discover_boot_option *find_boot_option_by_id(
570                 struct device_handler *handler, const char *id)
571 {
572         unsigned int i;
573
574         for (i = 0; i < handler->n_devices; i++) {
575                 struct discover_device *dev = handler->devices[i];
576                 struct discover_boot_option *opt;
577
578                 list_for_each_entry(&dev->boot_options, opt, list)
579                         if (!strcmp(opt->option->id, id))
580                                 return opt;
581         }
582
583         return NULL;
584 }
585
586 void device_handler_boot(struct device_handler *handler,
587                 struct boot_command *cmd)
588 {
589         struct discover_boot_option *opt;
590
591         opt = find_boot_option_by_id(handler, cmd->option_id);
592
593         boot(handler, opt->option, cmd, handler->dry_run);
594 }