Sublime Forum

Os.getlogin() -> 'root' (wrong)

#1

I have a script that inserts comments (datestamp, username, version number, etc.) at the top of a file… The username, for some reason, comes out as root (but I’m logged in as just me). I’m using os.getlogin() for this.

Workaround is to do:

                    login = os.getlogin()
                    if login == "root":
                        login = os.getenv("USER")

BTW,

    >>> os.geteuid()
    502
    >>> os.getuid()
    502

(so, everything under user 502, which is just me)
Sublime is being run the usual way (at the moment, auto-restarted after a reboot, but I have it running all the time, sometimes doing subl . when doing development, and other times, running from the task bar^H^H^H^H^H^H^H^H Dock), and ps shows

  502  2663     1   0 Thu10AM ??        28:56.77 /Applications/Sublime Text.app/Contents/MacOS/Sublime Text -psn_0_352342
  502  2746  2663   0 Thu10AM ??         2:50.15 /Applications/Sublime Text.app/Contents/MacOS/plugin_host 2663 --auto-shell-env
  502 28228 11965   0  6:19PM ttys002    0:00.00 grep -i sublime

so again id 502. My theory is some UI component (Dock?) is being run as root and Sublime is being run by that, probably setuid or something.

What else. It’s Sublime 3.2.2 b3211 on MacOS Catalina 10.15.4 Beta (19E224g) on a recent MacBook Pro, but I don’t think that matters much, it’s been happening for a while. Obviously not a huge priority.

Thanks

0 Likes

#2

Try using getpass:

import getpass
print(getpass.getuser())
1 Like

#3

Bingo. Thanks so much!

0 Likes