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