2007-11-24 07:02:26 +00:00
|
|
|
.\" Hey Emacs! This file is -*- nroff -*- source.
|
2004-11-03 13:51:07 +00:00
|
|
|
.\"
|
|
|
|
.\" Copyright 1995 James R. Van Zandt <jrv@vanzandt.mv.com>
|
|
|
|
.\"
|
|
|
|
.\" Permission is granted to make and distribute verbatim copies of this
|
|
|
|
.\" manual provided the copyright notice and this permission notice are
|
|
|
|
.\" preserved on all copies.
|
|
|
|
.\"
|
|
|
|
.\" Permission is granted to copy and distribute modified versions of this
|
|
|
|
.\" manual under the conditions for verbatim copying, provided that the
|
|
|
|
.\" entire resulting derived work is distributed under the terms of a
|
|
|
|
.\" permission notice identical to this one.
|
2007-04-12 22:42:49 +00:00
|
|
|
.\"
|
2004-11-03 13:51:07 +00:00
|
|
|
.\" Since the Linux kernel and libraries are constantly changing, this
|
|
|
|
.\" manual page may be incorrect or out-of-date. The author(s) assume no
|
|
|
|
.\" responsibility for errors or omissions, or for damages resulting from
|
|
|
|
.\" the use of the information contained herein. The author(s) may not
|
|
|
|
.\" have taken the same level of care in the production of this manual,
|
|
|
|
.\" which is licensed free of charge, as they might when working
|
|
|
|
.\" professionally.
|
2007-04-12 22:42:49 +00:00
|
|
|
.\"
|
2004-11-03 13:51:07 +00:00
|
|
|
.\" Formatted or processed versions of this manual, if unaccompanied by
|
|
|
|
.\" the source, must acknowledge the copyright and authors of this work.
|
|
|
|
.\"
|
|
|
|
.\" Changed Tue Sep 19 01:49:29 1995, aeb: moved from man2 to man3
|
|
|
|
.\" added ref to /etc/utmp, added BUGS section, etc.
|
|
|
|
.\" modified 2003 Walter Harms, aeb - added getlogin_r, note on stdin use
|
2008-06-29 07:38:30 +00:00
|
|
|
.TH GETLOGIN 3 2008-06-29 "GNU" "Linux Programmer's Manual"
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH NAME
|
2008-06-30 14:36:59 +00:00
|
|
|
getlogin, getlogin_r, cuserid \- get username
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH SYNOPSIS
|
|
|
|
.B #include <unistd.h>
|
|
|
|
.sp
|
|
|
|
.B "char *getlogin(void);"
|
|
|
|
.br
|
|
|
|
.BI "int getlogin_r(char *" buf ", size_t " bufsize );
|
|
|
|
.sp
|
|
|
|
.B #include <stdio.h>
|
|
|
|
.sp
|
|
|
|
.BI "char *cuserid(char *" string );
|
2007-07-08 12:11:40 +00:00
|
|
|
.sp
|
|
|
|
.in -4n
|
|
|
|
Feature Test Macro Requirements for glibc (see
|
|
|
|
.BR feature_test_macros (7)):
|
|
|
|
.in
|
|
|
|
.sp
|
|
|
|
.BR getlogin_r ():
|
2007-07-08 12:39:24 +00:00
|
|
|
_REENTRANT || _POSIX_C_SOURCE\ >=\ 199506L
|
2007-07-08 12:11:40 +00:00
|
|
|
.br
|
|
|
|
.BR cuserid ():
|
|
|
|
_XOPEN_SOURCE
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH DESCRIPTION
|
2007-05-12 09:06:04 +00:00
|
|
|
.BR getlogin ()
|
|
|
|
returns a pointer to a string containing the name of
|
2004-11-03 13:51:07 +00:00
|
|
|
the user logged in on the controlling terminal of the process, or a
|
2011-09-28 04:25:05 +00:00
|
|
|
NULL pointer if this information cannot be determined.
|
2007-04-12 22:42:49 +00:00
|
|
|
The string is
|
2004-11-03 13:51:07 +00:00
|
|
|
statically allocated and might be overwritten on subsequent calls to
|
2007-05-12 09:06:04 +00:00
|
|
|
this function or to
|
|
|
|
.BR cuserid ().
|
2004-11-03 13:51:07 +00:00
|
|
|
.PP
|
2007-05-12 09:06:04 +00:00
|
|
|
.BR getlogin_r ()
|
2008-06-30 14:36:59 +00:00
|
|
|
returns this same username in the array
|
2004-11-03 13:51:07 +00:00
|
|
|
.I buf
|
|
|
|
of size
|
|
|
|
.IR bufsize .
|
|
|
|
.PP
|
2007-05-12 09:06:04 +00:00
|
|
|
.BR cuserid ()
|
2008-06-30 14:36:59 +00:00
|
|
|
returns a pointer to a string containing a username
|
2007-04-12 22:42:49 +00:00
|
|
|
associated with the effective user ID of the process.
|
|
|
|
If \fIstring\fP
|
2011-09-28 04:25:05 +00:00
|
|
|
is not a NULL pointer, it should be an array that can hold at least
|
2004-11-03 13:51:07 +00:00
|
|
|
\fBL_cuserid\fP characters; the string is returned in this array.
|
2007-04-12 22:42:49 +00:00
|
|
|
Otherwise, a pointer to a string in a static area is returned.
|
|
|
|
This
|
2004-11-03 13:51:07 +00:00
|
|
|
string is statically allocated and might be overwritten on subsequent
|
2007-05-12 09:06:04 +00:00
|
|
|
calls to this function or to
|
|
|
|
.BR getlogin ().
|
2004-11-03 13:51:07 +00:00
|
|
|
.PP
|
|
|
|
The macro \fBL_cuserid\fP is an integer constant that indicates how
|
2008-06-30 14:36:59 +00:00
|
|
|
long an array you might need to store a username.
|
2007-05-12 13:22:06 +00:00
|
|
|
\fBL_cuserid\fP is declared in \fI<stdio.h>\fP.
|
2004-11-03 13:51:07 +00:00
|
|
|
.PP
|
|
|
|
These functions let your program identify positively the user who is
|
2007-06-21 22:55:04 +00:00
|
|
|
running
|
2007-05-12 09:06:04 +00:00
|
|
|
.RB ( cuserid ())
|
|
|
|
or the user who logged in this session
|
|
|
|
.RB ( getlogin ()).
|
|
|
|
(These can differ when set-user-ID programs are involved.)
|
2004-11-03 13:51:07 +00:00
|
|
|
.PP
|
|
|
|
For most purposes, it is more useful to use the environment variable
|
2007-04-12 22:42:49 +00:00
|
|
|
\fBLOGNAME\fP to find out who the user is.
|
|
|
|
This is more flexible
|
2004-11-03 13:51:07 +00:00
|
|
|
precisely because the user can set \fBLOGNAME\fP arbitrarily.
|
|
|
|
.SH "RETURN VALUE"
|
2007-05-12 09:06:04 +00:00
|
|
|
.BR getlogin ()
|
2008-06-30 14:36:59 +00:00
|
|
|
returns a pointer to the username when successful,
|
2004-11-03 13:51:07 +00:00
|
|
|
and NULL on failure.
|
2007-05-12 09:06:04 +00:00
|
|
|
.BR getlogin_r ()
|
intro.1, time.1, adjtimex.2, capget.2, eventfd.2, fcntl.2, getrlimit.2, getsockopt.2, gettimeofday.2, intro.2, ioctl_list.2, ioperm.2, mlock.2, pivot_root.2, poll.2, prctl.2, ptrace.2, sched_setscheduler.2, select_tut.2, semget.2, sigaltstack.2, signalfd.2, sysctl.2, timer_settime.2, timerfd_create.2, wait.2, CPU_SET.3, argz_add.3, assert_perror.3, atexit.3, backtrace.3, bcmp.3, clearenv.3, ctime.3, dl_iterate_phdr.3, dlopen.3, ecvt.3, errno.3, error.3, ether_aton.3, exit.3, fenv.3, ferror.3, finite.3, flockfile.3, fnmatch.3, fpathconf.3, fpclassify.3, ftime.3, ftok.3, ftw.3, fwide.3, getaddrinfo.3, gethostbyname.3, getlogin.3, getnameinfo.3, getnetent.3, getopt.3, getprotoent.3, getrpcent.3, getservent.3, glob.3, hsearch.3, inet.3, isalpha.3, iswalnum.3, iswalpha.3, iswblank.3, iswcntrl.3, iswctype.3, iswdigit.3, iswgraph.3, iswlower.3, iswprint.3, iswpunct.3, iswspace.3, iswupper.3, iswxdigit.3, longjmp.3, lsearch.3, malloc.3, matherr.3, mblen.3, mbsinit.3, mbtowc.3, on_exit.3, printf.3, pthread_attr_init.3, pthread_attr_setaffinity_np.3, pthread_attr_setdetachstate.3, pthread_attr_setguardsize.3, pthread_attr_setinheritsched.3, pthread_attr_setschedparam.3, pthread_attr_setschedpolicy.3, pthread_attr_setscope.3, pthread_attr_setstack.3, pthread_attr_setstackaddr.3, pthread_attr_setstacksize.3, pthread_cancel.3, pthread_cleanup_push.3, pthread_equal.3, pthread_getattr_np.3, pthread_getcpuclockid.3, pthread_setaffinity_np.3, pthread_setcancelstate.3, pthread_setconcurrency.3, pthread_setschedparam.3, pthread_setschedprio.3, ptsname.3, putenv.3, putgrent.3, raise.3, rcmd.3, regex.3, rexec.3, rpc.3, rpmatch.3, rtnetlink.3, scandir.3, sem_init.3, setaliasent.3, setbuf.3, setenv.3, setjmp.3, signbit.3, stdio_ext.3, strtod.3, strtol.3, strtoul.3, system.3, termios.3, timeradd.3, tzset.3, ualarm.3, wctomb.3, xdr.3, st.4, tty_ioctl.4, core.5, elf.5, proc.5, bootparam.7, capabilities.7, icmp.7, ip.7, ipv6.7, math_error.7, mdoc.samples.7, mq_overview.7, pthreads.7, raw.7, regex.7, socket.7, tcp.7, tzselect.8: Global fix: s/non-zero/nonzero/
The tendency in English, as prescribed in style guides like
Chicago MoS, is towards removing hyphens after prefixes
like "non-" etc.
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2010-01-16 16:40:55 +00:00
|
|
|
returns 0 when successful, and nonzero on failure.
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH ERRORS
|
|
|
|
POSIX specifies
|
|
|
|
.TP
|
|
|
|
.B EMFILE
|
|
|
|
The calling process already has the maximum allowed number of open files.
|
|
|
|
.TP
|
|
|
|
.B ENFILE
|
|
|
|
The system already has the maximum allowed number of open files.
|
|
|
|
.TP
|
|
|
|
.B ENXIO
|
|
|
|
The calling process has no controlling tty.
|
|
|
|
.TP
|
|
|
|
.B ERANGE
|
|
|
|
(getlogin_r)
|
2008-06-30 14:36:59 +00:00
|
|
|
The length of the username, including the terminating null byte,
|
2006-01-13 02:09:44 +00:00
|
|
|
is larger than
|
2004-11-03 13:51:07 +00:00
|
|
|
.IR bufsize .
|
|
|
|
.LP
|
|
|
|
Linux/glibc also has
|
|
|
|
.TP
|
|
|
|
.B ENOENT
|
|
|
|
There was no corresponding entry in the utmp-file.
|
|
|
|
.TP
|
|
|
|
.B ENOMEM
|
|
|
|
Insufficient memory to allocate passwd structure.
|
2008-06-29 07:34:44 +00:00
|
|
|
.TP
|
|
|
|
.B ENOTTY
|
|
|
|
Standard input didn't refer to a terminal.
|
|
|
|
(See BUGS.)
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH FILES
|
2007-10-18 06:09:42 +00:00
|
|
|
.TP
|
|
|
|
\fI/etc/passwd\fP
|
|
|
|
password database file
|
|
|
|
.TP
|
|
|
|
\fI/var/run/utmp\fP
|
|
|
|
(traditionally \fI/etc/utmp\fP;
|
|
|
|
some libc versions used \fI/var/adm/utmp\fP)
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH "CONFORMING TO"
|
2006-08-03 13:57:30 +00:00
|
|
|
.BR getlogin ()
|
|
|
|
and
|
|
|
|
.BR getlogin_r ()
|
|
|
|
specified in POSIX.1-2001.
|
|
|
|
|
2007-05-12 09:06:04 +00:00
|
|
|
System V has a
|
|
|
|
.BR cuserid ()
|
|
|
|
function which uses the real
|
2007-04-12 22:42:49 +00:00
|
|
|
user ID rather than the effective user ID.
|
2007-05-12 09:06:04 +00:00
|
|
|
The
|
|
|
|
.BR cuserid ()
|
|
|
|
function
|
2007-04-12 22:42:49 +00:00
|
|
|
was included in the 1988 version of POSIX,
|
2006-08-03 13:57:30 +00:00
|
|
|
but removed from the 1990 version.
|
|
|
|
It was present in SUSv2, but removed in POSIX.1-2001.
|
2004-11-03 13:51:07 +00:00
|
|
|
.LP
|
2007-05-12 09:06:04 +00:00
|
|
|
OpenBSD has
|
|
|
|
.BR getlogin ()
|
|
|
|
and
|
|
|
|
.BR setlogin (),
|
|
|
|
and a username
|
2004-11-03 13:51:07 +00:00
|
|
|
associated with a session, even if it has no controlling tty.
|
|
|
|
.SH BUGS
|
2007-04-12 22:42:49 +00:00
|
|
|
Unfortunately, it is often rather easy to fool
|
2005-10-19 14:48:35 +00:00
|
|
|
.BR getlogin ().
|
2004-11-03 13:51:07 +00:00
|
|
|
Sometimes it does not work at all, because some program messed up
|
2007-04-12 22:42:49 +00:00
|
|
|
the utmp file.
|
|
|
|
Often, it gives only the first 8 characters of
|
|
|
|
the login name.
|
|
|
|
The user currently logged in on the controlling tty
|
2004-11-03 13:51:07 +00:00
|
|
|
of our program need not be the user who started it.
|
2007-04-12 22:42:49 +00:00
|
|
|
Avoid
|
2005-10-19 14:48:35 +00:00
|
|
|
.BR getlogin ()
|
|
|
|
for security-related purposes.
|
2004-11-03 13:51:07 +00:00
|
|
|
.LP
|
2009-02-20 18:14:34 +00:00
|
|
|
Note that glibc does not follow the POSIX specification and uses
|
2006-08-03 13:57:30 +00:00
|
|
|
.I stdin
|
2004-11-03 13:51:07 +00:00
|
|
|
instead of
|
|
|
|
.IR /dev/tty .
|
2007-04-12 22:42:49 +00:00
|
|
|
A bug.
|
|
|
|
(Other recent systems, like SunOS 5.8 and HP-UX 11.11 and FreeBSD 4.8
|
|
|
|
all return the login name also when
|
|
|
|
.I stdin
|
2006-08-03 13:57:30 +00:00
|
|
|
is redirected.)
|
2004-11-03 13:51:07 +00:00
|
|
|
.LP
|
2007-04-12 22:42:49 +00:00
|
|
|
Nobody knows precisely what
|
|
|
|
.BR cuserid ()
|
2005-10-19 14:48:35 +00:00
|
|
|
does; avoid it in portable programs.
|
2007-04-12 22:42:49 +00:00
|
|
|
Or avoid it altogether: use
|
2005-10-19 14:48:35 +00:00
|
|
|
.I getpwuid(geteuid())
|
|
|
|
instead, if that is
|
2004-11-03 13:51:07 +00:00
|
|
|
what you meant.
|
2007-12-14 13:23:07 +00:00
|
|
|
.B Do not use
|
2005-10-19 14:48:35 +00:00
|
|
|
.BR cuserid ().
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH "SEE ALSO"
|
|
|
|
.BR geteuid (2),
|
2008-06-29 04:07:23 +00:00
|
|
|
.BR getuid (2),
|
|
|
|
.BR utmp (5)
|