diff options
| author | ad <ad@NetBSD.org> | 2008-06-25 11:10:24 +0000 |
|---|---|---|
| committer | ad <ad@NetBSD.org> | 2008-06-25 11:10:24 +0000 |
| commit | 5f64faa3a91c551e42e464c3f9d1c0cf8ebd690f (patch) | |
| tree | 3c7e2e4c424532e2a1363db4daa2690d6c869f10 /lib/libc | |
| parent | d29b52efe8625150feb6300703349f98a3511230 (diff) | |
Add getlogin_r. Manual page changes mostly lifted from FreeBSD.
Diffstat (limited to 'lib/libc')
| -rw-r--r-- | lib/libc/gen/getlogin.c | 79 | ||||
| -rw-r--r-- | lib/libc/include/namespace.h | 3 | ||||
| -rw-r--r-- | lib/libc/sys/Makefile.inc | 3 | ||||
| -rw-r--r-- | lib/libc/sys/getlogin.2 | 36 |
4 files changed, 109 insertions, 12 deletions
diff --git a/lib/libc/gen/getlogin.c b/lib/libc/gen/getlogin.c index ce5d67818c7..ebcfe4b56e6 100644 --- a/lib/libc/gen/getlogin.c +++ b/lib/libc/gen/getlogin.c @@ -1,4 +1,30 @@ -/* $NetBSD: getlogin.c,v 1.12 2003/08/07 16:42:50 agc Exp $ */ +/* $NetBSD: getlogin.c,v 1.13 2008/06/25 11:10:24 ad Exp $ */ + +/*- + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * 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. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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. + */ /* * Copyright (c) 1988, 1993 @@ -34,7 +60,7 @@ #if 0 static char sccsid[] = "@(#)getlogin.c 8.1 (Berkeley) 6/4/93"; #else -__RCSID("$NetBSD: getlogin.c,v 1.12 2003/08/07 16:42:50 agc Exp $"); +__RCSID("$NetBSD: getlogin.c,v 1.13 2008/06/25 11:10:24 ad Exp $"); #endif #endif /* LIBC_SCCS and not lint */ @@ -45,26 +71,67 @@ __RCSID("$NetBSD: getlogin.c,v 1.12 2003/08/07 16:42:50 agc Exp $"); #include <stdio.h> #include <string.h> #include <unistd.h> +#include <errno.h> +#include "reentrant.h" #include "extern.h" #ifdef __weak_alias __weak_alias(getlogin,_getlogin) +__weak_alias(getlogin_r,_getlogin_r) __weak_alias(setlogin,_setlogin) #endif int __logname_valid; /* known to setlogin() */ +static char logname[MAXLOGNAME + 1]; + +#ifdef _REENTRANT +static mutex_t logname_mutex = MUTEX_INITIALIZER; +#endif char * -getlogin() +getlogin(void) { - static char logname[MAXLOGNAME + 1]; + char *rv; + mutex_lock(&logname_mutex); if (__logname_valid == 0) { - if (__getlogin(logname, sizeof(logname) - 1) < 0) + if (__getlogin(logname, sizeof(logname) - 1) < 0) { + mutex_unlock(&logname_mutex); return ((char *)NULL); + } + __logname_valid = 1; + } + rv = (*logname ? logname : (char *)NULL); + mutex_unlock(&logname_mutex); + + return rv; +} + +int +getlogin_r(char *name, int namelen) +{ + size_t len; + int rv; + + mutex_lock(&logname_mutex); + if (__logname_valid == 0) { + if (__getlogin(logname, sizeof(logname) - 1) < 0) { + rv = errno; + mutex_unlock(&logname_mutex); + return (rv); + } __logname_valid = 1; } - return (*logname ? logname : (char *)NULL); + len = strlen(logname) + 1; + if (len > namelen) { + rv = ERANGE; + } else { + strncpy(name, logname, len); + rv = 0; + } + mutex_unlock(&logname_mutex); + + return (rv); } int diff --git a/lib/libc/include/namespace.h b/lib/libc/include/namespace.h index ebc26370ddf..daed09708df 100644 --- a/lib/libc/include/namespace.h +++ b/lib/libc/include/namespace.h @@ -1,4 +1,4 @@ -/* $NetBSD: namespace.h,v 1.131 2008/04/28 20:23:00 martin Exp $ */ +/* $NetBSD: namespace.h,v 1.132 2008/06/25 11:10:25 ad Exp $ */ /*- * Copyright (c) 1997-2004 The NetBSD Foundation, Inc. @@ -281,6 +281,7 @@ #define getifaddrs _getifaddrs #define getloadavg _getloadavg #define getlogin _getlogin +#define getlogin_r _getlogin_r #define getmntinfo _getmntinfo #define getmode _getmode #define getnameinfo _getnameinfo diff --git a/lib/libc/sys/Makefile.inc b/lib/libc/sys/Makefile.inc index 7e91155be24..04d44b5f58b 100644 --- a/lib/libc/sys/Makefile.inc +++ b/lib/libc/sys/Makefile.inc @@ -1,4 +1,4 @@ -# $NetBSD: Makefile.inc,v 1.188 2008/06/15 20:36:52 christos Exp $ +# $NetBSD: Makefile.inc,v 1.189 2008/06/25 11:10:25 ad Exp $ # @(#)Makefile.inc 8.3 (Berkeley) 10/24/94 # sys sources @@ -253,6 +253,7 @@ MLINKS+=getcontext.2 setcontext.2 MLINKS+=getgid.2 getegid.2 MLINKS+=getitimer.2 setitimer.2 MLINKS+=getlogin.2 setlogin.2 +MLINKS+=getlogin_r.2 setlogin_r.2 MLINKS+=getpgrp.2 getpgid.2 MLINKS+=getpid.2 getppid.2 MLINKS+=getpriority.2 setpriority.2 diff --git a/lib/libc/sys/getlogin.2 b/lib/libc/sys/getlogin.2 index 203a355ba2e..5152d0cbd04 100644 --- a/lib/libc/sys/getlogin.2 +++ b/lib/libc/sys/getlogin.2 @@ -1,4 +1,4 @@ -.\" $NetBSD: getlogin.2,v 1.19 2004/05/13 10:20:58 wiz Exp $ +.\" $NetBSD: getlogin.2,v 1.20 2008/06/25 11:10:25 ad Exp $ .\" .\" Copyright (c) 1989, 1991, 1993 .\" The Regents of the University of California. All rights reserved. @@ -29,11 +29,12 @@ .\" .\" @(#)getlogin.2 8.1 (Berkeley) 6/9/93 .\" -.Dd August 11, 2002 +.Dd June 25, 2008 .Dt GETLOGIN 2 .Os .Sh NAME .Nm getlogin , +.Nm getlogin_r , .Nm setlogin .Nd get/set login name .Sh LIBRARY @@ -43,6 +44,8 @@ .Ft char * .Fn getlogin void .Ft int +.Fn getlogin_r "char *name" "int len" +.Ft int .Fn setlogin "const char *name" .Sh DESCRIPTION The @@ -59,6 +62,21 @@ for example when .Xr su 1 is used.) .Pp +The +.Fn getlogin_r +function +provides the same service as +.Fn getlogin , +however the caller must provide the buffer +.Fa name +with length +.Fa len +bytes +to hold the result. +The buffer should be at least +.Dv MAXLOGNAME +bytes in length. +.Pp .Fn setlogin sets the login name of the user associated with the current session to .Fa name . @@ -119,6 +137,7 @@ If a call to succeeds, it returns a pointer to a null-terminated string in a static buffer. If the name has not been set, it returns .Dv NULL . +.Pp If a call to .Fn setlogin succeeds, a value of 0 is returned. @@ -127,6 +146,11 @@ If fails, a value of \-1 is returned and an error code is placed in the global location .Va errno . +.Pp +The +.Fn getlogin_r +function +returns zero if successful, or the error number upon failure. .Sh ERRORS The following errors may be returned by these calls: .Bl -tag -width Er @@ -147,14 +171,18 @@ Login names are limited to characters, currently 16. .It Bq Er EPERM The caller tried to set the login name and was not the super-user. +.It Bq Er ERANGE +The size of the buffer is smaller than the result to be returned. .El .Sh SEE ALSO .Xr setsid 2 .Sh STANDARDS The .Fn getlogin -function conforms to -.St -p1003.1-90 . +and +.Fn getlogin_r +functions conform to +.St -p1003.1-96 . .Sh HISTORY The .Fn getlogin |
