]> git.ipfire.org Git - thirdparty/systemd.git/blame - udev-add.c
[PATCH] add demo config file.
[thirdparty/systemd.git] / udev-add.c
CommitLineData
ea733a2f
GKH
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>
32ff5bca 30#include <sys/stat.h>
ea733a2f
GKH
31
32#include "udev.h"
33#include "udev_version.h"
34#include "namedev.h"
8e41d35d 35#include "udevdb.h"
ea733a2f
GKH
36#include "libsysfs/libsysfs.h"
37
ea733a2f
GKH
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:
727d1ba5 42 * MM:mm
ea733a2f
GKH
43 * MM is the major
44 * mm is the minor
727d1ba5 45 * The value is in decimal.
ea733a2f 46 */
30defadd 47static int get_major_minor(struct sysfs_class_device *class_dev, struct udevice *udev)
ea733a2f 48{
ea256f90 49 int retval = -ENODEV;
ea733a2f
GKH
50
51 char *dev;
52
53 dev = sysfs_get_value_from_attributes(class_dev->directory->attributes, "dev");
54 if (dev == NULL)
ea256f90 55 goto exit;
ea733a2f
GKH
56
57 dbg("dev = %s", dev);
58
30defadd 59 if (sscanf(dev, "%u:%u", &udev->major, &udev->minor) != 2)
ea256f90 60 goto exit;
ea733a2f 61
30defadd 62 dbg("found major = %d, minor = %d", udev->major, udev->minor);
ea733a2f
GKH
63
64 retval = 0;
ea256f90 65exit:
ea733a2f
GKH
66 return retval;
67}
68
69/*
c19a6b30
KS
70 * we possibly want to add some symlinks here
71 * only numeric owner/group id's are supported
ea733a2f 72 */
5840bc63 73static int create_node(struct udevice *dev)
ea733a2f 74{
ea733a2f
GKH
75 char filename[255];
76 int retval = 0;
e308ebb7
GKH
77 uid_t uid = 0;
78 gid_t gid = 0;
1e959a4b 79 dev_t res;
5840bc63 80
c056c514 81 strncpy(filename, udev_root, sizeof(filename));
5840bc63
GKH
82 strncat(filename, dev->name, sizeof(filename));
83
1e959a4b
GKH
84#ifdef __KLIBC__
85 res = (dev->major << 8) | (dev->minor);
86#else
87 res = makedev(dev->major, dev->minor);
88#endif
89
5840bc63 90 switch (dev->type) {
1331c889 91 case 'b':
5840bc63 92 dev->mode |= S_IFBLK;
1331c889
GKH
93 break;
94 case 'c':
95 case 'u':
5840bc63 96 dev->mode |= S_IFCHR;
1331c889
GKH
97 break;
98 case 'p':
5840bc63 99 dev->mode |= S_IFIFO;
1331c889
GKH
100 break;
101 default:
5840bc63 102 dbg("unknown node type %c\n", dev->type);
1331c889
GKH
103 return -EINVAL;
104 }
0abf54fc 105
218eae87
KS
106 /* create subdirectories if requested */
107 if (strchr(dev->name, '/')) {
108 char path[255];
109 char *pos;
110 struct stat stats;
111
112 strncpy(path, filename, sizeof(path));
113 pos = strchr(path+1, '/');
114 while (1) {
115 pos = strchr(pos+1, '/');
116 if (pos == NULL)
117 break;
118 *pos = 0x00;
119 if (stat(path, &stats)) {
120 retval = mkdir(path, 0755);
121 if (retval) {
122 dbg("mkdir(%s) failed with error '%s'",
123 path, strerror(errno));
124 return retval;
125 }
126 dbg("created %s", path);
127 }
128 *pos = '/';
129 }
130 }
131
5840bc63 132 dbg("mknod(%s, %#o, %u, %u)", filename, dev->mode, dev->major, dev->minor);
1e959a4b 133 retval = mknod(filename, dev->mode, res);
1331c889
GKH
134 if (retval)
135 dbg("mknod(%s, %#o, %u, %u) failed with error '%s'",
5840bc63
GKH
136 filename, dev->mode, dev->major, dev->minor, strerror(errno));
137
c19a6b30
KS
138 if (*dev->owner) {
139 char *endptr;
140 unsigned long id = strtoul(dev->owner, &endptr, 10);
141 if (*endptr == 0x00)
142 uid = (uid_t) id;
143 else
144 dbg("only numeric owner id supported: %s", dev->owner);
145 }
146
147 if (*dev->group) {
148 char *endptr;
149 unsigned long id = strtoul(dev->group, &endptr, 10);
150 if (*endptr == 0x00)
151 gid = (gid_t) id;
152 else
153 dbg("only numeric group id supported: %s", dev->group);
154 }
155
156 if (uid || gid) {
157 dbg("chown(%s, %u, %u)", filename, uid, gid);
158 retval = chown(filename, uid, gid);
159 if (retval)
160 dbg("chown(%s, %u, %u) failed with error '%s'", filename,
161 uid, gid, strerror(errno));
162 }
163
ea733a2f
GKH
164 return retval;
165}
166
d7e954a4 167static struct sysfs_class_device *get_class_dev(char *device_name)
ea733a2f
GKH
168{
169 char dev_path[SYSFS_PATH_MAX];
63dde9f8
GKH
170 struct sysfs_class_device *class_dev = NULL;
171
ea733a2f
GKH
172 strcpy(dev_path, sysfs_path);
173 strcat(dev_path, device_name);
174
175 dbg("looking at %s", dev_path);
176
177 /* open up the sysfs class device for this thing... */
178 class_dev = sysfs_open_class_device(dev_path);
179 if (class_dev == NULL) {
180 dbg ("sysfs_open_class_device failed");
63dde9f8 181 goto exit;
ea733a2f
GKH
182 }
183 dbg("class_dev->name = %s", class_dev->name);
184
63dde9f8 185exit:
ea733a2f
GKH
186 return class_dev;
187}
188
29fd7e67
GKH
189/* wait for the "dev" file to show up in the directory in sysfs.
190 * If it doesn't happen in about 10 seconds, give up.
191 */
192#define SECONDS_TO_WAIT_FOR_DEV 10
6089318c 193static int sleep_for_dev(char *path)
29fd7e67
GKH
194{
195 char filename[SYSFS_PATH_MAX + 6];
c332cfc7
RL
196 int loop = SECONDS_TO_WAIT_FOR_DEV;
197 int retval;
29fd7e67
GKH
198
199 strcpy(filename, sysfs_path);
5840bc63 200 strcat(filename, path);
29fd7e67
GKH
201 strcat(filename, "/dev");
202
c332cfc7
RL
203 while (loop--) {
204 struct stat buf;
205
29fd7e67
GKH
206 dbg("looking for %s", filename);
207 retval = stat(filename, &buf);
c332cfc7 208 if (!retval)
29fd7e67 209 goto exit;
29fd7e67
GKH
210
211 /* sleep for a second or two to give the kernel a chance to
212 * create the dev file */
213 sleep(1);
214 }
215 retval = -ENODEV;
216exit:
217 return retval;
218}
219
5840bc63 220int udev_add_device(char *path, char *subsystem)
ea733a2f 221{
30defadd 222 struct sysfs_class_device *class_dev = NULL;
5840bc63 223 struct udevice dev;
ea733a2f
GKH
224 int retval = -EINVAL;
225
ea733a2f
GKH
226 /* for now, the block layer is the only place where block devices are */
227 if (strcmp(subsystem, "block") == 0)
5840bc63 228 dev.type = 'b';
ea733a2f 229 else
5840bc63 230 dev.type = 'c';
ea733a2f 231
5840bc63 232 retval = sleep_for_dev(path);
29fd7e67
GKH
233 if (retval)
234 goto exit;
63dde9f8 235
5840bc63 236 class_dev = get_class_dev(path);
ea733a2f
GKH
237 if (class_dev == NULL)
238 goto exit;
239
30defadd 240 retval = get_major_minor(class_dev, &dev);
ea733a2f 241 if (retval) {
ca999860 242 dbg("get_major_minor failed");
ea733a2f
GKH
243 goto exit;
244 }
245
30defadd
GKH
246 retval = namedev_name_device(class_dev, &dev);
247 if (retval)
248 goto exit;
249
5840bc63 250 retval = udevdb_add_dev(path, &dev);
8e41d35d 251 if (retval != 0)
5840bc63 252 dbg("udevdb_add_dev failed, but we are going to try to create the node anyway. "
ca999860
GKH
253 "But remove might not work properly for this device.");
254
5840bc63
GKH
255 dbg("name = %s", dev.name);
256 retval = create_node(&dev);
ea733a2f
GKH
257
258exit:
30defadd
GKH
259 if (class_dev)
260 sysfs_close_class_device(class_dev);
261
ea733a2f
GKH
262 return retval;
263}
264