Problem
My program uses the library routine cuserid(S). When the program is built and run on SCO UNIX 3.2 Version 4.2, cuserid(S) returns the correct username. However, when the same program (already built on SCO UNIX Version 4.2) is run on OpenServer 5.0.2, cuserid(S) returns "uadmin" rather than the correct username. If I recompile the program on OpenServer 5.0.2, cuserid(S) once again behaves as expected.
Inspecting the file /etc/utmp, I can see the 'uadmin' login on ttyp2, but 'uadmin' is not used at all by me. This problem only occurs when I am logged in on /dev/ttyp2.
Cause
The implementation of getlogin(S), which is called by cuserid(S) to determine the correct username, has changed between releases.
The implementation of getlogin(S) on SCO UNIX Version 4.2 called ttyslot(S), which then searched the file /etc/utmp looking for the tty that was returned by ttyname(S). Since the program was built on SCO UNIX Version 4.2, it would have this getlogin(S) code statically linked into it, and would be using this rather than the OpenServer version of getlogin(S) when running on OpenServer 5.0.2. Therefore, if the user is logged in on ttyp2 and then runs this program, the old getlogin(S) code in the program would find the corresponding ttyp2 entry - which is the 'uadmin' one in this case.
getlogin(S) on OpenServer 5, however, determines the user name differently, by inspecting /etc/utmp and matching an entry if the entry's type is INIT_PROC or USER_PROC or LOGIN_PROC (see utmp(F)) and the session id (SID) of the calling process is the same as the entry's process id (PID). By using the OpenServer-version getlogin(S) in the recompiled program, this will look up the appropriate utmp entry based upon the SID matching the PID entry, and hence give the correct result.
Solution
This problem has been resolved in later releases of SCO OpenServer Release 5. Recompiling the program on OpenServer 5 will also address this problem.
NOTE: Information such as session id, process id, and so on, can be
determined from the command by using the ps(C) command. For
example:$ ps -opid,ppid,pgid,sess,uid,comm,tty
PID PPID PGID SESSION UID COMMAND TTY
3975 28942 3975 28941 0 ps ttyp2
28941 28940 28941 28941 0 login ttyp2
28942 28941 28942 28941 0 ksh ttyp2The following program will dump the contents of the /etc/utmp in a readable format for further analysis:
/****************************************************************************//* cc -o getut getut.c */#include <sys/types.h>
#include <stdio.h>
#include <utmp.h>
#include <fcntl.h>
#include <unistd.h>main(argc, argv) int argc ; char *argv[] ; { int fd; struct utmp utmp;
if ((fd = open ("/etc/utmp", O_RDONLY)) < 0) { perror("Unable to open /etc/utmp\n"); exit (1); }
printf("*********************************************\n");
printf("In OS3, getlogin() used ttyslot()->ttyname() to index into
utmp...\n");
printf("\tttyname(0): %s\n\tttyname(1): %s\n\tttyname(2): %s\n",
ttyname(0), ttyname(1), ttyname(2));
printf("\nIn OS5, getlogin() used SID of calling process...\n");
printf("\tSID of calling process: %d\n", getsid((pid_t) 0));
printf("\tPID of calling process: %d\n", getpid());
printf("\ngetlogin() returns: %s\n", getlogin());
printf("*********************************************\n");printf("\n");while (read (fd, &utmp, sizeof(utmp)) == sizeof(utmp)) { printf("USER %12.8s\tpid: %d\ttype: %d\tline: %.8s\tid: %.4s\n", utmp.ut_user, utmp.ut_pid, utmp.ut_type, utmp.ut_line, utmp.ut_id); } close (fd); }
/****************************************************************************/