chiark / gitweb /
path_id: fix fibre channel handling
[elogind.git] / extras / floppy / create_floppy_devices.c
1 /*
2  * create_floppy_devices
3  *
4  * Create all possible floppy device based on the CMOS type.
5  * Based upon code from drivers/block/floppy.c
6  *
7  * Copyright(C) 2005, SUSE Linux Products GmbH
8  *
9  * Author:
10  *      Hannes Reinecke <hare@suse.de>
11  *
12  *      This program is free software; you can redistribute it and/or modify it
13  *      under the terms of the GNU General Public License as published by the
14  *      Free Software Foundation version 2 of the License.
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <fcntl.h>
22 #include <unistd.h>
23 #include <string.h>
24 #include <pwd.h>
25 #include <grp.h>
26
27 #include "../../udev/udev.h"
28
29 static char *table[] = {
30         "", "d360", "h1200", "u360", "u720", "h360", "h720",
31         "u1440", "u2880", "CompaQ", "h1440", "u1680", "h410",
32         "u820", "h1476", "u1722", "h420", "u830", "h1494", "u1743",
33         "h880", "u1040", "u1120", "h1600", "u1760", "u1920",
34         "u3200", "u3520", "u3840", "u1840", "u800", "u1600",
35         NULL
36 };
37
38 static int t360[] = { 1, 0 };
39 static int t1200[] = { 2, 5, 6, 10, 12, 14, 16, 18, 20, 23, 0 };
40 static int t3in[] = { 8, 9, 26, 27, 28, 7, 11, 15, 19, 24, 25, 29, 31, 3, 4, 13, 17, 21, 22, 30, 0 };
41 static int *table_sup[] = { NULL, t360, t1200, t3in+5+8, t3in+5, t3in, t3in };
42
43 static void log_fn(struct udev *udev, int priority,
44                    const char *file, int line, const char *fn,
45                    const char *format, va_list args)
46 {
47         vsyslog(priority, format, args);
48 }
49
50 int main(int argc, char **argv)
51 {
52         struct udev *udev;
53         char *dev;
54         char *devname;
55         char node[64];
56         int type = 0, i, fdnum, c;
57         int major = 2, minor;
58         uid_t uid = 0;
59         gid_t gid = 0;
60         mode_t mode = 0660;
61         int create_nodes = 0;
62         int print_nodes = 0;
63         int is_err = 0;
64
65         udev = udev_new();
66         if (udev == NULL)
67                 goto exit;
68
69         logging_init("create_floppy_devices");
70         udev_set_log_fn(udev, log_fn);
71         udev_selinux_init(udev);
72
73         while ((c = getopt(argc, argv, "cudm:U:G:M:t:")) != -1) {
74                 switch (c) {
75                 case 'c':
76                         create_nodes = 1;
77                         break;
78                 case 'd':
79                         print_nodes = 1;
80                         break;
81                 case 'U':
82                         uid = util_lookup_user(udev, optarg);
83                         break;
84                 case 'G':
85                         gid = util_lookup_group(udev, optarg);
86                         break;
87                 case 'M':
88                         mode = strtol(optarg, NULL, 0);
89                         mode = mode & 0666;
90                         break;
91                 case 'm':
92                         major = strtol(optarg, NULL, 0);
93                         break;
94                 case 't':
95                         type = strtol(optarg, NULL, 0);
96                         break;
97                 default:
98                         is_err++;
99                         break;
100                 }
101         }
102
103         if (is_err || optind >= argc) {
104                 printf("Usage:  %s [OPTION] device\n"
105                        "  -c   create\n"
106                        "  -d   debug\n"
107                        "  -m   Major number\n"
108                        "  -t   floppy type number\n"
109                        "  -U   device node user ownership\n"
110                        "  -G   device node group owner\n"
111                        "  -M   device node mode\n"
112                        "\n", argv[0]);
113                 return 1;
114         }
115
116         dev = argv[optind];
117         devname = strrchr(dev, '/');
118         if (devname != NULL)
119                 devname = &devname[1];
120         else
121                 devname = dev;
122         if (strncmp(devname, "fd", 2) != 0) {
123                 fprintf(stderr,"Device '%s' is not a floppy device\n", dev);
124                 return 1;
125         }
126
127         fdnum = strtol(&devname[2], NULL, 10);
128         if (fdnum < 0 || fdnum > 7) {
129                 fprintf(stderr,"Floppy device number %d out of range (0-7)\n", fdnum);
130                 return 1;
131         }
132         if (fdnum > 3)
133                 fdnum += 128;
134
135         if (major < 1) {
136                 fprintf(stderr,"Invalid major number %d\n", major);
137                 return 1;
138         }
139
140         if (type < 0 || type > (int) sizeof(table)) {
141                 fprintf(stderr,"Invalid CMOS type %d\n", type);
142                 return 1;
143         }
144
145         if (type == 0)
146                 return 0;
147
148         i = 0;
149         while (table_sup[type][i]) {
150                 sprintf(node, "%s%s", dev, table[table_sup[type][i]]);
151                 minor = (table_sup[type][i] << 2) + fdnum;
152                 if (print_nodes)
153                         printf("%s b %d %d %d\n", node, mode, major, minor);
154                 if (create_nodes) {
155                         unlink(node);
156                         udev_selinux_setfscreatecon(udev, node, S_IFBLK | mode);
157                         mknod(node, S_IFBLK | mode, makedev(major,minor));
158                         udev_selinux_resetfscreatecon(udev);
159                         chown(node, uid, gid);
160                         chmod(node, S_IFBLK | mode);
161                 }
162                 i++;
163         }
164
165         udev_selinux_exit(udev);
166         udev_unref(udev);
167 exit:
168         return 0;
169 }