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