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.
31 #include <sys/socket.h>
36 #include <sys/utsname.h>
38 #include "udev_libc_wrapper.h"
41 #include "udev_utils.h"
44 /* compare string with pattern (supports * ? [0-9] [!A-Z]) */
45 int strcmp_pattern(const char *p, const char *s)
50 return (p[0] != '\0');
61 while ((p[0] != '\0') && (p[0] != ']')) {
64 if ((s[0] >= p[0]) && (s[0] <= p[2]))
68 match = (p[0] == s[0]);
72 while ((p[0] != '\0') && (p[0] != ']'))
75 return strcmp_pattern(p+1, s+1);
81 if (strcmp_pattern(p, s+1))
82 return strcmp_pattern(p+1, s);
90 if ((p[0] == s[0]) || (p[0] == '?'))
91 return strcmp_pattern(p+1, s+1);
97 int string_is_true(const char *str)
99 if (strcasecmp(str, "true") == 0)
101 if (strcasecmp(str, "yes") == 0)
103 if (strcasecmp(str, "1") == 0)
108 int log_priority(const char *priority)
113 prio = strtol(priority, &endptr, 10);
114 if (endptr[0] == '\0')
116 if (strncasecmp(priority, "err", 3) == 0)
118 if (strcasecmp(priority, "info") == 0)
120 if (strcasecmp(priority, "debug") == 0)
122 if (string_is_true(priority))
128 int kernel_release_satisfactory(unsigned int version, unsigned int patchlevel, unsigned int sublevel)
130 static unsigned int kversion = 0;
131 static unsigned int kpatchlevel;
132 static unsigned int ksublevel;
136 if (uname(&uts) != 0)
139 if (sscanf (uts.release, "%u.%u.%u", &kversion, &kpatchlevel, &ksublevel) != 3) {
145 if (kversion >= version && kpatchlevel >= patchlevel && ksublevel >= sublevel)
151 int create_path(const char *path)
158 pos = strrchr(p, '/');
159 if (pos == p || pos == NULL)
162 while (pos[-1] == '/')
167 dbg("stat '%s'\n", p);
168 if (stat (p, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR)
171 if (create_path (p) != 0)
174 dbg("mkdir '%s'\n", p);
175 return mkdir(p, 0755);
178 /* Reset permissions on the device node, before unlinking it to make sure,
179 * that permisions of possible hard links will be removed too.
181 int unlink_secure(const char *filename)
185 retval = chown(filename, 0, 0);
187 dbg("chown(%s, 0, 0) failed with error '%s'", filename, strerror(errno));
189 retval = chmod(filename, 0000);
191 dbg("chmod(%s, 0000) failed with error '%s'", filename, strerror(errno));
193 retval = unlink(filename);
198 dbg("unlink(%s) failed with error '%s'", filename, strerror(errno));
203 int file_map(const char *filename, char **buf, size_t *bufsize)
208 fd = open(filename, O_RDONLY);
213 if (fstat(fd, &stats) < 0) {
218 *buf = mmap(NULL, stats.st_size, PROT_READ, MAP_SHARED, fd, 0);
219 if (*buf == MAP_FAILED) {
223 *bufsize = stats.st_size;
230 void file_unmap(void *buf, size_t bufsize)
232 munmap(buf, bufsize);
235 /* return number of chars until the next newline, skip escaped newline */
236 size_t buf_get_line(const char *buf, size_t buflen, size_t cur)
241 for (count = cur; count < buflen; count++) {
242 if (!escape && buf[count] == '\n')
245 if (buf[count] == '\\')
254 void replace_untrusted_chars(char *string)
258 for (len = 0; string[len] != '\0'; len++) {
259 if (strchr(";,~\\()\'", string[len])) {
260 info("replace '%c' in '%s'", string[len], string);
266 void remove_trailing_char(char *path, char c)
271 while (len > 0 && path[len-1] == c)
275 int name_list_add(struct list_head *name_list, const char *name, int sort)
277 struct name_entry *loop_name;
278 struct name_entry *new_name;
280 list_for_each_entry(loop_name, name_list, node) {
282 if (strcmp(loop_name->name, name) == 0) {
283 dbg("'%s' is already in the list", name);
289 list_for_each_entry(loop_name, name_list, node) {
290 if (sort && strcmp(loop_name->name, name) > 0)
294 new_name = malloc(sizeof(struct name_entry));
295 if (new_name == NULL) {
300 strlcpy(new_name->name, name, sizeof(new_name->name));
301 dbg("adding '%s'", new_name->name);
302 list_add_tail(&new_name->node, &loop_name->node);
307 int name_list_key_add(struct list_head *name_list, const char *key, const char *value)
309 struct name_entry *loop_name;
310 struct name_entry *new_name;
312 list_for_each_entry(loop_name, name_list, node) {
313 if (strncmp(loop_name->name, key, strlen(key)) == 0) {
314 dbg("key already present '%s', replace it", loop_name->name);
315 snprintf(loop_name->name, sizeof(loop_name->name), "%s=%s", key, value);
316 loop_name->name[sizeof(loop_name->name)-1] = '\0';
321 new_name = malloc(sizeof(struct name_entry));
322 if (new_name == NULL) {
327 snprintf(new_name->name, sizeof(new_name->name), "%s=%s", key, value);
328 new_name->name[sizeof(new_name->name)-1] = '\0';
329 dbg("adding '%s'", new_name->name);
330 list_add_tail(&new_name->node, &loop_name->node);
335 /* calls function for every file found in specified directory */
336 int add_matching_files(struct list_head *name_list, const char *dirname, const char *suffix)
341 char filename[PATH_SIZE];
343 dbg("open directory '%s'", dirname);
344 dir = opendir(dirname);
346 dbg("unable to open '%s'", dirname);
352 if (ent == NULL || ent->d_name[0] == '\0')
355 if ((ent->d_name[0] == '.') || (ent->d_name[0] == COMMENT_CHARACTER))
358 /* look for file matching with specified suffix */
359 ext = strrchr(ent->d_name, '.');
363 if (strcmp(ext, suffix) != 0)
366 dbg("put file '%s/%s' in list", dirname, ent->d_name);
368 snprintf(filename, sizeof(filename), "%s/%s", dirname, ent->d_name);
369 filename[sizeof(filename)-1] = '\0';
370 name_list_add(name_list, filename, 1);
377 int pass_env_to_socket(const char *sockname, const char *devpath, const char *action)
380 struct sockaddr_un saddr;
387 dbg("pass environment to socket '%s'", sockname);
388 sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
389 memset(&saddr, 0x00, sizeof(struct sockaddr_un));
390 saddr.sun_family = AF_LOCAL;
391 /* only abstract namespace is supported */
392 strcpy(&saddr.sun_path[1], sockname);
393 addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
395 bufpos = snprintf(buf, sizeof(buf)-1, "%s@%s", action, devpath);
397 for (i = 0; environ[i] != NULL && bufpos < sizeof(buf); i++) {
398 bufpos += strlcpy(&buf[bufpos], environ[i], sizeof(buf) - bufpos-1);
402 retval = sendto(sock, &buf, bufpos, 0, (struct sockaddr *)&saddr, addrlen);
410 int execute_program(const char *command, const char *subsystem,
411 char *result, size_t ressize, size_t *reslen)
420 char *argv[(sizeof(arg) / 2) + 1];
425 strlcpy(arg, command, sizeof(arg));
427 if (strchr(arg, ' ')) {
429 while (pos != NULL) {
430 if (pos[0] == '\'') {
431 /* don't separate if in apostrophes */
433 argv[i] = strsep(&pos, "\'");
434 while (pos && pos[0] == ' ')
437 argv[i] = strsep(&pos, " ");
439 dbg("arg[%i] '%s'", i, argv[i]);
443 dbg("execute '%s' with parsed arguments", arg);
446 argv[1] = (char *) subsystem;
448 dbg("execute '%s' with subsystem '%s' argument", arg, argv[1]);
452 if (pipe(pipefds) != 0) {
461 /* child dup2 write side of pipe to STDOUT */
462 devnull = open("/dev/null", O_RDWR);
464 dup2(devnull, STDIN_FILENO);
466 dup2(devnull, STDOUT_FILENO);
467 dup2(devnull, STDERR_FILENO);
471 dup2(pipefds[1], STDOUT_FILENO);
473 err("exec of program failed");
476 err("fork of '%s' failed", arg);
479 /* parent reads from pipefds[0] */
484 count = read(pipefds[0], result + len, ressize - len-1);
486 err("read failed with '%s'", strerror(errno));
495 if (len >= ressize-1) {
496 err("ressize %ld too short", (long)ressize);
506 waitpid(pid, &status, 0);
508 if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
509 dbg("exec program status 0x%x", status);