Perl - getlogin, getpwuid and $ <

Want to understand the example line of code given by @ getlogin for getlogin

$login = getlogin || getpwuid($<) || "Kilroy";

It looks like he's trying to get the username from getlogin or getpwuid , but if either fails, use Kilroy . Maybe I'm wrong, so please correct me. Also, I used getlogin() in previous scripts - is there a difference between getlogin() and getlogin ?

What protects this code? Also, what is the purpose of $< ? I'm not quite sure what to look for, looking at what $< and what it does.


EDIT
found this in the special variables section - still don't know why this is needed or what does in the example above
 $< 

The real uid of this process. (Mnemonic: it's you you came from if you use setuid.) You can change both the real uid and the effective uid at the same time using POSIX :: setuid (). Since changes to $ <require a system call, check $! after trying to change, detect possible errors.


EDIT x2

Is this line comparable to the above example? (I am currently using to avoid any potential cron problems running the script - I have never encountered this problem, but I try to avoid any theoretical problem)

my $username = getlogin(); if(!($username)){$username = 'jsmith';}

+4
source share
2 answers

You are absolutely right. If getlogin returns false, it will check getpwuid($<) , if it returns false, it will set $login to "Kilroy"

$< is the real uid of the process. Even if you are working in a setuid environment, it will return the original uid from which the process was started.

Change according to your changes :)

getpwuid returns the username using the UID (in the scalar context that will take place here). Would you like $< be an argument if the program-switched UID at some point ( $< is the source from which it was started)

+3
source

The only thing he protects for is the fact that on some systems, in some cases, getlogin may not return anything useful. In particular, getlogin does just something useful when the process it is in has a β€œcontrol terminal,” which can be non-interactive processes. See, for example, http://www.perlmonks.org/?node_id=663562 .

I think the Kilroy rollback is just for fun, although in principle getpwuid may not return anything useful. (You may have a user ID that does not have an entry in the password database.)

+1
source

Source: https://habr.com/ru/post/1344610/


All Articles