summaryrefslogtreecommitdiff
path: root/extras/floppy/create_floppy_devices.c
blob: 3e73aa83c353f5b55494f60ac8574a5231e295d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
 * create_floppy_devices
 *
 * Create all possible floppy device based on the CMOS type.
 * Based upon code from drivers/block/floppy.c
 *
 * Copyright(C) 2005, SUSE Linux Products GmbH
 *
 * Author:
 *	Hannes Reinecke <hare@suse.de>
 *
 *	This program is free software; you can redistribute it and/or modify it
 *	under the terms of the GNU General Public License as published by the
 *	Free Software Foundation version 2 of the License.
 */

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <pwd.h>
#include <grp.h>

#include "../../udev/udev.h"
#include "../../udev/udev_selinux.h"

static char *table[] = {
	"", "d360", "h1200", "u360", "u720", "h360", "h720",
	"u1440", "u2880", "CompaQ", "h1440", "u1680", "h410",
	"u820", "h1476", "u1722", "h420", "u830", "h1494", "u1743",
	"h880", "u1040", "u1120", "h1600", "u1760", "u1920",
	"u3200", "u3520", "u3840", "u1840", "u800", "u1600",
	NULL
};

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;
	}

	if (priority > udev_log)
		return;

	va_start(args, format);
	vsyslog(priority, format, args);
	va_end(args);
}
#endif

int main(int argc, char **argv)
{
	char *dev;
	char *devname;
	char node[64];
	int type = 0, i, fdnum, c;
	int major = 2, minor;
	uid_t uid = 0;
	gid_t gid = 0;
	mode_t mode = 0660;
	int create_nodes = 0;
	int print_nodes = 0;
	int is_err = 0;

	while ((c = getopt(argc, argv, "cudm:U:G:M:t:")) != -1) {
		switch (c) {
		case 'c':
			create_nodes = 1;
			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, 0);
			mode = mode & 0666;
			break;
		case 'm':
			major = strtol(optarg, NULL, 0);
			break;
		case 't':
			type = strtol(optarg, NULL, 0);
			break;
		default:
			is_err++;
			break;
		}
	}

	if (is_err || optind >= argc) {
		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;
	}

	dev = argv[optind];
	devname = strrchr(dev, '/');
	if (devname != NULL)
		devname = &devname[1];
	else
		devname = dev;
	if (strncmp(devname, "fd", 2) != 0) {
		fprintf(stderr,"Device '%s' is not a floppy device\n", dev);
		return 1;
	}

	fdnum = strtol(&devname[2], NULL, 10);
	if (fdnum < 0 || fdnum > 7) {
		fprintf(stderr,"Floppy device number %d out of range (0-7)\n", fdnum);
		return 1;
	}
	if (fdnum > 3)
		fdnum += 128;

	if (major < 1) {
		fprintf(stderr,"Invalid major number %d\n", major);
		return 1;
	}

	if (type < 0 || type > (int) sizeof(table)) {
		fprintf(stderr,"Invalid CMOS type %d\n", type);
		return 1;
	}

	if (type == 0)
		return 0;

	udev_config_init();
	selinux_init();

	i = 0;
	while (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) {
			unlink(node);
			selinux_setfscreatecon(node, NULL, S_IFBLK | mode);
			mknod(node, S_IFBLK | mode, makedev(major,minor));
			selinux_resetfscreatecon();
			chown(node, uid, gid);
			chmod(node, S_IFBLK | mode);
		}
		i++;
	}

	selinux_exit();
	return 0;
}