summaryrefslogtreecommitdiffstats
path: root/lib/libc/gen/readdir.c
diff options
context:
space:
mode:
authorkurt <kurt@openbsd.org>2007-06-05 18:11:48 +0000
committerkurt <kurt@openbsd.org>2007-06-05 18:11:48 +0000
commit45e14c381361430fbd1edeac8cedeeb0decb1480 (patch)
treea5ed83726574726f3efb97709ca2097c63d5a220 /lib/libc/gen/readdir.c
parentremove ipx-related stuff. objections nobody. major bump by kurt in a few (diff)
downloadwireguard-openbsd-45e14c381361430fbd1edeac8cedeeb0decb1480.tar.xz
wireguard-openbsd-45e14c381361430fbd1edeac8cedeeb0decb1480.zip
_FD_LOCK/UNLOCK() is libpthread specific and not needed for librthread, so
isolate its usage to libpthread only and replace with generic non-static mutex support in the one place it is needed: - remove _FD_LOCK/UNLOCK from lseek and ftruncate in libc and make the functions weak so that libpthread can override with its own new versions that do the locking. - remove _thread_fd_lock/unlock() weak functions from libc and adjust libpthread for the change. - add generic _thread_mutex_lock/unlock/destroy() weak functions in libc to support non-static mutexes in libc and add libpthread and librthread implementations for them. libc can utilize non-static mutexes via the new _MUTEX_LOCK/UNLOCK/DESTROY() macros. Actually these new macros can support both static and non-static mutexes but currently only using them for non-static. - make opendir/closedir/readdir/readdir_r/seekdir/telldir() thread-safe for both thread libraries by using a non-static mutex in the struct _dirdesc (typedef DIR), utilizing it in the *dir functions and remove remaining and incorrect _FD_LOCK/UNLOCK() use in libc. - add comments to both thread libraries to indicate libc depends on the current implementation of static mutex initialization. suggested by marc@ - major bump libc and libpthread due to function removal, structure change and weak symbol conversions. okay marc@, tedu@
Diffstat (limited to 'lib/libc/gen/readdir.c')
-rw-r--r--lib/libc/gen/readdir.c32
1 files changed, 19 insertions, 13 deletions
diff --git a/lib/libc/gen/readdir.c b/lib/libc/gen/readdir.c
index b028c822396..5f4c447e576 100644
--- a/lib/libc/gen/readdir.c
+++ b/lib/libc/gen/readdir.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: readdir.c,v 1.11 2007/02/08 16:34:39 millert Exp $ */
+/* $OpenBSD: readdir.c,v 1.12 2007/06/05 18:11:48 kurt Exp $ */
/*
* Copyright (c) 1983, 1993
* The Regents of the University of California. All rights reserved.
@@ -38,7 +38,7 @@
* get next entry in a directory.
*/
struct dirent *
-readdir(DIR *dirp)
+_readdir_unlocked(DIR *dirp)
{
struct dirent *dp;
@@ -65,28 +65,34 @@ readdir(DIR *dirp)
}
}
+struct dirent *
+readdir(DIR *dirp)
+{
+ struct dirent *dp;
+
+ _MUTEX_LOCK(&dirp->dd_lock);
+ dp = _readdir_unlocked(dirp);
+ _MUTEX_UNLOCK(&dirp->dd_lock);
+
+ return (dp);
+}
+
int
readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result)
{
struct dirent *dp;
- int ret;
- if (dirp->dd_fd < 0) {
- return EBADF;
- }
- if ((ret = _FD_LOCK(dirp->dd_fd, FD_READ, NULL)) != 0)
- return ret;
- errno = 0;
- dp = readdir(dirp);
+ _MUTEX_LOCK(&dirp->dd_lock);
+ dp = _readdir_unlocked(dirp);
if (dp == NULL && errno != 0) {
- _FD_UNLOCK(dirp->dd_fd, FD_READ);
+ _MUTEX_UNLOCK(&dirp->dd_lock);
return errno;
}
if (dp != NULL)
memcpy(entry, dp, sizeof (struct dirent) - MAXNAMLEN + dp->d_namlen);
- _FD_UNLOCK(dirp->dd_fd, FD_READ);
+ _MUTEX_UNLOCK(&dirp->dd_lock);
if (dp != NULL)
- *result = entry;
+ *result = entry;
else
*result = NULL;
return 0;