xref: /minix3/lib/libc/gen/getlogin.c (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
12fe8fb19SBen Gras /*	$NetBSD: getlogin.c,v 1.15 2009/01/11 02:46:27 christos Exp $	*/
22fe8fb19SBen Gras 
32fe8fb19SBen Gras /*-
42fe8fb19SBen Gras  * Copyright (c) 2008 The NetBSD Foundation, Inc.
52fe8fb19SBen Gras  * All rights reserved.
62fe8fb19SBen Gras  *
72fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
82fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
92fe8fb19SBen Gras  * are met:
102fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
112fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
122fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
132fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
142fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
152fe8fb19SBen Gras  *
162fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
172fe8fb19SBen Gras  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
182fe8fb19SBen Gras  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
192fe8fb19SBen Gras  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
202fe8fb19SBen Gras  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
212fe8fb19SBen Gras  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
222fe8fb19SBen Gras  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
232fe8fb19SBen Gras  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
242fe8fb19SBen Gras  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
252fe8fb19SBen Gras  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
262fe8fb19SBen Gras  * POSSIBILITY OF SUCH DAMAGE.
272fe8fb19SBen Gras  */
282fe8fb19SBen Gras 
292fe8fb19SBen Gras /*
302fe8fb19SBen Gras  * Copyright (c) 1988, 1993
312fe8fb19SBen Gras  *	The Regents of the University of California.  All rights reserved.
322fe8fb19SBen Gras  *
332fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
342fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
352fe8fb19SBen Gras  * are met:
362fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
372fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
382fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
392fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
402fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
412fe8fb19SBen Gras  * 3. Neither the name of the University nor the names of its contributors
422fe8fb19SBen Gras  *    may be used to endorse or promote products derived from this software
432fe8fb19SBen Gras  *    without specific prior written permission.
442fe8fb19SBen Gras  *
452fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
462fe8fb19SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
472fe8fb19SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
482fe8fb19SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
492fe8fb19SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
502fe8fb19SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
512fe8fb19SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
522fe8fb19SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
532fe8fb19SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
542fe8fb19SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
552fe8fb19SBen Gras  * SUCH DAMAGE.
562fe8fb19SBen Gras  */
572fe8fb19SBen Gras 
582fe8fb19SBen Gras #include <sys/cdefs.h>
592fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
602fe8fb19SBen Gras #if 0
612fe8fb19SBen Gras static char sccsid[] = "@(#)getlogin.c	8.1 (Berkeley) 6/4/93";
622fe8fb19SBen Gras #else
632fe8fb19SBen Gras __RCSID("$NetBSD: getlogin.c,v 1.15 2009/01/11 02:46:27 christos Exp $");
642fe8fb19SBen Gras #endif
652fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
662fe8fb19SBen Gras 
672fe8fb19SBen Gras #include "namespace.h"
682fe8fb19SBen Gras #include <sys/param.h>
692fe8fb19SBen Gras #include <pwd.h>
702fe8fb19SBen Gras #include <utmp.h>
712fe8fb19SBen Gras #include <stdio.h>
722fe8fb19SBen Gras #include <string.h>
732fe8fb19SBen Gras #include <unistd.h>
742fe8fb19SBen Gras #include <errno.h>
752fe8fb19SBen Gras #include "reentrant.h"
762fe8fb19SBen Gras #include "extern.h"
772fe8fb19SBen Gras 
782fe8fb19SBen Gras #ifdef __weak_alias
792fe8fb19SBen Gras __weak_alias(getlogin,_getlogin)
802fe8fb19SBen Gras __weak_alias(getlogin_r,_getlogin_r)
81*84d9c625SLionel Sambuc #if !defined(__minix)
822fe8fb19SBen Gras __weak_alias(setlogin,_setlogin)
83*84d9c625SLionel Sambuc #endif /* !defined(__minix) */
842fe8fb19SBen Gras #endif
852fe8fb19SBen Gras 
862fe8fb19SBen Gras int	__logname_valid;		/* known to setlogin() */
872fe8fb19SBen Gras static char logname[MAXLOGNAME + 1];
882fe8fb19SBen Gras 
892fe8fb19SBen Gras #ifdef _REENTRANT
902fe8fb19SBen Gras static mutex_t	logname_mutex = MUTEX_INITIALIZER;
912fe8fb19SBen Gras #endif
922fe8fb19SBen Gras 
932fe8fb19SBen Gras char *
getlogin(void)942fe8fb19SBen Gras getlogin(void)
952fe8fb19SBen Gras {
962fe8fb19SBen Gras 	char *rv;
972fe8fb19SBen Gras 
982fe8fb19SBen Gras 	mutex_lock(&logname_mutex);
992fe8fb19SBen Gras 	if (__logname_valid == 0) {
1002fe8fb19SBen Gras 		if (__getlogin(logname, sizeof(logname) - 1) < 0) {
1012fe8fb19SBen Gras 			mutex_unlock(&logname_mutex);
1022fe8fb19SBen Gras 			return ((char *)NULL);
1032fe8fb19SBen Gras 		}
1042fe8fb19SBen Gras 		__logname_valid = 1;
1052fe8fb19SBen Gras 	}
1062fe8fb19SBen Gras 	rv = (*logname ? logname : (char *)NULL);
1072fe8fb19SBen Gras 	mutex_unlock(&logname_mutex);
1082fe8fb19SBen Gras 
1092fe8fb19SBen Gras 	return rv;
1102fe8fb19SBen Gras }
1112fe8fb19SBen Gras 
1122fe8fb19SBen Gras int
getlogin_r(char * name,size_t namelen)1132fe8fb19SBen Gras getlogin_r(char *name, size_t namelen)
1142fe8fb19SBen Gras {
1152fe8fb19SBen Gras 	size_t len;
1162fe8fb19SBen Gras 	int rv;
1172fe8fb19SBen Gras 
1182fe8fb19SBen Gras 	mutex_lock(&logname_mutex);
1192fe8fb19SBen Gras 	if (__logname_valid == 0) {
1202fe8fb19SBen Gras 		if (__getlogin(logname, sizeof(logname) - 1) < 0) {
1212fe8fb19SBen Gras 			rv = errno;
1222fe8fb19SBen Gras 			mutex_unlock(&logname_mutex);
1232fe8fb19SBen Gras 			return (rv);
1242fe8fb19SBen Gras 		}
1252fe8fb19SBen Gras 		__logname_valid = 1;
1262fe8fb19SBen Gras 	}
1272fe8fb19SBen Gras 	len = strlen(logname) + 1;
1282fe8fb19SBen Gras 	if (len > namelen) {
1292fe8fb19SBen Gras 		rv = ERANGE;
1302fe8fb19SBen Gras 	} else {
1312fe8fb19SBen Gras 		strncpy(name, logname, len);
1322fe8fb19SBen Gras 		rv = 0;
1332fe8fb19SBen Gras 	}
1342fe8fb19SBen Gras 	mutex_unlock(&logname_mutex);
1352fe8fb19SBen Gras 
1362fe8fb19SBen Gras 	return (rv);
1372fe8fb19SBen Gras }
1382fe8fb19SBen Gras 
139*84d9c625SLionel Sambuc #if !defined(__minix)
1402fe8fb19SBen Gras int
setlogin(const char * name)1412fe8fb19SBen Gras setlogin(const char *name)
1422fe8fb19SBen Gras {
1432fe8fb19SBen Gras 	int retval;
1442fe8fb19SBen Gras 
1452fe8fb19SBen Gras 	retval = __setlogin(name);
1462fe8fb19SBen Gras 	__logname_valid = 0;
1472fe8fb19SBen Gras 
1482fe8fb19SBen Gras 	return (retval);
1492fe8fb19SBen Gras }
150*84d9c625SLionel Sambuc #endif /* !defined(__minix) */
151