[vlc-devel] commit: Use getpwuid_r - thread safety ( Rémi Denis-Courmont )

git version control git at videolan.org
Mon May 5 22:40:09 CEST 2008


vlc | branch: master | Rémi Denis-Courmont <rem at videolan.org> | Mon May  5 22:35:18 2008 +0300| [e7125f4ec9d298e514b56f721ebac2a7be14b24a]

Use getpwuid_r - thread safety

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=e7125f4ec9d298e514b56f721ebac2a7be14b24a
---

 configure.ac      |    2 +-
 src/config/core.c |   37 +++++++++++++++----------------------
 2 files changed, 16 insertions(+), 23 deletions(-)

diff --git a/configure.ac b/configure.ac
index ae6dbd0..1b0340b 100644
--- a/configure.ac
+++ b/configure.ac
@@ -464,7 +464,7 @@ dnl Check for system libs needed
 need_libc=false
 
 dnl Check for usual libc functions
-AC_CHECK_FUNCS([gettimeofday strtod strtol strtof strtoll strtoull strsep isatty vasprintf asprintf swab sigrelse getpwuid memalign posix_memalign if_nametoindex atoll getenv putenv setenv gmtime_r ctime_r localtime_r lrintf daemon scandir fork bsearch lstat strlcpy strdup strndup strnlen atof lldiv posix_fadvise posix_madvise])
+AC_CHECK_FUNCS([gettimeofday strtod strtol strtof strtoll strtoull strsep isatty vasprintf asprintf swab sigrelse getpwuid_r memalign posix_memalign if_nametoindex atoll getenv putenv setenv gmtime_r ctime_r localtime_r lrintf daemon scandir fork bsearch lstat strlcpy strdup strndup strnlen atof lldiv posix_fadvise posix_madvise])
 AC_CHECK_FUNCS(strcasecmp,,[AC_CHECK_FUNCS(stricmp)])
 AC_CHECK_FUNCS(strncasecmp,,[AC_CHECK_FUNCS(strnicmp)])
 AC_CHECK_FUNCS(strcasestr,,[AC_CHECK_FUNCS(stristr)])
diff --git a/src/config/core.c b/src/config/core.c
index 28aea7d..b0f50f6 100644
--- a/src/config/core.c
+++ b/src/config/core.c
@@ -39,8 +39,8 @@
 #    include <unistd.h>                                          /* getuid() */
 #endif
 
-#if defined(HAVE_GETPWUID)
-#   include <pwd.h>                                            /* getpwuid() */
+#if defined(HAVE_GETPWUID_R)
+#   include <pwd.h>
 #endif
 
 #if defined( HAVE_SYS_STAT_H )
@@ -617,13 +617,6 @@ const char *config_GetDataDir( void )
 #endif
 }
 
-/*****************************************************************************
- * config_GetHomeDir, config_GetUserDir: find the user's home directory.
- *****************************************************************************
- * This function will try by different ways to find the user's home path.
- * Note that this function is not reentrant, it should be called only once
- * at the beginning of main where the result will be stored for later use.
- *****************************************************************************/
 static char *GetDir( bool b_appdata )
 {
     const char *psz_localhome = NULL;
@@ -671,7 +664,7 @@ static char *GetDir( bool b_appdata )
     }
 
 #elif defined(UNDER_CE)
-
+    (void)b_appdata;
 #ifndef CSIDL_APPDATA
 #   define CSIDL_APPDATA 0x1A
 #endif
@@ -681,25 +674,25 @@ static char *GetDir( bool b_appdata )
     /* get the "Application Data" folder for the current user */
     if( SHGetSpecialFolderPath( NULL, whomedir, CSIDL_APPDATA, 1 ) )
         return FromWide( whomedir );
+#else
+    (void)b_appdata;
 #endif
 
     psz_localhome = getenv( "HOME" );
+#if defined(HAVE_GETPWUID_R)
+    char buf[sysconf (_SC_GETPW_R_SIZE_MAX)];
     if( psz_localhome == NULL )
     {
-#if defined(HAVE_GETPWUID)
-        struct passwd *p_pw;
-        (void)b_appdata;
+        struct passwd pw, *res;
 
-        if( ( p_pw = getpwuid( getuid() ) ) != NULL )
-            psz_localhome = p_pw->pw_dir;
-        else
-#endif
-        {
-            psz_localhome = getenv( "TMP" );
-            if( psz_localhome == NULL )
-                psz_localhome = "/tmp";
-        }
+        if (!getpwuid_r (getuid (), &pw, buf, sizeof (buf), &res) && res)
+            psz_localhome = pw.pw_dir;
     }
+#endif
+    if (psz_localhome == NULL)
+        psz_localhome = getenv( "TMP" );
+    if (psz_localhome == NULL)
+        psz_localhome = "/tmp";
 
     return FromLocaleDup( psz_localhome );
 }




More information about the vlc-devel mailing list