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/libc/gen/getlogin.c | |
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/libc/gen/getlogin.c')
-rw-r--r-- | lib/libc/gen/getlogin.c | 65 |
1 files changed, 57 insertions, 8 deletions
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; } |