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->subsystem, subsystem);
50 strfieldcpy(udev->devpath, devpath);
51 no_trailing_slash(udev->devpath);
53 if (strncmp(udev->devpath, "/block/", 7) == 0)
55 else if (strncmp(udev->devpath, "/class/net/", 11) == 0)
57 else if (strncmp(udev->devpath, "/class/", 7) == 0)
59 else if (strncmp(udev->devpath, "/devices/", 9) == 0)
63 pos = strrchr(udev->devpath, '/');
65 strfieldcpy(udev->kernel_name, &pos[1]);
66 dbg("kernel_name='%s'", udev->kernel_name);
68 /* Some block devices have '!' in their name, change that to '/' */
69 pos = udev->kernel_name;
70 while (pos[0] != '\0') {
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);
86 strcpy(udev->owner, "root");
87 strcpy(udev->group, "root");
92 int kernel_release_satisfactory(unsigned int version, unsigned int patchlevel, unsigned int sublevel)
94 static unsigned int kversion = 0;
95 static unsigned int kpatchlevel;
96 static unsigned int ksublevel;
100 if (uname(&uts) != 0)
103 if (sscanf (uts.release, "%u.%u.%u", &kversion, &kpatchlevel, &ksublevel) != 3) {
109 if (kversion >= version && kpatchlevel >= patchlevel && ksublevel >= sublevel)
115 int create_path(const char *path)
122 pos = strrchr(p, '/');
123 if (pos == p || pos == NULL)
126 while (pos[-1] == '/')
131 dbg("stat '%s'\n", p);
132 if (stat (p, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR)
135 if (create_path (p) != 0)
138 dbg("mkdir '%s'\n", p);
139 return mkdir(p, 0755);
142 /* Reset permissions on the device node, before unlinking it to make sure,
143 * that permisions of possible hard links will be removed too.
145 int unlink_secure(const char *filename)
149 retval = chown(filename, 0, 0);
151 dbg("chown(%s, 0, 0) failed with error '%s'", filename, strerror(errno));
153 retval = chmod(filename, 0000);
155 dbg("chmod(%s, 0000) failed with error '%s'", filename, strerror(errno));
157 retval = unlink(filename);
162 dbg("unlink(%s) failed with error '%s'", filename, strerror(errno));
167 int parse_get_pair(char **orig_string, char **left, char **right)
170 char *string = *orig_string;
175 /* eat any whitespace */
176 while (isspace(*string) || *string == ',')
179 /* split based on '=' */
180 temp = strsep(&string, "=");
185 /* take the right side and strip off the '"' */
186 while (isspace(*string))
193 temp = strsep(&string, "\"");
194 if (!string || *temp == '\0')
197 *orig_string = string;
202 int file_map(const char *filename, char **buf, size_t *bufsize)
207 fd = open(filename, O_RDONLY);
212 if (fstat(fd, &stats) < 0) {
217 *buf = mmap(NULL, stats.st_size, PROT_READ, MAP_SHARED, fd, 0);
218 if (*buf == MAP_FAILED) {
222 *bufsize = stats.st_size;
229 void file_unmap(char *buf, size_t bufsize)
231 munmap(buf, bufsize);
234 /* return number of chars until the next newline, skip escaped newline */
235 size_t buf_get_line(const char *buf, size_t buflen, size_t cur)
240 for (count = cur; count < buflen; count++) {
241 if (!escape && buf[count] == '\n')
244 if (buf[count] == '\\')
253 void no_trailing_slash(char *path)
258 while (len > 0 && path[len-1] == '/')
263 struct list_head node;
264 char name[NAME_SIZE];
267 /* sort files in lexical order */
268 static int name_list_add(struct list_head *name_list, const char *name, int sort)
270 struct name_entry *loop_name;
271 struct name_entry *new_name;
273 list_for_each_entry(loop_name, name_list, node) {
275 if (strcmp(loop_name->name, name) == 0) {
276 dbg("'%s' is already in the list", name);
279 if (sort && strcmp(loop_name->name, name) > 0)
283 new_name = malloc(sizeof(struct name_entry));
284 if (new_name == NULL) {
289 strfieldcpy(new_name->name, name);
290 list_add_tail(&new_name->node, &loop_name->node);
294 /* calls function for every file found in specified directory */
295 int call_foreach_file(file_fnct_t fnct, const char *dirname, const char *suffix, void *data)
300 struct name_entry *loop_file;
301 struct name_entry *tmp_file;
302 LIST_HEAD(file_list);
304 dbg("open directory '%s'", dirname);
305 dir = opendir(dirname);
307 dbg("unable to open '%s'", dirname);
313 if (ent == NULL || ent->d_name[0] == '\0')
316 if ((ent->d_name[0] == '.') || (ent->d_name[0] == COMMENT_CHARACTER))
319 /* look for file with specified suffix */
320 ext = strrchr(ent->d_name, '.');
324 if (strcmp(ext, suffix) != 0)
327 dbg("put file '%s/%s' in list", dirname, ent->d_name);
328 name_list_add(&file_list, ent->d_name, 1);
331 /* call function for every file in the list */
332 list_for_each_entry_safe(loop_file, tmp_file, &file_list, node) {
333 char filename[NAME_SIZE];
335 snprintf(filename, NAME_SIZE, "%s/%s", dirname, loop_file->name);
336 filename[NAME_SIZE-1] = '\0';
338 fnct(filename, data);
340 list_del(&loop_file->node);