diff options
author | Jeff Johnston <jjohnstn@redhat.com> | 2002-06-05 20:58:59 +0000 |
---|---|---|
committer | Jeff Johnston <jjohnstn@redhat.com> | 2002-06-05 20:58:59 +0000 |
commit | 39e65e0113e188e4177077407d337bb3a1ac1853 (patch) | |
tree | 7afb2759a870354f5267c72215acfcfc89bb12fa /newlib/libc/posix | |
parent | ea4e6ec8f9fe4a784d4ce2ef71037e05fb9a876d (diff) | |
download | cygnal-39e65e0113e188e4177077407d337bb3a1ac1853.tar.gz cygnal-39e65e0113e188e4177077407d337bb3a1ac1853.tar.bz2 cygnal-39e65e0113e188e4177077407d337bb3a1ac1853.zip |
2002-06-05 Jeff Johnston <jjohnstn@redhat.com>
* libc/include/string.h[__linux__]: Add strsignal prototype.
* libc/include/sys/lock.h: New file with default locking support.
* libc/include/sys/reent.h: Add signal buffer support for strsignal
and psignal.
* libc/posix/Makefile.am: Add support for readdir_r.c.
* libc/posix/Makefile.in: Regenerated.
* libc/posix/closedir.c: Add locking support and hash table cleanup.
* libc/posix/opendir.c: Add lock support.
* libc/posix/readdir.c: Ditto.
* libc/posix/rewinddir.c: Ditto.
* libc/posix/scandir.c: Ditto.
* libc/posix/seekdir.c: Ditto.
* libc/posix/telldir.c: Ditto plus add _cleanupdir routine to
clean up leftover hash table entries.
* libc/posix/readdir_r.c: New file.
* libc/sys/linux/Makefile.am: Add psignal.c and strsignal.c support.
* libc/sys/linux/Makefile.in: Regenerated.
* libc/sys/linux/sys/dirent.h: Add dd_lock to DIR structure.
* libc/sys/linux/sys/signal.h: Add psignal prototype.
* libc/sys/linux/psignal.c: New file.
* libc/sys/linux/strsignal.c: Ditto.
Diffstat (limited to 'newlib/libc/posix')
-rw-r--r-- | newlib/libc/posix/Makefile.am | 2 | ||||
-rw-r--r-- | newlib/libc/posix/Makefile.in | 7 | ||||
-rw-r--r-- | newlib/libc/posix/closedir.c | 27 | ||||
-rw-r--r-- | newlib/libc/posix/opendir.c | 7 | ||||
-rw-r--r-- | newlib/libc/posix/readdir.c | 32 | ||||
-rw-r--r-- | newlib/libc/posix/readdir_r.c | 108 | ||||
-rw-r--r-- | newlib/libc/posix/rewinddir.c | 7 | ||||
-rw-r--r-- | newlib/libc/posix/scandir.c | 37 | ||||
-rw-r--r-- | newlib/libc/posix/seekdir.c | 8 | ||||
-rw-r--r-- | newlib/libc/posix/telldir.c | 62 |
10 files changed, 276 insertions, 21 deletions
diff --git a/newlib/libc/posix/Makefile.am b/newlib/libc/posix/Makefile.am index 1274137f9..a43bcc687 100644 --- a/newlib/libc/posix/Makefile.am +++ b/newlib/libc/posix/Makefile.am @@ -7,7 +7,7 @@ INCLUDES = $(NEWLIB_CFLAGS) $(CROSS_CFLAGS) $(TARGET_CFLAGS) LIB_SOURCES = \ closedir.c opendir.c readdir.c rewinddir.c scandir.c seekdir.c \ telldir.c execl.c execle.c execlp.c execv.c execve.c execvp.c \ - popen.c creat.c isatty.c + popen.c creat.c isatty.c readdir_r.c libposix_la_LDFLAGS = -Xcompiler -nostdlib diff --git a/newlib/libc/posix/Makefile.in b/newlib/libc/posix/Makefile.in index df94cf2cf..d955b243e 100644 --- a/newlib/libc/posix/Makefile.in +++ b/newlib/libc/posix/Makefile.in @@ -105,7 +105,7 @@ INCLUDES = $(NEWLIB_CFLAGS) $(CROSS_CFLAGS) $(TARGET_CFLAGS) LIB_SOURCES = \ closedir.c opendir.c readdir.c rewinddir.c scandir.c seekdir.c \ telldir.c execl.c execle.c execlp.c execv.c execve.c execvp.c \ - popen.c creat.c isatty.c + popen.c creat.c isatty.c readdir_r.c libposix_la_LDFLAGS = -Xcompiler -nostdlib @@ -138,14 +138,15 @@ lib_a_LIBADD = @USE_LIBTOOL_FALSE@lib_a_OBJECTS = closedir.o opendir.o readdir.o \ @USE_LIBTOOL_FALSE@rewinddir.o scandir.o seekdir.o telldir.o execl.o \ @USE_LIBTOOL_FALSE@execle.o execlp.o execv.o execve.o execvp.o popen.o \ -@USE_LIBTOOL_FALSE@creat.o isatty.o +@USE_LIBTOOL_FALSE@creat.o isatty.o readdir_r.o LTLIBRARIES = $(noinst_LTLIBRARIES) libposix_la_LIBADD = @USE_LIBTOOL_TRUE@libposix_la_OBJECTS = closedir.lo opendir.lo \ @USE_LIBTOOL_TRUE@readdir.lo rewinddir.lo scandir.lo seekdir.lo \ @USE_LIBTOOL_TRUE@telldir.lo execl.lo execle.lo execlp.lo execv.lo \ -@USE_LIBTOOL_TRUE@execve.lo execvp.lo popen.lo creat.lo isatty.lo +@USE_LIBTOOL_TRUE@execve.lo execvp.lo popen.lo creat.lo isatty.lo \ +@USE_LIBTOOL_TRUE@readdir_r.lo CFLAGS = @CFLAGS@ COMPILE = $(CC) $(DEFS) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) LTCOMPILE = $(LIBTOOL) --mode=compile $(CC) $(DEFS) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) diff --git a/newlib/libc/posix/closedir.c b/newlib/libc/posix/closedir.c index 4ad6e4c97..57222f947 100644 --- a/newlib/libc/posix/closedir.c +++ b/newlib/libc/posix/closedir.c @@ -41,6 +41,9 @@ static char sccsid[] = "@(#)closedir.c 5.9 (Berkeley) 2/23/91"; #include <dirent.h> #include <stdlib.h> #include <unistd.h> +#include <sys/lock.h> + +extern void _cleanupdir (DIR *dirp); /* * close a directory. @@ -49,14 +52,26 @@ int closedir(dirp) register DIR *dirp; { - int fd; + int fd, rc; +#ifdef HAVE_DD_LOCK + __lock_acquire_recursive(dirp->dd_lock); +#endif + rc = 0; fd = dirp->dd_fd; - dirp->dd_fd = -1; - dirp->dd_loc = 0; - (void)free((void *)dirp->dd_buf); - (void)free((void *)dirp); - return(close(fd)); + if (fd != -1) { + dirp->dd_fd = -1; + dirp->dd_loc = 0; + (void)free((void *)dirp->dd_buf); + (void)free((void *)dirp); + rc = close(fd); + _cleanupdir(dirp); + } +#ifdef HAVE_DD_LOCK + __lock_release_recursive(dirp->dd_lock); + __lock_close_recursive(dirp->dd_lock); +#endif + return rc; } #endif /* ! HAVE_OPENDIR */ diff --git a/newlib/libc/posix/opendir.c b/newlib/libc/posix/opendir.c index f924c0b52..de14edcad 100644 --- a/newlib/libc/posix/opendir.c +++ b/newlib/libc/posix/opendir.c @@ -41,6 +41,7 @@ static char sccsid[] = "@(#)opendir.c 5.11 (Berkeley) 2/23/91"; #include <fcntl.h> #include <stdlib.h> #include <unistd.h> +#include <sys/lock.h> /* * open a directory. @@ -78,6 +79,12 @@ opendir(name) /* * Set up seek point for rewinddir. */ + +#ifdef HAVE_DD_LOCK + /* if we have a locking mechanism, initialize it */ + __lock_init_recursive(dirp->dd_lock); +#endif + return dirp; } diff --git a/newlib/libc/posix/readdir.c b/newlib/libc/posix/readdir.c index 308330cb0..4de66e4f9 100644 --- a/newlib/libc/posix/readdir.c +++ b/newlib/libc/posix/readdir.c @@ -39,6 +39,8 @@ static char sccsid[] = "@(#)readdir.c 5.7 (Berkeley) 6/1/90"; #include <dirent.h> +extern int getdents (int fd, void *dp, int count); + /* * get next entry in a directory. */ @@ -46,29 +48,51 @@ struct dirent * readdir(dirp) register DIR *dirp; { register struct dirent *dp; - + +#ifdef HAVE_DD_LOCK + __lock_acquire_recursive(dirp->dd_lock); +#endif + + if (dirp->dd_fd == -1) + return NULL; + for (;;) { if (dirp->dd_loc == 0) { dirp->dd_size = getdents (dirp->dd_fd, dirp->dd_buf, dirp->dd_len); - if (dirp->dd_size <= 0) + if (dirp->dd_size <= 0) { +#ifdef HAVE_DD_LOCK + __lock_release_recursive(dirp->dd_lock); +#endif return NULL; + } } if (dirp->dd_loc >= dirp->dd_size) { dirp->dd_loc = 0; continue; } dp = (struct dirent *)(dirp->dd_buf + dirp->dd_loc); - if ((int)dp & 03) /* bogus pointer check */ + if ((int)dp & 03) { /* bogus pointer check */ +#ifdef HAVE_DD_LOCK + __lock_release_recursive(dirp->dd_lock); +#endif return NULL; + } if (dp->d_reclen <= 0 || - dp->d_reclen > dirp->dd_len + 1 - dirp->dd_loc) + dp->d_reclen > dirp->dd_len + 1 - dirp->dd_loc) { +#ifdef HAVE_DD_LOCK + __lock_release_recursive(dirp->dd_lock); +#endif return NULL; + } dirp->dd_loc += dp->d_reclen; if (dp->d_ino == 0) continue; +#ifdef HAVE_DD_LOCK + __lock_release_recursive(dirp->dd_lock); +#endif return (dp); } } diff --git a/newlib/libc/posix/readdir_r.c b/newlib/libc/posix/readdir_r.c new file mode 100644 index 000000000..805180cf5 --- /dev/null +++ b/newlib/libc/posix/readdir_r.c @@ -0,0 +1,108 @@ +#ifndef HAVE_OPENDIR + +/* + * Copyright (c) 1983 Regents of the University of California. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* this code is modified from readdir.c by Jeff Johnston, June 5, 2002 */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "@(#)readdir.c 5.7 (Berkeley) 6/1/90"; +#endif /* LIBC_SCCS and not lint */ + +#include <dirent.h> +#include <errno.h> +#include <string.h> + +extern int getdents (int fd, void *dp, int count); + +/* + * get next entry in a directory using supplied dirent structure. + */ +int +readdir_r(dirp, dp, dpp) + register DIR *dirp; + struct dirent *dp; + struct dirent **dpp; { + +struct dirent *tmpdp; + +#ifdef HAVE_DD_LOCK + __lock_acquire_recursive(dirp->dd_lock); +#endif + + if (dirp->dd_fd == -1) { + *dpp = NULL; + return errno = EBADF; + } + + for (;;) { + if (dirp->dd_loc == 0) { + dirp->dd_size = getdents (dirp->dd_fd, + dirp->dd_buf, + dirp->dd_len); + + if (dirp->dd_size <= 0) { +#ifdef HAVE_DD_LOCK + __lock_release_recursive(dirp->dd_lock); +#endif + *dpp = NULL; + return errno; + } + } + if (dirp->dd_loc >= dirp->dd_size) { + dirp->dd_loc = 0; + continue; + } + tmpdp = (struct dirent *)(dirp->dd_buf + dirp->dd_loc); + memcpy (dp, tmpdp, sizeof(struct dirent)); + + if (dp->d_reclen <= 0 || + dp->d_reclen > dirp->dd_len + 1 - dirp->dd_loc) { +#ifdef HAVE_DD_LOCK + __lock_release_recursive(dirp->dd_lock); +#endif + *dpp = NULL; + return -1; + } + dirp->dd_loc += dp->d_reclen; + if (dp->d_ino == 0) + continue; +#ifdef HAVE_DD_LOCK + __lock_release_recursive(dirp->dd_lock); +#endif + *dpp = dp; + return 0; + } +} + +#endif /* ! HAVE_OPENDIR */ diff --git a/newlib/libc/posix/rewinddir.c b/newlib/libc/posix/rewinddir.c index 6a3814b55..f7631f983 100644 --- a/newlib/libc/posix/rewinddir.c +++ b/newlib/libc/posix/rewinddir.c @@ -39,12 +39,19 @@ static char sccsid[] = "@(#)rewinddir.c 5.1 (Berkeley) 5/25/90"; #include <sys/types.h> #include <dirent.h> +#include <sys/lock.h> void rewinddir(dirp) DIR *dirp; { +#ifdef HAVE_DD_LOCK + __lock_acquire_recursive(dirp->dd_lock); +#endif _seekdir((dirp), (off_t)0); +#ifdef HAVE_DD_LOCK + __lock_release_recursive(dirp->dd_lock); +#endif } #endif /* ! HAVE_OPENDIR */ diff --git a/newlib/libc/posix/scandir.c b/newlib/libc/posix/scandir.c index 6acaff739..bcbe57fbd 100644 --- a/newlib/libc/posix/scandir.c +++ b/newlib/libc/posix/scandir.c @@ -49,6 +49,7 @@ static char sccsid[] = "@(#)scandir.c 5.10 (Berkeley) 2/23/91"; #include <dirent.h> #include <stdlib.h> #include <string.h> +#include <sys/lock.h> /* * The DIRSIZ macro gives the minimum record length which will hold @@ -84,8 +85,15 @@ scandir(dirname, namelist, select, dcomp) if ((dirp = opendir(dirname)) == NULL) return(-1); - if (fstat(dirp->dd_fd, &stb) < 0) +#ifdef HAVE_DD_LOCK + __lock_acquire_recursive(dirp->dd_lock); +#endif + if (fstat(dirp->dd_fd, &stb) < 0) { +#ifdef HAVE_DD_LOCK + __lock_release_recursive(dirp->dd_lock); +#endif return(-1); + } /* * estimate the array size by taking the size of the directory file @@ -93,8 +101,12 @@ scandir(dirname, namelist, select, dcomp) */ arraysz = (stb.st_size / 24); names = (struct dirent **)malloc(arraysz * sizeof(struct dirent *)); - if (names == NULL) + if (names == NULL) { +#ifdef HAVE_DD_LOCK + __lock_release_recursive(dirp->dd_lock); +#endif return(-1); + } nitems = 0; while ((d = readdir(dirp)) != NULL) { @@ -104,8 +116,12 @@ scandir(dirname, namelist, select, dcomp) * Make a minimum size copy of the data */ p = (struct dirent *)malloc(DIRSIZ(d)); - if (p == NULL) + if (p == NULL) { +#ifdef HAVE_DD_LOCK + __lock_release_recursive(dirp->dd_lock); +#endif return(-1); + } p->d_ino = d->d_ino; p->d_reclen = d->d_reclen; #ifdef _DIRENT_HAVE_D_NAMLEN @@ -119,13 +135,21 @@ scandir(dirname, namelist, select, dcomp) * realloc the maximum size. */ if (++nitems >= arraysz) { - if (fstat(dirp->dd_fd, &stb) < 0) + if (fstat(dirp->dd_fd, &stb) < 0) { +#ifdef HAVE_DD_LOCK + __lock_release_recursive(dirp->dd_lock); +#endif return(-1); /* just might have grown */ + } arraysz = stb.st_size / 12; names = (struct dirent **)realloc((char *)names, arraysz * sizeof(struct dirent *)); - if (names == NULL) + if (names == NULL) { +#ifdef HAVE_DD_LOCK + __lock_release_recursive(dirp->dd_lock); +#endif return(-1); + } } names[nitems-1] = p; } @@ -133,6 +157,9 @@ scandir(dirname, namelist, select, dcomp) if (nitems && dcomp != NULL) qsort(names, nitems, sizeof(struct dirent *), dcomp); *namelist = names; +#ifdef HAVE_DD_LOCK + __lock_release_recursive(dirp->dd_lock); +#endif return(nitems); } diff --git a/newlib/libc/posix/seekdir.c b/newlib/libc/posix/seekdir.c index dab3bfb24..a945201c4 100644 --- a/newlib/libc/posix/seekdir.c +++ b/newlib/libc/posix/seekdir.c @@ -39,6 +39,7 @@ static char sccsid[] = "@(#)seekdir.c 5.7 (Berkeley) 6/1/90"; #include <sys/param.h> #include <dirent.h> +#include <sys/lock.h> /* * Seek to an entry in a directory. @@ -49,8 +50,13 @@ seekdir(dirp, loc) DIR *dirp; long loc; { - +#ifdef HAVE_DD_LOCK + __lock_acquire_recursive(dirp->dd_lock); +#endif _seekdir(dirp, loc); +#ifdef HAVE_DD_LOCK + __lock_release_recursive(dirp->dd_lock); +#endif } #endif /* ! HAVE_OPENDIR */ diff --git a/newlib/libc/posix/telldir.c b/newlib/libc/posix/telldir.c index 30c003760..6022bbe19 100644 --- a/newlib/libc/posix/telldir.c +++ b/newlib/libc/posix/telldir.c @@ -41,6 +41,7 @@ static char sccsid[] = "@(#)telldir.c 5.9 (Berkeley) 2/23/91"; #include <dirent.h> #include <stdlib.h> #include <unistd.h> +#include <sys/lock.h> /* * The option SINGLEUSE may be defined to say that a telldir @@ -60,6 +61,7 @@ struct ddloc { long loc_index; /* key associated with structure */ long loc_seek; /* magic cookie returned by getdirentries */ long loc_loc; /* offset of entry in buffer */ + DIR *loc_dirp; /* DIR pointer */ }; #define NDIRHASH 32 /* Num of hash lists, must be a power of 2 */ @@ -67,6 +69,7 @@ struct ddloc { static long dd_loccnt; /* Index of entry for sequential readdir's */ static struct ddloc *dd_hash[NDIRHASH]; /* Hash list heads for ddlocs */ +__LOCK_INIT(static, dd_hash_lock); /* * return a pointer into a directory @@ -80,12 +83,22 @@ telldir(dirp) if ((lp = (struct ddloc *)malloc(sizeof(struct ddloc))) == NULL) return (-1); + +#ifdef HAVE_DD_LOCK + __lock_acquire_recursive(dirp->dd_lock); + __lock_acquire(dd_hash_lock); +#endif index = dd_loccnt++; lp->loc_index = index; lp->loc_seek = dirp->dd_seek; lp->loc_loc = dirp->dd_loc; + lp->loc_dirp = dirp; lp->loc_next = dd_hash[LOCHASH(index)]; dd_hash[LOCHASH(index)] = lp; +#ifdef HAVE_DD_LOCK + __lock_release(dd_hash_lock); + __lock_release_recursive(dirp->dd_lock); +#endif return (index); } @@ -103,6 +116,9 @@ _seekdir(dirp, loc) struct dirent *dp; extern long lseek(); +#ifdef HAVE_DD_LOCK + __lock_acquire(dd_hash_lock); +#endif prevlp = &dd_hash[LOCHASH(loc)]; lp = *prevlp; while (lp != NULL) { @@ -111,8 +127,12 @@ _seekdir(dirp, loc) prevlp = &lp->loc_next; lp = lp->loc_next; } - if (lp == NULL) + if (lp == NULL) { +#ifdef HAVE_DD_LOCK + __lock_release(dd_hash_lock); +#endif return; + } if (lp->loc_loc == dirp->dd_loc && lp->loc_seek == dirp->dd_seek) goto found; (void) lseek(dirp->dd_fd, lp->loc_seek, 0); @@ -128,6 +148,46 @@ found: *prevlp = lp->loc_next; free((caddr_t)lp); #endif +#ifdef HAVE_DD_LOCK + __lock_release(dd_hash_lock); +#endif } +/* clean out any hash entries from a closed directory */ +void +_cleanupdir (dirp) + register DIR *dirp; +{ + int i; + +#ifdef HAVE_DD_LOCK + __lock_acquire(dd_hash_lock); +#endif + for (i = 0; i < NDIRHASH; ++i) { + register struct ddloc *lp; + register struct ddloc *prevlp; + lp = dd_hash[i]; + while (lp != NULL && lp->loc_dirp == dirp) { + dd_hash[i] = lp->loc_next; + prevlp = lp; + free((caddr_t)lp); + lp = prevlp->loc_next; + } + prevlp = lp; + while (lp != NULL) { + lp = lp->loc_next; + if (lp != NULL && lp->loc_dirp == dirp) { + prevlp->loc_next = lp->loc_next; + free((caddr_t)lp); + lp = prevlp; + } + else + prevlp = lp; + } + } +#ifdef HAVE_DD_LOCK + __lock_release(dd_hash_lock); +#endif + +} #endif /* ! HAVE_OPENDIR */ |