trace: use standard POSIX header for basename()
[project/procd.git] / plug / hotplug.c
index fd29e626e13dea85159b556e220ad94ca66c50da..53f338339fdedf05ee82c21f795be21c93419458 100644 (file)
@@ -23,6 +23,7 @@
 #include <libubox/blobmsg_json.h>
 #include <libubox/json_script.h>
 #include <libubox/uloop.h>
+#include <libubox/utils.h>
 #include <json-c/json.h>
 
 #include <errno.h>
@@ -108,18 +109,6 @@ static char *hotplug_msg_find_var(struct blob_attr *msg, const char *name)
        return NULL;
 }
 
-static void mkdir_p(char *dir)
-{
-       char *l = strrchr(dir, '/');
-
-       if (l) {
-               *l = '\0';
-               mkdir_p(dir);
-               *l = '/';
-               mkdir(dir, 0755);
-       }
-}
-
 static void chgrp_error(const char *group, const char *target, const char *failed)
 {
        ERROR("cannot set group %s for %s (%s: %d)\n",
@@ -163,7 +152,7 @@ static void handle_makedev(struct blob_attr *msg, struct blob_attr *data)
                char *d = strdup(blobmsg_get_string(tb[0]));
 
                d = dirname(d);
-               mkdir_p(d);
+               mkdir_p(d, 0755);
                free(d);
 
                if (!strcmp(subsystem, "block"))
@@ -221,7 +210,7 @@ static void handle_exec(struct blob_attr *msg, struct blob_attr *data)
                argv[i] = NULL;
                execvp(argv[0], &argv[0]);
        }
-       exit(-1);
+       exit(EXIT_FAILURE);
 }
 
 static void handle_button_start(struct blob_attr *msg, struct blob_attr *data)
@@ -242,11 +231,11 @@ static void handle_firmware(struct blob_attr *msg, struct blob_attr *data)
        int fw, src, load, len;
        static char buf[4096];
 
-       DEBUG(2, "Firmware request for %s/%s\n", dir, file);
+       P_DEBUG(2, "Firmware request for %s/%s\n", dir, file);
 
        if (!file || !dir || !dev) {
                ERROR("Request for unknown firmware %s/%s\n", dir, file);
-               exit(-1);
+               exit(EXIT_FAILURE);
        }
 
        path = alloca(strlen(dir) + strlen(file) + 2);
@@ -271,11 +260,11 @@ send_to_kernel:
        load = open(loadpath, O_WRONLY);
        if (!load) {
                ERROR("Failed to open %s: %m\n", loadpath);
-               exit(-1);
+               exit(EXIT_FAILURE);
        }
        if (write(load, "1", 1) == -1) {
                ERROR("Failed to write to %s: %m\n", loadpath);
-               exit(-1);
+               exit(EXIT_FAILURE);
        }
        close(load);
 
@@ -283,7 +272,7 @@ send_to_kernel:
        fw = open(syspath, O_WRONLY);
        if (fw < 0) {
                ERROR("Failed to open %s: %m\n", syspath);
-               exit(-1);
+               exit(EXIT_FAILURE);
        }
 
        len = s.st_size;
@@ -307,9 +296,23 @@ send_to_kernel:
                ERROR("failed to write to %s: %m\n", loadpath);
        close(load);
 
-       DEBUG(2, "Done loading %s\n", path);
+       P_DEBUG(2, "Done loading %s\n", path);
 
-       exit(-1);
+       exit(EXIT_FAILURE);
+}
+
+static void handle_start_console(struct blob_attr *msg, struct blob_attr *data)
+{
+       char *dev = blobmsg_get_string(blobmsg_data(data));
+
+       P_DEBUG(2, "Start console request for %s\n", dev);
+
+       procd_inittab_run("respawn");
+       procd_inittab_run("askfirst");
+
+       P_DEBUG(2, "Done starting console for %s\n", dev);
+
+       exit(EXIT_FAILURE);
 }
 
 enum {
@@ -318,6 +321,7 @@ enum {
        HANDLER_EXEC,
        HANDLER_BUTTON,
        HANDLER_FW,
+       HANDLER_START_CONSOLE,
 };
 
 static struct cmd_handler {
@@ -351,6 +355,10 @@ static struct cmd_handler {
                .name = "load-firmware",
                .handler = handle_firmware,
        },
+       [HANDLER_START_CONSOLE] = {
+               .name = "start-console",
+               .handler = handle_start_console,
+       },
 };
 
 static void queue_next(void)
@@ -382,12 +390,12 @@ static void queue_next(void)
 
        uloop_process_add(&queue_proc);
 
-       DEBUG(4, "Launched hotplug exec instance, pid=%d\n", (int) queue_proc.pid);
+       P_DEBUG(4, "Launched hotplug exec instance, pid=%d\n", (int) queue_proc.pid);
 }
 
 static void queue_proc_cb(struct uloop_process *c, int ret)
 {
-       DEBUG(4, "Finished hotplug exec instance, pid=%d\n", (int) c->pid);
+       P_DEBUG(4, "Finished hotplug exec instance, pid=%d\n", (int) c->pid);
 
        if (current) {
                current->complete(current->msg, current->data, ret);
@@ -505,13 +513,13 @@ static void rule_handle_command(struct json_script_ctx *ctx, const char *name,
        int rem, i;
 
        if (debug > 3) {
-               DEBUG(4, "Command: %s\n", name);
+               P_DEBUG(4, "Command: %s\n", name);
                blobmsg_for_each_attr(cur, data, rem)
-                       DEBUG(4, " %s\n", (char *) blobmsg_data(cur));
+                       P_DEBUG(4, " %s\n", (char *) blobmsg_data(cur));
 
-               DEBUG(4, "Message:\n");
+               P_DEBUG(4, "Message:\n");
                blobmsg_for_each_attr(cur, vars, rem)
-                       DEBUG(4, " %s=%s\n", blobmsg_name(cur), (char *) blobmsg_data(cur));
+                       P_DEBUG(4, " %s=%s\n", blobmsg_name(cur), (char *) blobmsg_data(cur));
        }
 
        for (i = 0; i < ARRAY_SIZE(handlers); i++)
@@ -552,7 +560,7 @@ static void hotplug_handler_debug(struct blob_attr *data)
                return;
 
        str = blobmsg_format_json(data, true);
-       DEBUG(3, "%s\n", str);
+       P_DEBUG(3, "%s\n", str);
        free(str);
 }
 
@@ -604,7 +612,7 @@ void hotplug(char *rules)
 
        rule_file = strdup(rules);
        nls.nl_family = AF_NETLINK;
-       nls.nl_pid = getpid();
+       nls.nl_pid = 0;
        nls.nl_groups = -1;
 
        if ((hotplug_fd.fd = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_KOBJECT_UEVENT)) == -1) {