chiark / gitweb /
create_floppy_devices: add option for owner/group
authorKay Sievers <kay.sievers@vrfy.org>
Wed, 14 Feb 2007 21:29:03 +0000 (22:29 +0100)
committerKay Sievers <kay.sievers@vrfy.org>
Wed, 14 Feb 2007 21:29:03 +0000 (22:29 +0100)
extras/floppy/create_floppy_devices.c
udev_selinux.c

index 91d3a58..7b61ef0 100644 (file)
 #include <fcntl.h>
 #include <unistd.h>
 #include <string.h>
+#include <pwd.h>
+#include <grp.h>
+
+#include "../../udev.h"
+#include "../../udev_selinux.h"
 
 static char *table[] = {
        "", "d360", "h1200", "u360", "u720", "h360", "h720",
@@ -34,8 +39,32 @@ static char *table[] = {
 static int t360[] = { 1, 0 };
 static int t1200[] = { 2, 5, 6, 10, 12, 14, 16, 18, 20, 23, 0 };
 static int t3in[] = { 8, 9, 26, 27, 28, 7, 11, 15, 19, 24, 25, 29, 31, 3, 4, 13, 17, 21, 22, 30, 0 };
+static int *table_sup[] = { NULL, t360, t1200, t3in+5+8, t3in+5, t3in, t3in };
+
+#ifdef USE_LOG
+void log_message(int priority, const char *format, ...)
+{
+       va_list args;
+       static int udev_log = -1;
+
+       if (udev_log == -1) {
+               const char *value;
+
+               value = getenv("UDEV_LOG");
+               if (value)
+                       udev_log = log_priority(value);
+               else
+                       udev_log = LOG_ERR;
+       }
 
-static int *table_sup[] = { NULL, t360, t1200, t3in + 5 + 8, t3in + 5, t3in, t3in };
+       if (priority > udev_log)
+               return;
+
+       va_start(args, format);
+       vsyslog(priority, format, args);
+       va_end(args);
+}
+#endif
 
 int main(int argc, char **argv)
 {
@@ -43,33 +72,42 @@ int main(int argc, char **argv)
        char node[64];
        int type = 0, i, fdnum, c;
        int major = 2, minor;
-       int mode = 0;
+       uid_t uid = 0;
+       gid_t gid = 0;
+       mode_t mode = 0;
        int create_nodes = 0;
        int print_nodes = 0;
        int unlink_nodes = 0;
        int is_err = 0;
 
-       while ((c = getopt(argc, argv, "cdm:M:t:u")) != -1) {
+       while ((c = getopt(argc, argv, "cudm:U:G:M:t:")) != -1) {
                switch (c) {
                case 'c':
                        create_nodes = 1;
                        unlink_nodes = 0;
                        break;
+               case 'u':
+                       unlink_nodes = 1;
+                       create_nodes = 0;
+                       break;
                case 'd':
                        print_nodes = 1;
                        break;
+               case 'U':
+                       uid = lookup_user(optarg);
+                       break;
+               case 'G':
+                       gid = lookup_group(optarg);
+                       break;
                case 'M':
-                       mode = strtol(optarg, NULL, 10);
+                       mode = strtol(optarg, NULL, 0);
+                       mode = mode & 0666;
                        break;
                case 'm':
-                       major = strtol(optarg, NULL, 10);
+                       major = strtol(optarg, NULL, 0);
                        break;
                case 't':
-                       type = strtol(optarg, NULL, 10);
-                       break;
-               case 'u':
-                       unlink_nodes = 1;
-                       create_nodes = 0;
+                       type = strtol(optarg, NULL, 0);
                        break;
                default:
                        is_err++;
@@ -78,8 +116,15 @@ int main(int argc, char **argv)
        }
 
        if (is_err || optind >= argc) {
-               fprintf(stderr,"Usage: %s [-d|-c|-u|-m <major>|-t <type>] <device>\n",
-                       argv[0]);
+               printf("Usage:  %s [OPTION] device\n"
+                      "  -c   create\n"
+                      "  -d   debug\n"
+                      "  -m   Major number\n"
+                      "  -t   floppy type number\n"
+                      "  -U   device node user ownership\n"
+                      "  -G   device node group owner\n"
+                      "  -M   device node mode\n"
+                      "\n", argv[0]);
                return 1;
        }
 
@@ -110,17 +155,24 @@ int main(int argc, char **argv)
        if (type == 0)
                return 0;
 
+       selinux_init();
+
        i = 0;
        while (table_sup[type][i]) {
-               sprintf(node, "%s%s",dev, table[table_sup[type][i]]);
+               sprintf(node, "%s%s", dev, table[table_sup[type][i]]);
                minor = (table_sup[type][i] << 2) + fdnum;
                if (print_nodes)
                        printf("%s b %d %d %d\n", node, mode, major, minor);
-               if (create_nodes)
+               if (create_nodes) {
+                       unlink(node);
+                       selinux_setfscreatecon(node, NULL, mode);
                        mknod(node, S_IFBLK | mode, makedev(major,minor));
+                       selinux_resetfscreatecon();
+                       chown(node, uid, gid);
+               }
                i++;
        }
 
+       selinux_exit();
        return 0;
 }
-
index a305e24..42568e4 100644 (file)
@@ -90,8 +90,7 @@ void selinux_setfilecon(const char *file, const char *devname, unsigned int mode
                char *media;
                int ret = -1;
 
-               if(devname)
-               {
+               if (devname) {
                        media = get_media(devname, mode);
                        if (media) {
                                ret = matchmediacon(media, &scontext);