|
Packit |
6c4009 |
/* Reentrant function to return the current login name. Unix version.
|
|
Packit |
6c4009 |
Copyright (C) 1991-2018 Free Software Foundation, Inc.
|
|
Packit |
6c4009 |
This file is part of the GNU C Library.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit |
6c4009 |
modify it under the terms of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License as published by the Free Software Foundation; either
|
|
Packit |
6c4009 |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit |
6c4009 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
6c4009 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
6c4009 |
Lesser General Public License for more details.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
You should have received a copy of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License along with the GNU C Library; if not, see
|
|
Packit |
6c4009 |
<http://www.gnu.org/licenses/>. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <errno.h>
|
|
Packit |
6c4009 |
#include <unistd.h>
|
|
Packit |
6c4009 |
#include <string.h>
|
|
Packit |
6c4009 |
#include <stdio.h>
|
|
Packit |
6c4009 |
#include <limits.h>
|
|
Packit |
6c4009 |
#include <fcntl.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <utmp.h>
|
|
Packit |
6c4009 |
#include "../login/utmp-private.h"
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Return at most NAME_LEN characters of the login name of the user in NAME.
|
|
Packit |
6c4009 |
If it cannot be determined or some other error occurred, return the error
|
|
Packit |
6c4009 |
code. Otherwise return 0. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#ifdef STATIC
|
|
Packit |
6c4009 |
STATIC
|
|
Packit |
6c4009 |
#endif
|
|
Packit |
6c4009 |
int
|
|
Packit |
6c4009 |
__getlogin_r (char *name, size_t name_len)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
char tty_pathname[2 + 2 * NAME_MAX];
|
|
Packit |
6c4009 |
char *real_tty_path = tty_pathname;
|
|
Packit |
6c4009 |
int result;
|
|
Packit |
6c4009 |
struct utmp *ut, line, buffer;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Get name of tty connected to fd 0. Return if not a tty or
|
|
Packit |
6c4009 |
if fd 0 isn't open. Note that a lot of documentation says that
|
|
Packit |
6c4009 |
getlogin() is based on the controlling terminal---what they
|
|
Packit |
6c4009 |
really mean is "the terminal connected to standard input". The
|
|
Packit |
6c4009 |
getlogin() implementation of DEC Unix, SunOS, Solaris, HP-UX all
|
|
Packit |
6c4009 |
return NULL if fd 0 has been closed, so this is the compatible
|
|
Packit |
6c4009 |
thing to do. Note that ttyname(open("/dev/tty")) on those
|
|
Packit |
6c4009 |
systems returns /dev/tty, so that is not a possible solution for
|
|
Packit |
6c4009 |
getlogin(). */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
result = __ttyname_r (0, real_tty_path, sizeof (tty_pathname));
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (result != 0)
|
|
Packit |
6c4009 |
return result;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
real_tty_path += 5; /* Remove "/dev/". */
|
|
Packit |
6c4009 |
strncpy (line.ut_line, real_tty_path, sizeof line.ut_line);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* We don't use the normal entry points __setutent et al, because we
|
|
Packit |
6c4009 |
want setutent + getutline_r + endutent all to happen with the lock
|
|
Packit |
6c4009 |
held so that our search is thread-safe. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
__libc_lock_lock (__libc_utmp_lock);
|
|
Packit Service |
6de65a |
(*__libc_utmp_jump_table->setutent) ();
|
|
Packit Service |
6de65a |
result = (*__libc_utmp_jump_table->getutline_r) (&line, &buffer, &ut);
|
|
Packit |
6c4009 |
if (result < 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
if (errno == ESRCH)
|
|
Packit |
6c4009 |
/* The caller expects ENOENT if nothing is found. */
|
|
Packit |
6c4009 |
result = ENOENT;
|
|
Packit |
6c4009 |
else
|
|
Packit |
6c4009 |
result = errno;
|
|
Packit |
6c4009 |
}
|
|
Packit Service |
6de65a |
(*__libc_utmp_jump_table->endutent) ();
|
|
Packit Service |
6de65a |
__libc_utmp_jump_table = &__libc_utmp_unknown_functions;
|
|
Packit |
6c4009 |
__libc_lock_unlock (__libc_utmp_lock);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (result == 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
size_t needed = __strnlen (ut->ut_user, UT_NAMESIZE) + 1;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (needed > name_len)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
__set_errno (ERANGE);
|
|
Packit |
6c4009 |
result = ERANGE;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
else
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
memcpy (name, ut->ut_user, needed - 1);
|
|
Packit |
6c4009 |
name[needed - 1] = 0;
|
|
Packit |
6c4009 |
result = 0;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
return result;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
#ifndef STATIC
|
|
Packit |
6c4009 |
libc_hidden_def (__getlogin_r)
|
|
Packit |
6c4009 |
weak_alias (__getlogin_r, getlogin_r)
|
|
Packit |
6c4009 |
libc_hidden_weak (getlogin_r)
|
|
Packit |
6c4009 |
#endif
|