chiark / gitweb /
Merge keymap building in the top-level Makefile.am.
[elogind.git] / libudev / libudev-util-private.c
index f22c041..00d1c95 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * libudev - interface to udev device information
  *
- * Copyright (C) 2004-2009 Kay Sievers <kay.sievers@vrfy.org>
+ * Copyright (C) 2003-2009 Kay Sievers <kay.sievers@vrfy.org>
  *
  * 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 <pwd.h>
 #include <grp.h>
 #include <sys/wait.h>
+#include <sys/param.h>
 
 #include "libudev.h"
 #include "libudev-private.h"
@@ -148,8 +149,8 @@ uid_t util_lookup_user(struct udev *udev, const char *user)
 gid_t util_lookup_group(struct udev *udev, const char *group)
 {
        char *endptr;
-       int buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
-       char buf[buflen];
+       int buflen;
+       char *buf;
        struct group grbuf;
        struct group *gr;
        gid_t gid = 0;
@@ -160,15 +161,31 @@ gid_t util_lookup_group(struct udev *udev, const char *group)
        if (endptr[0] == '\0')
                return gid;
 
-       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;
+       buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
+       if (buflen < 0)
+               buflen = 1000;
+       buf = NULL;
+       gid = 0;
+       for (;;) {
+               buf = realloc(buf, buflen);
+               if (!buf)
+                       break;
+               errno = 0;
+               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 "[<SUBSYSTEM>/<KERNEL>]<attribute>" format */
@@ -248,6 +265,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 +291,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 +308,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;
        }
 
@@ -356,7 +374,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 +452,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;
                }
        }