Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Why "getlogin" not works in this case?

by bdimych (Monk)
on Jan 22, 2008 at 12:47 UTC ( [id://663562]=perlquestion: print w/replies, xml ) Need Help??

bdimych has asked for the wisdom of the Perl Monks concerning the following question:

I found such behaviour of the "getlogin" when maked some asynchronous testsuite with profiling. Simplified examples:
[dim@grivo out]$ sh -c "/usr/bin/time perl -e '$|=1; print getlogin(), + chr(10)'" dim 0.00user 0.00system 0:00.00elapsed 100%CPU (0avgtext+0avgdata 0maxresi +dent)k 0inputs+0outputs (0major+393minor)pagefaults 0swaps [dim@grivo out]$ [dim@grivo out]$ [dim@grivo out]$ [dim@grivo out]$ sh -c "/usr/bin/time perl -e '$|=1; print getlogin(), + chr(10)' & wait" 0.00user 0.00system 0:00.00elapsed 50%CPU (0avgtext+0avgdata 0maxresid +ent)k 0inputs+0outputs (0major+386minor)pagefaults 0swaps [dim@grivo out]$ [dim@grivo out]$ [dim@grivo out]$ [dim@grivo out]$ sh -c "/usr/bin/time perl -e '$|=1; print \$ENV{USER} +, chr(10)' & wait" dim 0.00user 0.00system 0:00.00elapsed 50%CPU (0avgtext+0avgdata 0maxresid +ent)k 0inputs+0outputs (0major+383minor)pagefaults 0swaps [dim@grivo out]$ [dim@grivo out]$ [dim@grivo out]$ [dim@grivo out]$ uname -a Linux grivo.bq 2.6.21-1.3194.fc7 #1 SMP Wed May 23 22:35:01 EDT 2007 i +686 i686 i386 GNU/Linux [dim@grivo out]$ [dim@grivo out]$

Replies are listed 'Best First'.
Re: Why "getlogin" not works in this case?
by thparkth (Beadle) on Jan 22, 2008 at 16:01 UTC
    The behaviour you are seeing is 100% expected and correct. getlogin() is meant to return null if the process is run with no controlling terminal, and that's what you're doing ;)

    The getlogin() syscall returns the user associated with the controlling terminal for the process, or null if there is no controlling terminal.

    The way you are running perl, via sh -c, with an & to background it, results in the process having no controlling terminal.

    You can more simply see this behaviour with the "tty" command, which shows the controlling terminal.

    Normally tty returns something like:

    /dev/pts/0
    

    But if you run

    sh -c "tty &"
    

    You'll see a different output:

    not a tty
    

    If you really want to do what you're doing, consider replacing "getlogin()" with "getpwuid($<)" which uses the real user ID rather than the controlling terminal owner, and so works even backgrounded.

      oops, sorry, many thanks

      i see that "getpwuid($<) fail with "Use of uninitialized value $< in concatenation (.) or string at" . It just failed once in 100 times. But i still would like to know when .

      Thanks in advance!

      Ajay
Re: Why "getlogin" not works in this case?
by apl (Monsignor) on Jan 22, 2008 at 15:44 UTC
    I don't really understand what you're asking. Could you clarify?
      sh -c "/usr/bin/time perl -e '$|=1; print getlogin(), chr(10)' + - prints username sh -c "/usr/bin/time perl -e '$|=1; print getlogin(), chr(10)' & wait" + - prints nothing - that is sh -c "/usr/bin/time perl -e '$|=1; print \$ENV{USER}, chr(10)' & wait +" - prints username

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://663562]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2024-03-28 19:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found