xref: /illumos-gate/usr/src/lib/libc/port/gen/getlogin.c (revision 03100a6332bd4edc7a53091fcf7c9a7131bcdaa7)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*	Copyright (c) 1988 AT&T	*/
30 /*	  All Rights Reserved  	*/
31 
32 
33 #pragma weak getlogin = _getlogin
34 #pragma weak getlogin_r = _getlogin_r
35 
36 #include "synonyms.h"
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <fcntl.h>
40 #include <string.h>
41 #include <stdlib.h>
42 #include <limits.h>
43 #include "utmpx.h"
44 #include <unistd.h>
45 #include <errno.h>
46 #include <thread.h>
47 #include <synch.h>
48 #include <mtlib.h>
49 #include "tsd.h"
50 
51 /*
52  * XXX - _POSIX_LOGIN_NAME_MAX limits the length of a login name.  The utmpx
53  * interface provides for a 32 character login name, but for the sake of
54  * compatibility, we are still using the old utmp-imposed limit.
55  */
56 
57 /*
58  * POSIX.1c Draft-6 version of the function getlogin_r.
59  * It was implemented by Solaris 2.3.
60  */
61 char *
62 _getlogin_r(char *answer, int namelen)
63 {
64 	int		uf;
65 	off64_t		me;
66 	struct futmpx	ubuf;
67 
68 	if (namelen < _POSIX_LOGIN_NAME_MAX) {
69 		errno = ERANGE;
70 		return (NULL);
71 	}
72 
73 	if ((me = (off64_t)ttyslot()) < 0)
74 		return (NULL);
75 	if ((uf = open64(UTMPX_FILE, 0)) < 0)
76 		return (NULL);
77 	(void) lseek64(uf, me * sizeof (ubuf), SEEK_SET);
78 	if (read(uf, &ubuf, sizeof (ubuf)) != sizeof (ubuf)) {
79 		(void) close(uf);
80 		return (NULL);
81 	}
82 	(void) close(uf);
83 	if (ubuf.ut_user[0] == '\0')
84 		return (NULL);
85 	(void) strncpy(&answer[0], &ubuf.ut_user[0],
86 	    _POSIX_LOGIN_NAME_MAX - 1);
87 	answer[_POSIX_LOGIN_NAME_MAX - 1] = '\0';
88 	return (&answer[0]);
89 }
90 
91 /*
92  * POSIX.1c standard version of the function getlogin_r.
93  * User gets it via static getlogin_r from the header file.
94  */
95 int
96 __posix_getlogin_r(char *name, int namelen)
97 {
98 	int nerrno = 0;
99 	int oerrno = errno;
100 
101 	errno = 0;
102 	if (_getlogin_r(name, namelen) == NULL) {
103 		if (errno == 0)
104 			nerrno = EINVAL;
105 		else
106 			nerrno = errno;
107 	}
108 	errno = oerrno;
109 	return (nerrno);
110 }
111 
112 char *
113 getlogin(void)
114 {
115 	char *answer = tsdalloc(_T_LOGIN, _POSIX_LOGIN_NAME_MAX, NULL);
116 
117 	if (answer == NULL)
118 		return (NULL);
119 	return (_getlogin_r(answer, _POSIX_LOGIN_NAME_MAX));
120 }
121