xref: /onnv-gate/usr/src/lib/libc/port/gen/getlogin.c (revision 6812:febeba71273d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*6812Sraf  * Common Development and Distribution License (the "License").
6*6812Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*6812Sraf 
220Sstevel@tonic-gate /*
23*6812Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
30*6812Sraf #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
32*6812Sraf #pragma weak _getlogin = getlogin
33*6812Sraf #pragma weak _getlogin_r = getlogin_r
340Sstevel@tonic-gate 
35*6812Sraf #include "lint.h"
360Sstevel@tonic-gate #include <sys/types.h>
370Sstevel@tonic-gate #include <sys/stat.h>
380Sstevel@tonic-gate #include <fcntl.h>
390Sstevel@tonic-gate #include <string.h>
400Sstevel@tonic-gate #include <stdlib.h>
410Sstevel@tonic-gate #include <limits.h>
420Sstevel@tonic-gate #include "utmpx.h"
430Sstevel@tonic-gate #include <unistd.h>
440Sstevel@tonic-gate #include <errno.h>
450Sstevel@tonic-gate #include <thread.h>
460Sstevel@tonic-gate #include <synch.h>
470Sstevel@tonic-gate #include <mtlib.h>
480Sstevel@tonic-gate #include "tsd.h"
490Sstevel@tonic-gate 
500Sstevel@tonic-gate /*
510Sstevel@tonic-gate  * XXX - _POSIX_LOGIN_NAME_MAX limits the length of a login name.  The utmpx
520Sstevel@tonic-gate  * interface provides for a 32 character login name, but for the sake of
530Sstevel@tonic-gate  * compatibility, we are still using the old utmp-imposed limit.
540Sstevel@tonic-gate  */
550Sstevel@tonic-gate 
560Sstevel@tonic-gate /*
570Sstevel@tonic-gate  * POSIX.1c Draft-6 version of the function getlogin_r.
580Sstevel@tonic-gate  * It was implemented by Solaris 2.3.
590Sstevel@tonic-gate  */
600Sstevel@tonic-gate char *
getlogin_r(char * answer,int namelen)61*6812Sraf getlogin_r(char *answer, int namelen)
620Sstevel@tonic-gate {
630Sstevel@tonic-gate 	int		uf;
640Sstevel@tonic-gate 	off64_t		me;
650Sstevel@tonic-gate 	struct futmpx	ubuf;
660Sstevel@tonic-gate 
670Sstevel@tonic-gate 	if (namelen < _POSIX_LOGIN_NAME_MAX) {
680Sstevel@tonic-gate 		errno = ERANGE;
690Sstevel@tonic-gate 		return (NULL);
700Sstevel@tonic-gate 	}
710Sstevel@tonic-gate 
720Sstevel@tonic-gate 	if ((me = (off64_t)ttyslot()) < 0)
730Sstevel@tonic-gate 		return (NULL);
740Sstevel@tonic-gate 	if ((uf = open64(UTMPX_FILE, 0)) < 0)
750Sstevel@tonic-gate 		return (NULL);
760Sstevel@tonic-gate 	(void) lseek64(uf, me * sizeof (ubuf), SEEK_SET);
770Sstevel@tonic-gate 	if (read(uf, &ubuf, sizeof (ubuf)) != sizeof (ubuf)) {
780Sstevel@tonic-gate 		(void) close(uf);
790Sstevel@tonic-gate 		return (NULL);
800Sstevel@tonic-gate 	}
810Sstevel@tonic-gate 	(void) close(uf);
820Sstevel@tonic-gate 	if (ubuf.ut_user[0] == '\0')
830Sstevel@tonic-gate 		return (NULL);
840Sstevel@tonic-gate 	(void) strncpy(&answer[0], &ubuf.ut_user[0],
850Sstevel@tonic-gate 	    _POSIX_LOGIN_NAME_MAX - 1);
860Sstevel@tonic-gate 	answer[_POSIX_LOGIN_NAME_MAX - 1] = '\0';
870Sstevel@tonic-gate 	return (&answer[0]);
880Sstevel@tonic-gate }
890Sstevel@tonic-gate 
900Sstevel@tonic-gate /*
910Sstevel@tonic-gate  * POSIX.1c standard version of the function getlogin_r.
920Sstevel@tonic-gate  * User gets it via static getlogin_r from the header file.
930Sstevel@tonic-gate  */
940Sstevel@tonic-gate int
__posix_getlogin_r(char * name,int namelen)950Sstevel@tonic-gate __posix_getlogin_r(char *name, int namelen)
960Sstevel@tonic-gate {
970Sstevel@tonic-gate 	int nerrno = 0;
980Sstevel@tonic-gate 	int oerrno = errno;
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 	errno = 0;
101*6812Sraf 	if (getlogin_r(name, namelen) == NULL) {
1020Sstevel@tonic-gate 		if (errno == 0)
1030Sstevel@tonic-gate 			nerrno = EINVAL;
1040Sstevel@tonic-gate 		else
1050Sstevel@tonic-gate 			nerrno = errno;
1060Sstevel@tonic-gate 	}
1070Sstevel@tonic-gate 	errno = oerrno;
1080Sstevel@tonic-gate 	return (nerrno);
1090Sstevel@tonic-gate }
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate char *
getlogin(void)1120Sstevel@tonic-gate getlogin(void)
1130Sstevel@tonic-gate {
1140Sstevel@tonic-gate 	char *answer = tsdalloc(_T_LOGIN, _POSIX_LOGIN_NAME_MAX, NULL);
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	if (answer == NULL)
1170Sstevel@tonic-gate 		return (NULL);
118*6812Sraf 	return (getlogin_r(answer, _POSIX_LOGIN_NAME_MAX));
1190Sstevel@tonic-gate }
120