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));
45 INIT_LIST_HEAD(&udev->symlink_list);
48 strfieldcpy(udev->subsystem, subsystem);
51 strfieldcpy(udev->devpath, devpath);
52 no_trailing_slash(udev->devpath);
54 if (strncmp(udev->devpath, "/block/", 7) == 0)
56 else if (strncmp(udev->devpath, "/class/net/", 11) == 0)
58 else if (strncmp(udev->devpath, "/class/", 7) == 0)
60 else if (strncmp(udev->devpath, "/devices/", 9) == 0)
64 pos = strrchr(udev->devpath, '/');
66 strfieldcpy(udev->kernel_name, &pos[1]);
67 dbg("kernel_name='%s'", udev->kernel_name);
69 /* Some block devices have '!' in their name, change that to '/' */
70 pos = udev->kernel_name;
71 while (pos[0] != '\0') {
77 /* get kernel number */
78 pos = &udev->kernel_name[strlen(udev->kernel_name)];
79 while (isdigit(pos[-1]))
81 strfieldcpy(udev->kernel_number, pos);
82 dbg("kernel_number='%s'", udev->kernel_number);
87 strcpy(udev->owner, "0");
88 strcpy(udev->group, "0");
93 void udev_cleanup_device(struct udevice *udev)
95 struct name_entry *name_loop;
96 struct name_entry *temp_loop;
98 list_for_each_entry_safe(name_loop, temp_loop, &udev->symlink_list, node) {
99 list_del(&name_loop->node);
104 int kernel_release_satisfactory(unsigned int version, unsigned int patchlevel, unsigned int sublevel)
106 static unsigned int kversion = 0;
107 static unsigned int kpatchlevel;
108 static unsigned int ksublevel;
112 if (uname(&uts) != 0)
115 if (sscanf (uts.release, "%u.%u.%u", &kversion, &kpatchlevel, &ksublevel) != 3) {
121 if (kversion >= version && kpatchlevel >= patchlevel && ksublevel >= sublevel)
127 int create_path(const char *path)
134 pos = strrchr(p, '/');
135 if (pos == p || pos == NULL)
138 while (pos[-1] == '/')
143 dbg("stat '%s'\n", p);
144 if (stat (p, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR)
147 if (create_path (p) != 0)
150 dbg("mkdir '%s'\n", p);
151 return mkdir(p, 0755);
154 /* Reset permissions on the device node, before unlinking it to make sure,
155 * that permisions of possible hard links will be removed too.
157 int unlink_secure(const char *filename)
161 retval = chown(filename, 0, 0);
163 dbg("chown(%s, 0, 0) failed with error '%s'", filename, strerror(errno));
165 retval = chmod(filename, 0000);
167 dbg("chmod(%s, 0000) failed with error '%s'", filename, strerror(errno));
169 retval = unlink(filename);
174 dbg("unlink(%s) failed with error '%s'", filename, strerror(errno));
179 int parse_get_pair(char **orig_string, char **left, char **right)
182 char *string = *orig_string;
187 /* eat any whitespace */
188 while (isspace(*string) || *string == ',')
191 /* split based on '=' */
192 temp = strsep(&string, "=");
197 /* take the right side and strip off the '"' */
198 while (isspace(*string))
205 temp = strsep(&string, "\"");
206 if (!string || *temp == '\0')
209 *orig_string = string;
214 int file_map(const char *filename, char **buf, size_t *bufsize)
219 fd = open(filename, O_RDONLY);
224 if (fstat(fd, &stats) < 0) {
229 *buf = mmap(NULL, stats.st_size, PROT_READ, MAP_SHARED, fd, 0);
230 if (*buf == MAP_FAILED) {
234 *bufsize = stats.st_size;
241 void file_unmap(char *buf, size_t bufsize)
243 munmap(buf, bufsize);
246 /* return number of chars until the next newline, skip escaped newline */
247 size_t buf_get_line(const char *buf, size_t buflen, size_t cur)
252 for (count = cur; count < buflen; count++) {
253 if (!escape && buf[count] == '\n')
256 if (buf[count] == '\\')
265 void no_trailing_slash(char *path)
270 while (len > 0 && path[len-1] == '/')
274 int name_list_add(struct list_head *name_list, const char *name, int sort)
276 struct name_entry *loop_name;
277 struct name_entry *new_name;
279 list_for_each_entry(loop_name, name_list, node) {
281 if (strcmp(loop_name->name, name) == 0) {
282 dbg("'%s' is already in the list", name);
285 if (sort && strcmp(loop_name->name, name) > 0)
289 new_name = malloc(sizeof(struct name_entry));
290 if (new_name == NULL) {
295 strfieldcpy(new_name->name, name);
296 list_add_tail(&new_name->node, &loop_name->node);
301 /* calls function for every file found in specified directory */
302 int call_foreach_file(int (*handler_function)(struct udevice *udev, const char *string),
303 struct udevice *udev, const char *dirname, const char *suffix)
308 struct name_entry *loop_file;
309 struct name_entry *tmp_file;
310 LIST_HEAD(file_list);
312 dbg("open directory '%s'", dirname);
313 dir = opendir(dirname);
315 dbg("unable to open '%s'", dirname);
321 if (ent == NULL || ent->d_name[0] == '\0')
324 if ((ent->d_name[0] == '.') || (ent->d_name[0] == COMMENT_CHARACTER))
327 /* look for file with specified suffix */
328 ext = strrchr(ent->d_name, '.');
332 if (strcmp(ext, suffix) != 0)
335 dbg("put file '%s/%s' in list", dirname, ent->d_name);
336 name_list_add(&file_list, ent->d_name, 1);
339 /* call function for every file in the list */
340 list_for_each_entry_safe(loop_file, tmp_file, &file_list, node) {
341 char filename[NAME_SIZE];
343 snprintf(filename, NAME_SIZE, "%s/%s", dirname, loop_file->name);
344 filename[NAME_SIZE-1] = '\0';
346 handler_function(udev, filename);
348 list_del(&loop_file->node);