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.
33 #include <sys/utsname.h>
35 #include "udev_libc_wrapper.h"
38 #include "udev_utils.h"
42 int udev_init_device(struct udevice *udev, const char* devpath, const char *subsystem, const char *action)
46 memset(udev, 0x00, sizeof(struct udevice));
47 INIT_LIST_HEAD(&udev->symlink_list);
50 strlcpy(udev->subsystem, subsystem, sizeof(udev->subsystem));
53 strlcpy(udev->devpath, devpath, sizeof(udev->devpath));
54 remove_trailing_char(udev->devpath, '/');
56 if (strncmp(udev->devpath, "/block/", 7) == 0)
57 udev->type = DEV_BLOCK;
58 else if (strncmp(udev->devpath, "/class/net/", 11) == 0)
60 else if (strncmp(udev->devpath, "/class/", 7) == 0)
61 udev->type = DEV_CLASS;
62 else if (strncmp(udev->devpath, "/devices/", 9) == 0)
63 udev->type = DEV_DEVICE;
66 pos = strrchr(udev->devpath, '/');
68 strlcpy(udev->kernel_name, &pos[1], sizeof(udev->kernel_name));
69 dbg("kernel_name='%s'", udev->kernel_name);
71 /* Some block devices have '!' in their name, change that to '/' */
72 pos = udev->kernel_name;
73 while (pos[0] != '\0') {
79 /* get kernel number */
80 pos = &udev->kernel_name[strlen(udev->kernel_name)];
81 while (isdigit(pos[-1]))
83 strlcpy(udev->kernel_number, pos, sizeof(udev->kernel_number));
84 dbg("kernel_number='%s'", udev->kernel_number);
89 strcpy(udev->owner, "root");
90 strcpy(udev->group, "root");
95 void udev_cleanup_device(struct udevice *udev)
97 struct name_entry *name_loop;
98 struct name_entry *temp_loop;
100 list_for_each_entry_safe(name_loop, temp_loop, &udev->symlink_list, node) {
101 list_del(&name_loop->node);
106 int kernel_release_satisfactory(unsigned int version, unsigned int patchlevel, unsigned int sublevel)
108 static unsigned int kversion = 0;
109 static unsigned int kpatchlevel;
110 static unsigned int ksublevel;
114 if (uname(&uts) != 0)
117 if (sscanf (uts.release, "%u.%u.%u", &kversion, &kpatchlevel, &ksublevel) != 3) {
123 if (kversion >= version && kpatchlevel >= patchlevel && ksublevel >= sublevel)
129 int create_path(const char *path)
136 pos = strrchr(p, '/');
137 if (pos == p || pos == NULL)
140 while (pos[-1] == '/')
145 dbg("stat '%s'\n", p);
146 if (stat (p, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR)
149 if (create_path (p) != 0)
152 dbg("mkdir '%s'\n", p);
153 return mkdir(p, 0755);
156 /* Reset permissions on the device node, before unlinking it to make sure,
157 * that permisions of possible hard links will be removed too.
159 int unlink_secure(const char *filename)
163 retval = chown(filename, 0, 0);
165 dbg("chown(%s, 0, 0) failed with error '%s'", filename, strerror(errno));
167 retval = chmod(filename, 0000);
169 dbg("chmod(%s, 0000) failed with error '%s'", filename, strerror(errno));
171 retval = unlink(filename);
176 dbg("unlink(%s) failed with error '%s'", filename, strerror(errno));
181 int file_map(const char *filename, char **buf, size_t *bufsize)
186 fd = open(filename, O_RDONLY);
191 if (fstat(fd, &stats) < 0) {
196 *buf = mmap(NULL, stats.st_size, PROT_READ, MAP_SHARED, fd, 0);
197 if (*buf == MAP_FAILED) {
201 *bufsize = stats.st_size;
208 void file_unmap(char *buf, size_t bufsize)
210 munmap(buf, bufsize);
213 /* return number of chars until the next newline, skip escaped newline */
214 size_t buf_get_line(const char *buf, size_t buflen, size_t cur)
219 for (count = cur; count < buflen; count++) {
220 if (!escape && buf[count] == '\n')
223 if (buf[count] == '\\')
232 void replace_untrusted_chars(char *string)
236 for (len = 0; string[len] != '\0'; len++) {
237 if (strchr(";,~\\()\'", string[len])) {
238 info("replace '%c' in '%s'", string[len], string);
244 void remove_trailing_char(char *path, char c)
249 while (len > 0 && path[len-1] == c)
253 int name_list_add(struct list_head *name_list, const char *name, int sort)
255 struct name_entry *loop_name;
256 struct name_entry *new_name;
258 list_for_each_entry(loop_name, name_list, node) {
260 if (strcmp(loop_name->name, name) == 0) {
261 dbg("'%s' is already in the list", name);
264 if (sort && strcmp(loop_name->name, name) > 0)
268 new_name = malloc(sizeof(struct name_entry));
269 if (new_name == NULL) {
274 strlcpy(new_name->name, name, sizeof(new_name->name));
275 list_add_tail(&new_name->node, &loop_name->node);
280 /* calls function for every file found in specified directory */
281 int add_matching_files(struct list_head *name_list, const char *dirname, const char *suffix)
286 char filename[PATH_SIZE];
288 dbg("open directory '%s'", dirname);
289 dir = opendir(dirname);
291 dbg("unable to open '%s'", dirname);
297 if (ent == NULL || ent->d_name[0] == '\0')
300 if ((ent->d_name[0] == '.') || (ent->d_name[0] == COMMENT_CHARACTER))
303 /* look for file matching with specified suffix */
304 ext = strrchr(ent->d_name, '.');
308 if (strcmp(ext, suffix) != 0)
311 dbg("put file '%s/%s' in list", dirname, ent->d_name);
313 snprintf(filename, sizeof(filename), "%s/%s", dirname, ent->d_name);
314 filename[sizeof(filename)-1] = '\0';
315 name_list_add(name_list, filename, 1);
322 int execute_command(const char *command, const char *subsystem)
327 char *argv[(PATH_SIZE / 2) + 1];
332 strlcpy(arg, command, sizeof(arg));
334 if (strchr(arg, ' ')) {
336 while (pos != NULL) {
337 if (pos[0] == '\'') {
338 /* don't separate if in apostrophes */
340 argv[i] = strsep(&pos, "\'");
341 while (pos && pos[0] == ' ')
344 argv[i] = strsep(&pos, " ");
346 dbg("arg[%i] '%s'", i, argv[i]);
350 dbg("execute '%s' with parsed arguments", arg);
353 argv[1] = (char *) subsystem;
355 dbg("execute '%s' with subsystem '%s' argument", arg, argv[1]);
362 devnull = open("/dev/null", O_RDWR);
364 dup2(devnull, STDIN_FILENO);
365 dup2(devnull, STDOUT_FILENO);
366 dup2(devnull, STDERR_FILENO);
369 retval = execv(arg, argv);
370 err("exec of child failed");
373 dbg("fork of child failed");
377 waitpid(pid, NULL, 0);