It seems, that the getlogin() function is not working when called from the Linux subsystem on Windows (it always returns null).

This leads to two questions:

  • Is there a working alternative?
  • Is it possible to determine if we are on a "normal" Linux or a "Windows Linux" like stated here?
  • It's not a system call. It relies on a login, specifically a utmp record, I guess your WSL is not bothering to set that up when you open a WSL window. getlogin is probably working correctly, it's just telling you there is no utmp record :-P. If you want some alternative, you need to explain exactly what behaviour you want. – sourcejedi May 11 '19 at 14:27
  • @sourcejedi You're right. Somewhere else, I read that this utmp record doesn't exist (which seems to produce some more problems). I just need an easy way to display the username of the user who started the process. – TimSch May 11 '19 at 14:40
  • good start. Is it a set-uid process, or does it run with the same UID as the process that exec()'d it ? Do you want sudo my-program to show root ? – sourcejedi May 11 '19 at 14:55

Assuming you are still running as the same user: getuid() then resolve it to a user name.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.