{"id":7191,"date":"2022-12-20T19:35:46","date_gmt":"2022-12-20T22:35:46","guid":{"rendered":"http:\/\/lode.uno\/linux-man\/index.php\/2022\/12\/20\/getlogin-man3\/"},"modified":"2022-12-20T19:35:46","modified_gmt":"2022-12-20T22:35:46","slug":"getlogin-man3","status":"publish","type":"post","link":"https:\/\/lode.uno\/linux-man\/2022\/12\/20\/getlogin-man3\/","title":{"rendered":"GETLOGIN (man3)"},"content":{"rendered":"<h1 align=\"center\">GETLOGIN<\/h1>\n<p> <a href=\"#NAME\">NAME<\/a><br \/> <a href=\"#SYNOPSIS\">SYNOPSIS<\/a><br \/> <a href=\"#DESCRIPTION\">DESCRIPTION<\/a><br \/> <a href=\"#RETURN VALUE\">RETURN VALUE<\/a><br \/> <a href=\"#ERRORS\">ERRORS<\/a><br \/> <a href=\"#FILES\">FILES<\/a><br \/> <a href=\"#ATTRIBUTES\">ATTRIBUTES<\/a><br \/> <a href=\"#CONFORMING TO\">CONFORMING TO<\/a><br \/> <a href=\"#BUGS\">BUGS<\/a><br \/> <a href=\"#SEE ALSO\">SEE ALSO<\/a><br \/> <a href=\"#COLOPHON\">COLOPHON<\/a> <\/p>\n<hr>\n<h2>NAME <a name=\"NAME\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">getlogin, getlogin_r, cuserid \u2212 get username<\/p>\n<h2>SYNOPSIS <a name=\"SYNOPSIS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>#include <unistd.h><\/b><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>char *getlogin(void); <br \/> int getlogin_r(char *<\/b><i>buf<\/i><b>, size_t<\/b> <i>bufsize<\/i><b>);<\/b><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>#include <stdio.h><\/b><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>char *cuserid(char *<\/b><i>string<\/i><b>);<\/b><\/p>\n<p style=\"margin-left:5%; margin-top: 1em\">Feature Test Macro Requirements for glibc (see <b>feature_test_macros<\/b>(7)):<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>getlogin_r<\/b>(): _POSIX_C_SOURCE\u00a0>=\u00a0199506L<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>cuserid<\/b>(): <br \/> Since glibc 2.24: <br \/> (_XOPEN_SOURCE &#038;&#038; ! (_POSIX_C_SOURCE >= 200112L) <br \/> || _GNU_SOURCE <br \/> Up to and including glibc 2.23: <br \/> _XOPEN_SOURCE<\/p>\n<h2>DESCRIPTION <a name=\"DESCRIPTION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>getlogin<\/b>() returns a pointer to a string containing the name of the user logged in on the controlling terminal of the process, or a null pointer if this information cannot be determined. The string is statically allocated and might be overwritten on subsequent calls to this function or to <b>cuserid<\/b>().<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>getlogin_r<\/b>() returns this same username in the array <i>buf<\/i> of size <i>bufsize<\/i>.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>cuserid<\/b>() returns a pointer to a string containing a username associated with the effective user ID of the process. If <i>string<\/i> is not a null pointer, it should be an array that can hold at least <b>L_cuserid<\/b> characters; the string is returned in this array. Otherwise, a pointer to a string in a static area is returned. This string is statically allocated and might be overwritten on subsequent calls to this function or to <b>getlogin<\/b>().<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The macro <b>L_cuserid<\/b> is an integer constant that indicates how long an array you might need to store a username. <b>L_cuserid<\/b> is declared in <i><stdio.h><\/i>.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">These functions let your program identify positively the user who is running (<b>cuserid<\/b>()) or the user who logged in this session (<b>getlogin<\/b>()). (These can differ when set-user-ID programs are involved.)<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">For most purposes, it is more useful to use the environment variable <b>LOGNAME<\/b> to find out who the user is. This is more flexible precisely because the user can set <b>LOGNAME<\/b> arbitrarily.<\/p>\n<h2>RETURN VALUE <a name=\"RETURN VALUE\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>getlogin<\/b>() returns a pointer to the username when successful, and NULL on failure, with <i>errno<\/i> set to indicate the cause of the error. <b>getlogin_r<\/b>() returns 0 when successful, and nonzero on failure.<\/p>\n<h2>ERRORS <a name=\"ERRORS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">POSIX specifies:<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\">\n<p><b>EMFILE<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"78%\">\n<p>The per-process limit on the number of open file descriptors has been reached.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\">\n<p><b>ENFILE<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"78%\">\n<p>The system-wide limit on the total number of open files has been reached.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\">\n<p><b>ENXIO<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"78%\">\n<p>The calling process has no controlling terminal.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\">\n<p><b>ERANGE<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"78%\">\n<p>(getlogin_r) The length of the username, including the terminating null byte (&#8216;\u0000&#8217;), is larger than <i>bufsize<\/i>.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%; margin-top: 1em\">Linux\/glibc also has:<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\">\n<p style=\"margin-top: 1em\"><b>ENOENT<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"78%\">\n<p style=\"margin-top: 1em\">There was no corresponding entry in the utmp-file.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\">\n<p><b>ENOMEM<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"78%\">\n<p>Insufficient memory to allocate passwd structure.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\">\n<p><b>ENOTTY<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"78%\">\n<p>Standard input didn\u2019t refer to a terminal. (See BUGS.)<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<h2>FILES <a name=\"FILES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><i>\/etc\/passwd<\/i><\/p>\n<p style=\"margin-left:22%;\">password database file<\/p>\n<p style=\"margin-left:11%;\"><i>\/var\/run\/utmp<\/i><\/p>\n<p style=\"margin-left:22%;\">(traditionally <i>\/etc\/utmp<\/i>; some libc versions used <i>\/var\/adm\/utmp<\/i>)<\/p>\n<h2>ATTRIBUTES <a name=\"ATTRIBUTES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">For an explanation of the terms used in this section, see <b>attributes<\/b>(7).<\/p>\n<p align=\"center\" style=\"margin-top: 1em\"><img decoding=\"async\" src=\"grohtml-1660611.png\" alt=\"Image grohtml-1660611.png\"><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">In the above table, <i>utent<\/i> in <i>race:utent<\/i> signifies that if any of the functions <b>setutent<\/b>(3), <b>getutent<\/b>(3), or <b>endutent<\/b>(3) are used in parallel in different threads of a program, then data races could occur. <b>getlogin<\/b>() and <b>getlogin_r<\/b>() call those functions, so we use race:utent to remind users.<\/p>\n<h2>CONFORMING TO <a name=\"CONFORMING TO\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>getlogin<\/b>() and <b>getlogin_r<\/b>(): POSIX.1-2001, POSIX.1-2008.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">System V has a <b>cuserid<\/b>() function which uses the real user ID rather than the effective user ID. The <b>cuserid<\/b>() function was included in the 1988 version of POSIX, but removed from the 1990 version. It was present in SUSv2, but removed in POSIX.1-2001.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">OpenBSD has <b>getlogin<\/b>() and <b>setlogin<\/b>(), and a username associated with a session, even if it has no controlling terminal.<\/p>\n<h2>BUGS <a name=\"BUGS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Unfortunately, it is often rather easy to fool <b>getlogin<\/b>(). Sometimes it does not work at all, because some program messed up the utmp file. Often, it gives only the first 8 characters of the login name. The user currently logged in on the controlling terminal of our program need not be the user who started it. Avoid <b>getlogin<\/b>() for security-related purposes.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Note that glibc does not follow the POSIX specification and uses <i>stdin<\/i> instead of <i>\/dev\/tty<\/i>. 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<\/i> is redirected.)<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Nobody knows precisely what <b>cuserid<\/b>() does; avoid it in portable programs. Or avoid it altogether: use <i>getpwuid(geteuid())<\/i> instead, if that is what you meant. <b>Do not use cuserid<\/b>().<\/p>\n<h2>SEE ALSO <a name=\"SEE ALSO\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>logname<\/b>(1), <b>geteuid<\/b>(2), <b>getuid<\/b>(2), <b>utmp<\/b>(5)<\/p>\n<h2>COLOPHON <a name=\"COLOPHON\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">This page is part of release 5.10 of the Linux <i>man-pages<\/i> project. A description of the project, information about reporting bugs, and the latest version of this page, can be found at https:\/\/www.kernel.org\/doc\/man\u2212pages\/.<\/p>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>  getlogin, getlogin_r, cuserid \u2212 get username <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2536],"tags":[2538,1711,3007],"class_list":["post-7191","post","type-post","status-publish","format-standard","hentry","category-3-llamadas-de-bibliotecas","tag-2538","tag-getlogin","tag-man3"],"gutentor_comment":0,"_links":{"self":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/7191","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/comments?post=7191"}],"version-history":[{"count":0,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/7191\/revisions"}],"wp:attachment":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/media?parent=7191"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/categories?post=7191"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/tags?post=7191"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}