diff options
author | David Leonard <d@cvs.openbsd.org> | 1998-11-20 11:19:02 +0000 |
---|---|---|
committer | David Leonard <d@cvs.openbsd.org> | 1998-11-20 11:19:02 +0000 |
commit | f547068f88348f54941dc06da46491f99701933e (patch) | |
tree | 8548a6b78719cba1de575b7f49e5f8ac4e6f1683 /lib | |
parent | 394c7a9821726b84f284c0c4385b1a9198afa0b0 (diff) |
Add thread-safety to libc, so that libc_r will build (on i386 at least).
All POSIX libc api now there (to P1003.1c/D10)
(more md stuff is needed for other libc/arch/*)
(setlogin is no longer a special syscall)
Add -pthread option to gcc (that makes it use -lc_r and -D_POSIX_THREADS).
Doc some re-entrant routines
Add libc_r to intro(3)
dig() uses some libc srcs and an extra -I was needed there.
Add more md stuff to libc_r.
Update includes for the pthreads api
Update libc_r TODO
Diffstat (limited to 'lib')
61 files changed, 1237 insertions, 320 deletions
diff --git a/lib/libc/Makefile b/lib/libc/Makefile index 0a073cad566..111a7a6138a 100644 --- a/lib/libc/Makefile +++ b/lib/libc/Makefile @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile,v 1.15 1998/08/08 04:12:16 form Exp $ +# $OpenBSD: Makefile,v 1.16 1998/11/20 11:18:28 d Exp $ # # All library objects contain sccsid strings by default; they may be # excluded as a space-saving measure. To produce a library that does @@ -16,61 +16,14 @@ .include <bsd.own.mk> LIB=c -CFLAGS+=-DNLS -DLIBC_SCCS -DSYSLIBC_SCCS -I${.CURDIR}/include -.if (${YP} == "yes") -CFLAGS+=-DYP -I${.CURDIR}/yp -.endif LINTFLAGS=-z -LLIBS= -AINC= -I${.CURDIR}/arch/${MACHINE_ARCH} -.if defined(DESTDIR) -AINC+= -nostdinc -idirafter ${DESTDIR}/usr/include -.endif CLEANFILES+=tags -.if exists (${.CURDIR}/arch/${MACHINE_ARCH}/Makefile.inc) -.PATH: ${.CURDIR}/arch/${MACHINE_ARCH} -.include "${.CURDIR}/arch/${MACHINE_ARCH}/Makefile.inc" -.endif - -.include "${.CURDIR}/db/Makefile.inc" -.include "${.CURDIR}/compat-43/Makefile.inc" -.include "${.CURDIR}/gen/Makefile.inc" -.include "${.CURDIR}/crypt/Makefile.inc" -.include "${.CURDIR}/gmon/Makefile.inc" -.include "${.CURDIR}/hash/Makefile.inc" -.include "${.CURDIR}/locale/Makefile.inc" -.include "${.CURDIR}/md/Makefile.inc" -.include "${.CURDIR}/net/Makefile.inc" -.include "${.CURDIR}/nls/Makefile.inc" -.if (${MACHINE_ARCH} != "alpha") -.include "${.CURDIR}/quad/Makefile.inc" -.endif -.include "${.CURDIR}/regex/Makefile.inc" -.include "${.CURDIR}/rpc/Makefile.inc" -.include "${.CURDIR}/stdio/Makefile.inc" -.include "${.CURDIR}/stdlib/Makefile.inc" -.include "${.CURDIR}/string/Makefile.inc" -.include "${.CURDIR}/termios/Makefile.inc" -.include "${.CURDIR}/time/Makefile.inc" -.include "${.CURDIR}/sys/Makefile.inc" -.if (${YP} == "yes") -.include "${.CURDIR}/yp/Makefile.inc" -.endif +LIBCSRCDIR=${.CURDIR} +.include "${LIBCSRCDIR}/Makefile.inc" NLS= C.msg Pig.msg de.msg es.msg fi.msg fr.msg nl.msg no.msg ru.msg sv.msg -LIBKERN= ${.CURDIR}/../../sys/lib/libkern - -KSRCS= bcmp.c bzero.c ffs.c strcat.c strcmp.c strcpy.c strlen.c strncmp.c \ - strncpy.c htonl.c htons.c ntohl.c ntohs.c -.if (${MACHINE_ARCH} != "alpha") -KSRCS+= adddi3.c anddi3.c ashldi3.c ashrdi3.c cmpdi2.c divdi3.c iordi3.c \ - lshldi3.c lshrdi3.c moddi3.c muldi3.c negdi2.c notdi2.c qdivrem.c \ - subdi3.c ucmpdi2.c udivdi3.c umoddi3.c xordi3.c -KINCLUDES+= quad/quad.h -.endif - copy-to-libkern: copy-to-libkern-machind copy-to-libkern-machdep copy-to-libkern-machind: ${KSRCS} @@ -102,6 +55,6 @@ tags: ${SRCS} beforeinstall: ${INSTALL} ${INSTALL_COPY} -o ${BINOWN} -g ${BINGRP} -m 444 tags \ - ${DESTDIR}/var/db/libc.tags + ${DESTDIR}/var/db/lib${LIB}.tags .include <bsd.lib.mk> diff --git a/lib/libc/Makefile.inc b/lib/libc/Makefile.inc new file mode 100644 index 00000000000..0d224d8fcfc --- /dev/null +++ b/lib/libc/Makefile.inc @@ -0,0 +1,65 @@ +# $OpenBSD: Makefile.inc,v 1.1 1998/11/20 11:18:28 d Exp $ +# +# This file contains make rules that are shared by libc and libc_r. +# + +.ifndef LIBCSRCDIR +all: + @echo "need to define LIBCSRCDIR" >&2; exit 1 +.endif + +CFLAGS+= -DLIBC_SCCS -DSYSLIBC_SCCS -I${LIBCSRCDIR}/include + +.if (${YP} == "yes") +CFLAGS+=-DYP -I${LIBCSRCDIR}/yp +.endif + +LLIBS= +AINC= -I${LIBCSRCDIR}/arch/${MACHINE_ARCH} +.if defined(DESTDIR) +AINC+= -nostdinc -idirafter ${DESTDIR}/usr/include +.endif + +.if exists (${LIBCSRCDIR}/arch/${MACHINE_ARCH}/Makefile.inc) +.PATH: ${LIBCSRCDIR}/arch/${MACHINE_ARCH} +.include "${LIBCSRCDIR}/arch/${MACHINE_ARCH}/Makefile.inc" +.endif + +.include "${LIBCSRCDIR}/db/Makefile.inc" +.include "${LIBCSRCDIR}/compat-43/Makefile.inc" +.include "${LIBCSRCDIR}/gen/Makefile.inc" +.include "${LIBCSRCDIR}/crypt/Makefile.inc" +.include "${LIBCSRCDIR}/gmon/Makefile.inc" +.include "${LIBCSRCDIR}/hash/Makefile.inc" +.include "${LIBCSRCDIR}/locale/Makefile.inc" +.include "${LIBCSRCDIR}/md/Makefile.inc" +.include "${LIBCSRCDIR}/net/Makefile.inc" +.include "${LIBCSRCDIR}/nls/Makefile.inc" +.if (${MACHINE_ARCH} != "alpha") +.include "${LIBCSRCDIR}/quad/Makefile.inc" +.endif +.include "${LIBCSRCDIR}/regex/Makefile.inc" +.include "${LIBCSRCDIR}/rpc/Makefile.inc" +.include "${LIBCSRCDIR}/stdio/Makefile.inc" +.include "${LIBCSRCDIR}/stdlib/Makefile.inc" +.include "${LIBCSRCDIR}/string/Makefile.inc" +.include "${LIBCSRCDIR}/termios/Makefile.inc" +.include "${LIBCSRCDIR}/time/Makefile.inc" +.include "${LIBCSRCDIR}/sys/Makefile.inc" +.if (${YP} == "yes") +.include "${LIBCSRCDIR}/yp/Makefile.inc" +.endif + +CFLAGS+=-DNLS + +LIBKERN= ${LIBCSRCDIR}/../../sys/lib/libkern + +KSRCS= bcmp.c bzero.c ffs.c strcat.c strcmp.c strcpy.c strlen.c strncmp.c \ + strncpy.c htonl.c htons.c ntohl.c ntohs.c +.if (${MACHINE_ARCH} != "alpha") +KSRCS+= adddi3.c anddi3.c ashldi3.c ashrdi3.c cmpdi2.c divdi3.c iordi3.c \ + lshldi3.c lshrdi3.c moddi3.c muldi3.c negdi2.c notdi2.c qdivrem.c \ + subdi3.c ucmpdi2.c udivdi3.c umoddi3.c xordi3.c +KINCLUDES+= quad/quad.h +.endif + diff --git a/lib/libc/arch/i386/SYS.h b/lib/libc/arch/i386/SYS.h index 1cf818fd304..e02d5cf985f 100644 --- a/lib/libc/arch/i386/SYS.h +++ b/lib/libc/arch/i386/SYS.h @@ -33,24 +33,80 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $OpenBSD: SYS.h,v 1.3 1996/08/19 08:12:12 tholo Exp $ + * $OpenBSD: SYS.h,v 1.4 1998/11/20 11:18:29 d Exp $ */ #include <machine/asm.h> #include <sys/syscall.h> #ifdef __STDC__ +# define __ENTRY(p,x) ENTRY(p##x) +# define __DO_SYSCALL(x) \ + movl $(SYS_##x),%eax; \ + int $0x80 +# define __LABEL2(p,x) _C_LABEL(p##x) +#else +# define __ENTRY(p,x) ENTRY(p/**/x) +# define __DO_SYSCALL(x) \ + movl $(SYS_/**/x),%eax; \ + int $0x80 +# define __LABEL2(p,x) _C_LABEL(p/**/x) +#endif -#define SYSCALL(x) .text; .align 2; 2: jmp PIC_PLT(cerror); ENTRY(x); movl $(SYS_ ## x),%eax; int $0x80; jc 2b -#define RSYSCALL(x) SYSCALL(x); ret -#define PSEUDO(x,y) ENTRY(x); movl $(SYS_ ## y),%eax; int $0x80; ret +/* perform a syscall, set errno */ +#define __SYSCALL(p,x) \ + .text; \ + .align 2; \ + 2: \ + jmp PIC_PLT(cerror); \ + __ENTRY(p,x); \ + __DO_SYSCALL(x); \ + jc 2b -#else /* !__STDC__ */ +/* perform a syscall, set errno, return */ +# define __RSYSCALL(p,x) __SYSCALL(p,x); ret -#define SYSCALL(x) .text; .align 2; 2: jmp PIC_PLT(cerror); ENTRY(x); movl $(SYS_/**/x),%eax; int $0x80; jc 2b -#define RSYSCALL(x) SYSCALL(x); ret -#define PSEUDO(x,y) ENTRY(x); movl $(SYS_/**/y),%eax; int $0x80; ret +/* perform a syscall, return */ +# define __PSEUDO(p,x,y) \ + __ENTRY(p,x); \ + __DO_SYSCALL(y); \ + ret -#endif +/* jump to the real syscall */ +/* XXX shouldn't be here */ +# define __PASSTHRU(p,x) \ + .globl __LABEL2(p,x); \ + ENTRY(x); \ + jmp PIC_PLT(__LABEL2(p,x)) +/* + * Design note: + * + * When the syscalls need to be renamed so they can be handled + * specially by the threaded library, these macros insert `_thread_sys_' + * in front of their name. This avoids the need to #ifdef _THREAD_SAFE + * everywhere that the renamed function needs to be called. + * The PASSTHRU macro is later used for system calls that don't need + * wrapping. (XXX its a shame the loader can't do this aliasing) + */ +#ifdef _THREAD_SAFE +/* + * For the thread_safe versions, we prepend _thread_sys_ to the function + * name so that the 'C' wrapper can go around the real name. + */ +# define SYSCALL(x) __SYSCALL(_thread_sys_,x) +# define RSYSCALL(x) __RSYSCALL(_thread_sys_,x) +# define PSEUDO(x,y) __PSEUDO(_thread_sys_,x,y) +# define SYSENTRY(x) __ENTRY(_thread_sys_,x) +# define PASSTHRU(x) __PASSTHRU(_thread_sys_,x) +#else _THREAD_SAFE +/* + * The non-threaded library defaults to traditional syscalls where + * the function name matches the syscall name. + */ +# define SYSCALL(x) __SYSCALL(,x) +# define RSYSCALL(x) __RSYSCALL(,x) +# define PSEUDO(x,y) __PSEUDO(,x,y) +# define SYSENTRY(x) __ENTRY(,x) +#endif _THREAD_SAFE .globl cerror diff --git a/lib/libc/arch/i386/sys/Ovfork.S b/lib/libc/arch/i386/sys/Ovfork.S index 97430e6ae5f..264e09e302f 100644 --- a/lib/libc/arch/i386/sys/Ovfork.S +++ b/lib/libc/arch/i386/sys/Ovfork.S @@ -38,7 +38,7 @@ #if defined(SYSLIBC_SCCS) .text - .asciz "$OpenBSD: Ovfork.S,v 1.2 1996/08/19 08:13:26 tholo Exp $" + .asciz "$OpenBSD: Ovfork.S,v 1.3 1998/11/20 11:18:29 d Exp $" #endif /* SYSLIB_SCCS */ /* @@ -48,7 +48,7 @@ * %eax == pid of child in parent, %eax == pid of parent in child. * */ -ENTRY(vfork) +SYSENTRY(vfork) popl %ecx /* my rta into ecx */ movl $(SYS_vfork),%eax int $0x80 diff --git a/lib/libc/arch/i386/sys/sigprocmask.S b/lib/libc/arch/i386/sys/sigprocmask.S index 17853ed793d..ee3d19b7d87 100644 --- a/lib/libc/arch/i386/sys/sigprocmask.S +++ b/lib/libc/arch/i386/sys/sigprocmask.S @@ -38,10 +38,10 @@ #if defined(SYSLIBC_SCCS) .text - .asciz "$OpenBSD: sigprocmask.S,v 1.3 1997/07/23 20:55:23 kstailey Exp $" + .asciz "$OpenBSD: sigprocmask.S,v 1.4 1998/11/20 11:18:30 d Exp $" #endif /* SYSLIBC_SCCS */ -ENTRY(sigprocmask) +SYSENTRY(sigprocmask) movl 8(%esp),%ecx # fetch new sigset pointer testl %ecx,%ecx # check new sigset pointer jnz 1f # if not null, indirect diff --git a/lib/libc/arch/i386/sys/sigsuspend.S b/lib/libc/arch/i386/sys/sigsuspend.S index c0254cfebbf..f0c84ce734c 100644 --- a/lib/libc/arch/i386/sys/sigsuspend.S +++ b/lib/libc/arch/i386/sys/sigsuspend.S @@ -38,10 +38,10 @@ #if defined(SYSLIBC_SCCS) .text - .asciz "$OpenBSD: sigsuspend.S,v 1.2 1996/08/19 08:13:44 tholo Exp $" + .asciz "$OpenBSD: sigsuspend.S,v 1.3 1998/11/20 11:18:30 d Exp $" #endif /* SYSLIBC_SCCS */ -ENTRY(sigsuspend) +SYSENTRY(sigsuspend) movl 4(%esp),%eax # fetch mask arg movl (%eax),%eax # indirect to mask arg movl %eax,4(%esp) diff --git a/lib/libc/arch/i386/sys/syscall.S b/lib/libc/arch/i386/sys/syscall.S index 4a6db63c799..6ecd61cd4ed 100644 --- a/lib/libc/arch/i386/sys/syscall.S +++ b/lib/libc/arch/i386/sys/syscall.S @@ -38,10 +38,10 @@ #if defined(SYSLIBC_SCCS) .text - .asciz "$OpenBSD: syscall.S,v 1.2 1996/08/19 08:13:46 tholo Exp $" + .asciz "$OpenBSD: syscall.S,v 1.3 1998/11/20 11:18:30 d Exp $" #endif /* SYSLIBC_SCCS */ -ENTRY(syscall) +SYSENTRY(syscall) pop %ecx /* rta */ pop %eax /* syscall number */ push %ecx diff --git a/lib/libc/compat-43/Makefile.inc b/lib/libc/compat-43/Makefile.inc index e59a1b9fb2c..373e7f4ca4b 100644 --- a/lib/libc/compat-43/Makefile.inc +++ b/lib/libc/compat-43/Makefile.inc @@ -1,7 +1,7 @@ -# $OpenBSD: Makefile.inc,v 1.2 1996/08/19 08:19:13 tholo Exp $ +# $OpenBSD: Makefile.inc,v 1.3 1998/11/20 11:18:32 d Exp $ # compat-43 sources -.PATH: ${.CURDIR}/arch/${MACHINE_ARCH}/compat-43 ${.CURDIR}/compat-43 +.PATH: ${LIBCSRCDIR}/arch/${MACHINE_ARCH}/compat-43 ${LIBCSRCDIR}/compat-43 SRCS+= __setreuid.c __setregid.c creat.c getdtablesize.c gethostid.c \ getwd.c killpg.c sethostid.c setpgrp.c setregid.c setreuid.c \ diff --git a/lib/libc/crypt/Makefile.inc b/lib/libc/crypt/Makefile.inc index 9d96d657dbe..8364da1e6e8 100644 --- a/lib/libc/crypt/Makefile.inc +++ b/lib/libc/crypt/Makefile.inc @@ -1,6 +1,6 @@ -# $OpenBSD: Makefile.inc,v 1.10 1998/07/21 22:23:20 provos Exp $ +# $OpenBSD: Makefile.inc,v 1.11 1998/11/20 11:18:33 d Exp $ -.PATH: ${.CURDIR}/arch/${MACHINE_ARCH}/crypt ${.CURDIR}/crypt +.PATH: ${LIBCSRCDIR}/arch/${MACHINE_ARCH}/crypt ${LIBCSRCDIR}/crypt SRCS+= cast.c crypt.c morecrypt.c md5crypt.c arc4random.c blowfish.c SRCS+= bcrypt.c diff --git a/lib/libc/db/Makefile.inc b/lib/libc/db/Makefile.inc index 44b5a0b1ee6..9f9a35830d4 100644 --- a/lib/libc/db/Makefile.inc +++ b/lib/libc/db/Makefile.inc @@ -1,10 +1,10 @@ -# $OpenBSD: Makefile.inc,v 1.2 1996/08/19 08:19:54 tholo Exp $ +# $OpenBSD: Makefile.inc,v 1.3 1998/11/20 11:18:33 d Exp $ # CFLAGS+=-D__DBINTERFACE_PRIVATE -.include "${.CURDIR}/db/btree/Makefile.inc" -.include "${.CURDIR}/db/db/Makefile.inc" -.include "${.CURDIR}/db/hash/Makefile.inc" -.include "${.CURDIR}/db/man/Makefile.inc" -.include "${.CURDIR}/db/mpool/Makefile.inc" -.include "${.CURDIR}/db/recno/Makefile.inc" +.include "${LIBCSRCDIR}/db/btree/Makefile.inc" +.include "${LIBCSRCDIR}/db/db/Makefile.inc" +.include "${LIBCSRCDIR}/db/hash/Makefile.inc" +.include "${LIBCSRCDIR}/db/man/Makefile.inc" +.include "${LIBCSRCDIR}/db/mpool/Makefile.inc" +.include "${LIBCSRCDIR}/db/recno/Makefile.inc" diff --git a/lib/libc/db/btree/Makefile.inc b/lib/libc/db/btree/Makefile.inc index c882f6f7932..ac2f83a919e 100644 --- a/lib/libc/db/btree/Makefile.inc +++ b/lib/libc/db/btree/Makefile.inc @@ -1,6 +1,6 @@ -# $OpenBSD: Makefile.inc,v 1.3 1996/08/19 08:20:00 tholo Exp $ +# $OpenBSD: Makefile.inc,v 1.4 1998/11/20 11:18:34 d Exp $ -.PATH: ${.CURDIR}/db/btree +.PATH: ${LIBCSRCDIR}/db/btree SRCS+= bt_close.c bt_conv.c bt_debug.c bt_delete.c bt_get.c bt_open.c \ bt_overflow.c bt_page.c bt_put.c bt_search.c bt_seq.c bt_split.c \ diff --git a/lib/libc/db/db/Makefile.inc b/lib/libc/db/db/Makefile.inc index c671521a627..d93f931be9b 100644 --- a/lib/libc/db/db/Makefile.inc +++ b/lib/libc/db/db/Makefile.inc @@ -1,5 +1,5 @@ -# $OpenBSD: Makefile.inc,v 1.2 1996/08/19 08:20:23 tholo Exp $ +# $OpenBSD: Makefile.inc,v 1.3 1998/11/20 11:18:34 d Exp $ -.PATH: ${.CURDIR}/db/db +.PATH: ${LIBCSRCDIR}/db/db SRCS+= db.c diff --git a/lib/libc/db/hash/Makefile.inc b/lib/libc/db/hash/Makefile.inc index a25fd9d0c9b..8022fe41e68 100644 --- a/lib/libc/db/hash/Makefile.inc +++ b/lib/libc/db/hash/Makefile.inc @@ -1,6 +1,6 @@ -# $OpenBSD: Makefile.inc,v 1.3 1996/08/19 08:20:27 tholo Exp $ +# $OpenBSD: Makefile.inc,v 1.4 1998/11/20 11:18:35 d Exp $ -.PATH: ${.CURDIR}/db/hash +.PATH: ${LIBCSRCDIR}/db/hash SRCS+= hash.c hash_bigkey.c hash_buf.c hash_func.c hash_log2.c \ hash_page.c hsearch.c ndbm.c diff --git a/lib/libc/db/man/Makefile.inc b/lib/libc/db/man/Makefile.inc index beea08edc40..8683e93b17b 100644 --- a/lib/libc/db/man/Makefile.inc +++ b/lib/libc/db/man/Makefile.inc @@ -1,6 +1,6 @@ -# $OpenBSD: Makefile.inc,v 1.6 1998/07/23 07:18:01 deraadt Exp $ +# $OpenBSD: Makefile.inc,v 1.7 1998/11/20 11:18:35 d Exp $ -.PATH: ${.CURDIR}/db/man +.PATH: ${LIBCSRCDIR}/db/man MAN+= btree.3 dbopen.3 hash.3 recno.3 ndbm.3 mpool.3 MLINKS+= dbopen.3 db.3 diff --git a/lib/libc/db/mpool/Makefile.inc b/lib/libc/db/mpool/Makefile.inc index 34cc816eef7..2e8eaf54761 100644 --- a/lib/libc/db/mpool/Makefile.inc +++ b/lib/libc/db/mpool/Makefile.inc @@ -1,5 +1,5 @@ -# $OpenBSD: Makefile.inc,v 1.2 1996/08/19 08:20:49 tholo Exp $ +# $OpenBSD: Makefile.inc,v 1.3 1998/11/20 11:18:36 d Exp $ -.PATH: ${.CURDIR}/db/mpool +.PATH: ${LIBCSRCDIR}/db/mpool SRCS+= mpool.c diff --git a/lib/libc/db/recno/Makefile.inc b/lib/libc/db/recno/Makefile.inc index a6fa64b5653..e9976e019d5 100644 --- a/lib/libc/db/recno/Makefile.inc +++ b/lib/libc/db/recno/Makefile.inc @@ -1,6 +1,6 @@ -# $OpenBSD: Makefile.inc,v 1.3 1996/08/19 08:20:54 tholo Exp $ +# $OpenBSD: Makefile.inc,v 1.4 1998/11/20 11:18:36 d Exp $ -.PATH: ${.CURDIR}/db/recno +.PATH: ${LIBCSRCDIR}/db/recno SRCS+= rec_close.c rec_delete.c rec_get.c rec_open.c rec_put.c rec_search.c \ rec_seq.c rec_utils.c diff --git a/lib/libc/gen/Makefile.inc b/lib/libc/gen/Makefile.inc index da3a40534c5..3ca692f95a8 100644 --- a/lib/libc/gen/Makefile.inc +++ b/lib/libc/gen/Makefile.inc @@ -1,7 +1,7 @@ -# $OpenBSD: Makefile.inc,v 1.18 1998/08/29 21:11:21 deraadt Exp $ +# $OpenBSD: Makefile.inc,v 1.19 1998/11/20 11:18:37 d Exp $ # gen sources -.PATH: ${.CURDIR}/arch/${MACHINE_ARCH}/gen ${.CURDIR}/gen +.PATH: ${LIBCSRCDIR}/arch/${MACHINE_ARCH}/gen ${LIBCSRCDIR}/gen SRCS+= alarm.c assert.c basename.c clock.c closedir.c confstr.c ctermid.c \ ctype_.c daemon.c devname.c dirname.c disklabel.c elf_hash.c err.c \ @@ -28,14 +28,14 @@ SRCS+= _err.c _errx.c _sys_errlist.c _sys_nerr.c _sys_siglist.c \ # _setjmp() fabs() frexp() infinity isinf() ldexp() modf() # setjmp() sigsetjmp() -.include "${.CURDIR}/arch/${MACHINE_ARCH}/gen/Makefile.inc" +.include "${LIBCSRCDIR}/arch/${MACHINE_ARCH}/gen/Makefile.inc" .ifdef _NOTDEF_XXX_ .if (${MACHINE} == "tahoe" ) errlst.o errlst.po: ${CC} -S ${CFLAGS} ${.IMPSRC} - ed - < ${.CURDIR}/${MACHINE}/:errfix errlst.s + ed - < ${LIBCSRCDIR}/${MACHINE}/:errfix errlst.s ${AS} -o ${.TARGET} errlst.s rm -f errlst.s .endif diff --git a/lib/libc/arch/i386/sys/setlogin.S b/lib/libc/gen/_spinlock_stub.c index acd1144725d..5d95a8aadd5 100644 --- a/lib/libc/arch/i386/sys/setlogin.S +++ b/lib/libc/gen/_spinlock_stub.c @@ -1,10 +1,7 @@ -/*- - * Copyright (c) 1991 The Regents of the University of California. +/* + * Copyright (c) 1998 John Birrell <jb@cimlogic.com.au>. * All rights reserved. * - * This code is derived from software contributed to Berkeley by - * William Jolitz. - * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: @@ -15,13 +12,12 @@ * 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 + * This product includes software developed by John Birrell. + * 4. Neither the name of the author nor the names of any co-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 + * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL 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 @@ -32,24 +28,26 @@ * 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. + * + * $Id: _spinlock_stub.c,v 1.1 1998/11/20 11:18:37 d Exp $ + * $OpenBSD: _spinlock_stub.c,v 1.1 1998/11/20 11:18:37 d Exp $ + * */ -#include "SYS.h" +#include <stdio.h> +#include <thread_private.h> + +/* Don't build these stubs into libc_r: */ +#ifndef _THREAD_SAFE +#include "spinlock.h" -#if defined(SYSLIBC_SCCS) - .text - .asciz "$OpenBSD: setlogin.S,v 1.2 1996/08/19 08:13:39 tholo Exp $" -#endif /* SYSLIB_SCCS */ +void +_spinlock(spinlock_t *lck) +{ +} - .globl ___logname_valid /* in getlogin() */ -SYSCALL(setlogin) - xorl %eax,%eax -#ifdef PIC - PIC_PROLOGUE - movl PIC_GOT(___logname_valid),%edx - PIC_EPILOGUE - movl %eax,(%edx) -#else - movl %eax,___logname_valid +void +_spinlock_debug(spinlock_t *lck, char *fname, int lineno) +{ +} #endif - ret /* setlogin(name) */ diff --git a/lib/libc/gen/closedir.c b/lib/libc/gen/closedir.c index 21a8375a8a5..11193a12e8a 100644 --- a/lib/libc/gen/closedir.c +++ b/lib/libc/gen/closedir.c @@ -32,13 +32,14 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char rcsid[] = "$OpenBSD: closedir.c,v 1.2 1996/08/19 08:21:55 tholo Exp $"; +static char rcsid[] = "$OpenBSD: closedir.c,v 1.3 1998/11/20 11:18:37 d Exp $"; #endif /* LIBC_SCCS and not lint */ #include <sys/types.h> #include <dirent.h> #include <stdlib.h> #include <unistd.h> +#include "thread_private.h" /* * close a directory. @@ -48,12 +49,17 @@ closedir(dirp) register DIR *dirp; { int fd; + int ret; + if ((ret = _FD_LOCK(dirp->dd_fd, FD_READ, NULL)) != 0) + return (ret); seekdir(dirp, dirp->dd_rewind); /* free seekdir storage */ fd = dirp->dd_fd; dirp->dd_fd = -1; dirp->dd_loc = 0; free((void *)dirp->dd_buf); free((void *)dirp); - return(close(fd)); + ret = close(fd); + _FD_UNLOCK(fd, FD_READ); + return (ret); } diff --git a/lib/libc/gen/ctermid.c b/lib/libc/gen/ctermid.c index 9bb0a594880..8b4d46d1f1c 100644 --- a/lib/libc/gen/ctermid.c +++ b/lib/libc/gen/ctermid.c @@ -32,7 +32,7 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char rcsid[] = "$OpenBSD: ctermid.c,v 1.2 1996/08/19 08:22:03 tholo Exp $"; +static char rcsid[] = "$OpenBSD: ctermid.c,v 1.3 1998/11/20 11:18:37 d Exp $"; #endif /* LIBC_SCCS and not lint */ #include <stdio.h> @@ -51,3 +51,13 @@ ctermid(s) } return(def); } + +char * +ctermid_r(s) + char *s; +{ + if (s) + return ctermid(s); + else + return(NULL); +} diff --git a/lib/libc/gen/getgrent.c b/lib/libc/gen/getgrent.c index df1f2a6d433..3660df72953 100644 --- a/lib/libc/gen/getgrent.c +++ b/lib/libc/gen/getgrent.c @@ -33,7 +33,7 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char rcsid[] = "$OpenBSD: getgrent.c,v 1.8 1997/12/19 09:42:22 deraadt Exp $"; +static char rcsid[] = "$OpenBSD: getgrent.c,v 1.9 1998/11/20 11:18:37 d Exp $"; #endif /* LIBC_SCCS and not lint */ #include <sys/types.h> @@ -48,12 +48,16 @@ static char rcsid[] = "$OpenBSD: getgrent.c,v 1.8 1997/12/19 09:42:22 deraadt Ex #include <rpcsvc/ypclnt.h> #include "ypinternal.h" #endif +#include "thread_private.h" +_THREAD_PRIVATE_KEY(gr) +_THREAD_PRIVATE_MUTEX(gr) static FILE *_gr_fp; static struct group _gr_group; static int _gr_stayopen; -static int grscan __P((int, gid_t, const char *)); +static int grscan __P((int, gid_t, const char *, struct group *)); static int start_gr __P((void)); +static void endgrent_basic __P((void)); #define MAXGRP 200 static char *members[MAXGRP]; @@ -68,43 +72,78 @@ static int __ypcurrentlen; #endif struct group * +getgrent_r(p_gr) +struct group * p_gr; +{ + _THREAD_PRIVATE_MUTEX_LOCK(gr); + if ((!_gr_fp && !start_gr()) || !grscan(0, 0, NULL, p_gr)) + p_gr = NULL; + _THREAD_PRIVATE_MUTEX_UNLOCK(gr); + return (p_gr); +} + +struct group * getgrent() { - if ((!_gr_fp && !start_gr()) || !grscan(0, 0, NULL)) - return(NULL); - return(&_gr_group); + struct group *p_gr = (struct group*)_THREAD_PRIVATE(gr,_gr_group,NULL); + + return getgrent_r(p_gr); } struct group * -getgrnam(name) +getgrnam_r(name, p_gr) const char *name; + struct group *p_gr; { int rval; + _THREAD_PRIVATE_MUTEX_LOCK(gr); if (!start_gr()) - return(NULL); - rval = grscan(1, 0, name); - if (!_gr_stayopen) - endgrent(); - return(rval ? &_gr_group : NULL); + rval = 0; + else { + rval = grscan(1, 0, name, p_gr); + if (!_gr_stayopen) + endgrent_basic(); + } + _THREAD_PRIVATE_MUTEX_UNLOCK(gr); + return(rval ? p_gr : NULL); } struct group * -#ifdef __STDC__ -getgrgid(gid_t gid) -#else -getgrgid(gid) +getgrnam(name) + const char *name; +{ + struct group *p_gr = (struct group*)_THREAD_PRIVATE(gr,_gr_group,NULL); + + return getgrnam_r(name, p_gr); +} + +struct group * +getgrgid_r(gid, p_gr) gid_t gid; -#endif + struct group * p_gr; { int rval; + _THREAD_PRIVATE_MUTEX_LOCK(gr); if (!start_gr()) - return(NULL); - rval = grscan(1, gid, NULL); - if (!_gr_stayopen) - endgrent(); - return(rval ? &_gr_group : NULL); + rval = 0; + else { + rval = grscan(1, gid, NULL, p_gr); + if (!_gr_stayopen) + endgrent_basic(); + } + _THREAD_PRIVATE_MUTEX_UNLOCK(gr); + return(rval ? p_gr : NULL); +} + +struct group * +getgrgid(gid) + gid_t gid; +{ + struct group *p_gr = (struct group*)_THREAD_PRIVATE(gr,_gr_group,NULL); + + return getgrgid_r(gid, p_gr); } static int @@ -133,14 +172,22 @@ int setgroupent(stayopen) int stayopen; { + int retval; + + _THREAD_PRIVATE_MUTEX_LOCK(gr); if (!start_gr()) - return(0); - _gr_stayopen = stayopen; - return(1); + retval = 0; + else { + _gr_stayopen = stayopen; + retval = 1; + } + _THREAD_PRIVATE_MUTEX_LOCK(gr); + return (retval); } +static void -endgrent() +endgrent_basic() { if (_gr_fp) { (void)fclose(_gr_fp); @@ -154,11 +201,20 @@ endgrent() } } +void +endgrent() +{ + _THREAD_PRIVATE_MUTEX_LOCK(gr); + endgrent_basic(); + _THREAD_PRIVATE_MUTEX_UNLOCK(gr); +} + static int -grscan(search, gid, name) +grscan(search, gid, name, p_gr) register int search; register gid_t gid; register const char *name; + struct group * p_gr; { register char *cp, **m; char *bp; @@ -282,12 +338,12 @@ grscan(search, gid, name) free(data); line[datalen] = '\0'; bp = line; - _gr_group.gr_name = strsep(&bp, ":\n"); - _gr_group.gr_passwd = + p_gr->gr_name = strsep(&bp, ":\n"); + p_gr->gr_passwd = strsep(&bp, ":\n"); if (!(cp = strsep(&bp, ":\n"))) continue; - _gr_group.gr_gid = + p_gr->gr_gid = name ? atoi(cp) : gid; goto found_it; } @@ -308,20 +364,20 @@ grscan(search, gid, name) } parse: #endif - _gr_group.gr_name = strsep(&bp, ":\n"); - if (search && name && strcmp(_gr_group.gr_name, name)) + p_gr->gr_name = strsep(&bp, ":\n"); + if (search && name && strcmp(p_gr->gr_name, name)) continue; - _gr_group.gr_passwd = strsep(&bp, ":\n"); + p_gr->gr_passwd = strsep(&bp, ":\n"); if (!(cp = strsep(&bp, ":\n"))) continue; - _gr_group.gr_gid = atoi(cp); - if (search && name == NULL && _gr_group.gr_gid != gid) + p_gr->gr_gid = atoi(cp); + if (search && name == NULL && p_gr->gr_gid != gid) continue; found_it: cp = NULL; if (bp == NULL) continue; - for (m = _gr_group.gr_mem = members;; bp++) { + for (m = p_gr->gr_mem = members;; bp++) { if (m == &members[MAXGRP - 1]) break; if (*bp == ',') { diff --git a/lib/libc/gen/getlogin.c b/lib/libc/gen/getlogin.c index abf13131da3..b532c406246 100644 --- a/lib/libc/gen/getlogin.c +++ b/lib/libc/gen/getlogin.c @@ -32,7 +32,7 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char rcsid[] = "$OpenBSD: getlogin.c,v 1.3 1997/07/09 00:28:21 millert Exp $"; +static char rcsid[] = "$OpenBSD: getlogin.c,v 1.4 1998/11/20 11:18:39 d Exp $"; #endif /* LIBC_SCCS and not lint */ #include <sys/param.h> @@ -41,19 +41,68 @@ static char rcsid[] = "$OpenBSD: getlogin.c,v 1.3 1997/07/09 00:28:21 millert Ex #include <stdio.h> #include <string.h> #include <unistd.h> +#include <errno.h> +#include "thread_private.h" + +_THREAD_PRIVATE_MUTEX(logname) +static int logname_valid = 0; +static char logname[MAXLOGNAME + 1]; -int __logname_valid; /* known to setlogin() */ int _getlogin __P((char *, size_t)); +int _setlogin __P((const char *)); char * getlogin() { - static char logname[MAXLOGNAME + 1]; + _THREAD_PRIVATE_KEY(getlogin) + char * name = (char *)_THREAD_PRIVATE(getlogin, logname, NULL); - if (__logname_valid == 0) { - if (_getlogin(logname, sizeof(logname) - 1) < 0) - return ((char *)NULL); - __logname_valid = 1; + if ((errno = getlogin_r(name, sizeof logname)) != 0) + return NULL; + if (*name == '\0') { + errno = ENOENT; /* well? */ + return NULL; } - return (*logname ? logname : (char *)NULL); + return name; +} + +int +getlogin_r(name, namelen) + char *name; + size_t namelen; +{ + int logname_size; + + if (name == NULL) + return EFAULT; + + _THREAD_PRIVATE_MUTEX_LOCK(logname); + if (!logname_valid) { + if (_getlogin(logname, sizeof(logname) - 1) < 0) { + _THREAD_PRIVATE_MUTEX_UNLOCK(logname); + return errno; + } + logname_valid = 1; + logname[MAXLOGNAME] = '\0'; /* paranoia */ + } + logname_size = strlen(logname) + 1; + if (namelen < logname_size) + return ERANGE; + memcpy(name, logname, logname_size); + _THREAD_PRIVATE_MUTEX_UNLOCK(logname); + return 0; +} + +int +setlogin(name) + const char *name; +{ + int ret; + + _THREAD_PRIVATE_MUTEX_LOCK(logname); + ret = _setlogin(name); + if (ret == 0) + logname_valid = 0; + _THREAD_PRIVATE_MUTEX_UNLOCK(logname); + return ret; } diff --git a/lib/libc/gen/isatty.c b/lib/libc/gen/isatty.c index d4dc9ad6cf6..f32cf3eaeb5 100644 --- a/lib/libc/gen/isatty.c +++ b/lib/libc/gen/isatty.c @@ -32,17 +32,25 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char rcsid[] = "$OpenBSD: isatty.c,v 1.2 1996/08/19 08:24:32 tholo Exp $"; +static char rcsid[] = "$OpenBSD: isatty.c,v 1.3 1998/11/20 11:18:39 d Exp $"; #endif /* LIBC_SCCS and not lint */ #include <termios.h> #include <unistd.h> +#include "thread_private.h" int isatty(fd) int fd; { + int retval; struct termios t; - return(tcgetattr(fd, &t) != -1); + if (_FD_LOCK(fd, FD_READ, NULL) == 0) { + retval = (tcgetattr(fd, &t) != -1); + _FD_UNLOCK(fd, FD_READ); + } else { + retval = 0; + } + return(retval); } diff --git a/lib/libc/gen/readdir.c b/lib/libc/gen/readdir.c index d73ebcd2843..3d3b2a2012b 100644 --- a/lib/libc/gen/readdir.c +++ b/lib/libc/gen/readdir.c @@ -32,11 +32,13 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char rcsid[] = "$OpenBSD: readdir.c,v 1.2 1996/08/19 08:25:35 tholo Exp $"; +static char rcsid[] = "$OpenBSD: readdir.c,v 1.3 1998/11/20 11:18:39 d Exp $"; #endif /* LIBC_SCCS and not lint */ #include <sys/param.h> #include <dirent.h> +#include <errno.h> +#include "thread_private.h" /* * get next entry in a directory. @@ -73,3 +75,33 @@ readdir(dirp) return (dp); } } + +int +readdir_r(dirp, entry, result) + 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); + if (dp == NULL && errno != 0) { + _FD_UNLOCK(dirp->dd_fd, FD_READ); + return errno; + } + if (dp != NULL) + memcpy(entry, dp, sizeof *entry); + _FD_UNLOCK(dirp->dd_fd, FD_READ); + if (dp != NULL) + *result = entry; + else + *result = NULL; + return 0; +} diff --git a/lib/libc/gen/ttyname.3 b/lib/libc/gen/ttyname.3 index 76830b638d2..728bc1509e3 100644 --- a/lib/libc/gen/ttyname.3 +++ b/lib/libc/gen/ttyname.3 @@ -1,4 +1,4 @@ -.\" $OpenBSD: ttyname.3,v 1.4 1998/06/08 17:21:45 deraadt Exp $ +.\" $OpenBSD: ttyname.3,v 1.5 1998/11/20 11:18:40 d Exp $ .\" .\" Copyright (c) 1991, 1993 .\" The Regents of the University of California. All rights reserved. @@ -36,6 +36,7 @@ .Os .Sh NAME .Nm ttyname , +.Nm ttyname_r , .Nm isatty , .Nm ttyslot .Nd get name of associated terminal (tty) from file descriptor @@ -44,6 +45,8 @@ .Ft char * .Fn ttyname "int fd" .Ft int +.Fn ttyname_r "int fd" "char *name" "size_t namesize" +.Ft int .Fn isatty "int fd" .Ft int .Fn ttyslot "void" @@ -72,11 +75,25 @@ terminal type device. .Pp The .Fn ttyname -function -gets the related device name of +and +.Fn ttyname_r +functions +get the related device name of a file descriptor for which .Fn isatty -is true +is true. +The +.Fn ttyname_r +function stores the nul-terminated pathname of the terminal associated with +the file descriptor +.Fa fd +in the character array referenced by +.Fa name . +The array is +.Fa namesize +characters long and should have space for the name and the terminating nul +character. The maximum length of the terminal name is +.Dv TTY_NAME_MAX . .Pp The .Fn ttyslot @@ -87,8 +104,10 @@ file entry. .Sh RETURN VALUES The .Fn ttyname -function -returns the null terminated name if the device is found and +and +.Fn ttyname_r +functions +return the nul terminated name if the device is found and .Fn isatty is true; otherwise a @@ -125,6 +144,11 @@ argument is not a valid file descriptor. The .Fa fd argument does not refer to a terminal device. +.It Bq Er ERANGE +The value of +.Fa namesize +is smaller than the length of the string to be returned including the +terminating nul character. .El .Sh FILES .Bl -tag -width /etc/ttys -compact @@ -133,7 +157,8 @@ argument does not refer to a terminal device. .El .Sh SEE ALSO .Xr ioctl 2 , -.Xr ttys 5 +.Xr ttys 5 , +.Xr dev_mkdb 8 .Sh HISTORY A .Fn isatty , @@ -143,6 +168,9 @@ and function appeared in .At v7 . +The +.Fn ttyname_r +function appeared in the POSIX Threads Extension (1003.1c-1995). .Sh BUGS The .Fn ttyname diff --git a/lib/libc/gen/ttyname.c b/lib/libc/gen/ttyname.c index 192072a208f..a18b68fdd2c 100644 --- a/lib/libc/gen/ttyname.c +++ b/lib/libc/gen/ttyname.c @@ -32,7 +32,7 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char rcsid[] = "$OpenBSD: ttyname.c,v 1.5 1998/08/27 06:42:16 deraadt Exp $"; +static char rcsid[] = "$OpenBSD: ttyname.c,v 1.6 1998/11/20 11:18:40 d Exp $"; #endif /* LIBC_SCCS and not lint */ #include <sys/types.h> @@ -44,13 +44,49 @@ static char rcsid[] = "$OpenBSD: ttyname.c,v 1.5 1998/08/27 06:42:16 deraadt Exp #include <string.h> #include <unistd.h> #include <paths.h> +#include <limits.h> +#include <errno.h> +#include "thread_private.h" -static char buf[sizeof(_PATH_DEV) + MAXNAMLEN]; -static char *oldttyname __P((int, struct stat *)); +static char buf[TTY_NAME_MAX]; +static int oldttyname __P((int, struct stat *, char *, size_t)); +static int __ttyname_r_basic __P((int, char *, size_t)); + +int +ttyname_r(int fd, char *buf, size_t buflen) +{ + int ret; + + if ((ret = _FD_LOCK(fd, FD_READ, NULL)) == 0) { + ret = __ttyname_r_basic(fd, buf, buflen); + _FD_UNLOCK(fd, FD_READ); + } + return ret; +} char * -ttyname(fd) +ttyname(int fd) +{ + _THREAD_PRIVATE_KEY(ttyname) + char * bufp = (char*) _THREAD_PRIVATE(ttyname, buf, NULL); + int err; + + if (bufp == NULL) + return NULL; + err = ttyname_r(fd, bufp, sizeof buf); + if (err) { + errno = err; + return NULL; + } + else + return bufp; +} + +static int +__ttyname_r_basic(fd, buf, len) int fd; + char *buf; + size_t len; { struct stat sb; struct termios ttyb; @@ -63,10 +99,14 @@ ttyname(fd) /* Must be a terminal. */ if (tcgetattr(fd, &ttyb) < 0) - return (NULL); + return (errno); /* Must be a character device. */ - if (fstat(fd, &sb) || !S_ISCHR(sb.st_mode)) - return (NULL); + if (fstat(fd, &sb)) + return (errno); + if (!S_ISCHR(sb.st_mode)) + return (ENOTTY); + if (len < sizeof(_PATH_DEV)) + return (ERANGE); memcpy(buf, _PATH_DEV, sizeof(_PATH_DEV)); @@ -77,40 +117,50 @@ ttyname(fd) key.data = &bkey; key.size = sizeof(bkey); if (!(db->get)(db, &key, &data, 0)) { + if (data.size > len - (sizeof(_PATH_DEV) - 1)) { + (void)(db->close)(db); + return (ERANGE); + } memcpy(buf + sizeof(_PATH_DEV) - 1, data.data, data.size); (void)(db->close)(db); - return (buf); + return (0); } (void)(db->close)(db); } - return (oldttyname(fd, &sb)); + return (oldttyname(fd, &sb, buf, len)); } /* ARGSUSED */ -static char * -oldttyname(fd, sb) +static int +oldttyname(fd, sb, buf, len) int fd; struct stat *sb; + char *buf; + size_t len; { register struct dirent *dirp; register DIR *dp; struct stat dsb; if ((dp = opendir(_PATH_DEV)) == NULL) - return (NULL); + return (errno); while ((dirp = readdir(dp))) { if (dirp->d_fileno != sb->st_ino) continue; + if (dirp->d_namlen > len - sizeof(_PATH_DEV)) { + (void)closedir(dp); + return (ERANGE); + } memcpy(buf + sizeof(_PATH_DEV) - 1, dirp->d_name, dirp->d_namlen + 1); if (stat(buf, &dsb) || sb->st_dev != dsb.st_dev || sb->st_ino != dsb.st_ino) continue; (void)closedir(dp); - return (buf); + return (0); } (void)closedir(dp); - return (NULL); + return (ENOTTY); } diff --git a/lib/libc/gmon/Makefile.inc b/lib/libc/gmon/Makefile.inc index 3c4d53b572f..f7c3e1ff6db 100644 --- a/lib/libc/gmon/Makefile.inc +++ b/lib/libc/gmon/Makefile.inc @@ -1,7 +1,7 @@ -# $OpenBSD: Makefile.inc,v 1.3 1998/08/29 21:11:38 deraadt Exp $ +# $OpenBSD: Makefile.inc,v 1.4 1998/11/20 11:18:40 d Exp $ # gmon sources -.PATH: ${.CURDIR}/gmon +.PATH: ${LIBCSRCDIR}/gmon SRCS+= gmon.c mcount.c MAN+= moncontrol.3 diff --git a/lib/libc/hash/Makefile.inc b/lib/libc/hash/Makefile.inc index d198805fbbf..e40edc01b8d 100644 --- a/lib/libc/hash/Makefile.inc +++ b/lib/libc/hash/Makefile.inc @@ -1,7 +1,7 @@ -# $OpenBSD: Makefile.inc,v 1.5 1997/07/17 06:02:42 millert Exp $ +# $OpenBSD: Makefile.inc,v 1.6 1998/11/20 11:18:41 d Exp $ # hash functions -.PATH: ${.CURDIR}/hash +.PATH: ${LIBCSRCDIR}/hash SRCS+= sha1.c sha1hl.c rmd160.c rmd160hl.c MAN+= sha1.3 rmd160.3 diff --git a/lib/libc/include/spinlock.h b/lib/libc/include/spinlock.h new file mode 100644 index 00000000000..e4871f9aede --- /dev/null +++ b/lib/libc/include/spinlock.h @@ -0,0 +1,73 @@ +/* + * Copyright (c) 1998 John Birrell <jb@cimlogic.com.au>. + * 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 John Birrell. + * 4. Neither the name of the author nor the names of any co-contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL 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. + * + * $Id: spinlock.h,v 1.1 1998/11/20 11:18:41 d Exp $ + * $OpenBSD: spinlock.h,v 1.1 1998/11/20 11:18:41 d Exp $ + * + * Lock definitions used in both libc and libpthread. + * + */ + +#ifndef _SPINLOCK_H_ +#define _SPINLOCK_H_ +#include <sys/cdefs.h> +#include <sys/types.h> + +/* + * Lock structure with room for debugging information. + */ +typedef struct { + volatile register_t access_lock; + volatile long lock_owner; + volatile const char * fname; + volatile int lineno; +} spinlock_t; + +#define _SPINLOCK_INITIALIZER { 0, 0, 0, 0 } + +#define _SPINUNLOCK(_lck) (_lck)->access_lock = 0 +#ifdef _LOCK_DEBUG +#define _SPINLOCK(_lck) _spinlock_debug(_lck, __FILE__, __LINE__) +#else +#define _SPINLOCK(_lck) _spinlock(_lck) +#endif + +/* + * Thread function prototype definitions: + */ +__BEGIN_DECLS +register_t _atomic_lock __P((volatile register_t *)); +register_t _thread_slow_atomic_lock __P((volatile register_t *)); +void _spinlock __P((spinlock_t *)); +void _spinlock_debug __P((spinlock_t *, const char *, int)); +__END_DECLS + +#endif /* _SPINLOCK_H_ */ diff --git a/lib/libc/include/thread_private.h b/lib/libc/include/thread_private.h new file mode 100644 index 00000000000..a7f77490780 --- /dev/null +++ b/lib/libc/include/thread_private.h @@ -0,0 +1,175 @@ +/* + * + * Support for thread-safety in libc and libc_r common code using macros + * to declare thread-safe data structures. + * + * $OpenBSD: thread_private.h,v 1.1 1998/11/20 11:18:41 d Exp $ + */ + +#ifndef _THREAD_PRIVATE_H_ +#define _THREAD_PRIVATE_H_ + +/* + * Parts of this file are + * Copyright (c) 1998 John Birrell <jb@cimlogic.com.au>. + * All rights reserved. + * + * $Id: thread_private.h,v 1.1 1998/11/20 11:18:41 d Exp $ + * $OpenBSD: thread_private.h,v 1.1 1998/11/20 11:18:41 d Exp $ + */ + +/* + * This global flag is non-zero when a process has created one + * or more threads. It is used to avoid calling locking functions + * when they are not required. In libc, this is always assumed + * to be zero. + */ + +extern volatile int __isthreaded; + +#ifdef _THREAD_SAFE + +#include <pthread.h> +#include "pthread_private.h" + +#ifdef __STDC__ +#define __THREAD_MUTEXP_NAME(name) _thread_mutexp_inst__ ## name +#define __THREAD_MUTEX_NAME(name) _thread_mutex_inst__ ## name +#define __THREAD_KEY_NAME(name) _thread_key_inst__ ## name +#else +#define __THREAD_MUTEXP_NAME(name) _thread_mutexp_inst__/**/name +#define __THREAD_MUTEX_NAME(name) _thread_mutex_inst__/**/name +#define __THREAD_KEY_NAME(name) _thread_key_inst__/**/name +#endif + +/* + * Mutex declare, lock and unlock macros. + */ + +#define _THREAD_PRIVATE_MUTEX(name) \ + static struct pthread_mutex __THREAD_MUTEXP_NAME(name) = \ + PTHREAD_MUTEX_STATIC_INITIALIZER; \ + static pthread_mutex_t __THREAD_MUTEX_NAME(name) = \ + &__THREAD_MUTEXP_NAME(name); + +#define _THREAD_PRIVATE_MUTEX_LOCK(name) \ + pthread_mutex_lock(&__THREAD_MUTEX_NAME(name)) + +#define _THREAD_PRIVATE_MUTEX_UNLOCK(name) \ + pthread_mutex_unlock(&__THREAD_MUTEX_NAME(name)) + +/* + * These macros help in making persistent storage thread-specific. + * Libc makes extensive use of private static data structures + * that hold state across function invocation, and these macros + * are no-ops when _THREAD_SAFE is not defined. + * In a thread-safe library, the static variables are used only for + * initialising the per-thread instances of the state variables. + */ + +/* + * a mutexed data structure used to hold the persistent state's key + */ +struct _thread_private_key_struct { + struct pthread_mutex lockd; + pthread_mutex_t lock; + int init; + pthread_key_t key; +}; + +/* + * Declaration of a per-thread state key. + */ +#define _THREAD_PRIVATE_KEY(name) \ + static volatile struct _thread_private_key_struct \ + __THREAD_KEY_NAME(name) = { \ + PTHREAD_MUTEX_STATIC_INITIALIZER, \ + &__THREAD_KEY_NAME(name).lockd, \ + 0 \ + }; + +/* + * Initialisation of storage space for a per-thread state variable. + * A pointer to a per-thread *copy* of the _initv parameter is returned. + * It calls malloc the first time and the space is automatically free'd + * when the thread dies. If you need something a bit more complicated + * than free() you will need to roll-your-own. + */ +#define _THREAD_PRIVATE(keyname, _initv, _errv) \ + ({ \ + struct _thread_private_key_struct * __k = \ + &__THREAD_KEY_NAME(keyname); \ + void* __p; \ + extern void free __P((void*)); \ + extern void* malloc __P((size_t)); \ + \ + if (!__isthreaded) { \ + /* non-threaded behaviour */ \ + __p = &(_initv); \ + goto _ok; \ + } \ + \ + /* create key for first thread */ \ + pthread_mutex_lock(&__k->lock); \ + if (__k->init == 0) { \ + if (pthread_key_create(&__k->key, free)) { \ + pthread_mutex_unlock(&__k->lock); \ + goto _err; \ + } \ + __k->init = 1; \ + } \ + pthread_mutex_unlock(&__k->lock); \ + \ + if ((__p = pthread_getspecific(__k->key)) == NULL) { \ + /* alloc space on 1st call in this thread */ \ + if ((__p = malloc(sizeof(_initv))) == NULL) \ + goto _err; \ + if (pthread_setspecific(__k->key, __p) != 0) { \ + free(__p); \ + goto _err; \ + } \ + /* initialise with _initv */ \ + memcpy(__p, &_initv, sizeof(_initv)); \ + } \ + goto _ok; \ + _err: \ + __p = (_errv); \ + _ok: \ + __p; \ + }) + +#else + + +/* + * do-nothing macros for single-threaded case + */ +#define _FD_LOCK(f,o,p) (0) +#define _FD_UNLOCK(f,o) /* nothing */ +#define _THREAD_PRIVATE_KEY(_key) /* nothing */ +#define _THREAD_PRIVATE(keyname, _initv, _errv) (&(_initv)) +#define _THREAD_PRIVATE_MUTEX(_name) /* nothing */ +#define _THREAD_PRIVATE_MUTEX_LOCK(_name) /* nothing */ +#define _THREAD_PRIVATE_MUTEX_UNLOCK(_name) /* nothing */ + +#endif + +/* + * File lock contention is difficult to diagnose without knowing + * where locks were set. Allow a debug library to be built which + * records the source file and line number of each lock call. + */ +#ifdef _FLOCK_DEBUG +#define _FLOCKFILE(x) _flockfile_debug(x, __FILE__, __LINE__) +#else +#define _FLOCKFILE(x) flockfile(x) +#endif + +/* + * Macros for locking and unlocking FILEs. These test if the + * process is threaded to avoid locking when not required. + */ +#define FLOCKFILE(fp) if (__isthreaded) _FLOCKFILE(fp) +#define FUNLOCKFILE(fp) if (__isthreaded) funlockfile(fp) + +#endif _THREAD_PRIVATE_H_ diff --git a/lib/libc/locale/Makefile.inc b/lib/libc/locale/Makefile.inc index a01e92b975f..0784dda22f3 100644 --- a/lib/libc/locale/Makefile.inc +++ b/lib/libc/locale/Makefile.inc @@ -1,7 +1,7 @@ -# $OpenBSD: Makefile.inc,v 1.3 1997/07/25 16:53:15 mickey Exp $ +# $OpenBSD: Makefile.inc,v 1.4 1998/11/20 11:18:42 d Exp $ # locale sources -.PATH: ${.CURDIR}/arch/${MACHINE_ARCH}/locale ${.CURDIR}/locale +.PATH: ${LIBCSRCDIR}/arch/${MACHINE_ARCH}/locale ${LIBCSRCDIR}/locale SRCS+= _def_messages.c _def_monetary.c _def_numeric.c _def_time.c \ localeconv.c nl_langinfo.c setlocale.c diff --git a/lib/libc/md/Makefile.inc b/lib/libc/md/Makefile.inc index 17d2124e72f..c862c56e486 100644 --- a/lib/libc/md/Makefile.inc +++ b/lib/libc/md/Makefile.inc @@ -1,6 +1,6 @@ -# $OpenBSD: Makefile.inc,v 1.4 1996/10/20 19:14:45 millert Exp $ +# $OpenBSD: Makefile.inc,v 1.5 1998/11/20 11:18:42 d Exp $ -.PATH: ${.CURDIR}/md +.PATH: ${LIBCSRCDIR}/md SRCS+= md4c.c md5c.c md4hl.c md5hl.c diff --git a/lib/libc/net/Makefile.inc b/lib/libc/net/Makefile.inc index 935a1904c12..35a9632a94e 100644 --- a/lib/libc/net/Makefile.inc +++ b/lib/libc/net/Makefile.inc @@ -1,7 +1,7 @@ -# $OpenBSD: Makefile.inc,v 1.16 1998/08/29 21:11:40 deraadt Exp $ +# $OpenBSD: Makefile.inc,v 1.17 1998/11/20 11:18:43 d Exp $ # net sources -.PATH: ${.CURDIR}/arch/${MACHINE_ARCH}/net ${.CURDIR}/net +.PATH: ${LIBCSRCDIR}/arch/${MACHINE_ARCH}/net ${LIBCSRCDIR}/net CFLAGS+=-DRESOLVSORT @@ -19,7 +19,7 @@ SRCS+= base64.c gethostnamadr.c getnetbyaddr.c getnetbyname.c getnetent.c \ # m-d Makefile.inc must include sources for: # htonl() htons() ntohl() ntohs() -.include "${.CURDIR}/arch/${MACHINE_ARCH}/net/Makefile.inc" +.include "${LIBCSRCDIR}/arch/${MACHINE_ARCH}/net/Makefile.inc" MAN+= byteorder.3 ethers.3 gethostbyname.3 getnetent.3 getprotoent.3 \ getservent.3 inet.3 inet_net.3 iso_addr.3 link_addr.3 ns.3 ipx.3 \ diff --git a/lib/libc/net/gethostnamadr.c b/lib/libc/net/gethostnamadr.c index 7321225863a..6ff456fb0c1 100644 --- a/lib/libc/net/gethostnamadr.c +++ b/lib/libc/net/gethostnamadr.c @@ -52,7 +52,7 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char rcsid[] = "$OpenBSD: gethostnamadr.c,v 1.30 1998/03/16 05:06:55 millert Exp $"; +static char rcsid[] = "$OpenBSD: gethostnamadr.c,v 1.31 1998/11/20 11:18:44 d Exp $"; #endif /* LIBC_SCCS and not lint */ #include <sys/param.h> @@ -67,12 +67,14 @@ static char rcsid[] = "$OpenBSD: gethostnamadr.c,v 1.30 1998/03/16 05:06:55 mill #include <errno.h> #include <string.h> #include <syslog.h> +#include <stdlib.h> #ifdef YP #include <rpc/rpc.h> #include <rpcsvc/yp.h> #include <rpcsvc/ypclnt.h> #include "ypinternal.h" #endif +#include "thread_private.h" #define MULTI_PTRS_ARE_ALIASES 1 /* XXX - experimental */ @@ -423,6 +425,60 @@ getanswer(answer, anslen, qname, qtype) return (NULL); } +#ifndef notyet +/* + * XXX This is an extremely bogus implementations. + * + * FreeBSD has this interface: + * int gethostbyaddr_r(const char *addr, int len, int type, + * struct hostent *result, struct hostent_data *buffer) + */ + +struct hostent * +gethostbyname_r(name, hp, buf, buflen, errorp) + const char * name; + struct hostent * hp; + char * buf; + int buflen; + int * errorp; +{ + struct hostent *res; + + res = gethostbyname(name); + *errorp = h_errno; + if (res == NULL) + return NULL; + memcpy(hp, res, sizeof *hp); /* XXX not sufficient */ + return hp; +} + +/* + * XXX This is an extremely bogus implementations. + */ +struct hostent * +gethostbyaddr_r(addr, len, af, he, buf, buflen, errorp) + const char *addr; /* XXX should have been def'd as u_char! */ + int len, af; + struct hostent * he; + char * buf; + int buflen; + int * errorp; +{ + struct hostent * res; + + res = gethostbyaddr(addr, len, af); + *errorp = h_errno; + if (res == NULL) + return NULL; + memcpy(he, res, sizeof *he); /* XXX not sufficient */ + return he; +} + +/* XXX RFC2133 expects a gethostbyname2_r() -- unimplemented */ +#endif + +_THREAD_PRIVATE_MUTEX(gethostnamadr) + struct hostent * gethostbyname(name) const char *name; @@ -430,15 +486,19 @@ gethostbyname(name) struct hostent *hp; extern struct hostent *_gethtbyname2(); + _THREAD_PRIVATE_MUTEX_LOCK(gethostnamadr); if ((_res.options & RES_INIT) == 0 && res_init() == -1) - return (_gethtbyname2(name, AF_INET)); + hp = _gethtbyname2(name, AF_INET); - if (_res.options & RES_USE_INET6) { + else if (_res.options & RES_USE_INET6) { hp = gethostbyname2(name, AF_INET6); - if (hp) - return (hp); + if (hp == NULL) + hp = gethostbyname2(name, AF_INET); } - return (gethostbyname2(name, AF_INET)); + else + hp = gethostbyname2(name, AF_INET); + _THREAD_PRIVATE_MUTEX_UNLOCK(gethostnamadr); + return hp; } struct hostent * @@ -599,9 +659,14 @@ gethostbyaddr(addr, len, af) char qbuf[MAXDNAME+1], *qp; extern struct hostent *_gethtbyaddr(), *_yp_gethtbyaddr(); char lookups[MAXDNSLUS]; + struct hostent *res; - if ((_res.options & RES_INIT) == 0 && res_init() == -1) - return (_gethtbyaddr(addr, len, af)); + _THREAD_PRIVATE_MUTEX_LOCK(gethostnamadr); + if ((_res.options & RES_INIT) == 0 && res_init() == -1) { + res = _gethtbyaddr(addr, len, af); + _THREAD_PRIVATE_MUTEX_UNLOCK(gethostnamadr); + return (res); + } if (af == AF_INET6 && len == IN6ADDRSZ && (!bcmp(uaddr, mapped, sizeof mapped) || @@ -622,11 +687,13 @@ gethostbyaddr(addr, len, af) default: errno = EAFNOSUPPORT; h_errno = NETDB_INTERNAL; + _THREAD_PRIVATE_MUTEX_UNLOCK(gethostnamadr); return (NULL); } if (size != len) { errno = EINVAL; h_errno = NETDB_INTERNAL; + _THREAD_PRIVATE_MUTEX_UNLOCK(gethostnamadr); return (NULL); } switch (af) { @@ -692,6 +759,7 @@ gethostbyaddr(addr, len, af) break; } } + _THREAD_PRIVATE_MUTEX_UNLOCK(gethostnamadr); /* XXX h_errno not correct in all cases... */ return (hp); } diff --git a/lib/libc/net/getservbyname.c b/lib/libc/net/getservbyname.c index 25f0e27d06a..7375c894042 100644 --- a/lib/libc/net/getservbyname.c +++ b/lib/libc/net/getservbyname.c @@ -32,21 +32,28 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char rcsid[] = "$OpenBSD: getservbyname.c,v 1.3 1997/07/09 01:08:34 millert Exp $"; +static char rcsid[] = "$OpenBSD: getservbyname.c,v 1.4 1998/11/20 11:18:44 d Exp $"; #endif /* LIBC_SCCS and not lint */ #include <netdb.h> #include <string.h> +#include "thread_private.h" extern int _serv_stayopen; +_THREAD_PRIVATE_MUTEX(getservbyname_r) + struct servent * -getservbyname(name, proto) +getservbyname_r(name, proto, se, buf, buflen) const char *name, *proto; + struct servent *se; + char *buf; + int buflen; { register struct servent *p; register char **cp; + _THREAD_PRIVATE_MUTEX_LOCK(getservbyname_r); setservent(_serv_stayopen); while ((p = getservent())) { if (strcmp(name, p->s_name) == 0) @@ -61,5 +68,20 @@ gotname: } if (!_serv_stayopen) endservent(); + _THREAD_PRIVATE_MUTEX_UNLOCK(getservbyname_r); return (p); } + +struct servent *getservbyname(name, proto) + const char *name, *proto; +{ + _THREAD_PRIVATE_KEY(getservbyname) + static char buf[4096]; + char *bufp = (char*)_THREAD_PRIVATE(getservbyname, buf, NULL); + + if (bufp == NULL) + return (NULL); + return getservbyname_r(name, proto, (struct servent*) bufp, + bufp + sizeof(struct servent), + sizeof buf - sizeof(struct servent) ); +} diff --git a/lib/libc/nls/Makefile.inc b/lib/libc/nls/Makefile.inc index e515e8c51d5..ac8eabe2d36 100644 --- a/lib/libc/nls/Makefile.inc +++ b/lib/libc/nls/Makefile.inc @@ -1,6 +1,6 @@ -# $OpenBSD: Makefile.inc,v 1.3 1996/08/19 08:29:58 tholo Exp $ +# $OpenBSD: Makefile.inc,v 1.4 1998/11/20 11:18:45 d Exp $ -.PATH: ${.CURDIR}/nls +.PATH: ${LIBCSRCDIR}/nls SRCS+= catclose.c catgets.c catopen.c MAN+= catclose.3 catgets.3 catopen.3 diff --git a/lib/libc/quad/Makefile.inc b/lib/libc/quad/Makefile.inc index b2373026187..b13c9be1d2f 100644 --- a/lib/libc/quad/Makefile.inc +++ b/lib/libc/quad/Makefile.inc @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile.inc,v 1.2 1996/08/19 08:30:12 tholo Exp $ +# $OpenBSD: Makefile.inc,v 1.3 1998/11/20 11:18:45 d Exp $ # Quad support SRCS+= adddi3.c anddi3.c ashldi3.c ashrdi3.c cmpdi2.c divdi3.c fixdfdi.c \ @@ -7,4 +7,4 @@ SRCS+= adddi3.c anddi3.c ashldi3.c ashrdi3.c cmpdi2.c divdi3.c fixdfdi.c \ negdi2.c notdi2.c qdivrem.c subdi3.c ucmpdi2.c udivdi3.c umoddi3.c \ xordi3.c -.PATH: ${.CURDIR}/arch/${MACHINE_ARCH}/quad ${.CURDIR}/quad +.PATH: ${LIBCSRCDIR}/arch/${MACHINE_ARCH}/quad ${LIBCSRCDIR}/quad diff --git a/lib/libc/regex/Makefile.inc b/lib/libc/regex/Makefile.inc index f4a8f687923..4a786818f42 100644 --- a/lib/libc/regex/Makefile.inc +++ b/lib/libc/regex/Makefile.inc @@ -1,8 +1,8 @@ -# $OpenBSD: Makefile.inc,v 1.3 1997/04/28 20:44:55 millert Exp $ +# $OpenBSD: Makefile.inc,v 1.4 1998/11/20 11:18:46 d Exp $ # @(#)Makefile.inc 8.1 (Berkeley) 6/4/93 # regex sources -.PATH: ${.CURDIR}/regex +.PATH: ${LIBCSRCDIR}/regex CFLAGS+=-DPOSIX_MISTAKE diff --git a/lib/libc/rpc/Makefile.inc b/lib/libc/rpc/Makefile.inc index 2265f6a8e0f..d4ea29b4c91 100644 --- a/lib/libc/rpc/Makefile.inc +++ b/lib/libc/rpc/Makefile.inc @@ -1,7 +1,7 @@ -# $OpenBSD: Makefile.inc,v 1.7 1998/08/29 21:11:47 deraadt Exp $ +# $OpenBSD: Makefile.inc,v 1.8 1998/11/20 11:18:47 d Exp $ # librpc sources -.PATH: ${.CURDIR}/arch/${MACHINE}/rpc ${.CURDIR}/rpc +.PATH: ${LIBCSRCDIR}/arch/${MACHINE}/rpc ${LIBCSRCDIR}/rpc SRCS+= auth_none.c auth_unix.c authunix_prot.c bindresvport.c \ clnt_generic.c clnt_perror.c clnt_raw.c clnt_simple.c clnt_tcp.c \ diff --git a/lib/libc/stdio/Makefile.inc b/lib/libc/stdio/Makefile.inc index c83779e225e..18eb2722926 100644 --- a/lib/libc/stdio/Makefile.inc +++ b/lib/libc/stdio/Makefile.inc @@ -1,7 +1,7 @@ -# $OpenBSD: Makefile.inc,v 1.7 1998/08/30 22:19:38 millert Exp $ +# $OpenBSD: Makefile.inc,v 1.8 1998/11/20 11:18:48 d Exp $ # stdio sources -.PATH: ${.CURDIR}/stdio +.PATH: ${LIBCSRCDIR}/stdio CFLAGS+=-DFLOATING_POINT @@ -14,7 +14,7 @@ SRCS+= asprintf.c clrerr.c fclose.c fdopen.c feof.c ferror.c fflush.c fgetc.c \ scanf.c setbuf.c setbuffer.c setvbuf.c snprintf.c sprintf.c sscanf.c \ stdio.c tempnam.c tmpfile.c tmpnam.c ungetc.c vasprintf.c vfprintf.c \ vfscanf.c vprintf.c vscanf.c vsnprintf.c vsprintf.c vsscanf.c \ - wbuf.c wsetup.c + wbuf.c wsetup.c flockfile.c MAN+= fclose.3 ferror.3 fflush.3 fgetln.3 fgets.3 fopen.3 fputs.3 \ fread.3 fseek.3 funopen.3 getc.3 mktemp.3 perror.3 printf.3 putc.3 \ diff --git a/lib/libc/stdio/clrerr.c b/lib/libc/stdio/clrerr.c index 90de2ddaaca..1bd72b93fb5 100644 --- a/lib/libc/stdio/clrerr.c +++ b/lib/libc/stdio/clrerr.c @@ -35,7 +35,7 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char rcsid[] = "$OpenBSD: clrerr.c,v 1.2 1996/08/19 08:32:17 tholo Exp $"; +static char rcsid[] = "$OpenBSD: clrerr.c,v 1.3 1998/11/20 11:18:48 d Exp $"; #endif /* LIBC_SCCS and not lint */ #include <stdio.h> @@ -45,5 +45,7 @@ void clearerr(fp) FILE *fp; { + flockfile(fp); __sclearerr(fp); + funlockfile(fp); } diff --git a/lib/libc/stdio/flockfile.c b/lib/libc/stdio/flockfile.c new file mode 100644 index 00000000000..d720aee991a --- /dev/null +++ b/lib/libc/stdio/flockfile.c @@ -0,0 +1,49 @@ +/* $OpenBSD: flockfile.c,v 1.1 1998/11/20 11:18:48 d Exp $ */ + +#include <stdio.h> +#include "thread_private.h" + +#ifndef _THREAD_SAFE + +/* + * Subroutine versions of the macros in <stdio.h> + * Note that these are all no-ops because libc does not do threads. + */ + +#undef flockfile +#undef ftrylockfile +#undef funlockfile +#undef _flockfile_debug + +void +flockfile(fp) + FILE * fp; +{ +} + +int +ftrylockfile(fp) + FILE * fp; +{ + return 0; +} + +void +funlockfile(fp) + FILE * fp; +{ +} + +void +_flockfile_debug(fp, fname, lineno) + FILE * fp; + const char * fname; + int lineno; +{ +} + +#else /* _THREAD_SAFE */ + +/* Actual implementation of file locking in libc_r/uthread/uthread_file.c */ + +#endif /* _THREAD_SAFE */ diff --git a/lib/libc/stdio/getc.c b/lib/libc/stdio/getc.c index ff4a5168a3f..ce990968202 100644 --- a/lib/libc/stdio/getc.c +++ b/lib/libc/stdio/getc.c @@ -35,12 +35,24 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char rcsid[] = "$OpenBSD: getc.c,v 1.2 1996/08/19 08:32:51 tholo Exp $"; +static char rcsid[] = "$OpenBSD: getc.c,v 1.3 1998/11/20 11:18:48 d Exp $"; #endif /* LIBC_SCCS and not lint */ #include <stdio.h> /* + * A subroutine version of the macro getc_unlocked. + */ +#undef getc_unlocked + +int +getc_unlocked(fp) + FILE *fp; +{ + return (__sgetc(fp)); +} + +/* * A subroutine version of the macro getc. */ #undef getc @@ -49,5 +61,10 @@ int getc(fp) register FILE *fp; { - return (__sgetc(fp)); + int c; + + flockfile(fp); + c = __sgetc(fp); + funlockfile(fp); + return (c); } diff --git a/lib/libc/stdio/putc.c b/lib/libc/stdio/putc.c index b43af508a55..f9a23b2cd41 100644 --- a/lib/libc/stdio/putc.c +++ b/lib/libc/stdio/putc.c @@ -35,7 +35,7 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char rcsid[] = "$OpenBSD: putc.c,v 1.3 1996/10/28 05:32:55 tholo Exp $"; +static char rcsid[] = "$OpenBSD: putc.c,v 1.4 1998/11/20 11:18:48 d Exp $"; #endif /* LIBC_SCCS and not lint */ #include <stdio.h> @@ -43,11 +43,12 @@ static char rcsid[] = "$OpenBSD: putc.c,v 1.3 1996/10/28 05:32:55 tholo Exp $"; #include "local.h" /* - * A subroutine version of the macro putc. + * A subroutine version of the macro putc_unlocked. */ -#undef putc +#undef putc_unlocked -putc(c, fp) +int +putc_unlocked(c, fp) int c; register FILE *fp; { @@ -57,3 +58,21 @@ putc(c, fp) } return (__sputc(c, fp)); } + +/* + * A subroutine version of the macro putc. + */ +#undef putc + +int +putc(c, fp) + int c; + FILE *fp; +{ + int ret; + + flockfile(fp); + ret = putc_unlocked(c, fp); + funlockfile(fp); + return (ret); +} diff --git a/lib/libc/stdlib/Makefile.inc b/lib/libc/stdlib/Makefile.inc index bdd5280ddec..3191f08328c 100644 --- a/lib/libc/stdlib/Makefile.inc +++ b/lib/libc/stdlib/Makefile.inc @@ -1,7 +1,7 @@ # $OpenBDS: Makefile.inc,v 1.6 1996/08/21 03:47:21 tholo Exp $ # stdlib sources -.PATH: ${.CURDIR}/arch/${MACHINE_ARCH}/stdlib ${.CURDIR}/stdlib +.PATH: ${LIBCSRCDIR}/arch/${MACHINE_ARCH}/stdlib ${LIBCSRCDIR}/stdlib SRCS+= a64l.c abort.c atexit.c atoi.c atof.c atol.c bsearch.c calloc.c \ cfree.c exit.c getenv.c getopt.c getsubopt.c heapsort.c l64a.c \ diff --git a/lib/libc/stdlib/abort.c b/lib/libc/stdlib/abort.c index 4ea8a2ca4b7..4cc6257acb9 100644 --- a/lib/libc/stdlib/abort.c +++ b/lib/libc/stdlib/abort.c @@ -32,12 +32,13 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char *rcsid = "$OpenBSD: abort.c,v 1.5 1997/06/22 20:21:25 tholo Exp $"; +static char *rcsid = "$OpenBSD: abort.c,v 1.6 1998/11/20 11:18:49 d Exp $"; #endif /* LIBC_SCCS and not lint */ #include <signal.h> #include <stdlib.h> #include <unistd.h> +#include "thread_private.h" void (*__cleanup)(); @@ -54,7 +55,11 @@ abort() * any errors -- X311J doesn't allow abort to return anyway. */ sigdelset(&mask, SIGABRT); +#ifdef _THREAD_SAFE + (void)_thread_sys_sigprocmask(SIG_SETMASK, &mask, (sigset_t *)NULL); +#else _THREAD_SAFE (void)sigprocmask(SIG_SETMASK, &mask, (sigset_t *)NULL); +#endif _THREAD_SAFE /* * POSIX requires we flush stdio buffers on abort @@ -71,7 +76,11 @@ abort() * it again, only harder. */ (void)signal(SIGABRT, SIG_DFL); +#ifdef _THREAD_SAFE + (void)_thread_sys_sigprocmask(SIG_SETMASK, &mask, (sigset_t *)NULL); +#else _THREAD_SAFE (void)sigprocmask(SIG_SETMASK, &mask, (sigset_t *)NULL); +#endif _THREAD_SAFE (void)kill(getpid(), SIGABRT); exit(1); } diff --git a/lib/libc/stdlib/exit.c b/lib/libc/stdlib/exit.c index e358c94fd66..bc7fd395ca3 100644 --- a/lib/libc/stdlib/exit.c +++ b/lib/libc/stdlib/exit.c @@ -32,7 +32,7 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char *rcsid = "$OpenBSD: exit.c,v 1.2 1996/08/19 08:33:30 tholo Exp $"; +static char *rcsid = "$OpenBSD: exit.c,v 1.3 1998/11/20 11:18:50 d Exp $"; #endif /* LIBC_SCCS and not lint */ #include <stdlib.h> @@ -42,6 +42,15 @@ static char *rcsid = "$OpenBSD: exit.c,v 1.2 1996/08/19 08:33:30 tholo Exp $"; void (*__cleanup)(); /* + * This variable is zero until a process has created a thread. + * It is used to avoid calling locking functions in libc when they + * are not required. By default, libc is intended to be(come) + * thread-safe, but without a (significant) penalty to non-threaded + * processes. + */ +int __isthreaded = 0; + +/* * Exit, flushing stdio buffers if necessary. */ void diff --git a/lib/libc/stdlib/malloc.c b/lib/libc/stdlib/malloc.c index d1d87597918..ecbf93dc489 100644 --- a/lib/libc/stdlib/malloc.c +++ b/lib/libc/stdlib/malloc.c @@ -8,7 +8,7 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char rcsid[] = "$OpenBSD: malloc.c,v 1.32 1998/08/06 16:26:32 millert Exp $"; +static char rcsid[] = "$OpenBSD: malloc.c,v 1.33 1998/11/20 11:18:50 d Exp $"; #endif /* LIBC_SCCS and not lint */ /* @@ -87,15 +87,27 @@ static char rcsid[] = "$OpenBSD: malloc.c,v 1.32 1998/08/06 16:26:32 millert Exp #endif /* __OpenBSD__ */ #ifdef _THREAD_SAFE -#include <pthread.h> -static pthread_mutex_t malloc_lock; -#define THREAD_LOCK() pthread_mutex_lock(&malloc_lock) -#define THREAD_UNLOCK() pthread_mutex_unlock(&malloc_lock) -#define THREAD_LOCK_INIT() pthread_mutex_init(&malloc_lock, 0); +# include "thread_private.h" +# if 0 + /* kernel threads */ +# include <pthread.h> + static pthread_mutex_t malloc_lock; +# define THREAD_LOCK() pthread_mutex_lock(&malloc_lock) +# define THREAD_UNLOCK() pthread_mutex_unlock(&malloc_lock) +# define THREAD_LOCK_INIT() pthread_mutex_init(&malloc_lock, 0); +# else + /* user threads */ +# include "spinlock.h" + static spinlock_t malloc_lock = _SPINLOCK_INITIALIZER; +# define THREAD_LOCK() if (__isthreaded) _SPINLOCK(&malloc_lock) +# define THREAD_UNLOCK() if (__isthreaded) _SPINUNLOCK(&malloc_lock) +# define THREAD_LOCK_INIT() +# endif #else -#define THREAD_LOCK() -#define THREAD_UNLOCK() -#define THREAD_LOCK_INIT() + /* no threads */ +# define THREAD_LOCK() +# define THREAD_UNLOCK() +# define THREAD_LOCK_INIT() #endif /* diff --git a/lib/libc/stdlib/rand.3 b/lib/libc/stdlib/rand.3 index 32d32761f19..28496ec12aa 100644 --- a/lib/libc/stdlib/rand.3 +++ b/lib/libc/stdlib/rand.3 @@ -33,7 +33,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.\" $OpenBSD: rand.3,v 1.4 1998/07/05 19:54:22 millert Exp $ +.\" $OpenBSD: rand.3,v 1.5 1998/11/20 11:18:50 d Exp $ .\" .Dd June 29, 1991 .Dt RAND 3 @@ -48,6 +48,8 @@ .Fn srand "unsigned seed" .Ft int .Fn rand void +.Ft int +.Fn rand_r "unsigned int *seed" .Sh DESCRIPTION .Bf -symbolic These interfaces are obsoleted by @@ -73,6 +75,14 @@ with the same seed value. .Pp If no seed value is provided, the functions are automatically seeded with a value of 1. +.Pp +The +.Fn rand_r +is a thread-safe version of +.Fn rand . +Storage for the seed must be provided through the +.Ar seed +argument, and needs to have been initialized by the caller. .Sh SEE ALSO .Xr arc4random 3 , .Xr rand48 3 , @@ -85,3 +95,10 @@ and functions conform to .St -ansiC . +.Pp +The +.Fn rand_r +function +conforms to ISO/IEC 9945-1 ANSI/IEEE +.Pq Dq Tn POSIX +Std 1003.1c Draft 10. diff --git a/lib/libc/stdlib/rand.c b/lib/libc/stdlib/rand.c index f270ffd9868..61fb66e5eca 100644 --- a/lib/libc/stdlib/rand.c +++ b/lib/libc/stdlib/rand.c @@ -32,7 +32,7 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char *rcsid = "$OpenBSD: rand.c,v 1.2 1996/08/19 08:33:44 tholo Exp $"; +static char *rcsid = "$OpenBSD: rand.c,v 1.3 1998/11/20 11:18:50 d Exp $"; #endif /* LIBC_SCCS and not lint */ #include <sys/types.h> @@ -41,9 +41,17 @@ static char *rcsid = "$OpenBSD: rand.c,v 1.2 1996/08/19 08:33:44 tholo Exp $"; static u_long next = 1; int +rand_r(seed) +u_int *seed; +{ + *seed = *seed * 1103515245 + 12345; + return (u_int)(*seed / 65536) % ((u_int)RAND_MAX + 1); +} + +int rand() { - return ((next = next * 1103515245 + 12345) % ((u_int)RAND_MAX + 1)); + return rand_r(&next); } void diff --git a/lib/libc/string/Makefile.inc b/lib/libc/string/Makefile.inc index 076db78945a..78eee7efa57 100644 --- a/lib/libc/string/Makefile.inc +++ b/lib/libc/string/Makefile.inc @@ -1,7 +1,7 @@ -# $OpenBSD: Makefile.inc,v 1.5 1998/07/01 01:29:44 millert Exp $ +# $OpenBSD: Makefile.inc,v 1.6 1998/11/20 11:18:51 d Exp $ # string sources -.PATH: ${.CURDIR}/arch/${MACHINE_ARCH}/string ${.CURDIR}/string +.PATH: ${LIBCSRCDIR}/arch/${MACHINE_ARCH}/string ${LIBCSRCDIR}/string SRCS+= bm.c memccpy.c strcasecmp.c strcoll.c strdup.c strerror.c \ strlcat.c strlcpy.c strmode.c strsignal.c strtok.c strxfrm.c \ @@ -16,7 +16,7 @@ SRCS+= bm.c memccpy.c strcasecmp.c strcoll.c strdup.c strerror.c \ # m-d Makefile.inc may include sources for: # memcpy() memmove() strchr() strrchr() -.include "${.CURDIR}/arch/${MACHINE_ARCH}/string/Makefile.inc" +.include "${LIBCSRCDIR}/arch/${MACHINE_ARCH}/string/Makefile.inc" # if no machine specific memmove(3), build one out of bcopy(3). .if empty(SRCS:Mmemmove.S) @@ -100,19 +100,19 @@ LOBJS+= memmove.ln memcpy.ln strchr.ln strrchr.ln memmove.ln: bcopy.c lint ${LINTFLAGS} -DMEMMOVE ${CFLAGS:M-[IDU]*} -i -o ${.TARGET} \ - ${.CURDIR}/string/bcopy.c + ${LIBCSRCDIR}/string/bcopy.c memcpy.ln: bcopy.c lint ${LINTFLAGS} -DMEMCOPY ${CFLAGS:M-[IDU]*} -i -o ${.TARGET} \ - ${.CURDIR}/string/bcopy.c + ${LIBCSRCDIR}/string/bcopy.c strchr.ln: index.c lint ${LINTFLAGS} -DSTRCHR ${CFLAGS:M-[IDU]*} -i -o ${.TARGET} \ - ${.CURDIR}/string/index.c + ${LIBCSRCDIR}/string/index.c strrchr.ln: rindex.c lint ${LINTFLAGS} -DSTRRCHR ${CFLAGS:M-[IDU]*} -i -o ${.TARGET} \ - ${.CURDIR}/string/rindex.c + ${LIBCSRCDIR}/string/rindex.c MAN+= bm.3 bcmp.3 bcopy.3 bstring.3 bzero.3 ffs.3 index.3 memccpy.3 memchr.3 \ memcmp.3 memcpy.3 memmove.3 memset.3 rindex.3 strcasecmp.3 strcat.3 \ diff --git a/lib/libc/sys/Makefile.inc b/lib/libc/sys/Makefile.inc index 801f728f13d..3bc64a19d3e 100644 --- a/lib/libc/sys/Makefile.inc +++ b/lib/libc/sys/Makefile.inc @@ -1,21 +1,21 @@ -# $OpenBSD: Makefile.inc,v 1.26 1998/09/18 00:48:35 art Exp $ +# $OpenBSD: Makefile.inc,v 1.27 1998/11/20 11:18:52 d Exp $ # $NetBSD: Makefile.inc,v 1.35 1995/10/16 23:49:07 jtc Exp $ # @(#)Makefile.inc 8.1 (Berkeley) 6/17/93 # sys sources -.PATH: ${.CURDIR}/arch/${MACHINE_ARCH}/sys ${.CURDIR}/sys +.PATH: ${LIBCSRCDIR}/arch/${MACHINE_ARCH}/sys ${LIBCSRCDIR}/sys # modules with non-default implementations on at least one architecture: SRCS+= Ovfork.S brk.S cerror.S exect.S fork.S pipe.S ptrace.S \ - sbrk.S setlogin.S sigpending.S sigprocmask.S sigreturn.S \ + sbrk.S sigpending.S sigprocmask.S sigreturn.S \ sigsuspend.S syscall.S LSRCS+= Lint_Ovfork.c Lint_brk.c Lint_exect.c Lint_fork.c \ - Lint_pipe.c Lint_ptrace.c Lint_sbrk.c Lint_setlogin.c \ + Lint_pipe.c Lint_ptrace.c Lint_sbrk.c \ Lint_sigpending.c Lint_sigprocmask.c Lint_sigreturn.c \ Lint_sigsuspend.c Lint_syscall.c DPSRCS+= Lint_Ovfork.c Lint_brk.c Lint_exect.c Lint_fork.c \ - Lint_pipe.c Lint_ptrace.c Lint_sbrk.c Lint_setlogin.c \ + Lint_pipe.c Lint_ptrace.c Lint_sbrk.c \ Lint_sigpending.c Lint_sigprocmask.c Lint_sigreturn.c \ Lint_sigsuspend.c Lint_syscall.c @@ -58,13 +58,13 @@ ASM= accept.o access.o acct.o adjtime.o bind.o chdir.o chflags.o chmod.o \ PASM= ${ASM:.o=.po} SASM= ${ASM:.o=.so} -PSEUDO= _exit.o _getlogin.o +PSEUDO= _exit.o _getlogin.o _setlogin.o PPSEUDO=${PSEUDO:.o=.po} SPSEUDO=${PSEUDO:.o=.so} OBJS+= ${ASM} ${PSEUDO} -${PASM}: ${.CURDIR}/arch/${MACHINE_ARCH}/SYS.h /usr/include/sys/syscall.h +${PASM}: ${LIBCSRCDIR}/arch/${MACHINE_ARCH}/SYS.h /usr/include/sys/syscall.h @echo creating ${.TARGET} @printf '#include "SYS.h"\nRSYSCALL(${.PREFIX})\n' | \ ${CPP} -DPROF ${CFLAGS:M-[ID]*} ${AINC} | ${AS} -o ${.TARGET}.o @@ -72,25 +72,25 @@ ${PASM}: ${.CURDIR}/arch/${MACHINE_ARCH}/SYS.h /usr/include/sys/syscall.h @rm -f ${.TARGET}.o .if (${MACHINE_ARCH} != "mips") -${SASM}: ${.CURDIR}/arch/${MACHINE_ARCH}/SYS.h /usr/include/sys/syscall.h +${SASM}: ${LIBCSRCDIR}/arch/${MACHINE_ARCH}/SYS.h /usr/include/sys/syscall.h @echo creating ${.TARGET} @printf '#include "SYS.h"\nRSYSCALL(${.PREFIX})\n' | \ ${CPP} -DPIC ${CFLAGS:M-[ID]*} ${AINC} | ${AS} -k -o ${.TARGET} .else -${SASM}: ${.CURDIR}/arch/${MACHINE_ARCH}/SYS.h /usr/include/sys/syscall.h +${SASM}: ${LIBCSRCDIR}/arch/${MACHINE_ARCH}/SYS.h /usr/include/sys/syscall.h @echo creating ${.TARGET} @printf '#include "SYS.h"\nRSYSCALL(${.PREFIX})\n' | \ ${CPP} -DPIC ${CFLAGS:M-[ID]*} ${AINC} | ${AS} -o ${.TARGET} .endif -${ASM}: ${.CURDIR}/arch/${MACHINE_ARCH}/SYS.h /usr/include/sys/syscall.h +${ASM}: ${LIBCSRCDIR}/arch/${MACHINE_ARCH}/SYS.h /usr/include/sys/syscall.h @echo creating ${.TARGET} @printf '#include "SYS.h"\nRSYSCALL(${.PREFIX})\n' | \ ${CPP} ${CFLAGS:M-[ID]*} ${AINC} | ${AS} -o ${.TARGET}.o @${LD} -x -r ${.TARGET}.o -o ${.TARGET} @rm -f ${.TARGET}.o -${PPSEUDO}: ${.CURDIR}/arch/${MACHINE_ARCH}/SYS.h /usr/include/sys/syscall.h +${PPSEUDO}: ${LIBCSRCDIR}/arch/${MACHINE_ARCH}/SYS.h /usr/include/sys/syscall.h @echo creating ${.TARGET} @printf '#include "SYS.h"\nPSEUDO(${.PREFIX},${.PREFIX:S/_//})\n' | \ ${CPP} -DPROF ${CFLAGS:M-[ID]*} ${AINC} | ${AS} -o ${.TARGET}.o @@ -98,18 +98,18 @@ ${PPSEUDO}: ${.CURDIR}/arch/${MACHINE_ARCH}/SYS.h /usr/include/sys/syscall.h @rm -f ${.TARGET}.o .if (${MACHINE_ARCH} != "mips") -${SPSEUDO}: ${.CURDIR}/arch/${MACHINE_ARCH}/SYS.h /usr/include/sys/syscall.h +${SPSEUDO}: ${LIBCSRCDIR}/arch/${MACHINE_ARCH}/SYS.h /usr/include/sys/syscall.h @echo creating ${.TARGET} @printf '#include "SYS.h"\nPSEUDO(${.PREFIX},${.PREFIX:S/_//})\n' | \ ${CPP} -DPIC ${CFLAGS:M-[ID]*} ${AINC} | ${AS} -k -o ${.TARGET} .else -${SPSEUDO}: ${.CURDIR}/arch/${MACHINE_ARCH}/SYS.h /usr/include/sys/syscall.h +${SPSEUDO}: ${LIBCSRCDIR}/arch/${MACHINE_ARCH}/SYS.h /usr/include/sys/syscall.h @echo creating ${.TARGET} @printf '#include "SYS.h"\nPSEUDO(${.PREFIX},${.PREFIX:S/_//})\n' | \ ${CPP} -DPIC ${CFLAGS:M-[ID]*} ${AINC} | ${AS} -o ${.TARGET} .endif -${PSEUDO}: ${.CURDIR}/arch/${MACHINE_ARCH}/SYS.h /usr/include/sys/syscall.h +${PSEUDO}: ${LIBCSRCDIR}/arch/${MACHINE_ARCH}/SYS.h /usr/include/sys/syscall.h @echo creating ${.TARGET} @printf '#include "SYS.h"\nPSEUDO(${.PREFIX},${.PREFIX:S/_//})\n' | \ ${CPP} ${CFLAGS:M-[ID]*} ${AINC} | ${AS} -o ${.TARGET}.o @@ -124,22 +124,22 @@ DPSRCS+= LintSysNormal.c CLEANFILES+= LintSysNormal.c LintSysNoerr.c LintSysPseudo.c -LintSysNormal.c: ${.CURDIR}/sys/makelintstub.sh \ +LintSysNormal.c: ${LIBCSRCDIR}/sys/makelintstub.sh \ ${DESTDIR}/usr/include/sys/syscall.h @echo creating ${.TARGET} - @sh ${.CURDIR}/sys/makelintstub.sh -o ${.TARGET} \ + @sh ${LIBCSRCDIR}/sys/makelintstub.sh -o ${.TARGET} \ -s ${DESTDIR}/usr/include/sys/syscall.h ${ASM} -LintSysNoerr.c: ${.CURDIR}/sys/makelintstub.sh \ +LintSysNoerr.c: ${LIBCSRCDIR}/sys/makelintstub.sh \ ${DESTDIR}/usr/include/sys/syscall.h @echo creating ${.TARGET} - @sh ${.CURDIR}/sys/makelintstub.sh -o ${.TARGET} -n \ + @sh ${LIBCSRCDIR}/sys/makelintstub.sh -o ${.TARGET} -n \ -s ${DESTDIR}/usr/include/sys/syscall.h ${NOERR} -LintSysPseudoNoerr.c: ${.CURDIR}/sys/makelintstub.sh \ +LintSysPseudoNoerr.c: ${LIBCSRCDIR}/sys/makelintstub.sh \ ${DESTDIR}/usr/include/sys/syscall.h @echo creating ${.TARGET} - @sh ${.CURDIR}/sys/makelintstub.sh -o ${.TARGET} -p \ + @sh ${LIBCSRCDIR}/sys/makelintstub.sh -o ${.TARGET} -p \ -s ${DESTDIR}/usr/include/sys/syscall.h ${PSEUDONOERR} MAN+= accept.2 access.2 acct.2 adjtime.2 bind.2 brk.2 chdir.2 chflags.2 \ diff --git a/lib/libc/sys/ftruncate.c b/lib/libc/sys/ftruncate.c index 108c056185d..c7f4bf3a6b6 100644 --- a/lib/libc/sys/ftruncate.c +++ b/lib/libc/sys/ftruncate.c @@ -32,11 +32,12 @@ */ #if defined(SYSLIBC_SCCS) && !defined(lint) -static char rcsid[] = "$OpenBSD: ftruncate.c,v 1.6 1997/04/26 08:50:12 tholo Exp $"; +static char rcsid[] = "$OpenBSD: ftruncate.c,v 1.7 1998/11/20 11:18:52 d Exp $"; #endif /* SYSLIBC_SCCS and not lint */ #include <sys/types.h> #include <sys/syscall.h> +#include "thread_private.h" #ifdef lint quad_t __syscall(quad_t, ...); @@ -51,6 +52,13 @@ ftruncate(fd, length) int fd; off_t length; { + int retval; - return(__syscall((quad_t)SYS_ftruncate, fd, 0, length)); + if (_FD_LOCK(fd, FD_RDWR, NULL) != 0) { + retval = -1; + } else { + retval = __syscall((quad_t)SYS_ftruncate, fd, 0, length); + _FD_UNLOCK(fd, FD_RDWR); + } + return retval; } diff --git a/lib/libc/sys/lseek.c b/lib/libc/sys/lseek.c index 62f7d581b9e..12d2a98c86f 100644 --- a/lib/libc/sys/lseek.c +++ b/lib/libc/sys/lseek.c @@ -32,11 +32,12 @@ */ #if defined(SYSLIBC_SCCS) && !defined(lint) -static char rcsid[] = "$OpenBSD: lseek.c,v 1.5 1997/04/26 08:50:12 tholo Exp $"; +static char rcsid[] = "$OpenBSD: lseek.c,v 1.6 1998/11/20 11:18:53 d Exp $"; #endif /* SYSLIBC_SCCS and not lint */ #include <sys/types.h> #include <sys/syscall.h> +#include "thread_private.h" /* * This function provides 64-bit offset padding that @@ -49,6 +50,13 @@ lseek(fd, offset, whence) int whence; { extern off_t __syscall(); + off_t retval; - return(__syscall((quad_t)SYS_lseek, fd, 0, offset, whence)); + if (_FD_LOCK(fd, FD_RDWR, NULL) != 0) { + retval = -1; + } else { + retval = __syscall((quad_t)SYS_lseek, fd, 0, offset, whence); + _FD_UNLOCK(fd, FD_RDWR); + } + return retval; } diff --git a/lib/libc/termios/Makefile.inc b/lib/libc/termios/Makefile.inc index 7ca8c61e7a5..14002b3d3bc 100644 --- a/lib/libc/termios/Makefile.inc +++ b/lib/libc/termios/Makefile.inc @@ -1,6 +1,6 @@ -# $OpenBSD: Makefile.inc,v 1.2 1996/08/19 08:34:34 tholo Exp $ +# $OpenBSD: Makefile.inc,v 1.3 1998/11/20 11:18:54 d Exp $ -.PATH: ${.CURDIR}/termios +.PATH: ${LIBCSRCDIR}/termios SRCS+= cfgetispeed.c cfgetospeed.c cfmakeraw.c cfsetispeed.c cfsetospeed.c \ cfsetspeed.c tcdrain.c tcflow.c tcflush.c tcgetattr.c tcgetpgrp.c \ diff --git a/lib/libc/time/Makefile.inc b/lib/libc/time/Makefile.inc index 9da5655a284..58ca1bf782a 100644 --- a/lib/libc/time/Makefile.inc +++ b/lib/libc/time/Makefile.inc @@ -1,6 +1,6 @@ -# $OpenBSD: Makefile.inc,v 1.4 1998/02/04 22:22:42 deraadt Exp $ +# $OpenBSD: Makefile.inc,v 1.5 1998/11/20 11:18:55 d Exp $ -.PATH: ${.CURDIR}/time +.PATH: ${LIBCSRCDIR}/time SRCS+= asctime.c difftime.c localtime.c strftime.c strptime.c MAN+= ctime.3 strftime.3 time2posix.3 tzfile.5 tzset.3 strptime.3 diff --git a/lib/libc/time/asctime.c b/lib/libc/time/asctime.c index 690430a94b1..7b5257488cb 100644 --- a/lib/libc/time/asctime.c +++ b/lib/libc/time/asctime.c @@ -5,21 +5,23 @@ #if defined(LIBC_SCCS) && !defined(lint) && !defined(NOID) static char elsieid[] = "@(#)asctime.c 7.8"; -static char rcsid[] = "$OpenBSD: asctime.c,v 1.4 1998/01/18 23:24:50 millert Exp $"; +static char rcsid[] = "$OpenBSD: asctime.c,v 1.5 1998/11/20 11:18:55 d Exp $"; #endif /* LIBC_SCCS and not lint */ /*LINTLIBRARY*/ #include "private.h" #include "tzfile.h" +#include "thread_private.h" /* ** A la X3J11, with core dump avoidance. */ char * -asctime(timeptr) +asctime_r(timeptr, result) register const struct tm * timeptr; +char *result; { static const char wday_name[][3] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" @@ -28,17 +30,10 @@ register const struct tm * timeptr; "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; - /* - ** Big enough for something such as - ** ??? ???-2147483648 -2147483648:-2147483648:-2147483648 -2147483648\n - ** (two three-character abbreviations, five strings denoting integers, - ** three explicit spaces, two explicit colons, a newline, - ** and a trailing ASCII nul). - */ - static char result[3 * 2 + 5 * INT_STRLEN_MAXIMUM(int) + - 3 + 2 + 1 + 1]; + register const char * wn; register const char * mn; + int size; if (timeptr->tm_wday < 0 || timeptr->tm_wday >= DAYSPERWEEK) wn = "???"; @@ -51,10 +46,38 @@ register const struct tm * timeptr; ** "%.3s %.3s%3d %02.2d:%02.2d:%02.2d %d\n" ** Since the .2 in 02.2d is ignored, we drop it. */ - (void) sprintf(result, "%.3s %.3s%3d %02d:%02d:%02d %d\n", + /* + * P1003 8.3.5.2 says that asctime_r() can only assume at most + * a 26 byte buffer. *XXX* + */ + size = snprintf(result, 26, "%.3s %.3s%3d %02d:%02d:%02d %d\n", wn, mn, timeptr->tm_mday, timeptr->tm_hour, timeptr->tm_min, timeptr->tm_sec, TM_YEAR_BASE + timeptr->tm_year); + if (size >= 26) + return NULL; return result; } + +/* + * Theoretically, the worst case string is of the form + * "www mmm-2147483648 -2147483648:-2147483648:-2147483648 -2147483648\n" + * but we only provide space for 26 since asctime_r won't use any more. + * "www mmmddd hh:mm:ss yyyy\n" + */ +char * +asctime(timeptr) +const struct tm * timeptr; +{ + + static char result[26]; + _THREAD_PRIVATE_KEY(asctime) + char *resultp = (char*) _THREAD_PRIVATE(asctime, result, NULL); + + if (resultp == NULL) + return NULL; + else + return asctime_r(timeptr, resultp); +} + diff --git a/lib/libc/time/localtime.c b/lib/libc/time/localtime.c index e8154d297ba..dc638321a7f 100644 --- a/lib/libc/time/localtime.c +++ b/lib/libc/time/localtime.c @@ -5,7 +5,7 @@ #if defined(LIBC_SCCS) && !defined(lint) && !defined(NOID) static char elsieid[] = "@(#)localtime.c 7.64"; -static char rcsid[] = "$OpenBSD: localtime.c,v 1.13 1998/07/11 23:17:20 deraadt Exp $"; +static char rcsid[] = "$OpenBSD: localtime.c,v 1.14 1998/11/20 11:18:55 d Exp $"; #endif /* LIBC_SCCS and not lint */ /* @@ -19,6 +19,7 @@ static char rcsid[] = "$OpenBSD: localtime.c,v 1.13 1998/07/11 23:17:20 deraadt #include "private.h" #include "tzfile.h" #include "fcntl.h" +#include "thread_private.h" /* ** SunOS 4.1.1 headers lack O_BINARY. @@ -166,6 +167,8 @@ static struct state gmtmem; static char lcl_TZname[TZ_STRLEN_MAX + 1]; static int lcl_is_set; static int gmt_is_set; +_THREAD_PRIVATE_MUTEX(lcl); +_THREAD_PRIVATE_MUTEX(gmt); char * tzname[2] = { wildabbr, @@ -920,16 +923,9 @@ struct state * const sp; if (tzload(gmt, sp) != 0) (void) tzparse(gmt, sp, TRUE); } - -#ifndef STD_INSPIRED -/* -** A non-static declaration of tzsetwall in a system header file -** may cause a warning about this upcoming static declaration... -*/ static -#endif /* !defined STD_INSPIRED */ void -tzsetwall P((void)) +tzsetwall_basic P((void)) { if (lcl_is_set < 0) return; @@ -949,14 +945,30 @@ tzsetwall P((void)) settzname(); } +#ifndef STD_INSPIRED +/* +** A non-static declaration of tzsetwall in a system header file +** may cause a warning about this upcoming static declaration... +*/ +static +#endif /* !defined STD_INSPIRED */ +void +tzsetwall P((void)) +{ + _THREAD_PRIVATE_MUTEX_LOCK(lcl); + tzsetwall_basic(); + _THREAD_PRIVATE_MUTEX_UNLOCK(lcl); +} + +static void -tzset P((void)) +tzset_basic P((void)) { register const char * name; name = getenv("TZ"); if (name == NULL) { - tzsetwall(); + tzsetwall_basic(); return; } @@ -990,6 +1002,14 @@ tzset P((void)) settzname(); } +void +tzset P((void)) +{ + _THREAD_PRIVATE_MUTEX_LOCK(lcl); + tzset_basic(); + _THREAD_PRIVATE_MUTEX_UNLOCK(lcl); +} + /* ** The easy way to behave "as if no library function calls" localtime ** is to not call it--so we drop its guts into "localsub", which can be @@ -1047,12 +1067,27 @@ struct tm * const tmp; } struct tm * +localtime_r(timep, p_tm) +const time_t * const timep; +struct tm *p_tm; +{ + _THREAD_PRIVATE_MUTEX_LOCK(lcl); + tzset_basic(); + localsub(timep, 0L, p_tm); + _THREAD_PRIVATE_MUTEX_UNLOCK(lcl); + return p_tm; +} + +struct tm * localtime(timep) const time_t * const timep; { - tzset(); - localsub(timep, 0L, &tm); - return &tm; + _THREAD_PRIVATE_KEY(localtime) + struct tm * p_tm = (struct tm*)_THREAD_PRIVATE(localtime, tm, NULL); + + if (p_tm == NULL) + return NULL; + return localtime_r(timep, p_tm); } /* @@ -1065,6 +1100,7 @@ const time_t * const timep; const long offset; struct tm * const tmp; { + _THREAD_PRIVATE_MUTEX_LOCK(gmt); if (!gmt_is_set) { gmt_is_set = TRUE; #ifdef ALL_STATE @@ -1073,6 +1109,7 @@ struct tm * const tmp; #endif /* defined ALL_STATE */ gmtload(gmtptr); } + _THREAD_PRIVATE_MUTEX_UNLOCK(gmt); timesub(timep, offset, gmtptr, tmp); #ifdef TM_ZONE /* @@ -1096,11 +1133,25 @@ struct tm * const tmp; } struct tm * +gmtime_r(timep, p_tm) +const time_t * timep; +struct tm * p_tm; +{ + gmtsub(timep, 0L, p_tm); + return p_tm; +} + +struct tm * gmtime(timep) const time_t * const timep; { - gmtsub(timep, 0L, &tm); - return &tm; + _THREAD_PRIVATE_KEY(gmtime) + struct tm * p_tm = (struct tm*) _THREAD_PRIVATE(gmtime, tm, NULL); + + if (p_tm == NULL) + return NULL; + return gmtime_r(timep, p_tm); + } #ifdef STD_INSPIRED @@ -1230,6 +1281,15 @@ const time_t * const timep; return asctime(localtime(timep)); } +char * +ctime_r(timep, buf) +const time_t * const timep; +char * buf; +{ + struct tm tm; + return asctime_r(localtime_r(timep, &tm), buf); +} + /* ** Adapted from code provided by Robert Elz, who writes: ** The "best" way to do mktime I think is based on an idea of Bob @@ -1534,8 +1594,13 @@ time_t mktime(tmp) struct tm * const tmp; { - tzset(); - return time1(tmp, localsub, 0L); + time_t ret; + + _THREAD_PRIVATE_MUTEX_LOCK(lcl); + tzset_basic(); + ret = time1(tmp, localsub, 0L); + _THREAD_PRIVATE_MUTEX_UNLOCK(lcl); + return ret; } #ifdef STD_INSPIRED diff --git a/lib/libc/yp/Makefile.inc b/lib/libc/yp/Makefile.inc index 8790fca3009..80fdff63f21 100644 --- a/lib/libc/yp/Makefile.inc +++ b/lib/libc/yp/Makefile.inc @@ -1,7 +1,7 @@ -# $OpenBSD: Makefile.inc,v 1.3 1996/07/01 07:08:52 deraadt Exp $ +# $OpenBSD: Makefile.inc,v 1.4 1998/11/20 11:18:56 d Exp $ # yp sources -.PATH: ${.CURDIR}/arch/${MACHINE_ARCH}/yp ${.CURDIR}/yp +.PATH: ${LIBCSRCDIR}/arch/${MACHINE_ARCH}/yp ${LIBCSRCDIR}/yp SRCS+= ypmatch_cache.c yp_bind.c yp_get_default_domain.c \ yp_first.c yp_all.c yp_order.c \ diff --git a/lib/libc_r/Makefile b/lib/libc_r/Makefile index c7b3ee47d5b..8c75d02a794 100644 --- a/lib/libc_r/Makefile +++ b/lib/libc_r/Makefile @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile,v 1.2 1998/11/09 03:12:53 d Exp $ +# $OpenBSD: Makefile,v 1.3 1998/11/20 11:18:57 d Exp $ # # All library objects contain sccsid strings by default; they may be # excluded as a space-saving measure. To produce a library that does @@ -40,8 +40,10 @@ HIDDEN_SYSCALLS= accept.o bind.o close.o connect.o dup.o dup2.o \ signanosleep.o socket.o socketpair.o wait4.o write.o writev.o \ _exit.o getlogin.o -.include "${.CURDIR}/../libc/Makefile.inc" -# let manual pages be built/installed from libc +LIBCSRCDIR = ${.CURDIR}/../libc +.include "${LIBCSRCDIR}/Makefile.inc" + +# libc man pages built/installed by libc MAN= MLINKS= diff --git a/lib/libc_r/TODO b/lib/libc_r/TODO index ab6650e1db3..b643c668900 100644 --- a/lib/libc_r/TODO +++ b/lib/libc_r/TODO @@ -1,12 +1,18 @@ This is a list of things that still need to be done: -* Fix the totally bogus _atomic_lock in arch/mips +* Fix the totally bogus _atomic_lock in arch/mips. + This is a nightmare because you really need to incur a ctxt switch + for pmaxen. R4000 has some instructions to do an atomic test-and-set + but not mips1. fuck fuck fuck. the ctx switch has to be sigsetmask * Add thread stuff to the other archs in libc/arch/; ie change some usages of ENTRY to SYSENTRY in some .S files and add the new macros to their SYS.h. Only the i386 arch is working. +* Finish/verify the md stuff for setjmp and atomic_lock for sparc and + alpha. Boy do they look dodgy. + * Find a better way of doing the _sys_aliases stuff; ideally by using the linker. jmp's (current implementation) are ugly, but I don't want to have to hack ../libc/sys/Makefile.inc just to @@ -17,16 +23,25 @@ This is a list of things that still need to be done: instead of libc_r.a ... some kind of shlib common problem? may need to use crt0 instead? __CTOR_LIST__ is just not being updated by ld. Frankly this one is driving me nuts. Maybe <bsd.lib.mk> is wrong. - -* Signals seem to cause busy-hangs. SIGINFO especially. test_sock_2 - fails because of its use of signals. still dumps /tmp/uthread.dump - though - needs debugging. + This varies on different architectures and exec format. * Test the objective-C compiler to see if it can use threads. + Really this shows up a nasty problem with needing to say -threads + whenever you want to use threads. Ideally, linking with -lc_r library + would override -lc; but the _POSIX_THREADS define would alter + some optimisations done in <stdio.h>... What to do!?! FreeBSD looks + like they are going to have an always-threaded libc with optimisations + for the 1-thread case and use weak symbols to override functions when + someone uses -lc_r. Do we have weak symbols everywhere? I wonder + what the performance hit is if #define LOCK() if(nthreads>1){lock();} + is used... need to profile this. * Find all the static buffers in libc and make them per-thread. This is not absolutely necessary but incurs no penalty for single-threaded operation, and makes life easier & safer for when threads are used. + On the other hand, it adds a bit more code bloat when you use threads + and makes openbsd programs non-portable to other OSs with less-nicer + libraries. If we do the bare posix minimum, we promote portable code. * Look into how netbsd are going with their kernel threads @@ -40,4 +55,9 @@ This is a list of things that still need to be done: May also need to document "This is not thread-safe" for some library functions (yet to be identified). -$OpenBSD: TODO,v 1.2 1998/11/09 03:12:54 d Exp $ +* Find out where freebsd/netbsd use pread() and pwrite().. i think + its in the database routines mostly. We will need to rip their code. + +* Figure out what to do with the configuration system variables (_SC_*) + +$OpenBSD: TODO,v 1.3 1998/11/20 11:18:57 d Exp $ |