]> git.ozlabs.org Git - petitboot/blob - discover/device-handler.c
Move --dry-run option to discover server
[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         const char *devname;
279         int rc;
280
281         /* create our context */
282         ctx = talloc(handler, struct discover_context);
283         ctx->event = event;
284         ctx->mount_path = NULL;
285         ctx->links = NULL;
286         ctx->n_links = 0;
287
288         ctx->id = talloc_strdup(ctx, event->device);
289
290         devname = event_get_param(ctx->event, "DEVNAME");
291         assert(devname);
292         ctx->device_path = talloc_strdup(ctx, devname);
293
294         rc = mount_device(ctx);
295         if (rc) {
296                 talloc_free(ctx);
297                 return 0;
298         }
299
300         list_add(&handler->contexts, &ctx->list);
301         talloc_set_destructor(ctx, destroy_context);
302
303         /* set up the top-level device */
304         ctx->device = talloc_zero(ctx, struct device);
305         ctx->device->id = talloc_strdup(ctx->device, ctx->id);
306         list_init(&ctx->device->boot_options);
307
308         /* run the parsers */
309         iterate_parsers(ctx);
310
311         /* add device to handler device array */
312         device_handler_add(handler, ctx->device);
313
314         discover_server_notify_add(handler->server, ctx->device);
315
316         return 0;
317 }
318
319 static int handle_remove_udev_event(struct device_handler *handler,
320                 struct event *event)
321 {
322         struct discover_context *ctx;
323
324         ctx = find_context(handler, event->device);
325         if (!ctx)
326                 return 0;
327
328         discover_server_notify_remove(handler->server, ctx->device);
329
330         /* remove device from handler device array */
331         device_handler_remove(handler, ctx->device);
332
333         talloc_free(ctx);
334
335         return 0;
336 }
337
338 static int handle_add_user_event(struct device_handler *handler,
339                 struct event *event)
340 {
341         struct device *device;
342
343         assert(event->device);
344
345         device = talloc_zero(handler, struct device);
346
347         if (!device)
348                 goto fail;
349
350         device->id = talloc_strdup(device, event->device);
351         list_init(&device->boot_options);
352
353         parse_user_event(device, event);
354
355         discover_server_notify_add(handler->server, device);
356
357         /* add device to handler device array */
358         device_handler_add(handler, device);
359
360         return 0;
361
362 fail:
363         talloc_free(device);
364         return 0;
365 }
366
367 static int handle_remove_user_event(struct device_handler *handler,
368                 struct event *event)
369 {
370         struct device *device = device_handler_find(handler, event->device);
371
372         if (!device)
373                 return 0;
374
375         discover_server_notify_remove(handler->server, device);
376
377         /* remove device from handler device array */
378         device_handler_remove(handler, device);
379
380         talloc_free(device);
381         return 0;
382 }
383
384 typedef int (*event_handler)(struct device_handler *, struct event *);
385
386 static event_handler handlers[EVENT_TYPE_MAX][EVENT_ACTION_MAX] = {
387         [EVENT_TYPE_UDEV] = {
388                 [EVENT_ACTION_ADD]      = handle_add_udev_event,
389                 [EVENT_ACTION_REMOVE]   = handle_remove_udev_event,
390         },
391         [EVENT_TYPE_USER] = {
392                 [EVENT_ACTION_ADD]      = handle_add_user_event,
393                 [EVENT_ACTION_REMOVE]   = handle_remove_user_event,
394         }
395 };
396
397 int device_handler_event(struct device_handler *handler,
398                 struct event *event)
399 {
400         if (event->type >= EVENT_TYPE_MAX ||
401                         event->action >= EVENT_ACTION_MAX ||
402                         !handlers[event->type][event->action]) {
403                 pb_log("%s unknown type/action: %d/%d\n", __func__,
404                                 event->type, event->action);
405                 return 0;
406         }
407
408         return handlers[event->type][event->action](handler, event);
409 }
410
411 struct device_handler *device_handler_init(struct discover_server *server,
412                 int dry_run)
413 {
414         struct device_handler *handler;
415
416         handler = talloc(NULL, struct device_handler);
417         handler->devices = NULL;
418         handler->n_devices = 0;
419         handler->server = server;
420         handler->dry_run = dry_run;
421
422         list_init(&handler->contexts);
423
424         /* set up our mount point base */
425         pb_mkdir_recursive(mount_base());
426
427         parser_init();
428
429         return handler;
430 }
431
432 void device_handler_destroy(struct device_handler *handler)
433 {
434         talloc_free(handler);
435 }
436
437 static struct boot_option *find_boot_option_by_id(
438                 struct device_handler *handler, const char *id)
439 {
440         unsigned int i;
441
442         for (i = 0; i < handler->n_devices; i++) {
443                 struct device *dev = handler->devices[i];
444                 struct boot_option *opt;
445
446                 list_for_each_entry(&dev->boot_options, opt, list)
447                         if (!strcmp(opt->id, id))
448                                 return opt;
449         }
450
451         return NULL;
452 }
453
454 void device_handler_boot(struct device_handler *handler,
455                 struct boot_command *cmd)
456 {
457         struct boot_option *opt;
458
459         opt = find_boot_option_by_id(handler, cmd->option_id);
460
461         boot(handler, opt, cmd, handler->dry_run);
462 }