]> git.ozlabs.org Git - petitboot/blob - discover/device-handler.c
discover: Implement device handler boot path
[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
27         struct device **devices;
28         unsigned int n_devices;
29
30         struct list contexts;
31 };
32
33 /**
34  * device_handler_add - Add a device to the handler device array.
35  */
36
37 static void device_handler_add(struct device_handler *handler,
38         struct device *device)
39 {
40         handler->n_devices++;
41         handler->devices = talloc_realloc(handler, handler->devices,
42                 struct device *, handler->n_devices);
43         handler->devices[handler->n_devices - 1] = device;
44 }
45
46 /**
47  * device_handler_remove - Remove a device from the handler device array.
48  */
49
50 static void device_handler_remove(struct device_handler *handler,
51         struct device *device)
52 {
53         unsigned int i;
54
55         for (i = 0; i < handler->n_devices; i++)
56                 if (handler->devices[i] == device)
57                         break;
58
59         if (i == handler->n_devices) {
60                 assert(0 && "unknown device");
61                 return;
62         }
63
64         handler->n_devices--;
65         memmove(&handler->devices[i], &handler->devices[i + 1],
66                 (handler->n_devices - i) * sizeof(handler->devices[0]));
67         handler->devices = talloc_realloc(handler, handler->devices,
68                 struct device *, handler->n_devices);
69 }
70
71 /**
72  * device_handler_find - Find a handler device by id.
73  */
74
75 static struct device *device_handler_find(struct device_handler *handler,
76         const char *id)
77 {
78         unsigned int i;
79
80         assert(id);
81
82         for (i = 0; i < handler->n_devices; i++)
83                 if (handler->devices[i]->id
84                         && streq(handler->devices[i]->id, id))
85                         return handler->devices[i];
86
87         pb_log("%s: unknown device: %s\n", __func__, id);
88         return NULL;
89 }
90
91 /**
92  * device_handler_get_device_count - Get the count of current handler devices.
93  */
94
95 int device_handler_get_device_count(const struct device_handler *handler)
96 {
97         return handler->n_devices;
98 }
99
100 /**
101  * device_handler_get_device - Get a handler device by index.
102  */
103
104 const struct device *device_handler_get_device(
105         const struct device_handler *handler, unsigned int index)
106 {
107         if (index >= handler->n_devices) {
108                 assert(0 && "bad index");
109                 return NULL;
110         }
111
112         return handler->devices[index];
113 }
114
115 static void setup_device_links(struct discover_context *ctx)
116 {
117         struct link {
118                 char *env, *dir;
119         } *link, links[] = {
120                 {
121                         .env = "ID_FS_UUID",
122                         .dir = "disk/by-uuid"
123                 },
124                 {
125                         .env = "ID_FS_LABEL",
126                         .dir = "disk/by-label"
127                 },
128                 {
129                         .env = NULL
130                 }
131         };
132
133         for (link = links; link->env; link++) {
134                 char *enc, *dir, *path;
135                 const char *value;
136
137                 value = event_get_param(ctx->event, link->env);
138                 if (!value || !*value)
139                         continue;
140
141                 enc = encode_label(ctx, value);
142                 dir = join_paths(ctx, mount_base(), link->dir);
143                 path = join_paths(ctx, dir, value);
144
145                 if (!pb_mkdir_recursive(dir)) {
146                         unlink(path);
147                         if (symlink(ctx->mount_path, path)) {
148                                 pb_log("symlink(%s,%s): %s\n",
149                                                 ctx->mount_path, path,
150                                                 strerror(errno));
151                                 talloc_free(path);
152                         } else {
153                                 int i = ctx->n_links++;
154                                 ctx->links = talloc_realloc(ctx,
155                                                 ctx->links, char *,
156                                                 ctx->n_links);
157                                 ctx->links[i] = path;
158                         }
159
160                 }
161
162                 talloc_free(dir);
163                 talloc_free(enc);
164         }
165 }
166
167 static void remove_device_links(struct discover_context *ctx)
168 {
169         int i;
170
171         for (i = 0; i < ctx->n_links; i++)
172                 unlink(ctx->links[i]);
173 }
174
175 static int mount_device(struct discover_context *ctx)
176 {
177         const char *mountpoint;
178         const char *argv[6];
179
180         if (!ctx->mount_path) {
181                 mountpoint = mountpoint_for_device(ctx->device_path);
182                 ctx->mount_path = talloc_strdup(ctx, mountpoint);
183         }
184
185         if (pb_mkdir_recursive(ctx->mount_path))
186                 pb_log("couldn't create mount directory %s: %s\n",
187                                 ctx->mount_path, strerror(errno));
188
189         argv[0] = pb_system_apps.mount;
190         argv[1] = ctx->device_path;
191         argv[2] = ctx->mount_path;
192         argv[3] = "-o";
193         argv[4] = "ro";
194         argv[5] = NULL;
195
196         if (pb_run_cmd(argv, 1, 0)) {
197
198                 /* Retry mount without ro option. */
199
200                 argv[0] = pb_system_apps.mount;
201                 argv[1] = ctx->device_path;
202                 argv[2] = ctx->mount_path;
203                 argv[3] = NULL;
204
205                 if (pb_run_cmd(argv, 1, 0))
206                         goto out_rmdir;
207         }
208
209         setup_device_links(ctx);
210         return 0;
211
212 out_rmdir:
213         pb_rmdir_recursive(mount_base(), ctx->mount_path);
214         return -1;
215 }
216
217 static int umount_device(struct discover_context *ctx)
218 {
219         int status;
220         pid_t pid;
221
222         remove_device_links(ctx);
223
224         pid = fork();
225         if (pid == -1) {
226                 pb_log("%s: fork failed: %s\n", __func__, strerror(errno));
227                 return -1;
228         }
229
230         if (pid == 0) {
231                 execl(pb_system_apps.umount, pb_system_apps.umount,
232                                                 ctx->mount_path, NULL);
233                 exit(EXIT_FAILURE);
234         }
235
236         if (waitpid(pid, &status, 0) == -1) {
237                 pb_log("%s: waitpid failed: %s\n", __func__,
238                                 strerror(errno));
239                 return -1;
240         }
241
242         if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
243                 return -1;
244
245         pb_rmdir_recursive(mount_base(), ctx->mount_path);
246
247         return 0;
248 }
249
250 static struct discover_context *find_context(struct device_handler *handler,
251                 const char *id)
252 {
253         struct discover_context *ctx;
254
255         list_for_each_entry(&handler->contexts, ctx, list) {
256                 if (!strcmp(ctx->id, id))
257                         return ctx;
258         }
259
260         return NULL;
261 }
262
263 static int destroy_context(void *arg)
264 {
265         struct discover_context *ctx = arg;
266
267         list_remove(&ctx->list);
268         umount_device(ctx);
269
270         return 0;
271 }
272
273 static int handle_add_udev_event(struct device_handler *handler,
274                 struct event *event)
275 {
276         struct discover_context *ctx;
277         const char *devname;
278         int rc;
279
280         /* create our context */
281         ctx = talloc(handler, struct discover_context);
282         ctx->event = event;
283         ctx->mount_path = NULL;
284         ctx->links = NULL;
285         ctx->n_links = 0;
286
287         ctx->id = talloc_strdup(ctx, event->device);
288
289         devname = event_get_param(ctx->event, "DEVNAME");
290         assert(devname);
291         ctx->device_path = talloc_strdup(ctx, devname);
292
293         rc = mount_device(ctx);
294         if (rc) {
295                 talloc_free(ctx);
296                 return 0;
297         }
298
299         list_add(&handler->contexts, &ctx->list);
300         talloc_set_destructor(ctx, destroy_context);
301
302         /* set up the top-level device */
303         ctx->device = talloc_zero(ctx, struct device);
304         ctx->device->id = talloc_strdup(ctx->device, ctx->id);
305         list_init(&ctx->device->boot_options);
306
307         /* run the parsers */
308         iterate_parsers(ctx);
309
310         /* add device to handler device array */
311         device_handler_add(handler, ctx->device);
312
313         discover_server_notify_add(handler->server, ctx->device);
314
315         return 0;
316 }
317
318 static int handle_remove_udev_event(struct device_handler *handler,
319                 struct event *event)
320 {
321         struct discover_context *ctx;
322
323         ctx = find_context(handler, event->device);
324         if (!ctx)
325                 return 0;
326
327         discover_server_notify_remove(handler->server, ctx->device);
328
329         /* remove device from handler device array */
330         device_handler_remove(handler, ctx->device);
331
332         talloc_free(ctx);
333
334         return 0;
335 }
336
337 static int handle_add_user_event(struct device_handler *handler,
338                 struct event *event)
339 {
340         struct device *device;
341
342         assert(event->device);
343
344         device = talloc_zero(handler, struct device);
345
346         if (!device)
347                 goto fail;
348
349         device->id = talloc_strdup(device, event->device);
350         list_init(&device->boot_options);
351
352         parse_user_event(device, event);
353
354         discover_server_notify_add(handler->server, device);
355
356         /* add device to handler device array */
357         device_handler_add(handler, device);
358
359         return 0;
360
361 fail:
362         talloc_free(device);
363         return 0;
364 }
365
366 static int handle_remove_user_event(struct device_handler *handler,
367                 struct event *event)
368 {
369         struct device *device = device_handler_find(handler, event->device);
370
371         if (!device)
372                 return 0;
373
374         discover_server_notify_remove(handler->server, device);
375
376         /* remove device from handler device array */
377         device_handler_remove(handler, device);
378
379         talloc_free(device);
380         return 0;
381 }
382
383 typedef int (*event_handler)(struct device_handler *, struct event *);
384
385 static event_handler handlers[EVENT_TYPE_MAX][EVENT_ACTION_MAX] = {
386         [EVENT_TYPE_UDEV] = {
387                 [EVENT_ACTION_ADD]      = handle_add_udev_event,
388                 [EVENT_ACTION_REMOVE]   = handle_remove_udev_event,
389         },
390         [EVENT_TYPE_USER] = {
391                 [EVENT_ACTION_ADD]      = handle_add_user_event,
392                 [EVENT_ACTION_REMOVE]   = handle_remove_user_event,
393         }
394 };
395
396 int device_handler_event(struct device_handler *handler,
397                 struct event *event)
398 {
399         if (event->type >= EVENT_TYPE_MAX ||
400                         event->action >= EVENT_ACTION_MAX ||
401                         !handlers[event->type][event->action]) {
402                 pb_log("%s unknown type/action: %d/%d\n", __func__,
403                                 event->type, event->action);
404                 return 0;
405         }
406
407         return handlers[event->type][event->action](handler, event);
408 }
409
410 struct device_handler *device_handler_init(struct discover_server *server)
411 {
412         struct device_handler *handler;
413
414         handler = talloc(NULL, struct device_handler);
415         handler->devices = NULL;
416         handler->n_devices = 0;
417         handler->server = server;
418
419         list_init(&handler->contexts);
420
421         /* set up our mount point base */
422         pb_mkdir_recursive(mount_base());
423
424         parser_init();
425
426         return handler;
427 }
428
429 void device_handler_destroy(struct device_handler *handler)
430 {
431         talloc_free(handler);
432 }
433
434 static struct boot_option *find_boot_option_by_id(
435                 struct device_handler *handler, const char *id)
436 {
437         unsigned int i;
438
439         for (i = 0; i < handler->n_devices; i++) {
440                 struct device *dev = handler->devices[i];
441                 struct boot_option *opt;
442
443                 list_for_each_entry(&dev->boot_options, opt, list)
444                         if (!strcmp(opt->id, id))
445                                 return opt;
446         }
447
448         return NULL;
449 }
450
451 void device_handler_boot(struct device_handler *handler,
452                 struct boot_command *cmd)
453 {
454         struct boot_option *opt;
455
456         opt = find_boot_option_by_id(handler, cmd->option_id);
457
458         boot(handler, opt, cmd, 0);
459 }