chiark / gitweb /
7c1ea0715a508a471c7c769586eecadaa346563a
[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.h"
28 #include "../../udev_selinux.h"
29
30 static char *table[] = {
31         "", "d360", "h1200", "u360", "u720", "h360", "h720",
32         "u1440", "u2880", "CompaQ", "h1440", "u1680", "h410",
33         "u820", "h1476", "u1722", "h420", "u830", "h1494", "u1743",
34         "h880", "u1040", "u1120", "h1600", "u1760", "u1920",
35         "u3200", "u3520", "u3840", "u1840", "u800", "u1600",
36         NULL
37 };
38
39 static int t360[] = { 1, 0 };
40 static int t1200[] = { 2, 5, 6, 10, 12, 14, 16, 18, 20, 23, 0 };
41 static int t3in[] = { 8, 9, 26, 27, 28, 7, 11, 15, 19, 24, 25, 29, 31, 3, 4, 13, 17, 21, 22, 30, 0 };
42 static int *table_sup[] = { NULL, t360, t1200, t3in+5+8, t3in+5, t3in, t3in };
43
44 #ifdef USE_LOG
45 void log_message(int priority, const char *format, ...)
46 {
47         va_list args;
48         static int udev_log = -1;
49
50         if (udev_log == -1) {
51                 const char *value;
52
53                 value = getenv("UDEV_LOG");
54                 if (value)
55                         udev_log = log_priority(value);
56                 else
57                         udev_log = LOG_ERR;
58         }
59
60         if (priority > udev_log)
61                 return;
62
63         va_start(args, format);
64         vsyslog(priority, format, args);
65         va_end(args);
66 }
67 #endif
68
69 int main(int argc, char **argv)
70 {
71         char *dev;
72         char node[64];
73         int type = 0, i, fdnum, c;
74         int major = 2, minor;
75         uid_t uid = 0;
76         gid_t gid = 0;
77         mode_t mode = 0660;
78         int create_nodes = 0;
79         int print_nodes = 0;
80         int unlink_nodes = 0;
81         int is_err = 0;
82
83         while ((c = getopt(argc, argv, "cudm:U:G:M:t:")) != -1) {
84                 switch (c) {
85                 case 'c':
86                         create_nodes = 1;
87                         unlink_nodes = 0;
88                         break;
89                 case 'u':
90                         unlink_nodes = 1;
91                         create_nodes = 0;
92                         break;
93                 case 'd':
94                         print_nodes = 1;
95                         break;
96                 case 'U':
97                         uid = lookup_user(optarg);
98                         break;
99                 case 'G':
100                         gid = lookup_group(optarg);
101                         break;
102                 case 'M':
103                         mode = strtol(optarg, NULL, 0);
104                         mode = mode & 0666;
105                         break;
106                 case 'm':
107                         major = strtol(optarg, NULL, 0);
108                         break;
109                 case 't':
110                         type = strtol(optarg, NULL, 0);
111                         break;
112                 default:
113                         is_err++;
114                         break;
115                 }
116         }
117
118         if (is_err || optind >= argc) {
119                 printf("Usage:  %s [OPTION] device\n"
120                        "  -c   create\n"
121                        "  -d   debug\n"
122                        "  -m   Major number\n"
123                        "  -t   floppy type number\n"
124                        "  -U   device node user ownership\n"
125                        "  -G   device node group owner\n"
126                        "  -M   device node mode\n"
127                        "\n", argv[0]);
128                 return 1;
129         }
130
131         dev = argv[optind];
132         if (dev[strlen(dev) - 3] != 'f' || dev[strlen(dev) -2 ] != 'd') {
133                 fprintf(stderr,"Device '%s' is not a floppy device\n", dev);
134                 return 1;
135         }
136
137         fdnum = strtol(dev + 2, NULL, 10);
138         if (fdnum < 0 || fdnum > 7) {
139                 fprintf(stderr,"Floppy device number %d out of range (0-7)\n", fdnum);
140                 return 1;
141         }
142         if (fdnum > 3)
143                 fdnum += 128;
144
145         if (major < 1) {
146                 fprintf(stderr,"Invalid major number %d\n", major);
147                 return 1;
148         }
149
150         if (type < 0 || type > (int) sizeof(table)) {
151                 fprintf(stderr,"Invalid CMOS type %d\n", type);
152                 return 1;
153         }
154
155         if (type == 0)
156                 return 0;
157
158         udev_config_init();
159         selinux_init();
160
161         i = 0;
162         while (table_sup[type][i]) {
163                 sprintf(node, "%s%s", dev, table[table_sup[type][i]]);
164                 minor = (table_sup[type][i] << 2) + fdnum;
165                 if (print_nodes)
166                         printf("%s b %d %d %d\n", node, mode, major, minor);
167                 if (create_nodes) {
168                         unlink(node);
169                         selinux_setfscreatecon(node, NULL, S_IFBLK | mode);
170                         mknod(node, S_IFBLK | mode, makedev(major,minor));
171                         selinux_resetfscreatecon();
172                         chown(node, uid, gid);
173                         chmod(node, S_IFBLK | mode);
174                 }
175                 i++;
176         }
177
178         selinux_exit();
179         return 0;
180 }