home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1989, 1990, John F. Haugh II
- * All rights reserved.
- *
- * Permission is granted to copy and create derivative works for any
- * non-commercial purpose, provided this copyright notice is preserved
- * in all copies of source code, or included in human readable form
- * and conspicuously displayed on all copies of object code or
- * distribution media.
- */
-
- #include <sys/types.h>
- #include <utmp.h>
- #include <fcntl.h>
- #ifndef BSD
- #include <string.h>
- #include <memory.h>
- #define bzero(a,n) memset(a, 0, n)
- #else
- #include <strings.h>
- #define strchr index
- #define strrchr rindex
- #endif
- #include <stdio.h>
- #include "config.h"
-
- #ifndef lint
- static char sccsid[] = "%W% %U% %G%";
- #endif
-
- extern struct utmp utent;
- extern char name[];
-
- extern struct utmp *getutent();
- extern void setutent();
- extern void endutent();
- extern time_t time();
- extern char *ttyname();
-
- #define NO_UTENT \
- "No utmp entry. You must exec \"login\" from the lowest level \"sh\""
-
- /*
- * checkutmp - see if utmp file is correct for this process
- *
- * System V is very picky about the contents of the utmp file
- * and requires that a slot for the current process exist.
- * The utmp file is scanned for an entry with the same process
- * ID. If no entry exists the process exits with a message.
- */
-
- void
- checkutmp (picky)
- int picky;
- {
- struct utmp *ut;
- char *line;
- #ifndef NDEBUG
- int pid = getppid ();
- #else
- int pid = getpid ();
- #endif
- setutent ();
-
- #ifndef BSD
- if (picky) {
- while (ut = getutent ())
- if (ut->ut_pid == pid)
- break;
-
- if (ut)
- utent = *ut;
-
- endutent ();
-
- if (ut && utent.ut_pid == pid)
- return;
-
- puts (NO_UTENT);
- exit (1);
- } else {
- line = ttyname (0);
- if (strncmp (line, "/dev/", 5) == 0)
- line += 5;
-
- strncpy (utent.ut_line, line, sizeof utent.ut_line);
- if (ut = getutline (&utent))
- strncpy (utent.ut_id, ut->ut_id, sizeof ut->ut_id);
-
- strcpy (utent.ut_user, "LOGIN");
- utent.ut_pid = getpid ();
- utent.ut_type = LOGIN_PROCESS;
- time (&utent.ut_time);
- }
- #endif
- }
-
- /*
- * setutmp - put a USER_PROCESS entry in the utmp file
- *
- * setutmp changes the type of the current utmp entry to
- * USER_PROCESS. the wtmp file will be updated as well.
- */
-
- void
- setutmp (name, line)
- char *name;
- char *line;
- {
- FILE *wtmp;
- struct utmp utent;
- int fd;
- int i;
- int found = 0;
-
- if (! (fd = open ("/etc/utmp", O_RDWR)))
- return;
-
- while (! found && read (fd, &utent, sizeof utent) == sizeof utent) {
- if (! strncmp (line, utent.ut_line, sizeof utent.ut_line))
- found++;
- }
- if (! found) {
- bzero (&utent, sizeof utent);
- strncpy (utent.ut_line, line, sizeof utent.ut_line);
- }
- (void) strncpy (utent.ut_user, name, sizeof utent.ut_user);
- #ifndef BSD
- utent.ut_type = USER_PROCESS;
- utent.ut_pid = getpid ();
- #endif
- (void) time (&utent.ut_time);
-
- if (found)
- lseek (fd, (long) - sizeof utent, 1);
-
- write (fd, &utent, sizeof utent);
- close (fd);
-
- if ((wtmp = fopen (WTMP_FILE, "a+"))) {
- fwrite (&utent, sizeof utent, 1, wtmp);
- fclose (wtmp);
- }
- }
-