getlogin.c revision 1.10
1/* $OpenBSD: getlogin.c,v 1.10 2005/08/08 08:05:34 espie Exp $ */ 2/* 3 * Copyright (c) 1988, 1993 4 * The Regents of the University of California. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. Neither the name of the University nor the names of its contributors 15 * may be used to endorse or promote products derived from this software 16 * without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31#include <sys/param.h> 32#include <pwd.h> 33#include <utmp.h> 34#include <stdio.h> 35#include <string.h> 36#include <unistd.h> 37#include <errno.h> 38#include "thread_private.h" 39 40_THREAD_PRIVATE_MUTEX(logname); 41static int logname_valid = 0; 42static char logname[MAXLOGNAME + 1]; 43 44int _getlogin(char *, size_t); 45int _setlogin(const char *); 46 47char * 48getlogin(void) 49{ 50 _THREAD_PRIVATE_KEY(getlogin); 51 char * name = (char *)_THREAD_PRIVATE(getlogin, logname, NULL); 52 53 if ((errno = getlogin_r(name, sizeof logname)) != 0) 54 return NULL; 55 if (*name == '\0') { 56 errno = ENOENT; /* well? */ 57 return NULL; 58 } 59 return name; 60} 61 62int 63getlogin_r(char *name, size_t namelen) 64{ 65 int logname_size; 66 67 if (name == NULL) 68 return EFAULT; 69 70 _THREAD_PRIVATE_MUTEX_LOCK(logname); 71 if (!logname_valid) { 72 if (_getlogin(logname, sizeof(logname) - 1) < 0) { 73 _THREAD_PRIVATE_MUTEX_UNLOCK(logname); 74 return errno; 75 } 76 logname_valid = 1; 77 logname[MAXLOGNAME] = '\0'; /* paranoia */ 78 } 79 logname_size = strlen(logname) + 1; 80 if (namelen < logname_size) { 81 _THREAD_PRIVATE_MUTEX_UNLOCK(logname); 82 return ERANGE; 83 } 84 memcpy(name, logname, logname_size); 85 _THREAD_PRIVATE_MUTEX_UNLOCK(logname); 86 return 0; 87} 88 89int 90setlogin(const char *name) 91{ 92 int ret; 93 94 _THREAD_PRIVATE_MUTEX_LOCK(logname); 95 ret = _setlogin(name); 96 if (ret == 0) 97 logname_valid = 0; 98 _THREAD_PRIVATE_MUTEX_UNLOCK(logname); 99 return ret; 100} 101