1818
1919
2020/*
21- * Threading sometimes requires specially-named versions of functions
22- * that return data in static buffers, like strerror_r() instead of
23- * strerror(). Other operating systems use pthread_setspecific()
24- * and pthread_getspecific() internally to allow standard library
25- * functions to return static data to threaded applications. And some
26- * operating systems have neither.
27- *
28- * Additional confusion exists because many operating systems that
29- * use pthread_setspecific/pthread_getspecific() also have *_r versions
30- * of standard library functions for compatibility with operating systems
31- * that require them. However, internally, these *_r functions merely
32- * call the thread-safe standard library functions.
33- *
34- * For example, BSD/OS 4.3 uses Bind 8.2.3 for getpwuid(). Internally,
35- * getpwuid() calls pthread_setspecific/pthread_getspecific() to return
36- * static data to the caller in a thread-safe manner. However, BSD/OS
37- * also has getpwuid_r(), which merely calls getpwuid() and shifts
38- * around the arguments to match the getpwuid_r() function declaration.
39- * Therefore, while BSD/OS has getpwuid_r(), it isn't required. It also
40- * doesn't have strerror_r(), so we can't fall back to only using *_r
41- * functions for threaded programs.
42- *
43- * The current setup is to try threading in this order:
44- *
45- * use *_r function names if they exit
46- * (*_THREADSAFE=yes)
47- * use non-*_r functions if they are thread-safe
21+ * Historically, the code in this module had to deal with operating systems
22+ * that lacked getpwuid_r().
4823 */
4924
5025#ifndef WIN32
5126
52- /*
53- * Wrapper around getpwuid() or getpwuid_r() to mimic POSIX getpwuid_r()
54- * behaviour, if that function is not available or required.
55- *
56- * Per POSIX, the possible cases are:
57- * success: returns zero, *result is non-NULL
58- * uid not found: returns zero, *result is NULL
59- * error during lookup: returns an errno code, *result is NULL
60- * (caller should *not* assume that the errno variable is set)
61- */
62- static int
63- pqGetpwuid (uid_t uid , struct passwd * resultbuf , char * buffer ,
64- size_t buflen , struct passwd * * result )
65- {
66- #if defined(FRONTEND ) && defined(ENABLE_THREAD_SAFETY ) && defined(HAVE_GETPWUID_R )
67- return getpwuid_r (uid , resultbuf , buffer , buflen , result );
68- #else
69- /* no getpwuid_r() available, just use getpwuid() */
70- errno = 0 ;
71- * result = getpwuid (uid );
72- /* paranoia: ensure we return zero on success */
73- return (* result == NULL ) ? errno : 0 ;
74- #endif
75- }
76-
7727/*
7828 * pg_get_user_name - get the name of the user with the given ID
7929 *
@@ -89,7 +39,7 @@ pg_get_user_name(uid_t user_id, char *buffer, size_t buflen)
8939 struct passwd * pw = NULL ;
9040 int pwerr ;
9141
92- pwerr = pqGetpwuid (user_id , & pwdstr , pwdbuf , sizeof (pwdbuf ), & pw );
42+ pwerr = getpwuid_r (user_id , & pwdstr , pwdbuf , sizeof (pwdbuf ), & pw );
9343 if (pw != NULL )
9444 {
9545 strlcpy (buffer , pw -> pw_name , buflen );
@@ -125,7 +75,7 @@ pg_get_user_home_dir(uid_t user_id, char *buffer, size_t buflen)
12575 struct passwd * pw = NULL ;
12676 int pwerr ;
12777
128- pwerr = pqGetpwuid (user_id , & pwdstr , pwdbuf , sizeof (pwdbuf ), & pw );
78+ pwerr = getpwuid_r (user_id , & pwdstr , pwdbuf , sizeof (pwdbuf ), & pw );
12979 if (pw != NULL )
13080 {
13181 strlcpy (buffer , pw -> pw_dir , buflen );
0 commit comments