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