9 #include <talloc/talloc.h>
10 #include <list/list.h>
12 #include <pb-protocol/pb-protocol.h>
14 #include "device-handler.h"
15 #include "discover-server.h"
20 #define MOUNT_BIN "/bin/mount"
22 #define UMOUNT_BIN "/bin/umount"
24 struct device_handler {
25 struct discover_server *server;
27 struct device *devices;
38 int device_handler_get_current_devices(struct device_handler *handler,
39 const struct device **devices)
41 *devices = handler->devices;
42 return handler->n_devices;
45 static int mkdir_recursive(const char *dir)
54 if (!stat(dir, &statbuf)) {
55 if (!S_ISDIR(statbuf.st_mode)) {
56 pb_log("%s: %s exists, but isn't a directory\n",
63 str = talloc_strdup(NULL, dir);
64 sep = strchr(*str == '/' ? str + 1 : str, '/');
68 /* terminate the path at sep */
72 if (mkdir(str, mode) && errno != EEXIST) {
73 pb_log("mkdir(%s): %s\n", str, strerror(errno));
80 /* reset dir to the full path */
82 sep = strchr(sep + 1, '/');
90 static int rmdir_recursive(const char *base, const char *dir)
94 /* sanity check: make sure that dir is within base */
95 if (strncmp(base, dir, strlen(base)))
98 cur = talloc_strdup(NULL, dir);
100 while (strcmp(base, dir)) {
104 /* null-terminate at the last slash */
105 pos = strrchr(dir, '/');
117 static void setup_device_links(struct discover_context *ctx)
124 .dir = "disk/by-uuid"
127 .env = "ID_FS_LABEL",
128 .dir = "disk/by-label"
135 for (link = links; link->env; link++) {
136 char *enc, *dir, *path;
139 value = udev_event_param(ctx->event, link->env);
140 if (!value || !*value)
143 enc = encode_label(ctx, value);
144 dir = join_paths(ctx, mount_base(), link->dir);
145 path = join_paths(ctx, dir, value);
147 if (!mkdir_recursive(dir)) {
149 if (symlink(ctx->mount_path, path)) {
150 pb_log("symlink(%s,%s): %s\n",
151 ctx->mount_path, path,
155 int i = ctx->n_links++;
156 ctx->links = talloc_realloc(ctx,
159 ctx->links[i] = path;
169 static void remove_device_links(struct discover_context *ctx)
173 for (i = 0; i < ctx->n_links; i++)
174 unlink(ctx->links[i]);
177 static int mount_device(struct discover_context *ctx)
179 const char *mountpoint;
183 if (!ctx->mount_path) {
184 mountpoint = mountpoint_for_device(ctx->device_path);
185 ctx->mount_path = talloc_strdup(ctx, mountpoint);
188 if (mkdir_recursive(ctx->mount_path))
189 pb_log("couldn't create mount directory %s: %s\n",
190 ctx->mount_path, strerror(errno));
194 pb_log("%s: fork failed: %s\n", __func__, strerror(errno));
199 execl(MOUNT_BIN, MOUNT_BIN, ctx->device_path, ctx->mount_path,
204 if (waitpid(pid, &status, 0) == -1) {
205 pb_log("%s: waitpid failed: %s\n", __func__,
210 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
211 pb_log("%s: mount failed (%d): %s\n", __func__,
212 WEXITSTATUS(status), ctx->event->device);
216 setup_device_links(ctx);
220 rmdir_recursive(mount_base(), ctx->mount_path);
224 static int umount_device(struct discover_context *ctx)
229 remove_device_links(ctx);
233 pb_log("%s: fork failed: %s\n", __func__, strerror(errno));
238 execl(UMOUNT_BIN, UMOUNT_BIN, ctx->mount_path, NULL);
242 if (waitpid(pid, &status, 0) == -1) {
243 pb_log("%s: waitpid failed: %s\n", __func__,
248 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
251 rmdir_recursive(mount_base(), ctx->mount_path);
256 static struct discover_context *find_context(struct device_handler *handler,
259 struct discover_context *ctx;
261 list_for_each_entry(&handler->contexts, ctx, list) {
262 if (!strcmp(ctx->id, id))
269 static int destroy_context(void *arg)
271 struct discover_context *ctx = arg;
273 list_remove(&ctx->list);
279 static int handle_add_event(struct device_handler *handler,
280 struct udev_event *event)
282 struct discover_context *ctx;
286 /* create our context */
287 ctx = talloc(handler, struct discover_context);
289 ctx->mount_path = NULL;
293 ctx->id = talloc_strdup(ctx, event->device);
295 devname = udev_event_param(ctx->event, "DEVNAME");
297 pb_log("no devname for %s?\n", event->device);
301 ctx->device_path = talloc_strdup(ctx, devname);
303 rc = mount_device(ctx);
309 list_add(&handler->contexts, &ctx->list);
310 talloc_set_destructor(ctx, destroy_context);
312 /* set up the top-level device */
313 ctx->device = talloc_zero(ctx, struct device);
314 ctx->device->id = talloc_strdup(ctx->device, ctx->id);
315 list_init(&ctx->device->boot_options);
317 /* run the parsers */
318 iterate_parsers(ctx);
320 discover_server_notify_add(handler->server, ctx->device);
325 static int handle_remove_event(struct device_handler *handler,
326 struct udev_event *event)
328 struct discover_context *ctx;
330 ctx = find_context(handler, event->device);
334 discover_server_notify_remove(handler->server, ctx->device);
341 int device_handler_event(struct device_handler *handler,
342 struct udev_event *event)
346 switch (event->action) {
347 case UDEV_ACTION_ADD:
348 rc = handle_add_event(handler, event);
351 case UDEV_ACTION_REMOVE:
352 rc = handle_remove_event(handler, event);
359 struct device_handler *device_handler_init(struct discover_server *server)
361 struct device_handler *handler;
363 handler = talloc(NULL, struct device_handler);
364 handler->devices = NULL;
365 handler->n_devices = 0;
366 handler->server = server;
368 list_init(&handler->contexts);
370 /* set up our mount point base */
371 mkdir_recursive(mount_base());
378 void device_handler_destroy(struct device_handler *handler)
380 talloc_free(handler);