8.15. User IdentificationSection 6.8), and the getlogin function provides a way to fetch that login name.
This function can fail if the process is not attached to a terminal that a user logged in to. We normally call these processes daemons . We discuss them in Chapter 13. Given the login name, we can then use it to look up the user in the password fileto determine the login shell, for exampleusing getpwnam. Section 18.9) and then tried to find a matching entry in the utmp file (Section 6.8). FreeBSD and Mac OS X store the login name in the session structure associated with the process table entry and provide system calls to fetch and store this name. System V provided the cuserid function to return the login name. This function called getlogin and, if that failed, did a getpwuid(getuid()). The IEEE Standard 1003.11988 specified cuserid, but it called for the effective user ID to be used, instead of the real user ID. The 1990 version of POSIX.1 dropped the cuserid function. The environment variable LOGNAME is usually initialized with the user's login name by login(1) and inherited by the login shell. Realize, however, that a user can modify an environment variable, so we shouldn't use LOGNAME to validate the user in any way. Instead, getlogin should be used. |