1/* $NetBSD: getlogin.c,v 1.17 2024/01/20 14:52:47 christos Exp $ */ 2 3/*- 4 * Copyright (c) 2008 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29/* 30 * Copyright (c) 1988, 1993 31 * The Regents of the University of California. All rights reserved. 32 * 33 * Redistribution and use in source and binary forms, with or without 34 * modification, are permitted provided that the following conditions 35 * are met: 36 * 1. Redistributions of source code must retain the above copyright 37 * notice, this list of conditions and the following disclaimer. 38 * 2. Redistributions in binary form must reproduce the above copyright 39 * notice, this list of conditions and the following disclaimer in the 40 * documentation and/or other materials provided with the distribution. 41 * 3. Neither the name of the University nor the names of its contributors 42 * may be used to endorse or promote products derived from this software 43 * without specific prior written permission. 44 * 45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 55 * SUCH DAMAGE. 56 */ 57 58#include <sys/cdefs.h> 59#if defined(LIBC_SCCS) && !defined(lint) 60#if 0 61static char sccsid[] = "@(#)getlogin.c 8.1 (Berkeley) 6/4/93"; 62#else 63__RCSID("$NetBSD: getlogin.c,v 1.17 2024/01/20 14:52:47 christos Exp $"); 64#endif 65#endif /* LIBC_SCCS and not lint */ 66 67#include "namespace.h" 68#include <sys/param.h> 69#include <pwd.h> 70#include <utmp.h> 71#include <stdio.h> 72#include <string.h> 73#include <unistd.h> 74#include <errno.h> 75#include "reentrant.h" 76#include "extern.h" 77 78#ifdef __weak_alias 79__weak_alias(getlogin,_getlogin) 80__weak_alias(getlogin_r,_getlogin_r) 81__weak_alias(setlogin,_setlogin) 82#endif 83 84static int __logname_valid; /* known to setlogin() */ 85static char logname[MAXLOGNAME + 1]; 86 87#ifdef _REENTRANT 88static mutex_t logname_mutex = MUTEX_INITIALIZER; 89#endif 90 91char * 92getlogin(void) 93{ 94 char *rv; 95 96 mutex_lock(&logname_mutex); 97 if (__logname_valid == 0) { 98 if (__getlogin(logname, sizeof(logname) - 1) < 0) { 99 mutex_unlock(&logname_mutex); 100 return ((char *)NULL); 101 } 102 __logname_valid = 1; 103 } 104 rv = (*logname ? logname : (char *)NULL); 105 mutex_unlock(&logname_mutex); 106 107 return rv; 108} 109 110int 111getlogin_r(char *name, size_t namelen) 112{ 113 size_t len; 114 int rv; 115 116 mutex_lock(&logname_mutex); 117 if (__logname_valid == 0) { 118 if (__getlogin(logname, sizeof(logname) - 1) < 0) { 119 rv = errno; 120 mutex_unlock(&logname_mutex); 121 return (rv); 122 } 123 __logname_valid = 1; 124 } 125 len = strlen(logname) + 1; 126 if (len > namelen) { 127 rv = ERANGE; 128 } else { 129 strcpy(name, logname); 130 rv = 0; 131 } 132 mutex_unlock(&logname_mutex); 133 134 return (rv); 135} 136 137int 138setlogin(const char *name) 139{ 140 int retval; 141 142 retval = __setlogin(name); 143 __logname_valid = 0; 144 145 return (retval); 146} 147