pg_slythe

Adrian Klaver aklaver at comcast.net
Fri May 12 19:42:31 UTC 2006


On Friday 12 May 2006 11:32 am, James William Pye wrote:
> On Fri, May 12, 2006 at 07:12:57AM -0700, Adrian Klaver wrote:
> > No I get the following.
> > aklaver at tucker:~> python -c 'from os import getlogin; print getlogin()'
> > Traceback (most recent call last):
> >   File "<string>", line 1, in ?
> > OSError: [Errno 2] No such file or directory
>
> Yeah, seems like something is broken on your system. Does your distro do
> some Python patching(or, perhaps, the lack thereof)? ie, os.getlogin()
> should work.
>
> [yes, I am going to fix my code so it's not so breakable]
>

While trying to track down the problem I did a little research. It seems 
getlogin() has a history of erratic behavior. From the man page-

BUGS
       Unfortunately,  it  is  often  rather  easy to fool getlogin().  
Sometimes it does not work at all, because some program messed up the utmp 
file. Often, it gives only the first 8 characters of the login name. The user 
currently logged in on the controlling tty of our program need not be the
user who started it.  Avoid getlogin() for security-related purposes.

       Note  that  glibc does not follow the POSIX spec and uses stdin instead 
of /dev/tty.  A bug. (Other recent systems, like SunOS 5.8 and HPUX 11.11
and FreeBSD 4.8 all return the login name also when stdin is redirected.)

       Nobody knows precisely what cuserid() does - avoid it in portable 
programs - avoid it altogether - use getpwuid(geteuid()) instead,  if  that  
is what you meant.  DO NOT USE cuserid().

This one of the places I got the alternate method of determining username.

Thank you,
-- 
Adrian Klaver	
aklaver at comcast.net


More information about the Python-general mailing list