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)
65 strcpy(udev->owner, "root");
66 strcpy(udev->group, "root");
69 pos = strrchr(udev->devpath, '/');
72 strfieldcpy(udev->kernel_name, &pos[1]);
74 /* get kernel number */
75 pos = &udev->kernel_name[strlen(udev->kernel_name)];
76 while (isdigit(pos[-1]))
78 strfieldcpy(udev->kernel_number, pos);
79 dbg("kernel_number='%s'", udev->kernel_number);
81 /* Some block devices have '!' in their name, change that to '/' */
82 pos = udev->kernel_name;
83 while (pos[0] != '\0') {
89 dbg("kernel_name='%s'", udev->kernel_name);
93 int kernel_release_satisfactory(unsigned int version, unsigned int patchlevel, unsigned int sublevel)
95 static unsigned int kversion = 0;
96 static unsigned int kpatchlevel;
97 static unsigned int ksublevel;
101 if (uname(&uts) != 0)
104 if (sscanf (uts.release, "%u.%u.%u", &kversion, &kpatchlevel, &ksublevel) != 3) {
110 if (kversion >= version && kpatchlevel >= patchlevel && ksublevel >= sublevel)
116 int create_path(const char *path)
123 pos = strrchr(p, '/');
124 if (pos == p || pos == NULL)
127 while (pos[-1] == '/')
132 dbg("stat '%s'\n", p);
133 if (stat (p, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR)
136 if (create_path (p) != 0)
139 dbg("mkdir '%s'\n", p);
140 return mkdir(p, 0755);
143 /* Reset permissions on the device node, before unlinking it to make sure,
144 * that permisions of possible hard links will be removed too.
146 int unlink_secure(const char *filename)
150 retval = chown(filename, 0, 0);
152 dbg("chown(%s, 0, 0) failed with error '%s'", filename, strerror(errno));
154 retval = chmod(filename, 0000);
156 dbg("chmod(%s, 0000) failed with error '%s'", filename, strerror(errno));
158 retval = unlink(filename);
163 dbg("unlink(%s) failed with error '%s'", filename, strerror(errno));
168 int parse_get_pair(char **orig_string, char **left, char **right)
171 char *string = *orig_string;
176 /* eat any whitespace */
177 while (isspace(*string) || *string == ',')
180 /* split based on '=' */
181 temp = strsep(&string, "=");
186 /* take the right side and strip off the '"' */
187 while (isspace(*string))
194 temp = strsep(&string, "\"");
195 if (!string || *temp == '\0')
198 *orig_string = string;
203 int file_map(const char *filename, char **buf, size_t *bufsize)
208 fd = open(filename, O_RDONLY);
213 if (fstat(fd, &stats) < 0) {
218 *buf = mmap(NULL, stats.st_size, PROT_READ, MAP_SHARED, fd, 0);
219 if (*buf == MAP_FAILED) {
223 *bufsize = stats.st_size;
230 void file_unmap(char *buf, size_t bufsize)
232 munmap(buf, bufsize);
235 /* return number of chars until the next newline, skip escaped newline */
236 size_t buf_get_line(const char *buf, size_t buflen, size_t cur)
241 for (count = cur; count < buflen; count++) {
242 if (!escape && buf[count] == '\n')
245 if (buf[count] == '\\')
254 void no_trailing_slash(char *path)
259 while (len > 0 && path[len-1] == '/')
264 struct list_head node;
265 char name[NAME_SIZE];
268 /* sort files in lexical order */
269 static int name_list_add(struct list_head *name_list, const char *name, int sort)
271 struct name_entry *loop_name;
272 struct name_entry *new_name;
274 list_for_each_entry(loop_name, name_list, node) {
276 if (strcmp(loop_name->name, name) == 0) {
277 dbg("'%s' is already in the list", name);
280 if (sort && strcmp(loop_name->name, name) > 0)
284 new_name = malloc(sizeof(struct name_entry));
285 if (new_name == NULL) {
290 strfieldcpy(new_name->name, name);
291 list_add_tail(&new_name->node, &loop_name->node);
295 /* calls function for every file found in specified directory */
296 int call_foreach_file(file_fnct_t fnct, const char *dirname,
297 const char *suffix, void *data)
302 struct name_entry *loop_file;
303 struct name_entry *tmp_file;
304 LIST_HEAD(file_list);
306 dbg("open directory '%s'", dirname);
307 dir = opendir(dirname);
309 dbg("unable to open '%s'", dirname);
315 if (ent == NULL || ent->d_name[0] == '\0')
318 if ((ent->d_name[0] == '.') || (ent->d_name[0] == COMMENT_CHARACTER))
321 /* look for file with specified suffix */
322 ext = strrchr(ent->d_name, '.');
326 if (strcmp(ext, suffix) != 0)
329 dbg("put file '%s/%s' in list", dirname, ent->d_name);
330 name_list_add(&file_list, ent->d_name, 1);
333 /* call function for every file in the list */
334 list_for_each_entry_safe(loop_file, tmp_file, &file_list, node) {
335 char filename[NAME_SIZE];
337 snprintf(filename, NAME_SIZE, "%s/%s", dirname, loop_file->name);
338 filename[NAME_SIZE-1] = '\0';
340 fnct(filename, data);
342 list_del(&loop_file->node);