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