hotplug: makedev should be able to set the group of a device node
authorJohn Crispin <john@phrozen.org>
Thu, 16 Jun 2016 09:49:04 +0000 (11:49 +0200)
committerJohn Crispin <john@phrozen.org>
Mon, 20 Jun 2016 09:46:53 +0000 (11:46 +0200)
Signed-off-by: John Crispin <john@phrozen.org>
plug/hotplug.c

index b5ebd07467b1376a5434a080c9c25f59249d2562..85959155634f1a6a9ada11c7045601885a0abad8 100644 (file)
@@ -28,6 +28,7 @@
 #include <unistd.h>
 #include <stdlib.h>
 #include <libgen.h>
+#include <grp.h>
 
 #include "../procd.h"
 
@@ -120,16 +121,17 @@ static void mkdir_p(char *dir)
 static void handle_makedev(struct blob_attr *msg, struct blob_attr *data)
 {
        unsigned int oldumask = umask(0);
-       static struct blobmsg_policy mkdev_policy[2] = {
+       static struct blobmsg_policy mkdev_policy[3] = {
+               { .type = BLOBMSG_TYPE_STRING },
                { .type = BLOBMSG_TYPE_STRING },
                { .type = BLOBMSG_TYPE_STRING },
        };
-       struct blob_attr *tb[2];
+       struct blob_attr *tb[3];
        char *minor = hotplug_msg_find_var(msg, "MINOR");
        char *major = hotplug_msg_find_var(msg, "MAJOR");
        char *subsystem = hotplug_msg_find_var(msg, "SUBSYSTEM");
 
-       blobmsg_parse_array(mkdev_policy, 2, tb, blobmsg_data(data), blobmsg_data_len(data));
+       blobmsg_parse_array(mkdev_policy, 3, tb, blobmsg_data(data), blobmsg_data_len(data));
        if (tb[0] && tb[1] && minor && major && subsystem) {
                mode_t m = S_IFCHR;
                char *d = strdup(blobmsg_get_string(tb[0]));
@@ -143,6 +145,16 @@ static void handle_makedev(struct blob_attr *msg, struct blob_attr *data)
                mknod(blobmsg_get_string(tb[0]),
                                m | strtoul(blobmsg_data(tb[1]), NULL, 8),
                                makedev(atoi(major), atoi(minor)));
+               if (tb[2]) {
+                       struct group *g = getgrnam(blobmsg_get_string(tb[2]));
+
+                       if (g)
+                               chown(blobmsg_get_string(tb[0]), 0, g->gr_gid);
+                       else
+                               ERROR("cannot set group %s for %s\n",
+                                       blobmsg_get_string(tb[2]),
+                                       blobmsg_get_string(tb[0]));
+               }
        }
        umask(oldumask);
 }