#include <list/list.h>
#include <log/log.h>
#include <pb-protocol/pb-protocol.h>
+#include <system/system.h>
#include "device-handler.h"
#include "discover-server.h"
+#include "event.h"
#include "parser.h"
#include "udev.h"
#include "paths.h"
if (handler->devices[i] == device)
break;
- if (i < handler->n_devices) {
+ if (i == handler->n_devices) {
assert(0 && "unknown device");
return;
}
&& streq(handler->devices[i]->id, id))
return handler->devices[i];
- assert(0 && "unknown device");
+ pb_log("%s: unknown device: %s\n", __func__, id);
return NULL;
}
return handler->devices[index];
}
-static int mkdir_recursive(const char *dir)
-{
- struct stat statbuf;
- char *str, *sep;
- int mode = 0755;
-
- if (!*dir)
- return 0;
-
- if (!stat(dir, &statbuf)) {
- if (!S_ISDIR(statbuf.st_mode)) {
- pb_log("%s: %s exists, but isn't a directory\n",
- __func__, dir);
- return -1;
- }
- return 0;
- }
-
- str = talloc_strdup(NULL, dir);
- sep = strchr(*str == '/' ? str + 1 : str, '/');
-
- while (1) {
-
- /* terminate the path at sep */
- if (sep)
- *sep = '\0';
-
- if (mkdir(str, mode) && errno != EEXIST) {
- pb_log("mkdir(%s): %s\n", str, strerror(errno));
- return -1;
- }
-
- if (!sep)
- break;
-
- /* reset dir to the full path */
- strcpy(str, dir);
- sep = strchr(sep + 1, '/');
- }
-
- talloc_free(str);
-
- return 0;
-}
-
-static int rmdir_recursive(const char *base, const char *dir)
-{
- char *cur, *pos;
-
- /* sanity check: make sure that dir is within base */
- if (strncmp(base, dir, strlen(base)))
- return -1;
-
- cur = talloc_strdup(NULL, dir);
-
- while (strcmp(base, dir)) {
-
- rmdir(dir);
-
- /* null-terminate at the last slash */
- pos = strrchr(dir, '/');
- if (!pos)
- break;
-
- *pos = '\0';
- }
-
- talloc_free(cur);
-
- return 0;
-}
-
static void setup_device_links(struct discover_context *ctx)
{
struct link {
char *enc, *dir, *path;
const char *value;
- value = udev_event_param(ctx->event, link->env);
+ value = event_get_param(ctx->event, link->env);
if (!value || !*value)
continue;
dir = join_paths(ctx, mount_base(), link->dir);
path = join_paths(ctx, dir, value);
- if (!mkdir_recursive(dir)) {
+ if (!pb_mkdir_recursive(dir)) {
unlink(path);
if (symlink(ctx->mount_path, path)) {
pb_log("symlink(%s,%s): %s\n",
static int mount_device(struct discover_context *ctx)
{
const char *mountpoint;
- int status;
- pid_t pid;
+ const char *argv[6];
if (!ctx->mount_path) {
mountpoint = mountpoint_for_device(ctx->device_path);
ctx->mount_path = talloc_strdup(ctx, mountpoint);
}
- if (mkdir_recursive(ctx->mount_path))
+ if (pb_mkdir_recursive(ctx->mount_path))
pb_log("couldn't create mount directory %s: %s\n",
ctx->mount_path, strerror(errno));
- pid = fork();
- if (pid == -1) {
- pb_log("%s: fork failed: %s\n", __func__, strerror(errno));
- goto out_rmdir;
- }
-
- if (pid == 0) {
- execl(MOUNT_BIN, MOUNT_BIN, ctx->device_path, ctx->mount_path,
- "-o", "ro", NULL);
- exit(EXIT_FAILURE);
- }
-
- if (waitpid(pid, &status, 0) == -1) {
- pb_log("%s: waitpid failed: %s\n", __func__,
- strerror(errno));
- goto out_rmdir;
- }
+ argv[0] = MOUNT_BIN;
+ argv[1] = ctx->device_path;
+ argv[2] = ctx->mount_path;
+ argv[3] = "-o";
+ argv[4] = "ro";
+ argv[5] = NULL;
- if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
- pb_log("%s: mount failed (%d): %s\n", __func__,
- WEXITSTATUS(status), ctx->event->device);
+ if (pb_run_cmd(argv))
goto out_rmdir;
- }
setup_device_links(ctx);
return 0;
out_rmdir:
- rmdir_recursive(mount_base(), ctx->mount_path);
+ pb_rmdir_recursive(mount_base(), ctx->mount_path);
return -1;
}
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
return -1;
- rmdir_recursive(mount_base(), ctx->mount_path);
+ pb_rmdir_recursive(mount_base(), ctx->mount_path);
return 0;
}
return 0;
}
-static int handle_add_event(struct device_handler *handler,
- struct udev_event *event)
+static int handle_add_udev_event(struct device_handler *handler,
+ struct event *event)
{
struct discover_context *ctx;
const char *devname;
ctx->id = talloc_strdup(ctx, event->device);
- devname = udev_event_param(ctx->event, "DEVNAME");
+ devname = event_get_param(ctx->event, "DEVNAME");
if (!devname) {
pb_log("no devname for %s?\n", event->device);
return 0;
return 0;
}
-static int handle_remove_event(struct device_handler *handler,
- struct udev_event *event)
+static int handle_remove_udev_event(struct device_handler *handler,
+ struct event *event)
{
struct discover_context *ctx;
return 0;
}
+static int handle_add_user_event(struct device_handler *handler,
+ struct event *event)
+{
+ struct device *device;
+
+ assert(event->device);
+
+ device = talloc_zero(handler, struct device);
+
+ if (!device)
+ goto fail;
+
+ device->id = talloc_strdup(device, event->device);
+ list_init(&device->boot_options);
+
+ parse_user_event(device, event);
+
+ discover_server_notify_add(handler->server, device);
+
+ /* add device to handler device array */
+ device_handler_add(handler, device);
+
+ return 0;
+
+fail:
+ talloc_free(device);
+ return 0;
+}
+
+static int handle_remove_user_event(struct device_handler *handler,
+ struct event *event)
+{
+ struct device *device = device_handler_find(handler, event->device);
+
+ if (!device)
+ return 0;
+
+ discover_server_notify_remove(handler->server, device);
+
+ /* remove device from handler device array */
+ device_handler_remove(handler, device);
+
+ talloc_free(device);
+ return 0;
+}
+
int device_handler_event(struct device_handler *handler,
- struct udev_event *event)
+ struct event *event)
{
- int rc;
+ int rc = 0;
- switch (event->action) {
- case UDEV_ACTION_ADD:
- rc = handle_add_event(handler, event);
+ switch (event->type) {
+ case EVENT_TYPE_UDEV:
+ switch (event->action) {
+ case EVENT_ACTION_ADD:
+ rc = handle_add_udev_event(handler, event);
+ break;
+ case EVENT_ACTION_REMOVE:
+ rc = handle_remove_udev_event(handler, event);
+ break;
+ default:
+ pb_log("%s unknown action: %d\n", __func__,
+ event->action);
+ break;
+ }
break;
-
- case UDEV_ACTION_REMOVE:
- rc = handle_remove_event(handler, event);
+ case EVENT_TYPE_USER:
+ switch (event->action) {
+ case EVENT_ACTION_ADD:
+ rc = handle_add_user_event(handler, event);
+ break;
+ case EVENT_ACTION_REMOVE:
+ rc = handle_remove_user_event(handler, event);
+ break;
+ default:
+ pb_log("%s unknown action: %d\n", __func__,
+ event->action);
+ break;
+ }
+ break;
+ default:
+ pb_log("%s unknown type: %d\n", __func__, event->type);
break;
}
list_init(&handler->contexts);
/* set up our mount point base */
- mkdir_recursive(mount_base());
+ pb_mkdir_recursive(mount_base());
parser_init();