]> granicus.if.org Git - php/commitdiff
Use reentrant version of readdir. If the target platform does not support
authorSascha Schumann <sas@php.net>
Tue, 23 May 2000 15:13:16 +0000 (15:13 +0000)
committerSascha Schumann <sas@php.net>
Tue, 23 May 2000 15:13:16 +0000 (15:13 +0000)
the POSIX-like readdir_r, we fall back to readdir. In ZTS mode, this will
cause php_readdir_r calls to be serialized.

acinclude.m4
configure.in
ext/session/mod_files.c
ext/standard/dir.c
main/php_reentrancy.h
main/reentrancy.c

index fea266109eac04047b7d5799d497348c36416dbf..4982bbb3e2fcd8207b028d7aa5a42bed3c7ef548 100644 (file)
@@ -4,6 +4,36 @@ dnl This file contains local autoconf functions.
 
 sinclude(dynlib.m4)
 
+AC_DEFUN(PHP_POSIX_READDIR_R,[
+  AC_CACHE_CHECK(for type of readdir_r, ac_cv_what_readdir_r,[
+    AC_TRY_RUN([
+#include <sys/types.h>
+#include <dirent.h>
+
+main() {
+       DIR *dir;
+       struct dirent entry, *pentry;
+
+       dir = opendir("/");
+       if (!dir) 
+               exit(1);
+       if (readdir_r(dir, &entry, &pentry) == 0)
+               exit(0);
+       exit(1);
+}
+    ],[
+      ac_cv_what_readdir_r=POSIX
+    ],[
+      ac_cv_what_readdir_r=none
+    ],[
+      ac_cv_what_readdir_r=none
+   ])
+  ])
+  if test "$ac_cv_what_readdir_r" = "POSIX"; then
+    AC_DEFINE(HAVE_POSIX_READDIR_R,1,[whether you have POSIX readdir_r])
+  fi
+])
+
 AC_DEFUN(PHP_SHLIB_SUFFIX_NAME,[
   PHP_SUBST(SHLIB_SUFFIX_NAME)
   SHLIB_SUFFIX_NAME=so
index 63d694a8a79acfb14fb13d5a3467d98f2b14e1db..4ed1a52d1785a5410fbcc4e7f3c0287af538c99f 100644 (file)
@@ -379,6 +379,7 @@ AC_FUNC_ALLOCA
 AC_BROKEN_SPRINTF
 PHP_DECLARED_TIMEZONE
 PHP_TIME_R_TYPE
+PHP_POSIX_READDIR_R
 
 dnl AIX keeps in_addr_t in /usr/include/netinet/in.h
 dnl AC_MSG_CHECKING(for in_addr_t)
index aefebc1df5a5efa8d45a0ad44ffc8f246c94a898..32f511d18a2cdf06169d4036d348afb5fe606097 100644 (file)
@@ -154,7 +154,7 @@ static void _ps_files_open(ps_files *data, const char *key)
 static int _ps_files_cleanup_dir(const char *dirname, int maxlifetime)
 {
        DIR *dir;
-       struct dirent *entry;
+       struct dirent *entry, dentry;
        struct stat sbuf;
        char buf[MAXPATHLEN];
        time_t now;
@@ -168,7 +168,7 @@ static int _ps_files_cleanup_dir(const char *dirname, int maxlifetime)
 
        time(&now);
 
-       while((entry = readdir(dir))) {
+       while (php_readdir_r(dir, &dentry, &entry) == 0 && entry) {
                /* does the file start with our prefix? */
                if (!strncmp(entry->d_name, FILE_PREFIX, sizeof(FILE_PREFIX) - 1) &&
                                /* create full path */
index 18c33db4ce7e1c6e0128247c762b0b7da75fb3dd..9b00a3879e6bf2ee78ec36235d93a09c760271fe 100644 (file)
@@ -273,14 +273,14 @@ PHP_FUNCTION(readdir)
 {
        pval **id, **tmp, *myself;
        php_dir *dirp;
-       struct dirent *direntp;
+       struct dirent entry;
+       struct dirent *result;
        DIRLS_FETCH();
 
        FETCH_DIRP();
-       
-       direntp = readdir(dirp->dir);
-       if (direntp) {
-               RETURN_STRINGL(direntp->d_name, strlen(direntp->d_name), 1);
+
+       if (php_readdir_r(dirp->dir, &entry, &result) == 0 && result) {
+               RETURN_STRINGL(result->d_name, strlen(result->d_name), 1);
        }
        RETURN_FALSE;
 }
index a6aeb363d157096299c58c1247e7114be26b7f87..0cba46e8960cd92f6e4a7712af0d55f8cc55e9df 100644 (file)
@@ -22,6 +22,8 @@
 
 #include "php.h"
 
+#include <sys/types.h>
+#include <dirent.h>
 #include <time.h>
 
 /* currently, PHP does not check for these functions, but assumes
 #undef HAVE_GMTIME_R
 #endif
 
+#if defined(HAVE_POSIX_READDIR_R)
+#define php_readdir_r readdir_r
+#else
+PHPAPI int php_readdir_r(DIR *dirp, struct dirent *entry,
+               struct dirent **result);
+#endif
+
 #if !defined(HAVE_LOCALTIME_R) && defined(HAVE_LOCALTIME)
 #define PHP_NEED_REENTRANCY 1
 PHPAPI struct tm *php_localtime_r(const time_t *const timep, struct tm *p_tm);
index 77529feb5a339837f1bf4c4387a881291a609963..8a135508a527ebf771a99dcba2ea3bc2ba3f3749 100644 (file)
  */
 
 
+#include <sys/types.h>
 #include <string.h>
+#include <errno.h>
+#ifdef HAVE_DIRENT_H
+#include <dirent.h>
+#endif
+
+#ifdef PHP_WIN32
+#define NEEDRDH 1
+#include "win32/readdir.h"
+#endif
 
 #include "php_reentrancy.h"
 #include "ext/standard/php_rand.h"                   /* for RAND_MAX */
@@ -27,6 +37,7 @@ enum {
        CTIME_R,
        ASCTIME_R,
        GMTIME_R,
+       READDIR_R,
        NUMBER_OF_LOCKS
 };
 
@@ -77,7 +88,37 @@ PHPAPI struct tm *php_gmtime_r(const time_t *const timep, struct tm *p_tm)
 }
 
 #endif
+
+#if !defined(HAVE_POSIX_READDIR_R)
+
+PHPAPI int php_readdir_r(DIR *dirp, struct dirent *entry, 
+               struct dirent **result)
+{
+       struct dirent *ptr;
+       int ret = 0;
+
+       local_lock(READDIR_R);
+       
+       errno = 0;
        
+       ptr = readdir(dirp);
+       
+       if (!ptr && errno != 0)
+               ret = errno;
+
+       if (entry && ptr)
+               memcpy(entry, ptr, sizeof(*ptr));
+
+       if (result) 
+               *result = ptr;
+
+       local_unlock(READDIR_R);
+
+       return ret;
+}
+
+#endif
+
 #if !defined(HAVE_LOCALTIME_R) && defined(HAVE_LOCALTIME)
 
 PHPAPI struct tm *php_localtime_r(const time_t *const timep, struct tm *p_tm)