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