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>
36 #include "udev_utils.h"
40 int udev_init_device(struct udevice *udev, const char* devpath, const char *subsystem)
44 memset(udev, 0x00, sizeof(struct udevice));
47 strfieldcpy(udev->devpath, devpath);
48 no_trailing_slash(udev->devpath);
51 strfieldcpy(udev->subsystem, subsystem);
53 if (strcmp(udev->subsystem, "block") == 0)
55 else if (strcmp(udev->subsystem, "net") == 0)
57 else if (strncmp(udev->devpath, "/block/", 7) == 0)
59 else if (strncmp(udev->devpath, "/class/net/", 11) == 0)
61 else if (strncmp(udev->devpath, "/class/", 7) == 0)
63 else if (strncmp(udev->devpath, "/devices/", 9) == 0)
67 strcpy(udev->owner, "root");
68 strcpy(udev->group, "root");
71 pos = strrchr(udev->devpath, '/');
74 strfieldcpy(udev->kernel_name, &pos[1]);
76 /* get kernel number */
77 pos = &udev->kernel_name[strlen(udev->kernel_name)];
78 while (isdigit(pos[-1]))
80 strfieldcpy(udev->kernel_number, pos);
81 dbg("kernel_number='%s'", udev->kernel_number);
83 /* Some block devices have '!' in their name, change that to '/' */
84 pos = udev->kernel_name;
85 while (pos[0] != '\0') {
91 dbg("kernel_name='%s'", udev->kernel_name);
95 int kernel_release_satisfactory(unsigned int version, unsigned int patchlevel, unsigned int sublevel)
97 static unsigned int kversion = 0;
98 static unsigned int kpatchlevel;
99 static unsigned int ksublevel;
103 if (uname(&uts) != 0)
106 if (sscanf (uts.release, "%u.%u.%u", &kversion, &kpatchlevel, &ksublevel) != 3) {
112 if (kversion >= version && kpatchlevel >= patchlevel && ksublevel >= sublevel)
118 int create_path(const char *path)
125 pos = strrchr(p, '/');
126 if (pos == p || pos == NULL)
129 while (pos[-1] == '/')
134 dbg("stat '%s'\n", p);
135 if (stat (p, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR)
138 if (create_path (p) != 0)
141 dbg("mkdir '%s'\n", p);
142 return mkdir(p, 0755);
145 /* Reset permissions on the device node, before unlinking it to make sure,
146 * that permisions of possible hard links will be removed too.
148 int unlink_secure(const char *filename)
152 retval = chown(filename, 0, 0);
154 dbg("chown(%s, 0, 0) failed with error '%s'", filename, strerror(errno));
156 retval = chmod(filename, 0000);
158 dbg("chmod(%s, 0000) failed with error '%s'", filename, strerror(errno));
160 retval = unlink(filename);
165 dbg("unlink(%s) failed with error '%s'", filename, strerror(errno));
170 int parse_get_pair(char **orig_string, char **left, char **right)
173 char *string = *orig_string;
178 /* eat any whitespace */
179 while (isspace(*string) || *string == ',')
182 /* split based on '=' */
183 temp = strsep(&string, "=");
188 /* take the right side and strip off the '"' */
189 while (isspace(*string))
196 temp = strsep(&string, "\"");
197 if (!string || *temp == '\0')
200 *orig_string = string;
205 int file_map(const char *filename, char **buf, size_t *bufsize)
210 fd = open(filename, O_RDONLY);
215 if (fstat(fd, &stats) < 0) {
220 *buf = mmap(NULL, stats.st_size, PROT_READ, MAP_SHARED, fd, 0);
221 if (*buf == MAP_FAILED) {
225 *bufsize = stats.st_size;
232 void file_unmap(char *buf, size_t bufsize)
234 munmap(buf, bufsize);
237 /* return number of chars until the next newline, skip escaped newline */
238 size_t buf_get_line(const char *buf, size_t buflen, size_t cur)
243 for (count = cur; count < buflen; count++) {
244 if (!escape && buf[count] == '\n')
247 if (buf[count] == '\\')
256 void no_trailing_slash(char *path)
261 while (len > 0 && path[len-1] == '/')
266 struct list_head node;
267 char name[NAME_SIZE];
270 /* sort files in lexical order */
271 static int name_list_add(struct list_head *name_list, const char *name, int sort)
273 struct name_entry *loop_name;
274 struct name_entry *new_name;
276 list_for_each_entry(loop_name, name_list, node) {
278 if (strcmp(loop_name->name, name) == 0) {
279 dbg("'%s' is already in the list", name);
282 if (sort && strcmp(loop_name->name, name) > 0)
286 new_name = malloc(sizeof(struct name_entry));
287 if (new_name == NULL) {
292 strfieldcpy(new_name->name, name);
293 list_add_tail(&new_name->node, &loop_name->node);
297 /* calls function for every file found in specified directory */
298 int call_foreach_file(file_fnct_t fnct, const char *dirname, const char *suffix, void *data)
303 struct name_entry *loop_file;
304 struct name_entry *tmp_file;
305 LIST_HEAD(file_list);
307 dbg("open directory '%s'", dirname);
308 dir = opendir(dirname);
310 dbg("unable to open '%s'", dirname);
316 if (ent == NULL || ent->d_name[0] == '\0')
319 if ((ent->d_name[0] == '.') || (ent->d_name[0] == COMMENT_CHARACTER))
322 /* look for file with specified suffix */
323 ext = strrchr(ent->d_name, '.');
327 if (strcmp(ext, suffix) != 0)
330 dbg("put file '%s/%s' in list", dirname, ent->d_name);
331 name_list_add(&file_list, ent->d_name, 1);
334 /* call function for every file in the list */
335 list_for_each_entry_safe(loop_file, tmp_file, &file_list, node) {
336 char filename[NAME_SIZE];
338 snprintf(filename, NAME_SIZE, "%s/%s", dirname, loop_file->name);
339 filename[NAME_SIZE-1] = '\0';
341 fnct(filename, data);
343 list_del(&loop_file->node);