1331722Seadler/*
21573Srgrimes * Copyright (c) 1988, 1993
31573Srgrimes *	The Regents of the University of California.  All rights reserved.
41573Srgrimes *
51573Srgrimes * Redistribution and use in source and binary forms, with or without
61573Srgrimes * modification, are permitted provided that the following conditions
71573Srgrimes * are met:
81573Srgrimes * 1. Redistributions of source code must retain the above copyright
91573Srgrimes *    notice, this list of conditions and the following disclaimer.
101573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111573Srgrimes *    notice, this list of conditions and the following disclaimer in the
121573Srgrimes *    documentation and/or other materials provided with the distribution.
131573Srgrimes * 4. Neither the name of the University nor the names of its contributors
141573Srgrimes *    may be used to endorse or promote products derived from this software
151573Srgrimes *    without specific prior written permission.
161573Srgrimes *
171573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271573Srgrimes * SUCH DAMAGE.
281573Srgrimes */
291573Srgrimes
301573Srgrimes#if defined(LIBC_SCCS) && !defined(lint)
311573Srgrimesstatic char sccsid[] = "@(#)getlogin.c	8.1 (Berkeley) 6/4/93";
321573Srgrimes#endif /* LIBC_SCCS and not lint */
3390039Sobrien#include <sys/cdefs.h>
3490039Sobrien__FBSDID("$FreeBSD$");
351573Srgrimes
361573Srgrimes#include <sys/param.h>
3770557Sdeischen#include <errno.h>
381573Srgrimes#include <pwd.h>
391573Srgrimes#include <stdio.h>
401573Srgrimes#include <string.h>
411573Srgrimes#include <unistd.h>
4271579Sdeischen#include "namespace.h"
4371579Sdeischen#include <pthread.h>
4471579Sdeischen#include "un-namespace.h"
451573Srgrimes
4693399Smarkm#include "libc_private.h"
471573Srgrimes
4871579Sdeischen#define	THREAD_LOCK()	if (__isthreaded) _pthread_mutex_lock(&logname_mutex)
4971579Sdeischen#define	THREAD_UNLOCK()	if (__isthreaded) _pthread_mutex_unlock(&logname_mutex)
5070557Sdeischen
5171579Sdeischenextern int		_getlogin(char *, int);
5270557Sdeischen
53254463Sjillesint			_logname_valid __hidden; /* known to setlogin() */
5471579Sdeischenstatic pthread_mutex_t	logname_mutex = PTHREAD_MUTEX_INITIALIZER;
5571579Sdeischen
5670557Sdeischenstatic char *
5770557Sdeischengetlogin_basic(int *status)
581573Srgrimes{
5923320Sache	static char logname[MAXLOGNAME];
601573Srgrimes
611573Srgrimes	if (_logname_valid == 0) {
6270557Sdeischen		if (_getlogin(logname, sizeof(logname)) < 0) {
6370557Sdeischen			*status = errno;
6470557Sdeischen			return (NULL);
6570557Sdeischen		}
661573Srgrimes		_logname_valid = 1;
671573Srgrimes	}
6870557Sdeischen	*status = 0;
6970557Sdeischen	return (*logname ? logname : NULL);
701573Srgrimes}
7153859Swes
7270557Sdeischenchar *
7370557Sdeischengetlogin(void)
7470557Sdeischen{
7570557Sdeischen	char	*result;
7670557Sdeischen	int	status;
7753859Swes
7870557Sdeischen	THREAD_LOCK();
7970557Sdeischen	result = getlogin_basic(&status);
8070557Sdeischen	THREAD_UNLOCK();
8170557Sdeischen	return (result);
8270557Sdeischen}
8370557Sdeischen
8470557Sdeischenint
8553859Swesgetlogin_r(char *logname, int namelen)
8653859Swes{
8770557Sdeischen	char	*result;
8870557Sdeischen	int	len;
8970557Sdeischen	int	status;
90264161Smarcel
91264161Smarcel	if (namelen < 1)
92264161Smarcel		return (ERANGE);
93264161Smarcel	logname[0] = '\0';
94264161Smarcel
9570557Sdeischen	THREAD_LOCK();
9670557Sdeischen	result = getlogin_basic(&status);
97264161Smarcel	if (status == 0 && result != NULL) {
98264161Smarcel		len = strlen(result) + 1;
99264161Smarcel		if (len > namelen)
10070557Sdeischen			status = ERANGE;
10170557Sdeischen		else
10270557Sdeischen			strncpy(logname, result, len);
10353859Swes	}
10470557Sdeischen	THREAD_UNLOCK();
10570557Sdeischen	return (status);
10653859Swes}
107