GETLOGIN(2)
π GETLOGIN π (section 2, the system calls one, bestie)
okay so you want to know who's logged in?? and like, set who's logged in?? this is THE page for that. it's in the System Calls Manual, and it lives in the Standard C Library (libc, -lc) β yes queen, -lc π
β¨ NAME β¨
getlogin, getlogin_r, setlogin β get/set login name π«Ά
π LIBRARY π
Standard C Library (libc, -lc)
π SYNOPSIS π
#include <unistd.h>
char *
getlogin(void);
#include <sys/param.h>
int
getlogin_r(char *name, size_t len);
int
setlogin(const char *name);
π¦ DESCRIPTION π¦
so. getlogin() hands you back the login name of the user that's tied to your current session β the one somebody set earlier with setlogin(). that name is normally hooked up to a login shell the moment the session gets created, and then every single process that descends from that login shell just inherits it. periodt. and yes, that's true even if some of those processes go and assume a different user ID β like when su(1) gets used. the name stays, no cap.
getlogin_r() does literally the same thing as getlogin() except you have to bring the buffer. the caller passes name along with its length len in bytes to hold the result. and bestie, make that buffer at least MAXLOGNAME bytes long, iykyk.
setlogin() is the system call that sets the login name of the user tied to the current session to name. it's locked down to the super-user only, and it's normally used just when a brand new session is being created on behalf of the named user β like at login time, or when a remote shell gets invoked. main character energy fr fr.
NOTE: there is only one login name per session. one. that's it.
okay now the part where I hold your hand, because this next bit is CRITICALLY important π₯
it is CRITICALLY important that you make sure setlogin() only ever gets called after the process has taken adequate steps to confirm it's been detached from its parent's session. and the ONLY way to do that is to make a setsid() system call. like, that's the whole list. setsid(). π
the daemon(3) function calls setsid() for you, which is an ideal way of detaching from a controlling terminal and forking into the background. obsessed tbh.
in particular β and I need you to hear me β doing a ioctl(ttyfd, TIOCNOTTY, ...) or setpgrp(...) is NOT sufficient. not it. π
once a parent process does a setsid() system call, it's totally acceptable for some child of that process to then do a setlogin() even though it is not the session leader. BUT β οΈ beware: ALL processes in the session will change their login name at the same time. all of them. even the parent. the way that works is honestly wild.
also just so we're clear: this is not the same as the traditional UNIX behavior of inheriting privilege. different vibe entirely.
and since the setlogin() system call is restricted to the super-user, it's assumed that (like all other privileged programs) the programmer has taken adequate precautions to prevent security violations. that's on you, bestie π«Ά
β RETURN VALUES β
if a call to getlogin() succeeds, it returns a pointer to a null-terminated string in a static buffer β or NULL if the name has not been set. (not set!! we love a little mystery π)
the getlogin_r() function returns zero if successful, or the error number upon failure.
the setlogin() function returns the value 0 if successful; otherwise the value -1 is returned and the global variable errno is set to indicate the error.
π ERRORS π
the following errors may be returned by these calls:
[EFAULT]β thenameargument gave an invalid address. bad address, babe. π«[EINVAL]β thenameargument pointed to a string that was too long. login names are limited toMAXLOGNAME(from<sys/param.h>) characters, currently33including null. thirty-three!! including the null!! that's the budget.[EPERM]β the caller tried to set the login name and was not the super-user. sorry bestie, that's a you problem π[ERANGE]β the size of the buffer is smaller than the result to be returned. buffer said "not today" π
π SEE ALSO π
πΈ STANDARDS πΈ
the getlogin() system call and the getlogin_r() function conform to ISO/IEC 9945-1:1996 (βPOSIX.1β). iconic. certified. π
π§ HISTORY π§
the getlogin() system call first appeared in 4.4BSD. and the return value of getlogin_r() was changed from earlier versions of FreeBSD to be conformant with ISO/IEC 9945-1:1996 (βPOSIX.1β). glow-up fr. β¨
π BUGS π
in earlier versions of the system, getlogin() failed unless the process was associated with a login terminal. the current implementation (using setlogin()) allows getlogin to succeed even when the process has no controlling terminal. slay, honestly.
and in earlier versions of the system, the value returned by getlogin() could not be trusted without checking the user ID. so portable programs should probably still make this check. yes, still. even now. do the check. π
k bye, this page is from the Debian crew, dated September 9, 2020 β go forth and know whose session it is, bestie ππ¦π
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 1989, 1991, 1993 The Regents of the University of California. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms