How to get the username in C/C++ in Linux?
The function getlogin_r()
defined in unistd.h
returns the username. See man getlogin_r
for more information.
Its signature is:
int getlogin_r(char *buf, size_t bufsize);
Needless to say, this function can just as easily be called in C or C++.
How to retrieve the user name from the user ID
You use getpwuid
to look up the password file entry for a particular UID (which includes the user name, but now not the password itself) and getgrgid
to look up the group file entry for a particular GID.
C/Linux: How to get users login name without `getlogin`
Use getresuid(2) or some of the more specific id retrieval functions to get the id you want (real, effective, or saved-set) (you probably want RUID, if you want to emulate getlogin
, in which case you can simply call getuid
and forget about the effective and saved-set uid), and then use getpwuid(3) or its reentrant counterpart to translate that to a user id string.
getenv("USER")
might give you the same result, but you can't rely on it if you want real security.
Technically, all these may be different from the result obtained by getlogin
when stdin is your controlling terminal. If you really need the same answer as what getlogin
would get you, you can temporarily make your fd 0 point to your controlling terminal again, then call getlogin
, and then restore your fd 0:
int saved_fd0;
if(0>(saved_fd0 = dup(0))
/*handle error*/;
close(0);
/*open always gets the lowest possible fd number == now 0*/
/*"/dev/tty" is always your current processes's controlling terminal*/
if(0>open("/dev/tty", O_RDONLY))
/*handle error*/;
/*
getlogin()
..
*/
/*restore saved_fd0*/
if(0>dup2(saved_fd0, 0))
/*handle error*/;
C++. Get logged in user name from Linux daemon
getlogin()
reads the login information from the utmp
file, so it won't work if the process isn't associated with a terminal.
I assume from the seteuid
call in your example that process A is running with the effective user ID of the original user.
If you don't have a terminal, but have the uid you need to use the "passwd" database (often, but not always, backed by the /etc/passwd
file):
struct passwd *pw = getpwuid(geteuid());
string userName = pw->name;
How do I get the current user's username in Bash?
On the command line, enter
whoami
or
echo "$USER"
How to get file's owner name in Linux using C++?
Use getpwuid()
and getgrgid()
.
#include <pwd.h>
#include <grp.h>
#include <sys/stat.h>
struct stat info;
stat(filename, &info); // Error check omitted
struct passwd *pw = getpwuid(info.st_uid);
struct group *gr = getgrgid(info.st_gid);
// If pw != 0, pw->pw_name contains the user name
// If gr != 0, gr->gr_name contains the group name
Reference Linux username in string to open file
You can use wordexp to translate "~" which is a UNIX
path element meaning the HOME directory. Something like this:
#include <wordexp.h>
std::string homedir()
{
std::string s;
wordexp_t p;
if(!wordexp("~", &p, 0))
{
if(p.we_wordc && p.we_wordv[0])
s = p.we_wordv[0];
wordfree(&p);
}
return s;
}
And then extract the username from the returned path.
But I normally use std::getenv()
like this:
auto HOME = std::getenv("HOME"); // may return nullptr
auto USER = std::getenv("USER"); // may return nullptr
Related Topics
Undefined Symbols for Architecture X86_64 - Mavericks (Yosemite, El Capitan...)
C++ Integer->Std::String Conversion. Simple Function
C++: Difference Between Ampersand "&" and Asterisk "*" in Function/Method Declaration
Friend Declaration Declares a Non-Template Function
How to Assign Multiple Values into a Struct at Once
C++11 Range-Based For-Loop Efficiency "Const Auto &I" Versus "Auto I"
Performance Penalty for Working with Interfaces in C++
Why Does Int Main() {} Compile
Operator << Must Take Exactly One Argument
Interpolate from One Color to Another
What Use Are Const Pointers (As Opposed to Pointers to Const Objects)
Do Polymorphism or Conditionals Promote Better Design
Is Returning with 'Std::Move' Sensible in the Case of Multiple Return Statements
C++11: What Happens If You Don't Call Join() for Std::Thread