2 * udev_utils.c - generic stuff used by udev
4 * Copyright (C) 2004-2005 Kay Sievers <kay.sievers@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"
42 /* compare string with pattern (supports * ? [0-9] [!A-Z]) */
43 int strcmp_pattern(const char *p, const char *s)
48 return (p[0] != '\0');
59 while ((p[0] != '\0') && (p[0] != ']')) {
62 if ((s[0] >= p[0]) && (s[0] <= p[2]))
66 match = (p[0] == s[0]);
70 while ((p[0] != '\0') && (p[0] != ']'))
73 return strcmp_pattern(p+1, s+1);
79 if (strcmp_pattern(p, s+1))
80 return strcmp_pattern(p+1, s);
88 if ((p[0] == s[0]) || (p[0] == '?'))
89 return strcmp_pattern(p+1, s+1);
95 int string_is_true(const char *str)
97 if (strcasecmp(str, "true") == 0)
99 if (strcasecmp(str, "yes") == 0)
101 if (strcasecmp(str, "1") == 0)
106 int log_priority(const char *priority)
111 prio = strtol(priority, &endptr, 10);
112 if (endptr[0] == '\0')
114 if (strncasecmp(priority, "err", 3) == 0)
116 if (strcasecmp(priority, "info") == 0)
118 if (strcasecmp(priority, "debug") == 0)
120 if (string_is_true(priority))
126 int kernel_release_satisfactory(unsigned int version, unsigned int patchlevel, unsigned int sublevel)
128 static unsigned int kversion = 0;
129 static unsigned int kpatchlevel;
130 static unsigned int ksublevel;
134 if (uname(&uts) != 0)
137 if (sscanf (uts.release, "%u.%u.%u", &kversion, &kpatchlevel, &ksublevel) != 3) {
143 if (kversion >= version && kpatchlevel >= patchlevel && ksublevel >= sublevel)
149 int create_path(const char *path)
156 pos = strrchr(p, '/');
157 if (pos == p || pos == NULL)
160 while (pos[-1] == '/')
165 dbg("stat '%s'\n", p);
166 if (stat (p, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR)
169 if (create_path (p) != 0)
172 dbg("mkdir '%s'\n", p);
173 return mkdir(p, 0755);
176 /* Reset permissions on the device node, before unlinking it to make sure,
177 * that permisions of possible hard links will be removed too.
179 int unlink_secure(const char *filename)
183 retval = chown(filename, 0, 0);
185 dbg("chown(%s, 0, 0) failed with error '%s'", filename, strerror(errno));
187 retval = chmod(filename, 0000);
189 dbg("chmod(%s, 0000) failed with error '%s'", filename, strerror(errno));
191 retval = unlink(filename);
196 dbg("unlink(%s) failed with error '%s'", filename, strerror(errno));
201 int file_map(const char *filename, char **buf, size_t *bufsize)
206 fd = open(filename, O_RDONLY);
211 if (fstat(fd, &stats) < 0) {
216 *buf = mmap(NULL, stats.st_size, PROT_READ, MAP_SHARED, fd, 0);
217 if (*buf == MAP_FAILED) {
221 *bufsize = stats.st_size;
228 void file_unmap(void *buf, size_t bufsize)
230 munmap(buf, bufsize);
233 /* return number of chars until the next newline, skip escaped newline */
234 size_t buf_get_line(const char *buf, size_t buflen, size_t cur)
239 for (count = cur; count < buflen; count++) {
240 if (!escape && buf[count] == '\n')
243 if (buf[count] == '\\')
252 void replace_untrusted_chars(char *string)
256 for (len = 0; string[len] != '\0'; len++) {
257 if (strchr(";,~\\()\'", string[len])) {
258 info("replace '%c' in '%s'", string[len], string);
264 void remove_trailing_char(char *path, char c)
269 while (len > 0 && path[len-1] == c)
273 int name_list_add(struct list_head *name_list, const char *name, int sort)
275 struct name_entry *loop_name;
276 struct name_entry *new_name;
278 list_for_each_entry(loop_name, name_list, node) {
280 if (strcmp(loop_name->name, name) == 0) {
281 dbg("'%s' is already in the list", name);
287 list_for_each_entry(loop_name, name_list, node) {
288 if (sort && strcmp(loop_name->name, name) > 0)
292 new_name = malloc(sizeof(struct name_entry));
293 if (new_name == NULL) {
298 strlcpy(new_name->name, name, sizeof(new_name->name));
299 dbg("adding '%s'", new_name->name);
300 list_add_tail(&new_name->node, &loop_name->node);
305 int name_list_key_add(struct list_head *name_list, const char *key, const char *value)
307 struct name_entry *loop_name;
308 struct name_entry *new_name;
310 list_for_each_entry(loop_name, name_list, node) {
311 if (strncmp(loop_name->name, key, strlen(key)) == 0) {
312 dbg("key already present '%s', replace it", loop_name->name);
313 snprintf(loop_name->name, sizeof(loop_name->name), "%s=%s", key, value);
314 loop_name->name[sizeof(loop_name->name)-1] = '\0';
319 new_name = malloc(sizeof(struct name_entry));
320 if (new_name == NULL) {
325 snprintf(new_name->name, sizeof(new_name->name), "%s=%s", key, value);
326 new_name->name[sizeof(new_name->name)-1] = '\0';
327 dbg("adding '%s'", new_name->name);
328 list_add_tail(&new_name->node, &loop_name->node);
333 /* calls function for every file found in specified directory */
334 int add_matching_files(struct list_head *name_list, const char *dirname, const char *suffix)
339 char filename[PATH_SIZE];
341 dbg("open directory '%s'", dirname);
342 dir = opendir(dirname);
344 dbg("unable to open '%s'", dirname);
350 if (ent == NULL || ent->d_name[0] == '\0')
353 if ((ent->d_name[0] == '.') || (ent->d_name[0] == COMMENT_CHARACTER))
356 /* look for file matching with specified suffix */
357 ext = strrchr(ent->d_name, '.');
361 if (strcmp(ext, suffix) != 0)
364 dbg("put file '%s/%s' in list", dirname, ent->d_name);
366 snprintf(filename, sizeof(filename), "%s/%s", dirname, ent->d_name);
367 filename[sizeof(filename)-1] = '\0';
368 name_list_add(name_list, filename, 1);
375 int execute_program(const char *command, const char *subsystem,
376 char *result, size_t ressize, size_t *reslen)
385 char *argv[(sizeof(arg) / 2) + 1];
390 strlcpy(arg, command, sizeof(arg));
392 if (strchr(arg, ' ')) {
394 while (pos != NULL) {
395 if (pos[0] == '\'') {
396 /* don't separate if in apostrophes */
398 argv[i] = strsep(&pos, "\'");
399 while (pos && pos[0] == ' ')
402 argv[i] = strsep(&pos, " ");
404 dbg("arg[%i] '%s'", i, argv[i]);
408 dbg("execute '%s' with parsed arguments", arg);
411 argv[1] = (char *) subsystem;
413 dbg("execute '%s' with subsystem '%s' argument", arg, argv[1]);
417 if (pipe(pipefds) != 0) {
426 /* child dup2 write side of pipe to STDOUT */
427 devnull = open("/dev/null", O_RDWR);
429 dup2(devnull, STDIN_FILENO);
431 dup2(devnull, STDOUT_FILENO);
432 dup2(devnull, STDERR_FILENO);
436 dup2(pipefds[1], STDOUT_FILENO);
438 err("exec of program failed");
441 err("fork of '%s' failed", arg);
444 /* parent reads from pipefds[0] */
449 count = read(pipefds[0], result + len, ressize - len-1);
451 err("read failed with '%s'", strerror(errno));
460 if (len >= ressize-1) {
461 err("ressize %ld too short", (long)ressize);
471 waitpid(pid, &status, 0);
473 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
474 dbg("exec program status 0x%x", status);