From c5bd72509ecae1c12bb523fa56432fd71428fbf1 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 20 Aug 2009 00:20:03 +0200 Subject: core: check return value of getgrnam_r() instead of errno According to POSIX getgrnam_r() returns the error code as return value, and not in errno. Honour that. Pointed out and inspired by a patch from Ted Percival. --- src/pulsecore/core-util.c | 74 ++++++++++++++++++++++------------------------- 1 file changed, 34 insertions(+), 40 deletions(-) (limited to 'src/pulsecore/core-util.c') diff --git a/src/pulsecore/core-util.c b/src/pulsecore/core-util.c index 6494244e..ef8c8472 100644 --- a/src/pulsecore/core-util.c +++ b/src/pulsecore/core-util.c @@ -975,6 +975,7 @@ static int is_group(gid_t gid, const char *name) { int r = -1; #ifdef HAVE_GETGRGID_R + #ifdef _SC_GETGR_R_SIZE_MAX n = sysconf(_SC_GETGR_R_SIZE_MAX); #else @@ -985,38 +986,25 @@ static int is_group(gid_t gid, const char *name) { data = pa_xmalloc((size_t) n); + if ((errno = getgrgid_r(gid, &group, data, (size_t) n, &result)) || !result) +#else errno = 0; - if (getgrgid_r(gid, &group, data, (size_t) n, &result) < 0 || !result) { - pa_log("getgrgid_r(%u): %s", (unsigned) gid, pa_cstrerror(errno)); - + if (!(result = getgrgid(gid))) +#endif + { if (!errno) errno = ENOENT; - goto finish; - } - - r = strcmp(name, result->gr_name) == 0; - -finish: - pa_xfree(data); -#else - /* XXX Not thread-safe, but needed on OSes (e.g. FreeBSD 4.X) that do not - * support getgrgid_r. */ - - errno = 0; - if (!(result = getgrgid(gid))) { pa_log("getgrgid(%u): %s", gid, pa_cstrerror(errno)); - if (!errno) - errno = ENOENT; - goto finish; } r = strcmp(name, result->gr_name) == 0; finish: -#endif + + pa_xfree(data); return r; } @@ -1065,12 +1053,14 @@ finish: /* Check whether the specifc user id is a member of the specified group */ int pa_uid_in_group(uid_t uid, const char *name) { - char *g_buf, *p_buf; + char *g_buf = NULL, *p_buf = NULL; long g_n, p_n; - struct group grbuf, *gr; + struct group grbuf, *gr = NULL; char **i; int r = -1; +#ifdef HAVE_GETGRNAM_R + #ifdef _SC_GETGR_R_SIZE_MAX g_n = sysconf(_SC_GETGR_R_SIZE_MAX); #else @@ -1081,6 +1071,19 @@ int pa_uid_in_group(uid_t uid, const char *name) { g_buf = pa_xmalloc((size_t) g_n); + if ((errno = getgrnam_r(name, &grbuf, g_buf, (size_t) g_n, &gr)) != 0 || !gr) +#else + errno = 0; + if (!(gr = getgrnam(name))) +#endif + { + if (!errno) + errno = ENOENT; + goto finish; + } + +#ifdef HAVE_GETPWNAM_R + #ifdef _SC_GETPW_R_SIZE_MAX p_n = sysconf(_SC_GETPW_R_SIZE_MAX); #else @@ -1090,26 +1093,16 @@ int pa_uid_in_group(uid_t uid, const char *name) { p_n = 512; p_buf = pa_xmalloc((size_t) p_n); - - errno = 0; -#ifdef HAVE_GETGRNAM_R - if (getgrnam_r(name, &grbuf, g_buf, (size_t) g_n, &gr) != 0 || !gr) -#else - if (!(gr = getgrnam(name))) #endif - { - if (!errno) - errno = ENOENT; - goto finish; - } r = 0; for (i = gr->gr_mem; *i; i++) { - struct passwd pwbuf, *pw; + struct passwd pwbuf, *pw = NULL; #ifdef HAVE_GETPWNAM_R - if (getpwnam_r(*i, &pwbuf, p_buf, (size_t) p_n, &pw) != 0 || !pw) + if ((errno = getpwnam_r(*i, &pwbuf, p_buf, (size_t) p_n, &pw)) != 0 || !pw) #else + errno = 0; if (!(pw = getpwnam(*i))) #endif continue; @@ -1130,9 +1123,11 @@ finish: /* Get the GID of a gfiven group, return (gid_t) -1 on failure. */ gid_t pa_get_gid_of_group(const char *name) { gid_t ret = (gid_t) -1; - char *g_buf; + char *g_buf = NULL; long g_n; - struct group grbuf, *gr; + struct group grbuf, *gr = NULL; + +#ifdef HAVE_GETGRNAM_R #ifdef _SC_GETGR_R_SIZE_MAX g_n = sysconf(_SC_GETGR_R_SIZE_MAX); @@ -1144,10 +1139,9 @@ gid_t pa_get_gid_of_group(const char *name) { g_buf = pa_xmalloc((size_t) g_n); - errno = 0; -#ifdef HAVE_GETGRNAM_R - if (getgrnam_r(name, &grbuf, g_buf, (size_t) g_n, &gr) != 0 || !gr) + if ((errno = getgrnam_r(name, &grbuf, g_buf, (size_t) g_n, &gr)) != 0 || !gr) #else + errno = 0; if (!(gr = getgrnam(name))) #endif { -- cgit From 8a2a6b2004cd299467de1955f7f99e25033faa63 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Fri, 21 Aug 2009 03:43:53 +0200 Subject: adjust various data/library paths automatically if we are run from a build tree --- src/pulsecore/core-util.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'src/pulsecore/core-util.c') diff --git a/src/pulsecore/core-util.c b/src/pulsecore/core-util.c index ef8c8472..843c8377 100644 --- a/src/pulsecore/core-util.c +++ b/src/pulsecore/core-util.c @@ -2862,3 +2862,22 @@ void pa_reset_personality(void) { #endif } + +#if defined(__linux__) && !defined(__OPTIMIZE__) + +pa_bool_t pa_run_from_build_tree(void) { + char *rp; + pa_bool_t b = FALSE; + + /* We abuse __OPTIMIZE__ as a check whether we are a debug build + * or not. */ + + if ((rp = pa_readlink("/proc/self/exe"))) { + b = pa_startswith(rp, PA_BUILDDIR); + pa_xfree(rp); + } + + return b; +} + +#endif -- cgit From 15eb03a5b39f8c54328caa7516a7870bf977db40 Mon Sep 17 00:00:00 2001 From: Ted Percival Date: Fri, 21 Aug 2009 16:02:57 -0600 Subject: core: Add thread-safe group info functions with dynamic buffers Provides getgrgid, getgrnam, getpwuid & getpwnam replacements that are thread safe (a la getgrgid_r() and friends) that internally handle allocating big-enough buffers to avoid ERANGE errors on large users or groups. --- src/pulsecore/core-util.c | 106 +++++++++------------------------------------- 1 file changed, 20 insertions(+), 86 deletions(-) (limited to 'src/pulsecore/core-util.c') diff --git a/src/pulsecore/core-util.c b/src/pulsecore/core-util.c index 843c8377..0eb32cc4 100644 --- a/src/pulsecore/core-util.c +++ b/src/pulsecore/core-util.c @@ -115,6 +115,7 @@ #include #include #include +#include #include "core-util.h" @@ -969,42 +970,24 @@ fail: /* Check whether the specified GID and the group name match */ static int is_group(gid_t gid, const char *name) { - struct group group, *result = NULL; - long n; - void *data; + struct group *group = NULL; int r = -1; -#ifdef HAVE_GETGRGID_R - -#ifdef _SC_GETGR_R_SIZE_MAX - n = sysconf(_SC_GETGR_R_SIZE_MAX); -#else - n = -1; -#endif - if (n <= 0) - n = 512; - - data = pa_xmalloc((size_t) n); - - if ((errno = getgrgid_r(gid, &group, data, (size_t) n, &result)) || !result) -#else errno = 0; - if (!(result = getgrgid(gid))) -#endif + if (!(group = pa_getgrgid_malloc(gid))) { if (!errno) errno = ENOENT; - pa_log("getgrgid(%u): %s", gid, pa_cstrerror(errno)); + pa_log("pa_getgrgid_malloc(%u): %s", gid, pa_cstrerror(errno)); goto finish; } - r = strcmp(name, result->gr_name) == 0; + r = strcmp(name, group->gr_name) == 0; finish: - - pa_xfree(data); + pa_getgrgid_free(group); return r; } @@ -1053,69 +1036,37 @@ finish: /* Check whether the specifc user id is a member of the specified group */ int pa_uid_in_group(uid_t uid, const char *name) { - char *g_buf = NULL, *p_buf = NULL; - long g_n, p_n; - struct group grbuf, *gr = NULL; + struct group *group = NULL; char **i; int r = -1; -#ifdef HAVE_GETGRNAM_R - -#ifdef _SC_GETGR_R_SIZE_MAX - g_n = sysconf(_SC_GETGR_R_SIZE_MAX); -#else - g_n = -1; -#endif - if (g_n <= 0) - g_n = 512; - - g_buf = pa_xmalloc((size_t) g_n); - - if ((errno = getgrnam_r(name, &grbuf, g_buf, (size_t) g_n, &gr)) != 0 || !gr) -#else errno = 0; - if (!(gr = getgrnam(name))) -#endif + if (!(group = pa_getgrnam_malloc(name))) { if (!errno) errno = ENOENT; goto finish; } -#ifdef HAVE_GETPWNAM_R - -#ifdef _SC_GETPW_R_SIZE_MAX - p_n = sysconf(_SC_GETPW_R_SIZE_MAX); -#else - p_n = -1; -#endif - if (p_n <= 0) - p_n = 512; - - p_buf = pa_xmalloc((size_t) p_n); -#endif - r = 0; - for (i = gr->gr_mem; *i; i++) { - struct passwd pwbuf, *pw = NULL; + for (i = group->gr_mem; *i; i++) { + struct passwd *pw = NULL; -#ifdef HAVE_GETPWNAM_R - if ((errno = getpwnam_r(*i, &pwbuf, p_buf, (size_t) p_n, &pw)) != 0 || !pw) -#else errno = 0; - if (!(pw = getpwnam(*i))) -#endif + if (!(pw = pa_getpwnam_malloc(*i))) continue; - if (pw->pw_uid == uid) { + if (pw->pw_uid == uid) r = 1; + + pa_getpwnam_free(pw); + + if (r == 1) break; - } } finish: - pa_xfree(g_buf); - pa_xfree(p_buf); + pa_getgrnam_free(group); return r; } @@ -1123,27 +1074,10 @@ finish: /* Get the GID of a gfiven group, return (gid_t) -1 on failure. */ gid_t pa_get_gid_of_group(const char *name) { gid_t ret = (gid_t) -1; - char *g_buf = NULL; - long g_n; - struct group grbuf, *gr = NULL; - -#ifdef HAVE_GETGRNAM_R - -#ifdef _SC_GETGR_R_SIZE_MAX - g_n = sysconf(_SC_GETGR_R_SIZE_MAX); -#else - g_n = -1; -#endif - if (g_n <= 0) - g_n = 512; - - g_buf = pa_xmalloc((size_t) g_n); + struct group *gr = NULL; - if ((errno = getgrnam_r(name, &grbuf, g_buf, (size_t) g_n, &gr)) != 0 || !gr) -#else errno = 0; - if (!(gr = getgrnam(name))) -#endif + if (!(gr = pa_getgrnam_malloc(name))) { if (!errno) errno = ENOENT; @@ -1153,7 +1087,7 @@ gid_t pa_get_gid_of_group(const char *name) { ret = gr->gr_gid; finish: - pa_xfree(g_buf); + pa_getgrnam_free(gr); return ret; } -- cgit From a0f01ddc951694e1d13f44dc3a5d0d3fb2daa142 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Sun, 23 Aug 2009 21:49:37 +0200 Subject: port a few things over to use xmalloc and friends instead of low-level libc malloc/free directly --- src/pulsecore/core-util.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/pulsecore/core-util.c') diff --git a/src/pulsecore/core-util.c b/src/pulsecore/core-util.c index 0eb32cc4..1c8c6780 100644 --- a/src/pulsecore/core-util.c +++ b/src/pulsecore/core-util.c @@ -2223,7 +2223,7 @@ int pa_close_all(int except_fd, ...) { va_end(ap); r = pa_close_allv(p); - free(p); + pa_xfree(p); return r; } -- cgit