X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=libudev%2Flibudev-util-private.c;h=fe8f29b3a89007e20b595a73693644e4b18484ca;hb=4c21b7156d951c99fe8d57a36bf3b006cc3d3a73;hp=f22c04184bce280557537fcff8af673a7337023d;hpb=44b49d3736bfdc94da31b5670a2c5a3477a201eb;p=elogind.git diff --git a/libudev/libudev-util-private.c b/libudev/libudev-util-private.c index f22c04184..fe8f29b3a 100644 --- a/libudev/libudev-util-private.c +++ b/libudev/libudev-util-private.c @@ -1,7 +1,7 @@ /* * libudev - interface to udev device information * - * Copyright (C) 2004-2009 Kay Sievers + * Copyright (C) 2003-2009 Kay Sievers * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -20,6 +20,7 @@ #include #include #include +#include #include "libudev.h" #include "libudev-private.h" @@ -29,15 +30,16 @@ int util_create_path(struct udev *udev, const char *path) char p[UTIL_PATH_SIZE]; char *pos; struct stat stats; - int ret; + int err; util_strscpy(p, sizeof(p), path); pos = strrchr(p, '/'); - if (pos == p || pos == NULL) + if (pos == NULL) return 0; - - while (pos[-1] == '/') + while (pos != p && pos[-1] == '/') pos--; + if (pos == p) + return 0; pos[0] = '\0'; dbg(udev, "stat '%s'\n", p); @@ -49,15 +51,12 @@ int util_create_path(struct udev *udev, const char *path) dbg(udev, "mkdir '%s'\n", p); udev_selinux_setfscreatecon(udev, p, S_IFDIR|0755); - ret = mkdir(p, 0755); - udev_selinux_resetfscreatecon(udev); - if (ret == 0) - return 0; - - if (errno == EEXIST) + err = mkdir(p, 0755); + if (err != 0 && errno == EEXIST) if (stat(p, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR) - return 0; - return -1; + err = 0; + udev_selinux_resetfscreatecon(udev); + return err; } int util_delete_path(struct udev *udev, const char *path) @@ -66,7 +65,10 @@ int util_delete_path(struct udev *udev, const char *path) char *pos; int retval; - strcpy (p, path); + if (path[0] == '/') + while(path[1] == '/') + path++; + util_strscpy(p, sizeof(p), path); pos = strrchr(p, '/'); if (pos == p || pos == NULL) return 0; @@ -95,28 +97,20 @@ int util_delete_path(struct udev *udev, const char *path) } /* Reset permissions on the device node, before unlinking it to make sure, - * that permisions of possible hard links will be removed too. + * that permissions of possible hard links will be removed too. */ int util_unlink_secure(struct udev *udev, const char *filename) { - int retval; - - retval = chown(filename, 0, 0); - if (retval) - err(udev, "chown(%s, 0, 0) failed: %m\n", filename); - - retval = chmod(filename, 0000); - if (retval) - err(udev, "chmod(%s, 0000) failed: %m\n", filename); + int err; - retval = unlink(filename); + chmod(filename, 0000); + chown(filename, 0, 0); + err = unlink(filename); if (errno == ENOENT) - retval = 0; - - if (retval) + err = 0; + if (err) err(udev, "unlink(%s) failed: %m\n", filename); - - return retval; + return err; } uid_t util_lookup_user(struct udev *udev, const char *user) @@ -134,8 +128,7 @@ uid_t util_lookup_user(struct udev *udev, const char *user) if (endptr[0] == '\0') return uid; - errno = 0; - getpwnam_r(user, &pwbuf, buf, buflen, &pw); + errno = getpwnam_r(user, &pwbuf, buf, buflen, &pw); if (pw != NULL) return pw->pw_uid; if (errno == 0 || errno == ENOENT || errno == ESRCH) @@ -149,7 +142,7 @@ gid_t util_lookup_group(struct udev *udev, const char *group) { char *endptr; int buflen = sysconf(_SC_GETGR_R_SIZE_MAX); - char buf[buflen]; + char *buf; struct group grbuf; struct group *gr; gid_t gid = 0; @@ -159,16 +152,30 @@ gid_t util_lookup_group(struct udev *udev, const char *group) gid = strtoul(group, &endptr, 10); if (endptr[0] == '\0') return gid; + buf = NULL; + gid = 0; + for (;;) { + char *newbuf; - errno = 0; - getgrnam_r(group, &grbuf, buf, buflen, &gr); - if (gr != NULL) - return gr->gr_gid; - if (errno == 0 || errno == ENOENT || errno == ESRCH) - err(udev, "specified group '%s' unknown\n", group); - else - err(udev, "error resolving group '%s': %m\n", group); - return 0; + newbuf = realloc(buf, buflen); + if (!newbuf) + break; + buf = newbuf; + errno = getgrnam_r(group, &grbuf, buf, buflen, &gr); + if (gr != NULL) { + gid = gr->gr_gid; + } else if (errno == ERANGE) { + buflen *= 2; + continue; + } else if (errno == 0 || errno == ENOENT || errno == ESRCH) { + err(udev, "specified group '%s' unknown\n", group); + } else { + err(udev, "error resolving group '%s': %m\n", group); + } + break; + } + free(buf); + return gid; } /* handle "[/]" format */ @@ -248,6 +255,8 @@ int util_run_program(struct udev *udev, const char *command, char **envp, int i; int err = 0; + info(udev, "'%s' started\n", command); + /* build argv from command */ util_strscpy(arg, sizeof(arg), command); i = 0; @@ -272,7 +281,6 @@ int util_run_program(struct udev *udev, const char *command, char **envp, argv[0] = arg; argv[1] = NULL; } - info(udev, "'%s'\n", command); /* prepare pipes from child to parent */ if (result != NULL || udev_get_log_priority(udev) >= LOG_INFO) { @@ -290,7 +298,7 @@ int util_run_program(struct udev *udev, const char *command, char **envp, /* allow programs in /lib/udev/ to be called without the path */ if (argv[0][0] != '/') { - util_strscpyl(program, sizeof(program), UDEV_PREFIX "/lib/udev/", argv[0], NULL); + util_strscpyl(program, sizeof(program), LIBEXECDIR "/", argv[0], NULL); argv[0] = program; } @@ -324,7 +332,7 @@ int util_run_program(struct udev *udev, const char *command, char **envp, } execve(argv[0], argv, envp); if (errno == ENOENT || errno == ENOTDIR) { - /* may be on a filesytem which is not mounted right now */ + /* may be on a filesystem which is not mounted right now */ info(udev, "program '%s' not found\n", argv[0]); } else { /* other problems */ @@ -356,7 +364,7 @@ int util_run_program(struct udev *udev, const char *command, char **envp, FD_SET(outpipe[READ_END], &readfds); if (errpipe[READ_END] > 0) FD_SET(errpipe[READ_END], &readfds); - fdcount = select(UDEV_MAX(outpipe[READ_END], errpipe[READ_END])+1, &readfds, NULL, NULL, NULL); + fdcount = select(MAX(outpipe[READ_END], errpipe[READ_END])+1, &readfds, NULL, NULL, NULL); if (fdcount < 0) { if (errno == EINTR) continue; @@ -434,11 +442,11 @@ int util_run_program(struct udev *udev, const char *command, char **envp, } waitpid(pid, &status, 0); if (WIFEXITED(status)) { - info(udev, "'%s' returned with status %i\n", argv[0], WEXITSTATUS(status)); + info(udev, "'%s' returned with exitcode %i\n", command, WEXITSTATUS(status)); if (WEXITSTATUS(status) != 0) err = -1; } else { - err(udev, "'%s' abnormal exit\n", command); + err(udev, "'%s' unexpected exit with status 0x%04x\n", command, status); err = -1; } }