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);
48 INIT_LIST_HEAD(&udev->run_list);
51 strlcpy(udev->subsystem, subsystem, sizeof(udev->subsystem));
54 strlcpy(udev->action, action, sizeof(udev->action));
57 strlcpy(udev->devpath, devpath, sizeof(udev->devpath));
58 remove_trailing_char(udev->devpath, '/');
60 if (strncmp(udev->devpath, "/block/", 7) == 0)
61 udev->type = DEV_BLOCK;
62 else if (strncmp(udev->devpath, "/class/net/", 11) == 0)
64 else if (strncmp(udev->devpath, "/class/", 7) == 0)
65 udev->type = DEV_CLASS;
66 else if (strncmp(udev->devpath, "/devices/", 9) == 0)
67 udev->type = DEV_DEVICE;
70 pos = strrchr(udev->devpath, '/');
72 strlcpy(udev->kernel_name, &pos[1], sizeof(udev->kernel_name));
73 dbg("kernel_name='%s'", udev->kernel_name);
75 /* Some block devices have '!' in their name, change that to '/' */
76 pos = udev->kernel_name;
77 while (pos[0] != '\0') {
83 /* get kernel number */
84 pos = &udev->kernel_name[strlen(udev->kernel_name)];
85 while (isdigit(pos[-1]))
87 strlcpy(udev->kernel_number, pos, sizeof(udev->kernel_number));
88 dbg("kernel_number='%s'", udev->kernel_number);
92 if (udev->type == DEV_BLOCK || udev->type == DEV_CLASS) {
94 strcpy(udev->owner, "root");
95 strcpy(udev->group, "root");
101 void udev_cleanup_device(struct udevice *udev)
103 struct name_entry *name_loop;
104 struct name_entry *temp_loop;
106 list_for_each_entry_safe(name_loop, temp_loop, &udev->symlink_list, node) {
107 list_del(&name_loop->node);
112 int kernel_release_satisfactory(unsigned int version, unsigned int patchlevel, unsigned int sublevel)
114 static unsigned int kversion = 0;
115 static unsigned int kpatchlevel;
116 static unsigned int ksublevel;
120 if (uname(&uts) != 0)
123 if (sscanf (uts.release, "%u.%u.%u", &kversion, &kpatchlevel, &ksublevel) != 3) {
129 if (kversion >= version && kpatchlevel >= patchlevel && ksublevel >= sublevel)
135 int create_path(const char *path)
142 pos = strrchr(p, '/');
143 if (pos == p || pos == NULL)
146 while (pos[-1] == '/')
151 dbg("stat '%s'\n", p);
152 if (stat (p, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR)
155 if (create_path (p) != 0)
158 dbg("mkdir '%s'\n", p);
159 return mkdir(p, 0755);
162 /* Reset permissions on the device node, before unlinking it to make sure,
163 * that permisions of possible hard links will be removed too.
165 int unlink_secure(const char *filename)
169 retval = chown(filename, 0, 0);
171 dbg("chown(%s, 0, 0) failed with error '%s'", filename, strerror(errno));
173 retval = chmod(filename, 0000);
175 dbg("chmod(%s, 0000) failed with error '%s'", filename, strerror(errno));
177 retval = unlink(filename);
182 dbg("unlink(%s) failed with error '%s'", filename, strerror(errno));
187 int file_map(const char *filename, char **buf, size_t *bufsize)
192 fd = open(filename, O_RDONLY);
197 if (fstat(fd, &stats) < 0) {
202 *buf = mmap(NULL, stats.st_size, PROT_READ, MAP_SHARED, fd, 0);
203 if (*buf == MAP_FAILED) {
207 *bufsize = stats.st_size;
214 void file_unmap(char *buf, size_t bufsize)
216 munmap(buf, bufsize);
219 /* return number of chars until the next newline, skip escaped newline */
220 size_t buf_get_line(const char *buf, size_t buflen, size_t cur)
225 for (count = cur; count < buflen; count++) {
226 if (!escape && buf[count] == '\n')
229 if (buf[count] == '\\')
238 void replace_untrusted_chars(char *string)
242 for (len = 0; string[len] != '\0'; len++) {
243 if (strchr(";,~\\()\'", string[len])) {
244 info("replace '%c' in '%s'", string[len], string);
250 void remove_trailing_char(char *path, char c)
255 while (len > 0 && path[len-1] == c)
259 int name_list_add(struct list_head *name_list, const char *name, int sort)
261 struct name_entry *loop_name;
262 struct name_entry *new_name;
264 list_for_each_entry(loop_name, name_list, node) {
266 if (strcmp(loop_name->name, name) == 0) {
267 dbg("'%s' is already in the list", name);
270 if (sort && strcmp(loop_name->name, name) > 0)
274 new_name = malloc(sizeof(struct name_entry));
275 if (new_name == NULL) {
280 strlcpy(new_name->name, name, sizeof(new_name->name));
281 list_add_tail(&new_name->node, &loop_name->node);
286 /* calls function for every file found in specified directory */
287 int add_matching_files(struct list_head *name_list, const char *dirname, const char *suffix)
292 char filename[PATH_SIZE];
294 dbg("open directory '%s'", dirname);
295 dir = opendir(dirname);
297 dbg("unable to open '%s'", dirname);
303 if (ent == NULL || ent->d_name[0] == '\0')
306 if ((ent->d_name[0] == '.') || (ent->d_name[0] == COMMENT_CHARACTER))
309 /* look for file matching with specified suffix */
310 ext = strrchr(ent->d_name, '.');
314 if (strcmp(ext, suffix) != 0)
317 dbg("put file '%s/%s' in list", dirname, ent->d_name);
319 snprintf(filename, sizeof(filename), "%s/%s", dirname, ent->d_name);
320 filename[sizeof(filename)-1] = '\0';
321 name_list_add(name_list, filename, 1);
328 int execute_command(const char *command, const char *subsystem)
333 char *argv[(PATH_SIZE / 2) + 1];
338 strlcpy(arg, command, sizeof(arg));
340 if (strchr(arg, ' ')) {
342 while (pos != NULL) {
343 if (pos[0] == '\'') {
344 /* don't separate if in apostrophes */
346 argv[i] = strsep(&pos, "\'");
347 while (pos && pos[0] == ' ')
350 argv[i] = strsep(&pos, " ");
352 dbg("arg[%i] '%s'", i, argv[i]);
356 dbg("execute '%s' with parsed arguments", arg);
359 argv[1] = (char *) subsystem;
361 dbg("execute '%s' with subsystem '%s' argument", arg, argv[1]);
368 devnull = open("/dev/null", O_RDWR);
370 dup2(devnull, STDIN_FILENO);
371 dup2(devnull, STDOUT_FILENO);
372 dup2(devnull, STDERR_FILENO);
375 retval = execv(arg, argv);
376 err("exec of child failed");
379 dbg("fork of child failed");
383 waitpid(pid, NULL, 0);