2 * udev_lib - generic stuff used by udev
4 * Copyright (C) 2004 Kay Sievers <kay@vrfy.org>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation version 2 of the License.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 675 Mass Ave, Cambridge, MA 02139, USA.
32 #include <sys/utsname.h>
34 #include "udev_libc_wrapper.h"
37 #include "udev_utils.h"
41 int udev_init_device(struct udevice *udev, const char* devpath, const char *subsystem)
45 memset(udev, 0x00, sizeof(struct udevice));
46 INIT_LIST_HEAD(&udev->symlink_list);
49 strlcpy(udev->subsystem, subsystem, sizeof(udev->subsystem));
52 strlcpy(udev->devpath, devpath, sizeof(udev->devpath));
53 no_trailing_slash(udev->devpath);
55 if (strncmp(udev->devpath, "/block/", 7) == 0)
56 udev->type = DEV_BLOCK;
57 else if (strncmp(udev->devpath, "/class/net/", 11) == 0)
59 else if (strncmp(udev->devpath, "/class/", 7) == 0)
60 udev->type = DEV_CLASS;
61 else if (strncmp(udev->devpath, "/devices/", 9) == 0)
62 udev->type = DEV_DEVICE;
65 pos = strrchr(udev->devpath, '/');
67 strlcpy(udev->kernel_name, &pos[1], sizeof(udev->kernel_name));
68 dbg("kernel_name='%s'", udev->kernel_name);
70 /* Some block devices have '!' in their name, change that to '/' */
71 pos = udev->kernel_name;
72 while (pos[0] != '\0') {
78 /* get kernel number */
79 pos = &udev->kernel_name[strlen(udev->kernel_name)];
80 while (isdigit(pos[-1]))
82 strlcpy(udev->kernel_number, pos, sizeof(udev->kernel_number));
83 dbg("kernel_number='%s'", udev->kernel_number);
88 strcpy(udev->owner, "root");
89 strcpy(udev->group, "root");
94 void udev_cleanup_device(struct udevice *udev)
96 struct name_entry *name_loop;
97 struct name_entry *temp_loop;
99 list_for_each_entry_safe(name_loop, temp_loop, &udev->symlink_list, node) {
100 list_del(&name_loop->node);
105 int kernel_release_satisfactory(unsigned int version, unsigned int patchlevel, unsigned int sublevel)
107 static unsigned int kversion = 0;
108 static unsigned int kpatchlevel;
109 static unsigned int ksublevel;
113 if (uname(&uts) != 0)
116 if (sscanf (uts.release, "%u.%u.%u", &kversion, &kpatchlevel, &ksublevel) != 3) {
122 if (kversion >= version && kpatchlevel >= patchlevel && ksublevel >= sublevel)
128 int create_path(const char *path)
135 pos = strrchr(p, '/');
136 if (pos == p || pos == NULL)
139 while (pos[-1] == '/')
144 dbg("stat '%s'\n", p);
145 if (stat (p, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR)
148 if (create_path (p) != 0)
151 dbg("mkdir '%s'\n", p);
152 return mkdir(p, 0755);
155 /* Reset permissions on the device node, before unlinking it to make sure,
156 * that permisions of possible hard links will be removed too.
158 int unlink_secure(const char *filename)
162 retval = chown(filename, 0, 0);
164 dbg("chown(%s, 0, 0) failed with error '%s'", filename, strerror(errno));
166 retval = chmod(filename, 0000);
168 dbg("chmod(%s, 0000) failed with error '%s'", filename, strerror(errno));
170 retval = unlink(filename);
175 dbg("unlink(%s) failed with error '%s'", filename, strerror(errno));
180 int file_map(const char *filename, char **buf, size_t *bufsize)
185 fd = open(filename, O_RDONLY);
190 if (fstat(fd, &stats) < 0) {
195 *buf = mmap(NULL, stats.st_size, PROT_READ, MAP_SHARED, fd, 0);
196 if (*buf == MAP_FAILED) {
200 *bufsize = stats.st_size;
207 void file_unmap(char *buf, size_t bufsize)
209 munmap(buf, bufsize);
212 /* return number of chars until the next newline, skip escaped newline */
213 size_t buf_get_line(const char *buf, size_t buflen, size_t cur)
218 for (count = cur; count < buflen; count++) {
219 if (!escape && buf[count] == '\n')
222 if (buf[count] == '\\')
231 void no_trailing_slash(char *path)
236 while (len > 0 && path[len-1] == '/')
240 int name_list_add(struct list_head *name_list, const char *name, int sort)
242 struct name_entry *loop_name;
243 struct name_entry *new_name;
245 list_for_each_entry(loop_name, name_list, node) {
247 if (strcmp(loop_name->name, name) == 0) {
248 dbg("'%s' is already in the list", name);
251 if (sort && strcmp(loop_name->name, name) > 0)
255 new_name = malloc(sizeof(struct name_entry));
256 if (new_name == NULL) {
261 strlcpy(new_name->name, name, sizeof(new_name->name));
262 list_add_tail(&new_name->node, &loop_name->node);
267 /* calls function for every file found in specified directory */
268 int call_foreach_file(int (*handler_function)(struct udevice *udev, const char *string),
269 struct udevice *udev, const char *dirname, const char *suffix)
274 struct name_entry *loop_file;
275 struct name_entry *tmp_file;
276 LIST_HEAD(file_list);
278 dbg("open directory '%s'", dirname);
279 dir = opendir(dirname);
281 dbg("unable to open '%s'", dirname);
287 if (ent == NULL || ent->d_name[0] == '\0')
290 if ((ent->d_name[0] == '.') || (ent->d_name[0] == COMMENT_CHARACTER))
293 /* look for file with specified suffix */
294 ext = strrchr(ent->d_name, '.');
298 if (strcmp(ext, suffix) != 0)
301 dbg("put file '%s/%s' in list", dirname, ent->d_name);
302 name_list_add(&file_list, ent->d_name, 1);
305 /* call function for every file in the list */
306 list_for_each_entry_safe(loop_file, tmp_file, &file_list, node) {
307 char filename[PATH_SIZE];
309 snprintf(filename, sizeof(filename), "%s/%s", dirname, loop_file->name);
310 filename[sizeof(filename)-1] = '\0';
312 handler_function(udev, filename);
314 list_del(&loop_file->node);