diff options
Diffstat (limited to 'lib/libc/gen')
-rw-r--r-- | lib/libc/gen/closedir.c | 12 | ||||
-rw-r--r-- | lib/libc/gen/opendir.c | 3 | ||||
-rw-r--r-- | lib/libc/gen/readdir.c | 32 | ||||
-rw-r--r-- | lib/libc/gen/seekdir.c | 6 | ||||
-rw-r--r-- | lib/libc/gen/telldir.c | 13 |
5 files changed, 40 insertions, 26 deletions
diff --git a/lib/libc/gen/closedir.c b/lib/libc/gen/closedir.c index 7315bcc1e41..48d2775d7ea 100644 --- a/lib/libc/gen/closedir.c +++ b/lib/libc/gen/closedir.c @@ -1,4 +1,4 @@ -/* $OpenBSD: closedir.c,v 1.7 2006/04/01 18:06:59 otto Exp $ */ +/* $OpenBSD: closedir.c,v 1.8 2007/06/05 18:11:48 kurt Exp $ */ /* * Copyright (c) 1983, 1993 * Regents of the University of California. All rights reserved. @@ -42,17 +42,15 @@ int closedir(DIR *dirp) { int fd; - int ret; - if ((ret = _FD_LOCK(dirp->dd_fd, FD_READ, NULL)) != 0) - return (ret); + _MUTEX_LOCK(&dirp->dd_lock); fd = dirp->dd_fd; dirp->dd_fd = -1; dirp->dd_loc = 0; free(dirp->dd_td->td_locs); free(dirp->dd_buf); + _MUTEX_UNLOCK(&dirp->dd_lock); + _MUTEX_DESTROY(&dirp->dd_lock); free(dirp); - ret = close(fd); - _FD_UNLOCK(fd, FD_READ); - return (ret); + return (close(fd)); } diff --git a/lib/libc/gen/opendir.c b/lib/libc/gen/opendir.c index 065c1aa9411..4028ba9c1eb 100644 --- a/lib/libc/gen/opendir.c +++ b/lib/libc/gen/opendir.c @@ -1,4 +1,4 @@ -/* $OpenBSD: opendir.c,v 1.18 2007/02/09 14:58:09 millert Exp $ */ +/* $OpenBSD: opendir.c,v 1.19 2007/06/05 18:11:48 kurt Exp $ */ /* * Copyright (c) 1983, 1993 * The Regents of the University of California. All rights reserved. @@ -97,6 +97,7 @@ __opendir2(const char *name, int flags) dirp->dd_loc = 0; dirp->dd_fd = fd; dirp->dd_flags = flags; + dirp->dd_lock = NULL; /* * Set up seek point for rewinddir. 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; diff --git a/lib/libc/gen/seekdir.c b/lib/libc/gen/seekdir.c index e472f62b789..2affbdd1ef1 100644 --- a/lib/libc/gen/seekdir.c +++ b/lib/libc/gen/seekdir.c @@ -1,4 +1,4 @@ -/* $OpenBSD: seekdir.c,v 1.8 2006/04/01 18:06:59 otto Exp $ */ +/* $OpenBSD: seekdir.c,v 1.9 2007/06/05 18:11:48 kurt Exp $ */ /* * Copyright (c) 1983, 1993 * The Regents of the University of California. All rights reserved. @@ -30,6 +30,7 @@ #include <sys/param.h> #include <dirent.h> +#include "thread_private.h" #include "telldir.h" /* @@ -39,6 +40,7 @@ void seekdir(DIR *dirp, long loc) { - + _MUTEX_LOCK(&dirp->dd_lock); __seekdir(dirp, loc); + _MUTEX_UNLOCK(&dirp->dd_lock); } diff --git a/lib/libc/gen/telldir.c b/lib/libc/gen/telldir.c index 708b923f3a0..1314397ce93 100644 --- a/lib/libc/gen/telldir.c +++ b/lib/libc/gen/telldir.c @@ -1,4 +1,4 @@ -/* $OpenBSD: telldir.c,v 1.9 2006/04/10 12:04:20 otto Exp $ */ +/* $OpenBSD: telldir.c,v 1.10 2007/06/05 18:11:48 kurt Exp $ */ /* * Copyright (c) 1983, 1993 * The Regents of the University of California. All rights reserved. @@ -34,6 +34,7 @@ #include <stdlib.h> #include <unistd.h> +#include "thread_private.h" #include "telldir.h" /* @@ -42,9 +43,11 @@ long telldir(DIR *dirp) { - long i = dirp->dd_td->td_last; + long i; struct ddloc *lp; + _MUTEX_LOCK(&dirp->dd_lock); + i = dirp->dd_td->td_last; lp = &dirp->dd_td->td_locs[i]; /* return previous telldir, if there */ @@ -52,6 +55,7 @@ telldir(DIR *dirp) if (lp->loc_seek == dirp->dd_seek && lp->loc_loc == dirp->dd_loc) { dirp->dd_td->td_last = i; + _MUTEX_UNLOCK(&dirp->dd_lock); return (i); } } @@ -60,8 +64,10 @@ telldir(DIR *dirp) size_t newsz = dirp->dd_td->td_sz * 2 + 1; struct ddloc *p; p = realloc(dirp->dd_td->td_locs, newsz * sizeof(*p)); - if (p == NULL) + if (p == NULL) { + _MUTEX_UNLOCK(&dirp->dd_lock); return (-1); + } dirp->dd_td->td_sz = newsz; dirp->dd_td->td_locs = p; lp = &dirp->dd_td->td_locs[i]; @@ -70,6 +76,7 @@ telldir(DIR *dirp) lp->loc_seek = dirp->dd_seek; lp->loc_loc = dirp->dd_loc; dirp->dd_td->td_last = i; + _MUTEX_UNLOCK(&dirp->dd_lock); return (i); } |