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.
34 #include <sys/utsname.h>
36 #include "udev_libc_wrapper.h"
39 #include "udev_utils.h"
43 int udev_init_device(struct udevice *udev, const char* devpath, const char *subsystem, const char *action)
47 memset(udev, 0x00, sizeof(struct udevice));
48 INIT_LIST_HEAD(&udev->symlink_list);
49 INIT_LIST_HEAD(&udev->run_list);
52 strlcpy(udev->subsystem, subsystem, sizeof(udev->subsystem));
55 strlcpy(udev->action, action, sizeof(udev->action));
58 strlcpy(udev->devpath, devpath, sizeof(udev->devpath));
59 remove_trailing_char(udev->devpath, '/');
61 if (strncmp(udev->devpath, "/block/", 7) == 0)
62 udev->type = DEV_BLOCK;
63 else if (strncmp(udev->devpath, "/class/net/", 11) == 0)
65 else if (strncmp(udev->devpath, "/class/", 7) == 0)
66 udev->type = DEV_CLASS;
67 else if (strncmp(udev->devpath, "/devices/", 9) == 0)
68 udev->type = DEV_DEVICE;
71 pos = strrchr(udev->devpath, '/');
73 strlcpy(udev->kernel_name, &pos[1], sizeof(udev->kernel_name));
74 dbg("kernel_name='%s'", udev->kernel_name);
76 /* Some block devices have '!' in their name, change that to '/' */
77 pos = udev->kernel_name;
78 while (pos[0] != '\0') {
84 /* get kernel number */
85 pos = &udev->kernel_name[strlen(udev->kernel_name)];
86 while (isdigit(pos[-1]))
88 strlcpy(udev->kernel_number, pos, sizeof(udev->kernel_number));
89 dbg("kernel_number='%s'", udev->kernel_number);
93 if (udev->type == DEV_BLOCK || udev->type == DEV_CLASS) {
95 strcpy(udev->owner, "root");
96 strcpy(udev->group, "root");
102 void udev_cleanup_device(struct udevice *udev)
104 struct name_entry *name_loop;
105 struct name_entry *temp_loop;
107 list_for_each_entry_safe(name_loop, temp_loop, &udev->symlink_list, node) {
108 list_del(&name_loop->node);
111 list_for_each_entry_safe(name_loop, temp_loop, &udev->run_list, node) {
112 list_del(&name_loop->node);
117 int string_is_true(const char *str)
119 if (strcasecmp(str, "true") == 0)
121 if (strcasecmp(str, "yes") == 0)
123 if (strcasecmp(str, "1") == 0)
128 int log_priority(const char *priority)
133 prio = strtol(priority, &endptr, 10);
134 if (endptr[0] == '\0')
136 if (strncasecmp(priority, "err", 3) == 0)
138 if (strcasecmp(priority, "info") == 0)
140 if (strcasecmp(priority, "debug") == 0)
142 if (string_is_true(priority))
148 int kernel_release_satisfactory(unsigned int version, unsigned int patchlevel, unsigned int sublevel)
150 static unsigned int kversion = 0;
151 static unsigned int kpatchlevel;
152 static unsigned int ksublevel;
156 if (uname(&uts) != 0)
159 if (sscanf (uts.release, "%u.%u.%u", &kversion, &kpatchlevel, &ksublevel) != 3) {
165 if (kversion >= version && kpatchlevel >= patchlevel && ksublevel >= sublevel)
171 int create_path(const char *path)
178 pos = strrchr(p, '/');
179 if (pos == p || pos == NULL)
182 while (pos[-1] == '/')
187 dbg("stat '%s'\n", p);
188 if (stat (p, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR)
191 if (create_path (p) != 0)
194 dbg("mkdir '%s'\n", p);
195 return mkdir(p, 0755);
198 /* Reset permissions on the device node, before unlinking it to make sure,
199 * that permisions of possible hard links will be removed too.
201 int unlink_secure(const char *filename)
205 retval = chown(filename, 0, 0);
207 dbg("chown(%s, 0, 0) failed with error '%s'", filename, strerror(errno));
209 retval = chmod(filename, 0000);
211 dbg("chmod(%s, 0000) failed with error '%s'", filename, strerror(errno));
213 retval = unlink(filename);
218 dbg("unlink(%s) failed with error '%s'", filename, strerror(errno));
223 int file_map(const char *filename, char **buf, size_t *bufsize)
228 fd = open(filename, O_RDONLY);
233 if (fstat(fd, &stats) < 0) {
238 *buf = mmap(NULL, stats.st_size, PROT_READ, MAP_SHARED, fd, 0);
239 if (*buf == MAP_FAILED) {
243 *bufsize = stats.st_size;
250 void file_unmap(char *buf, size_t bufsize)
252 munmap(buf, bufsize);
255 /* return number of chars until the next newline, skip escaped newline */
256 size_t buf_get_line(const char *buf, size_t buflen, size_t cur)
261 for (count = cur; count < buflen; count++) {
262 if (!escape && buf[count] == '\n')
265 if (buf[count] == '\\')
274 void replace_untrusted_chars(char *string)
278 for (len = 0; string[len] != '\0'; len++) {
279 if (strchr(";,~\\()\'", string[len])) {
280 info("replace '%c' in '%s'", string[len], string);
286 void remove_trailing_char(char *path, char c)
291 while (len > 0 && path[len-1] == c)
295 int name_list_add(struct list_head *name_list, const char *name, int sort)
297 struct name_entry *loop_name;
298 struct name_entry *new_name;
300 list_for_each_entry(loop_name, name_list, node) {
302 if (strcmp(loop_name->name, name) == 0) {
303 dbg("'%s' is already in the list", name);
306 if (sort && strcmp(loop_name->name, name) > 0)
310 new_name = malloc(sizeof(struct name_entry));
311 if (new_name == NULL) {
316 strlcpy(new_name->name, name, sizeof(new_name->name));
317 list_add_tail(&new_name->node, &loop_name->node);
322 /* calls function for every file found in specified directory */
323 int add_matching_files(struct list_head *name_list, const char *dirname, const char *suffix)
328 char filename[PATH_SIZE];
330 dbg("open directory '%s'", dirname);
331 dir = opendir(dirname);
333 dbg("unable to open '%s'", dirname);
339 if (ent == NULL || ent->d_name[0] == '\0')
342 if ((ent->d_name[0] == '.') || (ent->d_name[0] == COMMENT_CHARACTER))
345 /* look for file matching with specified suffix */
346 ext = strrchr(ent->d_name, '.');
350 if (strcmp(ext, suffix) != 0)
353 dbg("put file '%s/%s' in list", dirname, ent->d_name);
355 snprintf(filename, sizeof(filename), "%s/%s", dirname, ent->d_name);
356 filename[sizeof(filename)-1] = '\0';
357 name_list_add(name_list, filename, 1);
364 int execute_command(const char *command, const char *subsystem)
369 char *argv[(PATH_SIZE / 2) + 1];
374 strlcpy(arg, command, sizeof(arg));
376 if (strchr(arg, ' ')) {
378 while (pos != NULL) {
379 if (pos[0] == '\'') {
380 /* don't separate if in apostrophes */
382 argv[i] = strsep(&pos, "\'");
383 while (pos && pos[0] == ' ')
386 argv[i] = strsep(&pos, " ");
388 dbg("arg[%i] '%s'", i, argv[i]);
392 dbg("execute '%s' with parsed arguments", arg);
395 argv[1] = (char *) subsystem;
397 dbg("execute '%s' with subsystem '%s' argument", arg, argv[1]);
404 devnull = open("/dev/null", O_RDWR);
406 dup2(devnull, STDIN_FILENO);
407 dup2(devnull, STDOUT_FILENO);
408 dup2(devnull, STDERR_FILENO);
411 retval = execv(arg, argv);
412 err("exec of child failed");
415 dbg("fork of child failed");
419 waitpid(pid, NULL, 0);