]> git.ipfire.org Git - thirdparty/systemd.git/blob - udev-add.c
[PATCH] udevdb prototype
[thirdparty/systemd.git] / udev-add.c
1 /*
2 * udev-add.c
3 *
4 * Userspace devfs
5 *
6 * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
7 *
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation version 2 of the License.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 675 Mass Ave, Cambridge, MA 02139, USA.
21 *
22 */
23
24 #include <stdlib.h>
25 #include <string.h>
26 #include <stdio.h>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <errno.h>
30
31 #include "udev.h"
32 #include "udev_version.h"
33 #include "namedev.h"
34 #include "udevdb.h"
35 #include "libsysfs/libsysfs.h"
36
37
38 /*
39 * Right now the major/minor of a device is stored in a file called
40 * "dev" in sysfs.
41 * The number is stored as:
42 * MMmm
43 * MM is the major
44 * mm is the minor
45 * The value is in hex.
46 * Yes, this will probably change when we go to a bigger major/minor
47 * range, and will have to be changed at that time.
48 */
49 static int get_major_minor(struct sysfs_class_device *class_dev, int *major, int *minor)
50 {
51 int retval = -ENODEV;
52
53 char *dev;
54
55 dev = sysfs_get_value_from_attributes(class_dev->directory->attributes, "dev");
56 if (dev == NULL)
57 goto exit;
58
59 dbg("dev = %s", dev);
60
61 if (sscanf(dev, "%u:%u", major, minor) != 2)
62 goto exit;
63
64 dbg("found major = %d, minor = %d", *major, *minor);
65
66 retval = 0;
67 exit:
68 return retval;
69 }
70
71 /*
72 * We also want to add some permissions here, and possibly some symlinks
73 */
74 static int create_node(char *name, char type, int major, int minor, int mode)
75 {
76 char filename[255];
77 int retval = 0;
78 strncpy(filename, UDEV_ROOT, sizeof(filename));
79 strncat(filename, name, sizeof(filename));
80 switch (type) {
81 case 'b':
82 mode |= S_IFBLK;
83 break;
84 case 'c':
85 case 'u':
86 mode |= S_IFCHR;
87 break;
88 case 'p':
89 mode |= S_IFIFO;
90 break;
91 default:
92 dbg("unknown node type %c\n", type);
93 return -EINVAL;
94 }
95
96 retval = mknod(filename,mode,makedev(major,minor));
97 if (retval)
98 dbg("mknod(%s, %#o, %u, %u) failed with error '%s'",
99 filename, mode, major, minor, strerror(errno));
100 return retval;
101 }
102
103 struct sysfs_class_device *get_class_dev(char *device_name)
104 {
105 char sysfs_path[SYSFS_PATH_MAX];
106 char dev_path[SYSFS_PATH_MAX];
107 int retval;
108 struct sysfs_class_device *class_dev = NULL;
109
110
111 retval = sysfs_get_mnt_path(sysfs_path, SYSFS_PATH_MAX);
112 dbg("sysfs_path = %s", sysfs_path);
113 if (retval) {
114 dbg("sysfs_get_mnt_path failed");
115 goto exit;
116 }
117
118 strcpy(dev_path, sysfs_path);
119 strcat(dev_path, device_name);
120
121 dbg("looking at %s", dev_path);
122
123 /* open up the sysfs class device for this thing... */
124 class_dev = sysfs_open_class_device(dev_path);
125 if (class_dev == NULL) {
126 dbg ("sysfs_open_class_device failed");
127 goto exit;
128 }
129 dbg("class_dev->name = %s", class_dev->name);
130
131 exit:
132 return class_dev;
133 }
134
135 int udev_add_device(char *device, char *subsystem)
136 {
137 struct sysfs_class_device *class_dev;
138 struct device_attr attr;
139 int major;
140 int minor;
141 char type;
142 int retval = -EINVAL;
143
144 /* for now, the block layer is the only place where block devices are */
145 if (strcmp(subsystem, "block") == 0)
146 type = 'b';
147 else
148 type = 'c';
149
150 /* sleep for a second or two to give the kernel a chance to
151 * create the dev file
152 */
153 sleep(1);
154
155 class_dev = get_class_dev(device);
156 if (class_dev == NULL)
157 goto exit;
158
159 retval = namedev_name_device(class_dev, &attr);
160 if (retval)
161 return retval;
162
163 retval = get_major_minor(class_dev, &major, &minor);
164 if (retval) {
165 dbg ("get_major_minor failed");
166 goto exit;
167 }
168 memset(&dbdev, 0, sizeof(dbdev));
169 strncpy(dbdev.name, attr.name, NAME_SIZE);
170 strncpy(dbdev.sysfs_path, class_dev->sysdevice->directory->path,
171 PATH_SIZE);
172 strncpy(dbdev.class_dev_name, class_dev->name, NAME_SIZE);
173 if ((sysfs_get_name_from_path(subsystem, dbdev.class_name, NAME_SIZE))
174 != 0)
175 strcpy(dbdev.class_name, "unkown");
176 strncpy(dbdev.bus_id, class_dev->sysdevice->bus_id, ID_SIZE);
177 strcpy(dbdev.bus_name, "unknown");
178 if (class_dev->driver != NULL)
179 strncpy(dbdev.driver, class_dev->driver->name, NAME_SIZE);
180 else
181 strcpy(dbdev.driver, "unkown");
182 dbdev.type = type;
183 dbdev.major = major;
184 dbdev.minor = minor;
185 dbdev.mode = attr.mode;
186
187 sysfs_close_class_device(class_dev);
188
189 retval = udevdb_add_udevice(&dbdev);
190 if (retval != 0)
191 goto exit;
192
193 return create_node(attr.name, type, major, minor, attr.mode);
194
195 exit:
196 return retval;
197 }
198