xref: /titanic_50/usr/src/cmd/utmpd/utmpd.c (revision 1eabc4bec6d2a5ad71f6a1f0c019af5438d8b1bf)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5004388ebScasper  * Common Development and Distribution License (the "License").
6004388ebScasper  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
2292ba7109Seschrock  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * Portions of such source code were derived from Berkeley 4.3 BSD
317c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of California.
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate /*
357c478bd9Sstevel@tonic-gate  * utmpd	- utmp daemon
367c478bd9Sstevel@tonic-gate  *
377c478bd9Sstevel@tonic-gate  *		This program receives requests from  pututxline(3)
387c478bd9Sstevel@tonic-gate  *		via a named pipe to watch the process to make sure it cleans up
397c478bd9Sstevel@tonic-gate  *		its utmpx entry on termination.
407c478bd9Sstevel@tonic-gate  *		The program keeps a list of procs
417c478bd9Sstevel@tonic-gate  *		and uses poll() on their /proc files to detect termination.
427c478bd9Sstevel@tonic-gate  *		Also the  program periodically scans the /etc/utmpx file for
437c478bd9Sstevel@tonic-gate  *		processes that aren't in the table so they can be watched.
447c478bd9Sstevel@tonic-gate  *
457c478bd9Sstevel@tonic-gate  *		If utmpd doesn't hear back over the pipe from pututline(3) that
467c478bd9Sstevel@tonic-gate  *		the process has removed its entry it cleans the entry when the
477c478bd9Sstevel@tonic-gate  *		the process terminates.
487c478bd9Sstevel@tonic-gate  *		The AT&T Copyright above is there since we borrowed the pipe
497c478bd9Sstevel@tonic-gate  *		mechanism from init(1m).
507c478bd9Sstevel@tonic-gate  */
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #include	<sys/types.h>
547c478bd9Sstevel@tonic-gate #include	<signal.h>
557c478bd9Sstevel@tonic-gate #include	<stdio.h>
56004388ebScasper #include	<stdio_ext.h>
577c478bd9Sstevel@tonic-gate #include	<unistd.h>
587c478bd9Sstevel@tonic-gate #include	<utmpx.h>
597c478bd9Sstevel@tonic-gate #include	<errno.h>
607c478bd9Sstevel@tonic-gate #include	<termio.h>
617c478bd9Sstevel@tonic-gate #include	<sys/termios.h>
627c478bd9Sstevel@tonic-gate #include	<sys/tty.h>
637c478bd9Sstevel@tonic-gate #include	<ctype.h>
647c478bd9Sstevel@tonic-gate #include	<sys/stat.h>
657c478bd9Sstevel@tonic-gate #include	<sys/statvfs.h>
667c478bd9Sstevel@tonic-gate #include	<fcntl.h>
677c478bd9Sstevel@tonic-gate #include	<time.h>
687c478bd9Sstevel@tonic-gate #include	<sys/stropts.h>
697c478bd9Sstevel@tonic-gate #include	<wait.h>
707c478bd9Sstevel@tonic-gate #include	<syslog.h>
717c478bd9Sstevel@tonic-gate #include	<stdlib.h>
727c478bd9Sstevel@tonic-gate #include	<string.h>
737c478bd9Sstevel@tonic-gate #include	<poll.h>
747c478bd9Sstevel@tonic-gate #include	<deflt.h>
757c478bd9Sstevel@tonic-gate #include	<procfs.h>
767c478bd9Sstevel@tonic-gate #include	<sys/resource.h>
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate #define	dprintf(x)	if (Debug) (void) printf x
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate /*
817c478bd9Sstevel@tonic-gate  * Memory allocation keyed off MAX_FDS
827c478bd9Sstevel@tonic-gate  */
837c478bd9Sstevel@tonic-gate #define	MAX_FDS		4064	/* Maximum # file descriptors */
847c478bd9Sstevel@tonic-gate #define	EXTRA_MARGIN	32	/* Allocate this many more FDS over Max_Fds */
857c478bd9Sstevel@tonic-gate /*
867c478bd9Sstevel@tonic-gate  * MAX_POLLNV & RESETS - paranoia to cover an error case that might not exist
877c478bd9Sstevel@tonic-gate  */
887c478bd9Sstevel@tonic-gate #define	MAX_POLL_ERRS	1024	/* Count of bad errors */
897c478bd9Sstevel@tonic-gate #define	MAX_RESETS	1024	/* Maximum times to reload tables */
907c478bd9Sstevel@tonic-gate #define	POLL_TIMEOUT	300	/* Default Timeout for poll() in seconds */
917c478bd9Sstevel@tonic-gate #define	CLEANIT		1	/* Used by rem_pid() */
927c478bd9Sstevel@tonic-gate #define	DONT_CLEAN	0	/* Used by rem_pid() */
937c478bd9Sstevel@tonic-gate #define	UTMP_DEFAULT	"/etc/default/utmpd"
947c478bd9Sstevel@tonic-gate #define	WARN_TIME	3600	/* seconds between utmp checks */
957c478bd9Sstevel@tonic-gate #define	WTMPX_UFREQ	60	/* seconds between updating WTMPX's atime */
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate /*
997c478bd9Sstevel@tonic-gate  * The pidrec structure describes the data shipped down the pipe to
1007c478bd9Sstevel@tonic-gate  * us from the pututxline() library in
1017c478bd9Sstevel@tonic-gate  * lib/libc/port/gen/getutx.c
1027c478bd9Sstevel@tonic-gate  */
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate /*
1057c478bd9Sstevel@tonic-gate  * pd_type's
1067c478bd9Sstevel@tonic-gate  */
1077c478bd9Sstevel@tonic-gate #define	ADDPID  1
1087c478bd9Sstevel@tonic-gate #define	REMPID  2
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate struct  pidrec {
1117c478bd9Sstevel@tonic-gate 	int	pd_type;		/* Command type */
1127c478bd9Sstevel@tonic-gate 	pid_t	pd_pid;			/* pid to add or remove */
1137c478bd9Sstevel@tonic-gate };
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate /*
1177c478bd9Sstevel@tonic-gate  * Since this program uses poll(2) and poll takes an array of file descriptors
1187c478bd9Sstevel@tonic-gate  * as an argument we maintain our data in tables.
1197c478bd9Sstevel@tonic-gate  * One table is the file descriptor array for poll, another parallel
1207c478bd9Sstevel@tonic-gate  * array is a table which contains the process ID of the corresponding
1217c478bd9Sstevel@tonic-gate  * open fd.  These tables are kept sorted by process ID for quick lookups.
1227c478bd9Sstevel@tonic-gate  */
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate struct  pidentry {
1257c478bd9Sstevel@tonic-gate 	pid_t	pl_pid;			/* pid to watch for */
1267c478bd9Sstevel@tonic-gate 	int 	pl_status;		/* Exit status of proc */
1277c478bd9Sstevel@tonic-gate };
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate static struct pidentry *pidtable = NULL;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate static pollfd_t *fdtable = NULL;
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate static int	pidcnt = 0;		/* Number of procs being watched */
1347c478bd9Sstevel@tonic-gate static char	*prog_name;		/* To save the invocation name away */
13592ba7109Seschrock static char	*UTMPPIPE_DIR =	"/var/run";
13692ba7109Seschrock static char	*UTMPPIPE = "/var/run/utmppipe";
1377c478bd9Sstevel@tonic-gate static int	Pfd = -1;		/* File descriptor of named pipe */
1387c478bd9Sstevel@tonic-gate static int 	Poll_timeout = POLL_TIMEOUT;
1397c478bd9Sstevel@tonic-gate static int	WTMPXfd = -1;		/* File descriptor of WTMPX_FILE */
1407c478bd9Sstevel@tonic-gate static int	WTMPX_ufreq = WTMPX_UFREQ;
1417c478bd9Sstevel@tonic-gate static int	Debug = 0;		/* Set by command line argument */
1427c478bd9Sstevel@tonic-gate static int	Max_fds		= MAX_FDS;
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate /*
1457c478bd9Sstevel@tonic-gate  * This program has three main components plus utilities and debug routines
1467c478bd9Sstevel@tonic-gate  *	Receiver - receives the process ID or process for us to watch.
1477c478bd9Sstevel@tonic-gate  *		   (Uses a named pipe to get messages)
1487c478bd9Sstevel@tonic-gate  *	Watcher	 - Use poll(2) to watch for processes to die so they
1497c478bd9Sstevel@tonic-gate  *		   can be cleaned up (get marked as DEAD_PROCESS)
1507c478bd9Sstevel@tonic-gate  *	Scanner  - periodically scans the utmpx file for stale entries
1517c478bd9Sstevel@tonic-gate  *		   or live entries that we don't know about.
1527c478bd9Sstevel@tonic-gate  */
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate static int wait_for_pids();	/* Watcher - uses poll */
1557c478bd9Sstevel@tonic-gate static void scan_utmps();	/* Scanner, reads utmpx file */
1567c478bd9Sstevel@tonic-gate static void drain_pipe();	/* Receiver - reads mesgs over UTMPPIPE */
1577c478bd9Sstevel@tonic-gate static void setup_pipe();	/* For setting up receiver */
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate static void add_pid();		/* Adds a process to the table */
1607c478bd9Sstevel@tonic-gate static void rem_pid();		/* Removes a process from the table */
1617c478bd9Sstevel@tonic-gate static int find_pid();		/* Finds a process in the table */
1627c478bd9Sstevel@tonic-gate static int proc_to_fd();	/* Takes a pid and returns an fd for its proc */
1637c478bd9Sstevel@tonic-gate static void load_tables();	/* Loads up the tables the first time around */
1647c478bd9Sstevel@tonic-gate static int pidcmp();		/* For sorting pids */
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate static void clean_entry();	/* Removes entry from our table and calls ... */
1677c478bd9Sstevel@tonic-gate static void clean_utmpx_ent();	/* Cleans a utmpx entry */
1687c478bd9Sstevel@tonic-gate 
169d2117003Sdp static void fatal() __NORETURN;	/* Prints error message and calls exit */
1707c478bd9Sstevel@tonic-gate static void nonfatal();		/* Prints error message */
1717c478bd9Sstevel@tonic-gate static void print_tables();	/* Prints out internal tables for Debug */
1727c478bd9Sstevel@tonic-gate static int proc_is_alive(pid_t pid);	/* Check if a process is alive */
1737c478bd9Sstevel@tonic-gate static void warn_utmp(void);
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate /*
1767c478bd9Sstevel@tonic-gate  * main()  - Main does basic setup and calls wait_for_pids() to do the work
1777c478bd9Sstevel@tonic-gate  */
1787c478bd9Sstevel@tonic-gate 
179d2117003Sdp int
1807c478bd9Sstevel@tonic-gate main(argc, argv)
1817c478bd9Sstevel@tonic-gate 	char **argv;
1827c478bd9Sstevel@tonic-gate {
1837c478bd9Sstevel@tonic-gate 	char *defp;
1847c478bd9Sstevel@tonic-gate 	struct rlimit rlim;
1857c478bd9Sstevel@tonic-gate 	int i;
1867c478bd9Sstevel@tonic-gate 	time_t curtime, now;
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	prog_name = argv[0];			/* Save invocation name */
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	if (getuid() != 0)  {
1917c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1927c478bd9Sstevel@tonic-gate 			"You must be root to run this program\n");
1937c478bd9Sstevel@tonic-gate 		fatal("You must be root to run this program");
1947c478bd9Sstevel@tonic-gate 	}
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	if (argc > 1) {
1977c478bd9Sstevel@tonic-gate 		if ((argc == 2 && (int)strlen(argv[1]) >= 2) &&
1987c478bd9Sstevel@tonic-gate 		    (argv[1][0] == '-' && argv[1][1] == 'd')) {
1997c478bd9Sstevel@tonic-gate 			Debug = 1;
2007c478bd9Sstevel@tonic-gate 		} else {
2017c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
2027c478bd9Sstevel@tonic-gate 				"%s: Wrong number of arguments\n", prog_name);
2037c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
2047c478bd9Sstevel@tonic-gate 				"Usage: %s [-debug]\n", prog_name);
205d2117003Sdp 			exit(2);
2067c478bd9Sstevel@tonic-gate 		}
2077c478bd9Sstevel@tonic-gate 	}
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	/*
2107c478bd9Sstevel@tonic-gate 	 * Read defaults file for poll timeout
2117c478bd9Sstevel@tonic-gate 	 */
2127c478bd9Sstevel@tonic-gate 	if (defopen(UTMP_DEFAULT) == 0) {
2137c478bd9Sstevel@tonic-gate 		if ((defp = defread("SCAN_PERIOD=")) != NULL) {
2147c478bd9Sstevel@tonic-gate 			Poll_timeout = atol(defp);
2157c478bd9Sstevel@tonic-gate 			dprintf(("Poll timeout set to %d\n", Poll_timeout));
2167c478bd9Sstevel@tonic-gate 		}
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 		if ((defp = defread("WTMPX_UPDATE_FREQ=")) != NULL) {
2197c478bd9Sstevel@tonic-gate 			WTMPX_ufreq = atol(defp);
2207c478bd9Sstevel@tonic-gate 			dprintf(("WTMPX update frequency set to %d\n",
2217c478bd9Sstevel@tonic-gate 			    WTMPX_ufreq));
2227c478bd9Sstevel@tonic-gate 		}
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 		/*
2257c478bd9Sstevel@tonic-gate 		 * Paranoia - if polling on large number of FDs is expensive /
2267c478bd9Sstevel@tonic-gate 		 * buggy the number can be set lower in the field.
2277c478bd9Sstevel@tonic-gate 		 */
2287c478bd9Sstevel@tonic-gate 		if ((defp = defread("MAX_FDS=")) != NULL) {
2297c478bd9Sstevel@tonic-gate 			Max_fds = atol(defp);
2307c478bd9Sstevel@tonic-gate 			dprintf(("Max_fds set to %d\n", Max_fds));
2317c478bd9Sstevel@tonic-gate 		}
2327c478bd9Sstevel@tonic-gate 		(void) defopen((char *)NULL);
2337c478bd9Sstevel@tonic-gate 	}
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	if (Debug == 0) {
2367c478bd9Sstevel@tonic-gate 		/*
2377c478bd9Sstevel@tonic-gate 		 * Daemonize ourselves
2387c478bd9Sstevel@tonic-gate 		 */
2397c478bd9Sstevel@tonic-gate 		if (fork()) {
2407c478bd9Sstevel@tonic-gate 			exit(0);
2417c478bd9Sstevel@tonic-gate 		}
2427c478bd9Sstevel@tonic-gate 		(void) close(0);
2437c478bd9Sstevel@tonic-gate 		(void) close(1);
2447c478bd9Sstevel@tonic-gate 		(void) close(2);
2457c478bd9Sstevel@tonic-gate 		/*
2467c478bd9Sstevel@tonic-gate 		 * We open these to avoid accidentally writing to a proc file
2477c478bd9Sstevel@tonic-gate 		 */
2487c478bd9Sstevel@tonic-gate 		(void) open("/dev/null", O_RDONLY);
2497c478bd9Sstevel@tonic-gate 		(void) open("/dev/null", O_WRONLY);
2507c478bd9Sstevel@tonic-gate 		(void) open("/dev/null", O_WRONLY);
2517c478bd9Sstevel@tonic-gate 		(void) setsid();		/* release process from tty */
2527c478bd9Sstevel@tonic-gate 	}
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	openlog(prog_name, LOG_PID, LOG_DAEMON);	/* For error messages */
2557c478bd9Sstevel@tonic-gate 	warn_utmp();	/* check to see if utmp came back by accident */
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	/*
2587c478bd9Sstevel@tonic-gate 	 * Allocate the pidtable and fdtable.  An earlier version did
2597c478bd9Sstevel@tonic-gate 	 * this as we go, but this is simpler.
2607c478bd9Sstevel@tonic-gate 	 */
2617c478bd9Sstevel@tonic-gate 	if ((pidtable = malloc(Max_fds * sizeof (struct pidentry))) == NULL)
2627c478bd9Sstevel@tonic-gate 		fatal("Malloc failed");
2637c478bd9Sstevel@tonic-gate 	if ((fdtable = malloc(Max_fds * sizeof (pollfd_t))) == NULL)
2647c478bd9Sstevel@tonic-gate 		fatal("Malloc failed");
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	/*
2677c478bd9Sstevel@tonic-gate 	 * Up the limit on FDs
2687c478bd9Sstevel@tonic-gate 	 */
2697c478bd9Sstevel@tonic-gate 	if (getrlimit(RLIMIT_NOFILE, &rlim) == 0) {
2707c478bd9Sstevel@tonic-gate 		rlim.rlim_cur = Max_fds + EXTRA_MARGIN + 1;
2717c478bd9Sstevel@tonic-gate 		rlim.rlim_max = Max_fds + EXTRA_MARGIN + 1;
2727c478bd9Sstevel@tonic-gate 		if (setrlimit(RLIMIT_NOFILE, &rlim) != 0) {
2737c478bd9Sstevel@tonic-gate 			fatal("Out of File Descriptors");
2747c478bd9Sstevel@tonic-gate 		}
2757c478bd9Sstevel@tonic-gate 	} else
2767c478bd9Sstevel@tonic-gate 		fatal("getrlimit returned failure");
2777c478bd9Sstevel@tonic-gate 
278004388ebScasper 	(void) enable_extended_FILE_stdio(-1, -1);
279004388ebScasper 
2807c478bd9Sstevel@tonic-gate 	if ((WTMPXfd = open(WTMPX_FILE, O_RDONLY)) < 0)
2817c478bd9Sstevel@tonic-gate 		nonfatal("WARNING: unable to open " WTMPX_FILE "for update.");
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	/*
2847c478bd9Sstevel@tonic-gate 	 * Loop here scanning the utmpx file and waiting for processes
2857c478bd9Sstevel@tonic-gate 	 * to terminate.  Most of the activity is directed out of wait_for_pids.
2867c478bd9Sstevel@tonic-gate 	 * If wait_for_pids fails we reload the table and try again.
2877c478bd9Sstevel@tonic-gate 	 */
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	curtime = time(NULL);
2907c478bd9Sstevel@tonic-gate 	dprintf(("utmp warning timer set to %d seconds\n", WARN_TIME));
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	for (i = 0; i < MAX_RESETS; i++) {
2937c478bd9Sstevel@tonic-gate 		load_tables();
2947c478bd9Sstevel@tonic-gate 		while (wait_for_pids() == 1) {
2957c478bd9Sstevel@tonic-gate 			now = time(NULL);
2967c478bd9Sstevel@tonic-gate 			if ((now - curtime) >= WARN_TIME) {
2977c478bd9Sstevel@tonic-gate 				dprintf(("utmp warning timer expired\n"));
2987c478bd9Sstevel@tonic-gate 				warn_utmp();
2997c478bd9Sstevel@tonic-gate 				curtime = now;
3007c478bd9Sstevel@tonic-gate 			}
3017c478bd9Sstevel@tonic-gate 		}
3027c478bd9Sstevel@tonic-gate 	}
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	(void) close(WTMPXfd);
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	/*
3077c478bd9Sstevel@tonic-gate 	 * We only get here if we had a bunch of resets - so give up
3087c478bd9Sstevel@tonic-gate 	 */
3097c478bd9Sstevel@tonic-gate 	fatal("Too many resets, giving up");
310d2117003Sdp 	return (1);
3117c478bd9Sstevel@tonic-gate }
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate /*
3147c478bd9Sstevel@tonic-gate  * load_tables()	- Designed to be called repeatedly if we need to
3157c478bd9Sstevel@tonic-gate  *			  restart things.  Zeros the pidcount, and loads
3167c478bd9Sstevel@tonic-gate  *			  the tables by scanning utmpx
3177c478bd9Sstevel@tonic-gate  */
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate static void
3207c478bd9Sstevel@tonic-gate load_tables()
3217c478bd9Sstevel@tonic-gate {
3227c478bd9Sstevel@tonic-gate 	int i;
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	dprintf(("Load tables\n"));
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	/*
3277c478bd9Sstevel@tonic-gate 	 * Close any open files.
3287c478bd9Sstevel@tonic-gate 	 */
3297c478bd9Sstevel@tonic-gate 	for (i = 0; i < pidcnt; i++)
3307c478bd9Sstevel@tonic-gate 		(void) close(fdtable[i].fd);
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	pidcnt = 0;
3337c478bd9Sstevel@tonic-gate 	Pfd = -1;
3347c478bd9Sstevel@tonic-gate 	setup_pipe();		/* Setup the pipe to receive messages */
3357c478bd9Sstevel@tonic-gate 	scan_utmps();		/* Read in USER procs entries to watch */
3367c478bd9Sstevel@tonic-gate }
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate /*
3407c478bd9Sstevel@tonic-gate  *			*** The Watcher ***
3417c478bd9Sstevel@tonic-gate  *
3427c478bd9Sstevel@tonic-gate  * Wait_for_pids	- wait for the termination of a process in the table.
3437c478bd9Sstevel@tonic-gate  *			  Returns 1 on normal exist, 0 on failure.
3447c478bd9Sstevel@tonic-gate  */
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate static int
3477c478bd9Sstevel@tonic-gate wait_for_pids()
3487c478bd9Sstevel@tonic-gate {
3497c478bd9Sstevel@tonic-gate 	register struct pollfd *pfd;
3507c478bd9Sstevel@tonic-gate 	register int i;
3517c478bd9Sstevel@tonic-gate 	pid_t pid;
3527c478bd9Sstevel@tonic-gate 	int ret_val;
3537c478bd9Sstevel@tonic-gate 	int timeout;
3547c478bd9Sstevel@tonic-gate 	static time_t last_timeout  = 0;
3557c478bd9Sstevel@tonic-gate 	static int bad_error  = 0;	/* Count of POLL errors */
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	/*
3587c478bd9Sstevel@tonic-gate 	 * First time through we initialize last_timeout to now.
3597c478bd9Sstevel@tonic-gate 	 */
3607c478bd9Sstevel@tonic-gate 	if (last_timeout == 0)
3617c478bd9Sstevel@tonic-gate 		last_timeout = time(NULL);
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 	/*
3647c478bd9Sstevel@tonic-gate 	 * Recalculate timeout - checking to see if time expired.
3657c478bd9Sstevel@tonic-gate 	 */
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	if ((timeout = Poll_timeout - (time(NULL) - last_timeout)) <= 0) {
3687c478bd9Sstevel@tonic-gate 		timeout = Poll_timeout;
3697c478bd9Sstevel@tonic-gate 		last_timeout = time(NULL);
3707c478bd9Sstevel@tonic-gate 		scan_utmps();
3717c478bd9Sstevel@tonic-gate 	}
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	fdtable[0].events = POLLRDNORM;
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	for (i = 0; i < (timeout / WTMPX_ufreq); i++) {
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 		/*
3787c478bd9Sstevel@tonic-gate 		 * Loop here while getting EAGAIN
3797c478bd9Sstevel@tonic-gate 		 */
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 		while ((ret_val = poll(fdtable, pidcnt, WTMPX_ufreq*1000)) < 0)
3827c478bd9Sstevel@tonic-gate 			if (errno == EAGAIN)
3837c478bd9Sstevel@tonic-gate 				(void) sleep(2);
3847c478bd9Sstevel@tonic-gate 			else
3857c478bd9Sstevel@tonic-gate 				fatal("poll");
3867c478bd9Sstevel@tonic-gate 		/*
3877c478bd9Sstevel@tonic-gate 		 * The results of pread(2) are discarded; we only want
3887c478bd9Sstevel@tonic-gate 		 * to update the access time of WTMPX_FILE.
3897c478bd9Sstevel@tonic-gate 		 * Periodically touching WTMPX helps determine when the
3907c478bd9Sstevel@tonic-gate 		 * OS became unavailable when the OS boots again .
3917c478bd9Sstevel@tonic-gate 		 * See PSARC 2004/462 for more information.
3927c478bd9Sstevel@tonic-gate 		 */
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 		(void) pread(WTMPXfd, (void *)&pid, sizeof (pid), 0);
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 		if (ret_val)		/* file descriptor(s) need attention */
3977c478bd9Sstevel@tonic-gate 			break;
3987c478bd9Sstevel@tonic-gate 	}
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	/*
4017c478bd9Sstevel@tonic-gate 	 * If ret_val == 0 the poll timed out - reset last_time and
4027c478bd9Sstevel@tonic-gate 	 * call scan_utmps
4037c478bd9Sstevel@tonic-gate 	 */
4047c478bd9Sstevel@tonic-gate 	if (ret_val == 0) {
4057c478bd9Sstevel@tonic-gate 		last_timeout = time(NULL);
4067c478bd9Sstevel@tonic-gate 		scan_utmps();
4077c478bd9Sstevel@tonic-gate 		return (1);
4087c478bd9Sstevel@tonic-gate 	}
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	/*
4117c478bd9Sstevel@tonic-gate 	 * Check the pipe file descriptor
4127c478bd9Sstevel@tonic-gate 	 */
4137c478bd9Sstevel@tonic-gate 	if (fdtable[0].revents & POLLRDNORM) {
4147c478bd9Sstevel@tonic-gate 		drain_pipe();
4157c478bd9Sstevel@tonic-gate 		fdtable[0].revents = 0;
4167c478bd9Sstevel@tonic-gate 		ret_val--;
4177c478bd9Sstevel@tonic-gate 	}
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 	(void) sleep(5);	/* Give parents time to cleanup children */
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 	/*
4227c478bd9Sstevel@tonic-gate 	 * We got here because the status of one of the pids that
4237c478bd9Sstevel@tonic-gate 	 * we are polling on has changed, so search the table looking
4247c478bd9Sstevel@tonic-gate 	 * for the entry.
4257c478bd9Sstevel@tonic-gate 	 *
4267c478bd9Sstevel@tonic-gate 	 * The table is scanned backwards so that entries can be removed
4277c478bd9Sstevel@tonic-gate 	 * while we go since the table is compacted from high down to low
4287c478bd9Sstevel@tonic-gate 	 */
4297c478bd9Sstevel@tonic-gate 	for (i = pidcnt - 1; i > 0; i--) {
4307c478bd9Sstevel@tonic-gate 		/*
4317c478bd9Sstevel@tonic-gate 		 * Break out of the loop if we've processed all the entries.
4327c478bd9Sstevel@tonic-gate 		 */
4337c478bd9Sstevel@tonic-gate 		if (ret_val == 0)
4347c478bd9Sstevel@tonic-gate 			break;
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 		pfd = &fdtable[i];
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 		if (pfd->fd < 0) {
4397c478bd9Sstevel@tonic-gate 			rem_pid((pid_t)0, i, DONT_CLEAN);
4407c478bd9Sstevel@tonic-gate 			continue;
4417c478bd9Sstevel@tonic-gate 		}
4427c478bd9Sstevel@tonic-gate 		/*
4437c478bd9Sstevel@tonic-gate 		 * POLLHUP	- Process terminated
4447c478bd9Sstevel@tonic-gate 		 */
4457c478bd9Sstevel@tonic-gate 		if (pfd->revents & POLLHUP) {
4467c478bd9Sstevel@tonic-gate 			psinfo_t psinfo;
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 			if (pread(pfd->fd, &psinfo, sizeof (psinfo), (off_t)0)
4497c478bd9Sstevel@tonic-gate 			    != sizeof (psinfo)) {
4507c478bd9Sstevel@tonic-gate 				dprintf(("! %d: terminated, status 0x%.4x\n", \
4517c478bd9Sstevel@tonic-gate 				    (int)pidtable[i].pl_pid, psinfo.pr_wstat));
4527c478bd9Sstevel@tonic-gate 				pidtable[i].pl_status = psinfo.pr_wstat;
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 			} else {
4557c478bd9Sstevel@tonic-gate 				dprintf(("! %d: terminated\n", \
4567c478bd9Sstevel@tonic-gate 				    (int)pidtable[i].pl_pid));
4577c478bd9Sstevel@tonic-gate 				pidtable[i].pl_status = 0;
4587c478bd9Sstevel@tonic-gate 			}
4597c478bd9Sstevel@tonic-gate 			/*
4607c478bd9Sstevel@tonic-gate 			 * PID gets removed when terminated only
4617c478bd9Sstevel@tonic-gate 			 */
4627c478bd9Sstevel@tonic-gate 			rem_pid((pid_t)0, i, CLEANIT);
4637c478bd9Sstevel@tonic-gate 			ret_val--;
4647c478bd9Sstevel@tonic-gate 			continue;
4657c478bd9Sstevel@tonic-gate 		}
4667c478bd9Sstevel@tonic-gate 		/*
4677c478bd9Sstevel@tonic-gate 		 * POLLNVAL and POLLERR
4687c478bd9Sstevel@tonic-gate 		 *	These error's shouldn't occurr but until their fixed
4697c478bd9Sstevel@tonic-gate 		 *	we perform some simple error recovery.
4707c478bd9Sstevel@tonic-gate 		 */
4717c478bd9Sstevel@tonic-gate 		if (pfd->revents & (POLLNVAL|POLLERR)) {
4727c478bd9Sstevel@tonic-gate 			dprintf(("Poll Err = %d pid = %d i = %d\n", \
473d2117003Sdp 			    pfd->revents, (int)pidtable[i].pl_pid, i));
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 			pid = pidtable[i].pl_pid; /* Save pid for below */
4767c478bd9Sstevel@tonic-gate 			/*
4777c478bd9Sstevel@tonic-gate 			 * If its POLLNVAL we just remove the process for
4787c478bd9Sstevel@tonic-gate 			 * now, it will get picked up in the next scan.
4797c478bd9Sstevel@tonic-gate 			 * POLLERR pids get re-added after being deleted.
4807c478bd9Sstevel@tonic-gate 			 */
4817c478bd9Sstevel@tonic-gate 			if (pfd->revents & POLLNVAL) {
4827c478bd9Sstevel@tonic-gate 				rem_pid((pid_t)0, i, DONT_CLEAN);
4837c478bd9Sstevel@tonic-gate 			} else {			/* Else... POLLERR */
4847c478bd9Sstevel@tonic-gate 				rem_pid((pid_t)0, i, DONT_CLEAN);
4857c478bd9Sstevel@tonic-gate 				add_pid(pid);
4867c478bd9Sstevel@tonic-gate 			}
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 			if (bad_error++ > MAX_POLL_ERRS) {
4897c478bd9Sstevel@tonic-gate 				bad_error = 0;
4907c478bd9Sstevel@tonic-gate 				return (0);	/* 0 Indicates severe error */
4917c478bd9Sstevel@tonic-gate 			}
4927c478bd9Sstevel@tonic-gate 			ret_val--;
4937c478bd9Sstevel@tonic-gate 			continue;
4947c478bd9Sstevel@tonic-gate 		}
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 		/*
4977c478bd9Sstevel@tonic-gate 		 * No more bits should be set in revents but check anyway
4987c478bd9Sstevel@tonic-gate 		 */
4997c478bd9Sstevel@tonic-gate 		if (pfd->revents != 0) {
5007c478bd9Sstevel@tonic-gate 			dprintf(("%d: unknown err %d\n", \
5017c478bd9Sstevel@tonic-gate 			    (int)pidtable[i].pl_pid, pfd->revents));
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate 			rem_pid((pid_t)0, i, DONT_CLEAN);
5047c478bd9Sstevel@tonic-gate 			ret_val--;
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 			if (bad_error++ > MAX_POLL_ERRS) {
5077c478bd9Sstevel@tonic-gate 				bad_error = 0;
5087c478bd9Sstevel@tonic-gate 				return (0);	/* 0 Indicates severe error */
5097c478bd9Sstevel@tonic-gate 			}
5107c478bd9Sstevel@tonic-gate 			return (1);
5117c478bd9Sstevel@tonic-gate 		}
5127c478bd9Sstevel@tonic-gate 	}
5137c478bd9Sstevel@tonic-gate 	return (1);			/* 1 Indicates Everything okay */
5147c478bd9Sstevel@tonic-gate }
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate /*
5177c478bd9Sstevel@tonic-gate  *		*** The Scanner ***
5187c478bd9Sstevel@tonic-gate  *
5197c478bd9Sstevel@tonic-gate  * scan_utmps()		- Scan the utmpx file.
5207c478bd9Sstevel@tonic-gate  *			  For each USER_PROCESS check
5217c478bd9Sstevel@tonic-gate  *			  if its alive or dead.  If alive and its not in
5227c478bd9Sstevel@tonic-gate  *			  our table to be watched, put it there.  If its
5237c478bd9Sstevel@tonic-gate  *			  dead, remove it from our table and clean it up.
5247c478bd9Sstevel@tonic-gate  */
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate static void
5277c478bd9Sstevel@tonic-gate scan_utmps()
5287c478bd9Sstevel@tonic-gate {
5297c478bd9Sstevel@tonic-gate 	struct	utmpx	*utmpx;
5307c478bd9Sstevel@tonic-gate 	int	i;
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 	dprintf(("Scan utmps\n"));
5337c478bd9Sstevel@tonic-gate 	/*
5347c478bd9Sstevel@tonic-gate 	 * Scan utmpx.
5357c478bd9Sstevel@tonic-gate 	 */
5367c478bd9Sstevel@tonic-gate 	setutxent();
5377c478bd9Sstevel@tonic-gate 	while ((utmpx = getutxent()) != NULL) {
5387c478bd9Sstevel@tonic-gate 		if (utmpx->ut_type == USER_PROCESS) {
5397c478bd9Sstevel@tonic-gate 			/*
5407c478bd9Sstevel@tonic-gate 			 * Is the process alive?
5417c478bd9Sstevel@tonic-gate 			 */
5427c478bd9Sstevel@tonic-gate 			if (proc_is_alive(utmpx->ut_pid)) {
5437c478bd9Sstevel@tonic-gate 				/*
5447c478bd9Sstevel@tonic-gate 				 * Yes, the process is alive, so add it if we
5457c478bd9Sstevel@tonic-gate 				 * don't have it in our table.
5467c478bd9Sstevel@tonic-gate 				 */
5477c478bd9Sstevel@tonic-gate 				if (find_pid(utmpx->ut_pid, &i) == 0)
5487c478bd9Sstevel@tonic-gate 					add_pid(utmpx->ut_pid);	/* No, add it */
5497c478bd9Sstevel@tonic-gate 			} else {
5507c478bd9Sstevel@tonic-gate 				/*
5517c478bd9Sstevel@tonic-gate 				 * No, the process is dead, so remove it if its
5527c478bd9Sstevel@tonic-gate 				 * in our table, otherwise just clean it.
5537c478bd9Sstevel@tonic-gate 				 */
5547c478bd9Sstevel@tonic-gate 				if (find_pid(utmpx->ut_pid, &i) == 1)
5557c478bd9Sstevel@tonic-gate 					rem_pid(utmpx->ut_pid, i, CLEANIT);
5567c478bd9Sstevel@tonic-gate 				else
5577c478bd9Sstevel@tonic-gate 					clean_utmpx_ent(utmpx);
5587c478bd9Sstevel@tonic-gate 			}
5597c478bd9Sstevel@tonic-gate 		}
5607c478bd9Sstevel@tonic-gate 	}
5617c478bd9Sstevel@tonic-gate 	/*
5627c478bd9Sstevel@tonic-gate 	 * Close it to flush the buffer.
5637c478bd9Sstevel@tonic-gate 	 */
5647c478bd9Sstevel@tonic-gate 	endutxent();
5657c478bd9Sstevel@tonic-gate }
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate /*
5697c478bd9Sstevel@tonic-gate  *			*** Receiver Routines ***
5707c478bd9Sstevel@tonic-gate  */
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate /*
5737c478bd9Sstevel@tonic-gate  * setup_pipe	- Set up the pipe to read pids over
5747c478bd9Sstevel@tonic-gate  */
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate static void
5777c478bd9Sstevel@tonic-gate setup_pipe()
5787c478bd9Sstevel@tonic-gate {
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 	struct statvfs statvfs_buf;
5817c478bd9Sstevel@tonic-gate 	/*
5827c478bd9Sstevel@tonic-gate 	 * This code & comments swiped from init and left stock since it works
5837c478bd9Sstevel@tonic-gate 	 */
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 	if (Pfd < 0) {
5867c478bd9Sstevel@tonic-gate 		if ((statvfs(UTMPPIPE_DIR, &statvfs_buf) == 0) &&
5877c478bd9Sstevel@tonic-gate 		    ((statvfs_buf.f_flag & ST_RDONLY) == 0)) {
5887c478bd9Sstevel@tonic-gate 			(void) unlink(UTMPPIPE);
5897c478bd9Sstevel@tonic-gate 			(void) mknod(UTMPPIPE, S_IFIFO | 0600, 0);
5907c478bd9Sstevel@tonic-gate 		}
5917c478bd9Sstevel@tonic-gate 		Pfd = open(UTMPPIPE, O_RDWR | O_NDELAY);
5927c478bd9Sstevel@tonic-gate 	}
5937c478bd9Sstevel@tonic-gate 	if (Pfd < 0)
5947c478bd9Sstevel@tonic-gate 		nonfatal(UTMPPIPE);
5957c478bd9Sstevel@tonic-gate 	/*
5967c478bd9Sstevel@tonic-gate 	 * This code from init modified to be poll based instead of SIGPOLL,
5977c478bd9Sstevel@tonic-gate 	 * signal based.
5987c478bd9Sstevel@tonic-gate 	 */
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	if (Pfd >= 0) {
6017c478bd9Sstevel@tonic-gate 		/*
6027c478bd9Sstevel@tonic-gate 		 * Read pipe in message discard mode.  When read reads a
6037c478bd9Sstevel@tonic-gate 		 * pidrec size record, the remainder of the message will
6047c478bd9Sstevel@tonic-gate 		 * be discarded.  Though there shouldn't be any it will
6057c478bd9Sstevel@tonic-gate 		 * help resynch if someone else wrote some garbage.
6067c478bd9Sstevel@tonic-gate 		 */
6077c478bd9Sstevel@tonic-gate 		(void) ioctl(Pfd, I_SRDOPT, RMSGD);
6087c478bd9Sstevel@tonic-gate 	}
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 	/*
6117c478bd9Sstevel@tonic-gate 	 * My code.  We use slot 0 in the table to hold the fd of the pipe
6127c478bd9Sstevel@tonic-gate 	 */
6137c478bd9Sstevel@tonic-gate 	add_pid(0);			/* Proc 0 guaranteed to get slot 0 */
6147c478bd9Sstevel@tonic-gate 	fdtable[0].fd = Pfd;		/* Pfd could be -1, should be okay */
6157c478bd9Sstevel@tonic-gate 	fdtable[0].events = POLLRDNORM;
6167c478bd9Sstevel@tonic-gate }
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate /*
6197c478bd9Sstevel@tonic-gate  * drain_pipe()		- The receiver routine that reads the pipe
6207c478bd9Sstevel@tonic-gate  */
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate static void
6237c478bd9Sstevel@tonic-gate drain_pipe()
6247c478bd9Sstevel@tonic-gate {
6257c478bd9Sstevel@tonic-gate 	struct pidrec prec;
6267c478bd9Sstevel@tonic-gate 	register struct pidrec *p = &prec;
6277c478bd9Sstevel@tonic-gate 	int bytes_read;
6287c478bd9Sstevel@tonic-gate 	int i;
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 	for (;;) {
6317c478bd9Sstevel@tonic-gate 		/*
6327c478bd9Sstevel@tonic-gate 		 * Important Note: Either read will really fail (in which case
6337c478bd9Sstevel@tonic-gate 		 * return is all we can do) or will get EAGAIN (Pfd was opened
6347c478bd9Sstevel@tonic-gate 		 * O_NDELAY), in which case we also want to return.
6357c478bd9Sstevel@tonic-gate 		 */
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 		if ((bytes_read = read(Pfd, p, sizeof (struct pidrec))) !=
6387c478bd9Sstevel@tonic-gate 		    sizeof (struct pidrec))  {
6397c478bd9Sstevel@tonic-gate 			/*
6407c478bd9Sstevel@tonic-gate 			 * Something went wrong reading, so read until pipe
6417c478bd9Sstevel@tonic-gate 			 * is empty
6427c478bd9Sstevel@tonic-gate 			 */
6437c478bd9Sstevel@tonic-gate 			if (bytes_read > 0)
6447c478bd9Sstevel@tonic-gate 				while (read(Pfd, p, sizeof (struct pidrec)) > 0)
6457c478bd9Sstevel@tonic-gate 					;
6467c478bd9Sstevel@tonic-gate 			return;
6477c478bd9Sstevel@tonic-gate 		}
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 		dprintf(("drain_pipe: Recd command %d, pid %d\n",
6507c478bd9Sstevel@tonic-gate 		    p->pd_type, (int)p->pd_pid));
6517c478bd9Sstevel@tonic-gate 		switch (p->pd_type) {
6527c478bd9Sstevel@tonic-gate 		case ADDPID:
6537c478bd9Sstevel@tonic-gate 			/*
6547c478bd9Sstevel@tonic-gate 			 * Check if we already have the process, adding it
6557c478bd9Sstevel@tonic-gate 			 * if we don't.
6567c478bd9Sstevel@tonic-gate 			 */
6577c478bd9Sstevel@tonic-gate 			if (find_pid(p->pd_pid, &i) == 0)
6587c478bd9Sstevel@tonic-gate 				add_pid(p->pd_pid);
6597c478bd9Sstevel@tonic-gate 			break;
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate 		case REMPID:
6627c478bd9Sstevel@tonic-gate 			rem_pid(p->pd_pid, -1, DONT_CLEAN);
6637c478bd9Sstevel@tonic-gate 			break;
6647c478bd9Sstevel@tonic-gate 		default:
6657c478bd9Sstevel@tonic-gate 			nonfatal("Bad message on utmppipe\n");
6667c478bd9Sstevel@tonic-gate 				break;
6677c478bd9Sstevel@tonic-gate 		}
6687c478bd9Sstevel@tonic-gate 	}
6697c478bd9Sstevel@tonic-gate }
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate /*
6737c478bd9Sstevel@tonic-gate  *		*** Utilities for add and removing entries in the tables ***
6747c478bd9Sstevel@tonic-gate  */
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate /*
6777c478bd9Sstevel@tonic-gate  * add_pid	- add a pid to the fd table and the pidtable.
6787c478bd9Sstevel@tonic-gate  *		  these tables are sorted tables for quick lookups.
6797c478bd9Sstevel@tonic-gate  *
6807c478bd9Sstevel@tonic-gate  */
6817c478bd9Sstevel@tonic-gate static void
6827c478bd9Sstevel@tonic-gate add_pid(pid)
6837c478bd9Sstevel@tonic-gate 	pid_t pid;
6847c478bd9Sstevel@tonic-gate {
6857c478bd9Sstevel@tonic-gate 	int fd = 0;
6867c478bd9Sstevel@tonic-gate 	int i = 0, move_amt;
6877c478bd9Sstevel@tonic-gate 	int j;
6887c478bd9Sstevel@tonic-gate 	static int first_time = 1;
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 	/*
6917c478bd9Sstevel@tonic-gate 	 * Check to see if the pid is already in our table, or being passed
6927c478bd9Sstevel@tonic-gate 	 * pid zero.
6937c478bd9Sstevel@tonic-gate 	 */
6947c478bd9Sstevel@tonic-gate 	if (pidcnt != 0 && (find_pid(pid, &j) == 1 || pid == 0))
6957c478bd9Sstevel@tonic-gate 		return;
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate 	if (pidcnt >= Max_fds) {
6987c478bd9Sstevel@tonic-gate 		if (first_time == 1) {
6997c478bd9Sstevel@tonic-gate 			/*
7007c478bd9Sstevel@tonic-gate 			 * Print this error only once
7017c478bd9Sstevel@tonic-gate 			 */
7027c478bd9Sstevel@tonic-gate 			nonfatal("File Descriptor limit exceeded");
7037c478bd9Sstevel@tonic-gate 			first_time = 0;
7047c478bd9Sstevel@tonic-gate 		}
7057c478bd9Sstevel@tonic-gate 		return;
7067c478bd9Sstevel@tonic-gate 	}
7077c478bd9Sstevel@tonic-gate 	/*
7087c478bd9Sstevel@tonic-gate 	 * Open the /proc file checking if there's still a valid proc file.
7097c478bd9Sstevel@tonic-gate 	 */
7107c478bd9Sstevel@tonic-gate 	if (pid != 0 && (fd = proc_to_fd(pid)) == -1) {
7117c478bd9Sstevel@tonic-gate 		/*
7127c478bd9Sstevel@tonic-gate 		 * No so the process died before we got to watch for him
7137c478bd9Sstevel@tonic-gate 		 */
7147c478bd9Sstevel@tonic-gate 		return;
7157c478bd9Sstevel@tonic-gate 	}
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate 	/*
7187c478bd9Sstevel@tonic-gate 	 * We only do this code if we're not putting in the first element
7197c478bd9Sstevel@tonic-gate 	 * Which we know will be for proc zero which is used by setup_pipe
7207c478bd9Sstevel@tonic-gate 	 * for its pipe fd.
7217c478bd9Sstevel@tonic-gate 	 */
7227c478bd9Sstevel@tonic-gate 	if (pidcnt != 0) {
7237c478bd9Sstevel@tonic-gate 		for (i = 0; i < pidcnt; i++) {
7247c478bd9Sstevel@tonic-gate 			if (pid <= pidtable[i].pl_pid)
7257c478bd9Sstevel@tonic-gate 				break;
7267c478bd9Sstevel@tonic-gate 		}
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate 		/*
7297c478bd9Sstevel@tonic-gate 		 * Handle the case where we're not sticking our entry on the
7307c478bd9Sstevel@tonic-gate 		 * the end, or overwriting an existing entry.
7317c478bd9Sstevel@tonic-gate 		 */
7327c478bd9Sstevel@tonic-gate 		if (i != pidcnt && pid != pidtable[i].pl_pid) {
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 			move_amt = pidcnt - i;
7357c478bd9Sstevel@tonic-gate 			/*
7367c478bd9Sstevel@tonic-gate 			 * Move table down
7377c478bd9Sstevel@tonic-gate 			 */
7387c478bd9Sstevel@tonic-gate 			if (move_amt != 0) {
7397c478bd9Sstevel@tonic-gate 				(void) memmove(&pidtable[i+1], &pidtable[i],
7407c478bd9Sstevel@tonic-gate 					move_amt * sizeof (struct pidentry));
7417c478bd9Sstevel@tonic-gate 				(void) memmove(&fdtable[i+1], &fdtable[i],
7427c478bd9Sstevel@tonic-gate 					move_amt * sizeof (pollfd_t));
7437c478bd9Sstevel@tonic-gate 			}
7447c478bd9Sstevel@tonic-gate 		}
7457c478bd9Sstevel@tonic-gate 	}
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 	/*
7487c478bd9Sstevel@tonic-gate 	 * Fill in the events field for poll and copy the entry into the array
7497c478bd9Sstevel@tonic-gate 	 */
7507c478bd9Sstevel@tonic-gate 	fdtable[i].events = 0;
7517c478bd9Sstevel@tonic-gate 	fdtable[i].revents = 0;
7527c478bd9Sstevel@tonic-gate 	fdtable[i].fd = fd;
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 	/*
7557c478bd9Sstevel@tonic-gate 	 * Likewise, setup pid field and pointer (index) to the fdtable entry
7567c478bd9Sstevel@tonic-gate 	 */
7577c478bd9Sstevel@tonic-gate 	pidtable[i].pl_pid = pid;
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 	pidcnt++;			/* Bump the pid count */
7607c478bd9Sstevel@tonic-gate 	dprintf(("  add_pid: pid = %d fd = %d index = %d pidcnt = %d\n",
7617c478bd9Sstevel@tonic-gate 		(int)pid, fd, i, pidcnt));
7627c478bd9Sstevel@tonic-gate }
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate /*
7667c478bd9Sstevel@tonic-gate  * rem_pid	- Remove an entry from the table and check to see if its
7677c478bd9Sstevel@tonic-gate  *		  not in the utmpx file.
7687c478bd9Sstevel@tonic-gate  *		  If i != -1 don't look up the pid, use i as index
7697c478bd9Sstevel@tonic-gate  */
7707c478bd9Sstevel@tonic-gate 
7717c478bd9Sstevel@tonic-gate static void
7727c478bd9Sstevel@tonic-gate rem_pid(pid, i, clean_it)
7737c478bd9Sstevel@tonic-gate 	pid_t pid;	/* Pid of process to clean or 0 if we don't know it */
7747c478bd9Sstevel@tonic-gate 	int i;		/* Index into table or -1 if we need to look it up */
7757c478bd9Sstevel@tonic-gate 	int clean_it;	/* Clean the entry, or just remove from table? */
7767c478bd9Sstevel@tonic-gate {
7777c478bd9Sstevel@tonic-gate 	int move_amt;
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate 	dprintf(("  rem_pid: pid = %d i = %d", (int)pid, i));
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate 	/*
7827c478bd9Sstevel@tonic-gate 	 * Don't allow slot 0 in the table to be removed - utmppipe fd
7837c478bd9Sstevel@tonic-gate 	 */
7847c478bd9Sstevel@tonic-gate 	if ((i == -1 && pid == 0) || (i == 0))	{
7857c478bd9Sstevel@tonic-gate 		dprintf((" - attempted to remove proc 0\n"));
7867c478bd9Sstevel@tonic-gate 		return;
7877c478bd9Sstevel@tonic-gate 	}
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate 	if (i != -1 || find_pid(pid, &i) == 1) {	/* Found the entry */
7907c478bd9Sstevel@tonic-gate 		(void) close(fdtable[i].fd);	/* We're done with the fd */
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate 		dprintf((" fd = %d\n", fdtable[i].fd));
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate 		if (clean_it == CLEANIT)
7957c478bd9Sstevel@tonic-gate 			clean_entry(i);
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate 		move_amt = (pidcnt - i) - 1;
7987c478bd9Sstevel@tonic-gate 		/*
7997c478bd9Sstevel@tonic-gate 		 * Remove entries from the tables.
8007c478bd9Sstevel@tonic-gate 		 */
8017c478bd9Sstevel@tonic-gate 		(void) memmove(&pidtable[i], &pidtable[i+1],
8027c478bd9Sstevel@tonic-gate 			move_amt * sizeof (struct pidentry));
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate 		(void) memmove(&fdtable[i], &fdtable[i+1],
8057c478bd9Sstevel@tonic-gate 			move_amt * sizeof (pollfd_t));
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 		/*
8087c478bd9Sstevel@tonic-gate 		 * decrement the pid count - one less pid to worry about
8097c478bd9Sstevel@tonic-gate 		 */
8107c478bd9Sstevel@tonic-gate 		pidcnt--;
8117c478bd9Sstevel@tonic-gate 	}
8127c478bd9Sstevel@tonic-gate 	if (i == -1)
8137c478bd9Sstevel@tonic-gate 		dprintf((" - entry not found \n"));
8147c478bd9Sstevel@tonic-gate }
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate /*
8187c478bd9Sstevel@tonic-gate  * find_pid	- Returns an index into the pidtable of the specifed pid,
8197c478bd9Sstevel@tonic-gate  *		  else -1 if not found
8207c478bd9Sstevel@tonic-gate  */
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate static int
8237c478bd9Sstevel@tonic-gate find_pid(pid, i)
8247c478bd9Sstevel@tonic-gate 	pid_t pid;
8257c478bd9Sstevel@tonic-gate 	int *i;
8267c478bd9Sstevel@tonic-gate {
8277c478bd9Sstevel@tonic-gate 	struct pidentry pe;
8287c478bd9Sstevel@tonic-gate 	struct pidentry *p;
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate 	pe.pl_pid = pid;
8317c478bd9Sstevel@tonic-gate 	p = bsearch(&pe, pidtable, pidcnt, sizeof (struct pidentry), pidcmp);
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate 	if (p == NULL)
8347c478bd9Sstevel@tonic-gate 		return (0);
8357c478bd9Sstevel@tonic-gate 	else {
8367c478bd9Sstevel@tonic-gate 		*i = p - (struct pidentry *)pidtable;
8377c478bd9Sstevel@tonic-gate 		return (1);
8387c478bd9Sstevel@tonic-gate 	}
8397c478bd9Sstevel@tonic-gate }
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate /*
8437c478bd9Sstevel@tonic-gate  * Pidcmp - Used by besearch for sorting and finding  process IDs.
8447c478bd9Sstevel@tonic-gate  */
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate static int
8477c478bd9Sstevel@tonic-gate pidcmp(a, b)
8487c478bd9Sstevel@tonic-gate 	struct pidentry *a, *b;
8497c478bd9Sstevel@tonic-gate {
8507c478bd9Sstevel@tonic-gate 	if (b == NULL || a == NULL)
8517c478bd9Sstevel@tonic-gate 		return (0);
8527c478bd9Sstevel@tonic-gate 	return (a->pl_pid - b->pl_pid);
8537c478bd9Sstevel@tonic-gate }
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate /*
8577c478bd9Sstevel@tonic-gate  * proc_to_fd	- Take a process ID and return an open file descriptor to the
8587c478bd9Sstevel@tonic-gate  *		  /proc file for the specified process.
8597c478bd9Sstevel@tonic-gate  */
8607c478bd9Sstevel@tonic-gate static int
8617c478bd9Sstevel@tonic-gate proc_to_fd(pid)
8627c478bd9Sstevel@tonic-gate 	pid_t pid;
8637c478bd9Sstevel@tonic-gate {
8647c478bd9Sstevel@tonic-gate 	char procname[64];
8657c478bd9Sstevel@tonic-gate 	int fd, dfd;
8667c478bd9Sstevel@tonic-gate 
8677c478bd9Sstevel@tonic-gate 	(void) sprintf(procname, "/proc/%d/psinfo", (int)pid);
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate 	if ((fd = open(procname, O_RDONLY)) >= 0) {
8707c478bd9Sstevel@tonic-gate 		/*
8717c478bd9Sstevel@tonic-gate 		 * dup the fd above the low order values to assure
8727c478bd9Sstevel@tonic-gate 		 * stdio works for other fds - paranoia.
8737c478bd9Sstevel@tonic-gate 		 */
8747c478bd9Sstevel@tonic-gate 		if (fd < EXTRA_MARGIN) {
8757c478bd9Sstevel@tonic-gate 			dfd = fcntl(fd, F_DUPFD, EXTRA_MARGIN);
8767c478bd9Sstevel@tonic-gate 			if (dfd > 0) {
8777c478bd9Sstevel@tonic-gate 				(void) close(fd);
8787c478bd9Sstevel@tonic-gate 				fd = dfd;
8797c478bd9Sstevel@tonic-gate 			}
8807c478bd9Sstevel@tonic-gate 		}
8817c478bd9Sstevel@tonic-gate 		/*
8827c478bd9Sstevel@tonic-gate 		 * More paranoia - set the close on exec flag
8837c478bd9Sstevel@tonic-gate 		 */
8847c478bd9Sstevel@tonic-gate 		(void) fcntl(fd, F_SETFD, 1);
8857c478bd9Sstevel@tonic-gate 		return (fd);
8867c478bd9Sstevel@tonic-gate 	}
8877c478bd9Sstevel@tonic-gate 	if (errno == ENOENT)
8887c478bd9Sstevel@tonic-gate 		return (-1);
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate 	if (errno == EMFILE) {
8917c478bd9Sstevel@tonic-gate 		/*
8927c478bd9Sstevel@tonic-gate 		 * This is fatal, since libc won't be able to allocate
8937c478bd9Sstevel@tonic-gate 		 * any fds for the pututxline() routines
8947c478bd9Sstevel@tonic-gate 		 */
8957c478bd9Sstevel@tonic-gate 		fatal("Out of file descriptors");
8967c478bd9Sstevel@tonic-gate 	}
8977c478bd9Sstevel@tonic-gate 	fatal(procname);		/* Only get here on error */
8987c478bd9Sstevel@tonic-gate 	return (-1);
8997c478bd9Sstevel@tonic-gate }
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate 
9027c478bd9Sstevel@tonic-gate /*
9037c478bd9Sstevel@tonic-gate  *		*** Utmpx Cleaning Utilities ***
9047c478bd9Sstevel@tonic-gate  */
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate /*
9077c478bd9Sstevel@tonic-gate  * Clean_entry	- Cleans the specified entry - where i is an index
9087c478bd9Sstevel@tonic-gate  *		  into the pid_table.
9097c478bd9Sstevel@tonic-gate  */
9107c478bd9Sstevel@tonic-gate static void
9117c478bd9Sstevel@tonic-gate clean_entry(i)
9127c478bd9Sstevel@tonic-gate 	int i;
9137c478bd9Sstevel@tonic-gate {
9147c478bd9Sstevel@tonic-gate 	struct utmpx *u;
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate 	if (pidcnt == 0)
9177c478bd9Sstevel@tonic-gate 		return;
9187c478bd9Sstevel@tonic-gate 
9197c478bd9Sstevel@tonic-gate 	dprintf(("    Cleaning %d\n", (int)pidtable[i].pl_pid));
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate 	/*
9227c478bd9Sstevel@tonic-gate 	 * Double check if the process is dead.
9237c478bd9Sstevel@tonic-gate 	 */
9247c478bd9Sstevel@tonic-gate 	if (proc_is_alive(pidtable[i].pl_pid)) {
9257c478bd9Sstevel@tonic-gate 		dprintf(("      Bad attempt to clean %d\n", \
9267c478bd9Sstevel@tonic-gate 			(int)pidtable[i].pl_pid));
9277c478bd9Sstevel@tonic-gate 		return;
9287c478bd9Sstevel@tonic-gate 	}
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate 	/*
9317c478bd9Sstevel@tonic-gate 	 * Find the entry that corresponds to this pid.
9327c478bd9Sstevel@tonic-gate 	 * Do nothing if entry not found in utmpx file.
9337c478bd9Sstevel@tonic-gate 	 */
9347c478bd9Sstevel@tonic-gate 	setutxent();
9357c478bd9Sstevel@tonic-gate 	while ((u = getutxent()) != NULL) {
9367c478bd9Sstevel@tonic-gate 		if (u->ut_pid == pidtable[i].pl_pid) {
9377c478bd9Sstevel@tonic-gate 			if (u->ut_type == USER_PROCESS) {
9387c478bd9Sstevel@tonic-gate 				clean_utmpx_ent(u);
9397c478bd9Sstevel@tonic-gate 			}
9407c478bd9Sstevel@tonic-gate 		}
9417c478bd9Sstevel@tonic-gate 	}
9427c478bd9Sstevel@tonic-gate 	endutxent();
9437c478bd9Sstevel@tonic-gate }
9447c478bd9Sstevel@tonic-gate 
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate /*
9477c478bd9Sstevel@tonic-gate  * clean_utmpx_ent	- Clean a utmpx entry
9487c478bd9Sstevel@tonic-gate  */
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate static void
9517c478bd9Sstevel@tonic-gate clean_utmpx_ent(u)
9527c478bd9Sstevel@tonic-gate 	struct utmpx *u;
9537c478bd9Sstevel@tonic-gate {
9547c478bd9Sstevel@tonic-gate 	dprintf(("      clean_utmpx_ent: %d\n", (int)u->ut_pid));
9557c478bd9Sstevel@tonic-gate 	u->ut_type = DEAD_PROCESS;
9567c478bd9Sstevel@tonic-gate 	(void) time(&u->ut_xtime);
9577c478bd9Sstevel@tonic-gate 	(void) pututxline(u);
9587c478bd9Sstevel@tonic-gate 	updwtmpx(WTMPX_FILE, u);
9597c478bd9Sstevel@tonic-gate 	/*
960*1eabc4beSSachidananda Urs 	 * XXX update wtmp for ! nonuserx entries?
9617c478bd9Sstevel@tonic-gate 	 */
9627c478bd9Sstevel@tonic-gate }
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate /*
9657c478bd9Sstevel@tonic-gate  *		*** Error Handling and Debugging Routines ***
9667c478bd9Sstevel@tonic-gate  */
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate /*
9697c478bd9Sstevel@tonic-gate  * fatal - Catastrophic failure
9707c478bd9Sstevel@tonic-gate  */
9717c478bd9Sstevel@tonic-gate 
9727c478bd9Sstevel@tonic-gate static void
9737c478bd9Sstevel@tonic-gate fatal(char *str)
9747c478bd9Sstevel@tonic-gate {
9757c478bd9Sstevel@tonic-gate 	int oerrno = errno;
9767c478bd9Sstevel@tonic-gate 
977d2117003Sdp 	syslog(LOG_ALERT, "%s", str);
9787c478bd9Sstevel@tonic-gate 	if (Debug == 1) {
9797c478bd9Sstevel@tonic-gate 		if ((errno = oerrno) != 0)
9807c478bd9Sstevel@tonic-gate 			perror(prog_name);
9817c478bd9Sstevel@tonic-gate 		dprintf(("%s\n", str));
9827c478bd9Sstevel@tonic-gate 	}
983d2117003Sdp 	exit(1);
9847c478bd9Sstevel@tonic-gate }
9857c478bd9Sstevel@tonic-gate 
9867c478bd9Sstevel@tonic-gate /*
9877c478bd9Sstevel@tonic-gate  * nonfatal - Non-Catastrophic failure - print message and errno
9887c478bd9Sstevel@tonic-gate  */
9897c478bd9Sstevel@tonic-gate 
9907c478bd9Sstevel@tonic-gate static void
9917c478bd9Sstevel@tonic-gate nonfatal(char *str)
9927c478bd9Sstevel@tonic-gate {
993d2117003Sdp 	syslog(LOG_WARNING, "%s", str);
9947c478bd9Sstevel@tonic-gate 
9957c478bd9Sstevel@tonic-gate 	if (Debug == 1) {
9967c478bd9Sstevel@tonic-gate 		if (errno != 0)
9977c478bd9Sstevel@tonic-gate 			perror(prog_name);
9987c478bd9Sstevel@tonic-gate 		dprintf(("%c%s\n", 7, str));
9997c478bd9Sstevel@tonic-gate 		print_tables();
10007c478bd9Sstevel@tonic-gate 		(void) sleep(5);	/* Time to read debug messages */
10017c478bd9Sstevel@tonic-gate 	}
10027c478bd9Sstevel@tonic-gate }
10037c478bd9Sstevel@tonic-gate 
10047c478bd9Sstevel@tonic-gate /*
10057c478bd9Sstevel@tonic-gate  * print_tables	- Print internal tables - for debugging
10067c478bd9Sstevel@tonic-gate  */
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate static void
10097c478bd9Sstevel@tonic-gate print_tables()
10107c478bd9Sstevel@tonic-gate {
10117c478bd9Sstevel@tonic-gate 	int i;
10127c478bd9Sstevel@tonic-gate 
10137c478bd9Sstevel@tonic-gate 	if (Debug == 0)
10147c478bd9Sstevel@tonic-gate 		return;
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate 	dprintf(("pidtable: "));
10177c478bd9Sstevel@tonic-gate 	for (i = 0; i < pidcnt; i++)
10187c478bd9Sstevel@tonic-gate 		dprintf(("%d: %d  ", i, (int)pidtable[i].pl_pid));
10197c478bd9Sstevel@tonic-gate 	dprintf(("\n"));
10207c478bd9Sstevel@tonic-gate 	dprintf(("fdtable:  "));
10217c478bd9Sstevel@tonic-gate 	for (i = 0; i < pidcnt; i++)
10227c478bd9Sstevel@tonic-gate 		dprintf(("%d: %d  ", i, fdtable[i].fd));
10237c478bd9Sstevel@tonic-gate 	dprintf(("\n"));
10247c478bd9Sstevel@tonic-gate }
10257c478bd9Sstevel@tonic-gate 
10267c478bd9Sstevel@tonic-gate /*
10277c478bd9Sstevel@tonic-gate  * proc_is_alive	- Check to see if a process is alive AND its
10287c478bd9Sstevel@tonic-gate  *			  not a zombie.  Returns 1 if process is alive
10297c478bd9Sstevel@tonic-gate  *			  and zero if it is dead or a zombie.
10307c478bd9Sstevel@tonic-gate  */
10317c478bd9Sstevel@tonic-gate 
10327c478bd9Sstevel@tonic-gate static int
10337c478bd9Sstevel@tonic-gate proc_is_alive(pid)
10347c478bd9Sstevel@tonic-gate 	pid_t pid;
10357c478bd9Sstevel@tonic-gate {
10367c478bd9Sstevel@tonic-gate 	char psinfoname[64];
10377c478bd9Sstevel@tonic-gate 	int fd;
10387c478bd9Sstevel@tonic-gate 	psinfo_t psinfo;
10397c478bd9Sstevel@tonic-gate 
10407c478bd9Sstevel@tonic-gate 	if (kill(pid, 0) != 0)
10417c478bd9Sstevel@tonic-gate 		return (0);		/* Kill failed - no process */
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate 	/*
10447c478bd9Sstevel@tonic-gate 	 * The process exists, so check if it's a zombie.
10457c478bd9Sstevel@tonic-gate 	 */
10467c478bd9Sstevel@tonic-gate 	(void) sprintf(psinfoname, "/proc/%d/psinfo", (int)pid);
10477c478bd9Sstevel@tonic-gate 
10487c478bd9Sstevel@tonic-gate 	if ((fd = open(psinfoname, O_RDONLY)) < 0 ||
10497c478bd9Sstevel@tonic-gate 	    read(fd, &psinfo, sizeof (psinfo)) != sizeof (psinfo)) {
10507c478bd9Sstevel@tonic-gate 		/*
10517c478bd9Sstevel@tonic-gate 		 * We either couldn't open the proc, or we did but the
10527c478bd9Sstevel@tonic-gate 		 * read of the psinfo file failed, so pid is nonexistent.
10537c478bd9Sstevel@tonic-gate 		 */
10547c478bd9Sstevel@tonic-gate 		psinfo.pr_nlwp = 0;
10557c478bd9Sstevel@tonic-gate 	}
10567c478bd9Sstevel@tonic-gate 	if (fd >= 0)
10577c478bd9Sstevel@tonic-gate 		(void) close(fd);
10587c478bd9Sstevel@tonic-gate 
10597c478bd9Sstevel@tonic-gate 	/* if pr_nlwp == 0, process is a zombie */
10607c478bd9Sstevel@tonic-gate 	return (psinfo.pr_nlwp != 0);
10617c478bd9Sstevel@tonic-gate }
10627c478bd9Sstevel@tonic-gate 
10637c478bd9Sstevel@tonic-gate /*
10647c478bd9Sstevel@tonic-gate  * warn_utmp -	/var/adm/utmp has been deprecated. It should no longer
10657c478bd9Sstevel@tonic-gate  *		be used.  Applications that try to directly manipulate
10667c478bd9Sstevel@tonic-gate  *		it may cause problems. Since the file is no longer
10677c478bd9Sstevel@tonic-gate  *		shipped, if it appears on a system it's because an
10687c478bd9Sstevel@tonic-gate  *		old application created it.  We'll have utmpd
10697c478bd9Sstevel@tonic-gate  *		complain about it periodically.
10707c478bd9Sstevel@tonic-gate  */
10717c478bd9Sstevel@tonic-gate 
10727c478bd9Sstevel@tonic-gate static void
10737c478bd9Sstevel@tonic-gate warn_utmp()
10747c478bd9Sstevel@tonic-gate {
10757c478bd9Sstevel@tonic-gate 	struct stat s;
10767c478bd9Sstevel@tonic-gate 
10777c478bd9Sstevel@tonic-gate 	if (lstat(UTMP_FILE, &s) == 0 &&
10787c478bd9Sstevel@tonic-gate 	    s.st_size % sizeof (struct utmp) == 0) {
10797c478bd9Sstevel@tonic-gate 		nonfatal("WARNING: /var/adm/utmp exists!\nSee "
10807c478bd9Sstevel@tonic-gate 		    "utmp(4) for more information");
10817c478bd9Sstevel@tonic-gate 	}
10827c478bd9Sstevel@tonic-gate }
1083