Blame man3/getlogin.3

Packit 7cfc04
.\" Copyright 1995  James R. Van Zandt <jrv@vanzandt.mv.com>
Packit 7cfc04
.\"
Packit 7cfc04
.\" %%%LICENSE_START(VERBATIM)
Packit 7cfc04
.\" Permission is granted to make and distribute verbatim copies of this
Packit 7cfc04
.\" manual provided the copyright notice and this permission notice are
Packit 7cfc04
.\" preserved on all copies.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Permission is granted to copy and distribute modified versions of this
Packit 7cfc04
.\" manual under the conditions for verbatim copying, provided that the
Packit 7cfc04
.\" entire resulting derived work is distributed under the terms of a
Packit 7cfc04
.\" permission notice identical to this one.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Since the Linux kernel and libraries are constantly changing, this
Packit 7cfc04
.\" manual page may be incorrect or out-of-date.  The author(s) assume no
Packit 7cfc04
.\" responsibility for errors or omissions, or for damages resulting from
Packit 7cfc04
.\" the use of the information contained herein.  The author(s) may not
Packit 7cfc04
.\" have taken the same level of care in the production of this manual,
Packit 7cfc04
.\" which is licensed free of charge, as they might when working
Packit 7cfc04
.\" professionally.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Formatted or processed versions of this manual, if unaccompanied by
Packit 7cfc04
.\" the source, must acknowledge the copyright and authors of this work.
Packit 7cfc04
.\" %%%LICENSE_END
Packit 7cfc04
.\"
Packit 7cfc04
.\" Changed Tue Sep 19 01:49:29 1995, aeb: moved from man2 to man3
Packit 7cfc04
.\"  added ref to /etc/utmp, added BUGS section, etc.
Packit 7cfc04
.\" modified 2003 Walter Harms, aeb - added getlogin_r, note on stdin use
Packit 7cfc04
.TH GETLOGIN 3 2017-09-15 "GNU" "Linux Programmer's Manual"
Packit 7cfc04
.SH NAME
Packit 7cfc04
getlogin, getlogin_r, cuserid \- get username
Packit 7cfc04
.SH SYNOPSIS
Packit 7cfc04
.B #include <unistd.h>
Packit 7cfc04
.PP
Packit 7cfc04
.B "char *getlogin(void);"
Packit 7cfc04
.br
Packit 7cfc04
.BI "int getlogin_r(char *" buf ", size_t " bufsize );
Packit 7cfc04
.PP
Packit 7cfc04
.B #include <stdio.h>
Packit 7cfc04
.PP
Packit 7cfc04
.BI "char *cuserid(char *" string );
Packit 7cfc04
.PP
Packit 7cfc04
.in -4n
Packit 7cfc04
Feature Test Macro Requirements for glibc (see
Packit 7cfc04
.BR feature_test_macros (7)):
Packit 7cfc04
.in
Packit 7cfc04
.PP
Packit 7cfc04
.BR getlogin_r ():
Packit 7cfc04
.\" Deprecated: _REENTRANT ||
Packit 7cfc04
_POSIX_C_SOURCE\ >=\ 199506L
Packit 7cfc04
.PP
Packit 7cfc04
.BR cuserid ():
Packit 7cfc04
.nf
Packit 7cfc04
    Since glibc 2.24:
Packit 7cfc04
        (_XOPEN_SOURCE && ! (_POSIX_C_SOURCE >= 200112L)
Packit 7cfc04
        || __GNU_SOURCE
Packit 7cfc04
    Up to and including glibc 2.23:
Packit 7cfc04
        _XOPEN_SOURCE
Packit 7cfc04
.fi
Packit 7cfc04
.SH DESCRIPTION
Packit 7cfc04
.BR getlogin ()
Packit 7cfc04
returns a pointer to a string containing the name of
Packit 7cfc04
the user logged in on the controlling terminal of the process, or a
Packit 7cfc04
null pointer if this information cannot be determined.
Packit 7cfc04
The string is
Packit 7cfc04
statically allocated and might be overwritten on subsequent calls to
Packit 7cfc04
this function or to
Packit 7cfc04
.BR cuserid ().
Packit 7cfc04
.PP
Packit 7cfc04
.BR getlogin_r ()
Packit 7cfc04
returns this same username in the array
Packit 7cfc04
.I buf
Packit 7cfc04
of size
Packit 7cfc04
.IR bufsize .
Packit 7cfc04
.PP
Packit 7cfc04
.BR cuserid ()
Packit 7cfc04
returns a pointer to a string containing a username
Packit 7cfc04
associated with the effective user ID of the process.
Packit 7cfc04
If \fIstring\fP
Packit 7cfc04
is not a null pointer, it should be an array that can hold at least
Packit 7cfc04
\fBL_cuserid\fP characters; the string is returned in this array.
Packit 7cfc04
Otherwise, a pointer to a string in a static area is returned.
Packit 7cfc04
This
Packit 7cfc04
string is statically allocated and might be overwritten on subsequent
Packit 7cfc04
calls to this function or to
Packit 7cfc04
.BR getlogin ().
Packit 7cfc04
.PP
Packit 7cfc04
The macro \fBL_cuserid\fP is an integer constant that indicates how
Packit 7cfc04
long an array you might need to store a username.
Packit 7cfc04
\fBL_cuserid\fP is declared in \fI<stdio.h>\fP.
Packit 7cfc04
.PP
Packit 7cfc04
These functions let your program identify positively the user who is
Packit 7cfc04
running
Packit 7cfc04
.RB ( cuserid ())
Packit 7cfc04
or the user who logged in this session
Packit 7cfc04
.RB ( getlogin ()).
Packit 7cfc04
(These can differ when set-user-ID programs are involved.)
Packit 7cfc04
.PP
Packit 7cfc04
For most purposes, it is more useful to use the environment variable
Packit 7cfc04
\fBLOGNAME\fP to find out who the user is.
Packit 7cfc04
This is more flexible
Packit 7cfc04
precisely because the user can set \fBLOGNAME\fP arbitrarily.
Packit 7cfc04
.SH RETURN VALUE
Packit 7cfc04
.BR getlogin ()
Packit 7cfc04
returns a pointer to the username when successful,
Packit 7cfc04
and NULL on failure, with
Packit 7cfc04
.I errno
Packit 7cfc04
set to indicate the cause of the error.
Packit 7cfc04
.BR getlogin_r ()
Packit 7cfc04
returns 0 when successful, and nonzero on failure.
Packit 7cfc04
.SH ERRORS
Packit 7cfc04
POSIX specifies
Packit 7cfc04
.TP
Packit 7cfc04
.B EMFILE
Packit 7cfc04
The per-process limit on the number of open file descriptors has been reached.
Packit 7cfc04
.TP
Packit 7cfc04
.B ENFILE
Packit 7cfc04
The system-wide limit on the total number of open files has been reached.
Packit 7cfc04
.TP
Packit 7cfc04
.B ENXIO
Packit 7cfc04
The calling process has no controlling terminal.
Packit 7cfc04
.TP
Packit 7cfc04
.B ERANGE
Packit 7cfc04
(getlogin_r)
Packit 7cfc04
The length of the username, including the terminating null byte (\(aq\\0\(aq),
Packit 7cfc04
is larger than
Packit 7cfc04
.IR bufsize .
Packit 7cfc04
.PP
Packit 7cfc04
Linux/glibc also has
Packit 7cfc04
.TP
Packit 7cfc04
.B ENOENT
Packit 7cfc04
There was no corresponding entry in the utmp-file.
Packit 7cfc04
.TP
Packit 7cfc04
.B ENOMEM
Packit 7cfc04
Insufficient memory to allocate passwd structure.
Packit 7cfc04
.TP
Packit 7cfc04
.B ENOTTY
Packit 7cfc04
Standard input didn't refer to a terminal.
Packit 7cfc04
(See BUGS.)
Packit 7cfc04
.SH FILES
Packit 7cfc04
.TP
Packit 7cfc04
\fI/etc/passwd\fP
Packit 7cfc04
password database file
Packit 7cfc04
.TP
Packit 7cfc04
\fI/var/run/utmp\fP
Packit 7cfc04
(traditionally \fI/etc/utmp\fP;
Packit 7cfc04
some libc versions used \fI/var/adm/utmp\fP)
Packit 7cfc04
.SH ATTRIBUTES
Packit 7cfc04
For an explanation of the terms used in this section, see
Packit 7cfc04
.BR attributes (7).
Packit 7cfc04
.TS
Packit 7cfc04
allbox;
Packit 7cfc04
lb lb lb
Packit 7cfc04
l l l.
Packit 7cfc04
Interface	Attribute	Value
Packit 7cfc04
T{
Packit 7cfc04
.BR getlogin ()
Packit 7cfc04
T}	Thread safety	T{
Packit 7cfc04
MT-Unsafe race:getlogin race:utent
Packit 7cfc04
.br
Packit 7cfc04
sig:ALRM timer locale
Packit 7cfc04
T}
Packit 7cfc04
T{
Packit 7cfc04
.BR getlogin_r ()
Packit 7cfc04
T}	Thread safety	T{
Packit 7cfc04
MT-Unsafe race:utent sig:ALRM timer
Packit 7cfc04
.br
Packit 7cfc04
locale
Packit 7cfc04
T}
Packit 7cfc04
T{
Packit 7cfc04
.BR cuserid ()
Packit 7cfc04
T}	Thread safety	MT-Unsafe race:cuserid/!string locale
Packit 7cfc04
.TE
Packit 7cfc04
.sp 1
Packit 7cfc04
In the above table,
Packit 7cfc04
.I utent
Packit 7cfc04
in
Packit 7cfc04
.I race:utent
Packit 7cfc04
signifies that if any of the functions
Packit 7cfc04
.BR setutent (3),
Packit 7cfc04
.BR getutent (3),
Packit 7cfc04
or
Packit 7cfc04
.BR endutent (3)
Packit 7cfc04
are used in parallel in different threads of a program,
Packit 7cfc04
then data races could occur.
Packit 7cfc04
.BR getlogin ()
Packit 7cfc04
and
Packit 7cfc04
.BR getlogin_r ()
Packit 7cfc04
call those functions,
Packit 7cfc04
so we use race:utent to remind users.
Packit 7cfc04
.SH CONFORMING TO
Packit 7cfc04
.BR getlogin ()
Packit 7cfc04
and
Packit 7cfc04
.BR getlogin_r ():
Packit 7cfc04
POSIX.1-2001, POSIX.1-2008.
Packit 7cfc04
.PP
Packit 7cfc04
System V has a
Packit 7cfc04
.BR cuserid ()
Packit 7cfc04
function which uses the real
Packit 7cfc04
user ID rather than the effective user ID.
Packit 7cfc04
The
Packit 7cfc04
.BR cuserid ()
Packit 7cfc04
function
Packit 7cfc04
was included in the 1988 version of POSIX,
Packit 7cfc04
but removed from the 1990 version.
Packit 7cfc04
It was present in SUSv2, but removed in POSIX.1-2001.
Packit 7cfc04
.PP
Packit 7cfc04
OpenBSD has
Packit 7cfc04
.BR getlogin ()
Packit 7cfc04
and
Packit 7cfc04
.BR setlogin (),
Packit 7cfc04
and a username
Packit 7cfc04
associated with a session, even if it has no controlling terminal.
Packit 7cfc04
.SH BUGS
Packit 7cfc04
Unfortunately, it is often rather easy to fool
Packit 7cfc04
.BR getlogin ().
Packit 7cfc04
Sometimes it does not work at all, because some program messed up
Packit 7cfc04
the utmp file.
Packit 7cfc04
Often, it gives only the first 8 characters of
Packit 7cfc04
the login name.
Packit 7cfc04
The user currently logged in on the controlling terminal
Packit 7cfc04
of our program need not be the user who started it.
Packit 7cfc04
Avoid
Packit 7cfc04
.BR getlogin ()
Packit 7cfc04
for security-related purposes.
Packit 7cfc04
.PP
Packit 7cfc04
Note that glibc does not follow the POSIX specification and uses
Packit 7cfc04
.I stdin
Packit 7cfc04
instead of
Packit 7cfc04
.IR /dev/tty .
Packit 7cfc04
A bug.
Packit 7cfc04
(Other recent systems, like SunOS 5.8 and HP-UX 11.11 and FreeBSD 4.8
Packit 7cfc04
all return the login name also when
Packit 7cfc04
.I stdin
Packit 7cfc04
is redirected.)
Packit 7cfc04
.PP
Packit 7cfc04
Nobody knows precisely what
Packit 7cfc04
.BR cuserid ()
Packit 7cfc04
does; avoid it in portable programs.
Packit 7cfc04
Or avoid it altogether: use
Packit 7cfc04
.I getpwuid(geteuid())
Packit 7cfc04
instead, if that is
Packit 7cfc04
what you meant.
Packit 7cfc04
.B Do not use
Packit 7cfc04
.BR cuserid ().
Packit 7cfc04
.SH SEE ALSO
Packit 7cfc04
.BR logname (1),
Packit 7cfc04
.BR geteuid (2),
Packit 7cfc04
.BR getuid (2),
Packit 7cfc04
.BR utmp (5)
Packit 7cfc04
.SH COLOPHON
Packit 7cfc04
This page is part of release 4.15 of the Linux
Packit 7cfc04
.I man-pages
Packit 7cfc04
project.
Packit 7cfc04
A description of the project,
Packit 7cfc04
information about reporting bugs,
Packit 7cfc04
and the latest version of this page,
Packit 7cfc04
can be found at
Packit 7cfc04
\%https://www.kernel.org/doc/man\-pages/.