0

I'm working on a C application in which I need the name of the currently logged in user. I have tried using getlogin() and getlogin_r() with no success (tested on multiple systems with Ubuntu 16.04 LTS). The application will run as root so I cannot use the environment variables.

Both getlogin() and getlogin_r() work just fine on other Ubuntu 17.04/17.10/18.04(beta) so I don't understand why it doesn't work in 16.04.

Here is a code snippet that I used to test:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>

int main(int argc, char *argv[])
{
    char user[512] = {0};
    int ret = getlogin_r(user, 512);
    if ( ret != 0){
        fprintf(stderr, "Unable to get User name. Return: %d\n", ret);
    }
    else{
        fprintf(stdout, "Username: %s\n", user);
    }

    char *lgn;
    struct passwd *pw;
    if ((lgn = getlogin()) == NULL || (pw = getpwnam(lgn)) == NULL)
    {
        fprintf(stderr, "Get of user information failed.\n");
    }

    struct passwd *pwd = getpwuid(getuid());
    if (pwd){
       fprintf(stdout, "Success! Username: %s\n", pwd->pw_name);
    }else
        fprintf(stderr, "Failed");

    return 0;
}

This is the output generated when I execute the code as root:

Unable to get User name. Return : 2 
Get of user information failed.
Success! Username: root

getpwuid returns the details of the user running the process so it is not helpful.

I'm kind of stuck now and any help is highly appreciated.

Output using strerror()

getlogin_r() : No such process
getlogin() : No such file or directory
Success! Username: root
  • [Here](https://stackoverflow.com/questions/8953424/how-to-get-the-username-in-c-c-in-linux)'s a solution that might work for you. – Bálint Budavölgyi Apr 20 '18 at 07:19
  • For more meaningful errors, try `perror()` and/or `strerror()` –  Apr 20 '18 at 07:21
  • @FelixPalmen I've added the output from using `strerror()`. But those don't make any sense to me. :/ –  Apr 20 '18 at 07:44
  • @BálintBudavölgyi Neither of those answers work for me. I have already tried. –  Apr 20 '18 at 07:47
  • @Koderkid are you sure? "No such process" would be ESRCH, this isn't documented as a possible error code from `getlogin_r()` in the manpage. I **think** code 2 would be ENOENT, which in context of `getlogin_r()` means there wasn't a matching entry found in [utmp](https://en.wikipedia.org/wiki/Utmp). –  Apr 20 '18 at 07:52
  • And, as `getuid()` returns 0, are you sure you're running this program from the terminal you're logged on as a normal user? otherwise, there's no way to know which user id you actually want, after all, you're on a multitasking/multiuser system. –  Apr 20 '18 at 08:01
  • @FelixPalmen The application needs to run as root. So I run it with sudo which is why `getuid()` returns 0. I need the name of the logged in user for logging purposes. –  Apr 20 '18 at 08:18
  • @Koderkid well, check your utmp :) And then, if it's an option to make your own program suid root instead of using sudo, this would simplify things, because the real uid will stay that of the calling process, so `getuid()` won't return 0 (but `geteuid()` will). –  Apr 20 '18 at 08:33
  • @FelixPalmen Ok. I think I might be able to come up with a way to get it from utmp. Thank you! :) –  Apr 20 '18 at 08:48

0 Answers0