]>
git.ipfire.org Git - thirdparty/systemd.git/blob - udev-add.c
6 * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
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.
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.
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.
31 #include <sys/types.h>
38 #include "udev_version.h"
39 #include "udev_dbus.h"
43 #include "libsysfs/libsysfs.h"
44 #include "klibc_fixups.h"
47 * Right now the major/minor of a device is stored in a file called
49 * The number is stored as:
53 * The value is in decimal.
55 static int get_major_minor(struct sysfs_class_device
*class_dev
, struct udevice
*udev
)
58 struct sysfs_attribute
*attr
= NULL
;
60 attr
= sysfs_get_classdev_attr(class_dev
, "dev");
63 dbg("dev='%s'", attr
->value
);
65 if (sscanf(attr
->value
, "%u:%u", &udev
->major
, &udev
->minor
) != 2)
67 dbg("found major=%d, minor=%d", udev
->major
, udev
->minor
);
74 static int create_path(char *file
)
81 strncpy(p
, file
, sizeof(p
));
82 pos
= strchr(p
+1, '/');
84 pos
= strchr(pos
+1, '/');
88 if (stat(p
, &stats
)) {
89 retval
= mkdir(p
, 0755);
91 dbg("mkdir(%s) failed with error '%s'",
95 dbg("created '%s'", p
);
102 static int create_node(struct udevice
*dev
)
106 char linktarget
[255];
116 strncpy(filename
, udev_root
, sizeof(filename
));
117 strncat(filename
, dev
->name
, sizeof(filename
));
120 res
= (dev
->major
<< 8) | (dev
->minor
);
122 res
= makedev(dev
->major
, dev
->minor
);
127 dev
->mode
|= S_IFBLK
;
131 dev
->mode
|= S_IFCHR
;
134 dev
->mode
|= S_IFIFO
;
137 dbg("unknown node type %c\n", dev
->type
);
141 /* create parent directories if needed */
142 if (strrchr(dev
->name
, '/'))
143 create_path(filename
);
145 info("creating device node '%s'", filename
);
146 dbg("mknod(%s, %#o, %u, %u)", filename
, dev
->mode
, dev
->major
, dev
->minor
);
147 retval
= mknod(filename
, dev
->mode
, res
);
149 dbg("mknod(%s, %#o, %u, %u) failed with error '%s'",
150 filename
, dev
->mode
, dev
->major
, dev
->minor
, strerror(errno
));
152 dbg("chmod(%s, %#o)", filename
, dev
->mode
);
153 retval
= chmod(filename
, dev
->mode
);
155 dbg("chmod(%s, %#o) failed with error '%s'",
156 filename
, dev
->mode
, strerror(errno
));
160 unsigned long id
= strtoul(dev
->owner
, &endptr
, 10);
161 if (endptr
[0] == '\0')
164 struct passwd
*pw
= getpwnam(dev
->owner
);
166 dbg("specified user unknown '%s'", dev
->owner
);
174 unsigned long id
= strtoul(dev
->group
, &endptr
, 10);
175 if (endptr
[0] == '\0')
178 struct group
*gr
= getgrnam(dev
->group
);
180 dbg("specified group unknown '%s'", dev
->group
);
187 dbg("chown(%s, %u, %u)", filename
, uid
, gid
);
188 retval
= chown(filename
, uid
, gid
);
190 dbg("chown(%s, %u, %u) failed with error '%s'",
191 filename
, uid
, gid
, strerror(errno
));
194 /* create symlink if requested */
195 if (dev
->symlink
[0]) {
196 symlinks
= dev
->symlink
;
198 linkname
= strsep(&symlinks
, " ");
199 if (linkname
== NULL
|| linkname
[0] == '\0')
202 strncpy(filename
, udev_root
, sizeof(filename
));
203 strncat(filename
, linkname
, sizeof(filename
));
204 dbg("symlink '%s' to node '%s' requested", filename
, dev
->name
);
205 if (strrchr(linkname
, '/'))
206 create_path(filename
);
208 /* optimize relative link */
209 linktarget
[0] = '\0';
212 while ((dev
->name
[i
] == linkname
[i
]) && dev
->name
[i
]) {
213 if (dev
->name
[i
] == '/')
217 while (linkname
[i
]) {
218 if (linkname
[i
] == '/')
219 strcat(linktarget
, "../");
223 if (*linktarget
== '\0')
224 strcpy(linktarget
, "./");
225 strcat(linktarget
, &dev
->name
[tail
]);
227 /* unlink existing non-directories to ensure that our symlink
229 if (lstat(filename
, &stats
) == 0) {
230 if ((stats
.st_mode
& S_IFMT
) != S_IFDIR
) {
231 if (unlink(filename
))
232 dbg("unlink(%s) failed with error '%s'",
233 filename
, strerror(errno
));
237 dbg("symlink(%s, %s)", linktarget
, filename
);
238 retval
= symlink(linktarget
, filename
);
240 dbg("symlink(%s, %s) failed with error '%s'",
241 linktarget
, filename
, strerror(errno
));
248 static struct sysfs_class_device
*get_class_dev(char *device_name
)
250 char dev_path
[SYSFS_PATH_MAX
];
251 struct sysfs_class_device
*class_dev
= NULL
;
253 strcpy(dev_path
, sysfs_path
);
254 strcat(dev_path
, device_name
);
255 dbg("looking at '%s'", dev_path
);
257 /* open up the sysfs class device for this thing... */
258 class_dev
= sysfs_open_class_device_path(dev_path
);
259 if (class_dev
== NULL
) {
260 dbg ("sysfs_open_class_device_path failed");
263 dbg("class_dev->name='%s'", class_dev
->name
);
269 /* wait for the "dev" file to show up in the directory in sysfs.
270 * If it doesn't happen in about 10 seconds, give up.
272 #define SECONDS_TO_WAIT_FOR_DEV 10
273 static int sleep_for_dev(char *path
)
275 char filename
[SYSFS_PATH_MAX
+ 6];
276 int loop
= SECONDS_TO_WAIT_FOR_DEV
;
279 strcpy(filename
, sysfs_path
);
280 strcat(filename
, path
);
281 strcat(filename
, "/dev");
286 dbg("looking for '%s'", filename
);
287 retval
= stat(filename
, &buf
);
291 /* sleep to give the kernel a chance to create the dev file */
299 int udev_add_device(char *path
, char *subsystem
)
301 struct sysfs_class_device
*class_dev
= NULL
;
303 int retval
= -EINVAL
;
305 memset(&dev
, 0x00, sizeof(dev
));
307 /* for now, the block layer is the only place where block devices are */
308 if (strcmp(subsystem
, "block") == 0)
313 retval
= sleep_for_dev(path
);
317 class_dev
= get_class_dev(path
);
318 if (class_dev
== NULL
)
321 retval
= get_major_minor(class_dev
, &dev
);
323 dbg("get_major_minor failed");
327 retval
= namedev_name_device(class_dev
, &dev
);
331 retval
= udevdb_add_dev(path
, &dev
);
333 dbg("udevdb_add_dev failed, but we are going to try to create the node anyway. "
334 "But remove might not work properly for this device.");
336 dbg("name='%s'", dev
.name
);
337 retval
= create_node(&dev
);
340 sysbus_send_create(&dev
, path
);
344 sysfs_close_class_device(class_dev
);