10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*1914Scasper * Common Development and Distribution License (the "License"). 6*1914Scasper * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 22*1914Scasper * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 270Sstevel@tonic-gate /* All Rights Reserved */ 280Sstevel@tonic-gate 290Sstevel@tonic-gate /* 300Sstevel@tonic-gate * Portions of such source code were derived from Berkeley 4.3 BSD 310Sstevel@tonic-gate * under license from the Regents of the University of California. 320Sstevel@tonic-gate */ 330Sstevel@tonic-gate 340Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 350Sstevel@tonic-gate 360Sstevel@tonic-gate /* 370Sstevel@tonic-gate * utmpd - utmp daemon 380Sstevel@tonic-gate * 390Sstevel@tonic-gate * This program receives requests from pututxline(3) 400Sstevel@tonic-gate * via a named pipe to watch the process to make sure it cleans up 410Sstevel@tonic-gate * its utmpx entry on termination. 420Sstevel@tonic-gate * The program keeps a list of procs 430Sstevel@tonic-gate * and uses poll() on their /proc files to detect termination. 440Sstevel@tonic-gate * Also the program periodically scans the /etc/utmpx file for 450Sstevel@tonic-gate * processes that aren't in the table so they can be watched. 460Sstevel@tonic-gate * 470Sstevel@tonic-gate * If utmpd doesn't hear back over the pipe from pututline(3) that 480Sstevel@tonic-gate * the process has removed its entry it cleans the entry when the 490Sstevel@tonic-gate * the process terminates. 500Sstevel@tonic-gate * The AT&T Copyright above is there since we borrowed the pipe 510Sstevel@tonic-gate * mechanism from init(1m). 520Sstevel@tonic-gate */ 530Sstevel@tonic-gate 540Sstevel@tonic-gate 550Sstevel@tonic-gate #include <sys/types.h> 560Sstevel@tonic-gate #include <signal.h> 570Sstevel@tonic-gate #include <stdio.h> 58*1914Scasper #include <stdio_ext.h> 590Sstevel@tonic-gate #include <unistd.h> 600Sstevel@tonic-gate #include <utmpx.h> 610Sstevel@tonic-gate #include <errno.h> 620Sstevel@tonic-gate #include <termio.h> 630Sstevel@tonic-gate #include <sys/termios.h> 640Sstevel@tonic-gate #include <sys/tty.h> 650Sstevel@tonic-gate #include <ctype.h> 660Sstevel@tonic-gate #include <sys/stat.h> 670Sstevel@tonic-gate #include <sys/statvfs.h> 680Sstevel@tonic-gate #include <fcntl.h> 690Sstevel@tonic-gate #include <time.h> 700Sstevel@tonic-gate #include <sys/stropts.h> 710Sstevel@tonic-gate #include <wait.h> 720Sstevel@tonic-gate #include <syslog.h> 730Sstevel@tonic-gate #include <stdlib.h> 740Sstevel@tonic-gate #include <string.h> 750Sstevel@tonic-gate #include <poll.h> 760Sstevel@tonic-gate #include <deflt.h> 770Sstevel@tonic-gate #include <procfs.h> 780Sstevel@tonic-gate #include <sys/resource.h> 790Sstevel@tonic-gate 800Sstevel@tonic-gate #define dprintf(x) if (Debug) (void) printf x 810Sstevel@tonic-gate 820Sstevel@tonic-gate /* 830Sstevel@tonic-gate * Memory allocation keyed off MAX_FDS 840Sstevel@tonic-gate */ 850Sstevel@tonic-gate #define MAX_FDS 4064 /* Maximum # file descriptors */ 860Sstevel@tonic-gate #define EXTRA_MARGIN 32 /* Allocate this many more FDS over Max_Fds */ 870Sstevel@tonic-gate /* 880Sstevel@tonic-gate * MAX_POLLNV & RESETS - paranoia to cover an error case that might not exist 890Sstevel@tonic-gate */ 900Sstevel@tonic-gate #define MAX_POLL_ERRS 1024 /* Count of bad errors */ 910Sstevel@tonic-gate #define MAX_RESETS 1024 /* Maximum times to reload tables */ 920Sstevel@tonic-gate #define POLL_TIMEOUT 300 /* Default Timeout for poll() in seconds */ 930Sstevel@tonic-gate #define CLEANIT 1 /* Used by rem_pid() */ 940Sstevel@tonic-gate #define DONT_CLEAN 0 /* Used by rem_pid() */ 950Sstevel@tonic-gate #define UTMP_DEFAULT "/etc/default/utmpd" 960Sstevel@tonic-gate #define WARN_TIME 3600 /* seconds between utmp checks */ 970Sstevel@tonic-gate #define WTMPX_UFREQ 60 /* seconds between updating WTMPX's atime */ 980Sstevel@tonic-gate 990Sstevel@tonic-gate 1000Sstevel@tonic-gate /* 1010Sstevel@tonic-gate * The pidrec structure describes the data shipped down the pipe to 1020Sstevel@tonic-gate * us from the pututxline() library in 1030Sstevel@tonic-gate * lib/libc/port/gen/getutx.c 1040Sstevel@tonic-gate */ 1050Sstevel@tonic-gate 1060Sstevel@tonic-gate /* 1070Sstevel@tonic-gate * pd_type's 1080Sstevel@tonic-gate */ 1090Sstevel@tonic-gate #define ADDPID 1 1100Sstevel@tonic-gate #define REMPID 2 1110Sstevel@tonic-gate 1120Sstevel@tonic-gate struct pidrec { 1130Sstevel@tonic-gate int pd_type; /* Command type */ 1140Sstevel@tonic-gate pid_t pd_pid; /* pid to add or remove */ 1150Sstevel@tonic-gate }; 1160Sstevel@tonic-gate 1170Sstevel@tonic-gate 1180Sstevel@tonic-gate /* 1190Sstevel@tonic-gate * Since this program uses poll(2) and poll takes an array of file descriptors 1200Sstevel@tonic-gate * as an argument we maintain our data in tables. 1210Sstevel@tonic-gate * One table is the file descriptor array for poll, another parallel 1220Sstevel@tonic-gate * array is a table which contains the process ID of the corresponding 1230Sstevel@tonic-gate * open fd. These tables are kept sorted by process ID for quick lookups. 1240Sstevel@tonic-gate */ 1250Sstevel@tonic-gate 1260Sstevel@tonic-gate struct pidentry { 1270Sstevel@tonic-gate pid_t pl_pid; /* pid to watch for */ 1280Sstevel@tonic-gate int pl_status; /* Exit status of proc */ 1290Sstevel@tonic-gate }; 1300Sstevel@tonic-gate 1310Sstevel@tonic-gate static struct pidentry *pidtable = NULL; 1320Sstevel@tonic-gate 1330Sstevel@tonic-gate static pollfd_t *fdtable = NULL; 1340Sstevel@tonic-gate 1350Sstevel@tonic-gate static int pidcnt = 0; /* Number of procs being watched */ 1360Sstevel@tonic-gate static char *prog_name; /* To save the invocation name away */ 1370Sstevel@tonic-gate static char *UTMPPIPE_DIR = "/etc"; 1380Sstevel@tonic-gate static char *UTMPPIPE = "/etc/utmppipe"; 1390Sstevel@tonic-gate static int Pfd = -1; /* File descriptor of named pipe */ 1400Sstevel@tonic-gate static int Poll_timeout = POLL_TIMEOUT; 1410Sstevel@tonic-gate static int WTMPXfd = -1; /* File descriptor of WTMPX_FILE */ 1420Sstevel@tonic-gate static int WTMPX_ufreq = WTMPX_UFREQ; 1430Sstevel@tonic-gate static int Debug = 0; /* Set by command line argument */ 1440Sstevel@tonic-gate static int Max_fds = MAX_FDS; 1450Sstevel@tonic-gate 1460Sstevel@tonic-gate /* 1470Sstevel@tonic-gate * This program has three main components plus utilities and debug routines 1480Sstevel@tonic-gate * Receiver - receives the process ID or process for us to watch. 1490Sstevel@tonic-gate * (Uses a named pipe to get messages) 1500Sstevel@tonic-gate * Watcher - Use poll(2) to watch for processes to die so they 1510Sstevel@tonic-gate * can be cleaned up (get marked as DEAD_PROCESS) 1520Sstevel@tonic-gate * Scanner - periodically scans the utmpx file for stale entries 1530Sstevel@tonic-gate * or live entries that we don't know about. 1540Sstevel@tonic-gate */ 1550Sstevel@tonic-gate 1560Sstevel@tonic-gate static int wait_for_pids(); /* Watcher - uses poll */ 1570Sstevel@tonic-gate static void scan_utmps(); /* Scanner, reads utmpx file */ 1580Sstevel@tonic-gate static void drain_pipe(); /* Receiver - reads mesgs over UTMPPIPE */ 1590Sstevel@tonic-gate static void setup_pipe(); /* For setting up receiver */ 1600Sstevel@tonic-gate 1610Sstevel@tonic-gate static void add_pid(); /* Adds a process to the table */ 1620Sstevel@tonic-gate static void rem_pid(); /* Removes a process from the table */ 1630Sstevel@tonic-gate static int find_pid(); /* Finds a process in the table */ 1640Sstevel@tonic-gate static int proc_to_fd(); /* Takes a pid and returns an fd for its proc */ 1650Sstevel@tonic-gate static void load_tables(); /* Loads up the tables the first time around */ 1660Sstevel@tonic-gate static int pidcmp(); /* For sorting pids */ 1670Sstevel@tonic-gate 1680Sstevel@tonic-gate static void clean_entry(); /* Removes entry from our table and calls ... */ 1690Sstevel@tonic-gate static void clean_utmpx_ent(); /* Cleans a utmpx entry */ 1700Sstevel@tonic-gate 171350Sdp static void fatal() __NORETURN; /* Prints error message and calls exit */ 1720Sstevel@tonic-gate static void nonfatal(); /* Prints error message */ 1730Sstevel@tonic-gate static void print_tables(); /* Prints out internal tables for Debug */ 1740Sstevel@tonic-gate static int proc_is_alive(pid_t pid); /* Check if a process is alive */ 1750Sstevel@tonic-gate static void warn_utmp(void); 1760Sstevel@tonic-gate 1770Sstevel@tonic-gate /* 1780Sstevel@tonic-gate * main() - Main does basic setup and calls wait_for_pids() to do the work 1790Sstevel@tonic-gate */ 1800Sstevel@tonic-gate 181350Sdp int 1820Sstevel@tonic-gate main(argc, argv) 1830Sstevel@tonic-gate char **argv; 1840Sstevel@tonic-gate { 1850Sstevel@tonic-gate char *defp; 1860Sstevel@tonic-gate struct rlimit rlim; 1870Sstevel@tonic-gate int i; 1880Sstevel@tonic-gate time_t curtime, now; 1890Sstevel@tonic-gate 1900Sstevel@tonic-gate prog_name = argv[0]; /* Save invocation name */ 1910Sstevel@tonic-gate 1920Sstevel@tonic-gate if (getuid() != 0) { 1930Sstevel@tonic-gate (void) fprintf(stderr, 1940Sstevel@tonic-gate "You must be root to run this program\n"); 1950Sstevel@tonic-gate fatal("You must be root to run this program"); 1960Sstevel@tonic-gate } 1970Sstevel@tonic-gate 1980Sstevel@tonic-gate if (argc > 1) { 1990Sstevel@tonic-gate if ((argc == 2 && (int)strlen(argv[1]) >= 2) && 2000Sstevel@tonic-gate (argv[1][0] == '-' && argv[1][1] == 'd')) { 2010Sstevel@tonic-gate Debug = 1; 2020Sstevel@tonic-gate } else { 2030Sstevel@tonic-gate (void) fprintf(stderr, 2040Sstevel@tonic-gate "%s: Wrong number of arguments\n", prog_name); 2050Sstevel@tonic-gate (void) fprintf(stderr, 2060Sstevel@tonic-gate "Usage: %s [-debug]\n", prog_name); 207350Sdp exit(2); 2080Sstevel@tonic-gate } 2090Sstevel@tonic-gate } 2100Sstevel@tonic-gate 2110Sstevel@tonic-gate /* 2120Sstevel@tonic-gate * Read defaults file for poll timeout 2130Sstevel@tonic-gate */ 2140Sstevel@tonic-gate if (defopen(UTMP_DEFAULT) == 0) { 2150Sstevel@tonic-gate if ((defp = defread("SCAN_PERIOD=")) != NULL) { 2160Sstevel@tonic-gate Poll_timeout = atol(defp); 2170Sstevel@tonic-gate dprintf(("Poll timeout set to %d\n", Poll_timeout)); 2180Sstevel@tonic-gate } 2190Sstevel@tonic-gate 2200Sstevel@tonic-gate if ((defp = defread("WTMPX_UPDATE_FREQ=")) != NULL) { 2210Sstevel@tonic-gate WTMPX_ufreq = atol(defp); 2220Sstevel@tonic-gate dprintf(("WTMPX update frequency set to %d\n", 223350Sdp WTMPX_ufreq)); 2240Sstevel@tonic-gate } 2250Sstevel@tonic-gate 2260Sstevel@tonic-gate /* 2270Sstevel@tonic-gate * Paranoia - if polling on large number of FDs is expensive / 2280Sstevel@tonic-gate * buggy the number can be set lower in the field. 2290Sstevel@tonic-gate */ 2300Sstevel@tonic-gate if ((defp = defread("MAX_FDS=")) != NULL) { 2310Sstevel@tonic-gate Max_fds = atol(defp); 2320Sstevel@tonic-gate dprintf(("Max_fds set to %d\n", Max_fds)); 2330Sstevel@tonic-gate } 2340Sstevel@tonic-gate (void) defopen((char *)NULL); 2350Sstevel@tonic-gate } 2360Sstevel@tonic-gate 2370Sstevel@tonic-gate if (Debug == 0) { 2380Sstevel@tonic-gate /* 2390Sstevel@tonic-gate * Daemonize ourselves 2400Sstevel@tonic-gate */ 2410Sstevel@tonic-gate if (fork()) { 2420Sstevel@tonic-gate exit(0); 2430Sstevel@tonic-gate } 2440Sstevel@tonic-gate (void) close(0); 2450Sstevel@tonic-gate (void) close(1); 2460Sstevel@tonic-gate (void) close(2); 2470Sstevel@tonic-gate /* 2480Sstevel@tonic-gate * We open these to avoid accidentally writing to a proc file 2490Sstevel@tonic-gate */ 2500Sstevel@tonic-gate (void) open("/dev/null", O_RDONLY); 2510Sstevel@tonic-gate (void) open("/dev/null", O_WRONLY); 2520Sstevel@tonic-gate (void) open("/dev/null", O_WRONLY); 2530Sstevel@tonic-gate (void) setsid(); /* release process from tty */ 2540Sstevel@tonic-gate } 2550Sstevel@tonic-gate 2560Sstevel@tonic-gate openlog(prog_name, LOG_PID, LOG_DAEMON); /* For error messages */ 2570Sstevel@tonic-gate warn_utmp(); /* check to see if utmp came back by accident */ 2580Sstevel@tonic-gate 2590Sstevel@tonic-gate /* 2600Sstevel@tonic-gate * Allocate the pidtable and fdtable. An earlier version did 2610Sstevel@tonic-gate * this as we go, but this is simpler. 2620Sstevel@tonic-gate */ 2630Sstevel@tonic-gate if ((pidtable = malloc(Max_fds * sizeof (struct pidentry))) == NULL) 2640Sstevel@tonic-gate fatal("Malloc failed"); 2650Sstevel@tonic-gate if ((fdtable = malloc(Max_fds * sizeof (pollfd_t))) == NULL) 2660Sstevel@tonic-gate fatal("Malloc failed"); 2670Sstevel@tonic-gate 2680Sstevel@tonic-gate /* 2690Sstevel@tonic-gate * Up the limit on FDs 2700Sstevel@tonic-gate */ 2710Sstevel@tonic-gate if (getrlimit(RLIMIT_NOFILE, &rlim) == 0) { 2720Sstevel@tonic-gate rlim.rlim_cur = Max_fds + EXTRA_MARGIN + 1; 2730Sstevel@tonic-gate rlim.rlim_max = Max_fds + EXTRA_MARGIN + 1; 2740Sstevel@tonic-gate if (setrlimit(RLIMIT_NOFILE, &rlim) != 0) { 2750Sstevel@tonic-gate fatal("Out of File Descriptors"); 2760Sstevel@tonic-gate } 2770Sstevel@tonic-gate } else 2780Sstevel@tonic-gate fatal("getrlimit returned failure"); 2790Sstevel@tonic-gate 280*1914Scasper (void) enable_extended_FILE_stdio(-1, -1); 281*1914Scasper 2820Sstevel@tonic-gate if ((WTMPXfd = open(WTMPX_FILE, O_RDONLY)) < 0) 2830Sstevel@tonic-gate nonfatal("WARNING: unable to open " WTMPX_FILE "for update."); 2840Sstevel@tonic-gate 2850Sstevel@tonic-gate /* 2860Sstevel@tonic-gate * Loop here scanning the utmpx file and waiting for processes 2870Sstevel@tonic-gate * to terminate. Most of the activity is directed out of wait_for_pids. 2880Sstevel@tonic-gate * If wait_for_pids fails we reload the table and try again. 2890Sstevel@tonic-gate */ 2900Sstevel@tonic-gate 2910Sstevel@tonic-gate curtime = time(NULL); 2920Sstevel@tonic-gate dprintf(("utmp warning timer set to %d seconds\n", WARN_TIME)); 2930Sstevel@tonic-gate 2940Sstevel@tonic-gate for (i = 0; i < MAX_RESETS; i++) { 2950Sstevel@tonic-gate load_tables(); 2960Sstevel@tonic-gate while (wait_for_pids() == 1) { 2970Sstevel@tonic-gate now = time(NULL); 2980Sstevel@tonic-gate if ((now - curtime) >= WARN_TIME) { 2990Sstevel@tonic-gate dprintf(("utmp warning timer expired\n")); 3000Sstevel@tonic-gate warn_utmp(); 3010Sstevel@tonic-gate curtime = now; 3020Sstevel@tonic-gate } 3030Sstevel@tonic-gate } 3040Sstevel@tonic-gate } 3050Sstevel@tonic-gate 3060Sstevel@tonic-gate (void) close(WTMPXfd); 3070Sstevel@tonic-gate 3080Sstevel@tonic-gate /* 3090Sstevel@tonic-gate * We only get here if we had a bunch of resets - so give up 3100Sstevel@tonic-gate */ 3110Sstevel@tonic-gate fatal("Too many resets, giving up"); 312350Sdp return (1); 3130Sstevel@tonic-gate } 3140Sstevel@tonic-gate 3150Sstevel@tonic-gate /* 3160Sstevel@tonic-gate * load_tables() - Designed to be called repeatedly if we need to 3170Sstevel@tonic-gate * restart things. Zeros the pidcount, and loads 3180Sstevel@tonic-gate * the tables by scanning utmpx 3190Sstevel@tonic-gate */ 3200Sstevel@tonic-gate 3210Sstevel@tonic-gate static void 3220Sstevel@tonic-gate load_tables() 3230Sstevel@tonic-gate { 3240Sstevel@tonic-gate int i; 3250Sstevel@tonic-gate 3260Sstevel@tonic-gate dprintf(("Load tables\n")); 3270Sstevel@tonic-gate 3280Sstevel@tonic-gate /* 3290Sstevel@tonic-gate * Close any open files. 3300Sstevel@tonic-gate */ 3310Sstevel@tonic-gate for (i = 0; i < pidcnt; i++) 3320Sstevel@tonic-gate (void) close(fdtable[i].fd); 3330Sstevel@tonic-gate 3340Sstevel@tonic-gate pidcnt = 0; 3350Sstevel@tonic-gate Pfd = -1; 3360Sstevel@tonic-gate setup_pipe(); /* Setup the pipe to receive messages */ 3370Sstevel@tonic-gate scan_utmps(); /* Read in USER procs entries to watch */ 3380Sstevel@tonic-gate } 3390Sstevel@tonic-gate 3400Sstevel@tonic-gate 3410Sstevel@tonic-gate /* 3420Sstevel@tonic-gate * *** The Watcher *** 3430Sstevel@tonic-gate * 3440Sstevel@tonic-gate * Wait_for_pids - wait for the termination of a process in the table. 3450Sstevel@tonic-gate * Returns 1 on normal exist, 0 on failure. 3460Sstevel@tonic-gate */ 3470Sstevel@tonic-gate 3480Sstevel@tonic-gate static int 3490Sstevel@tonic-gate wait_for_pids() 3500Sstevel@tonic-gate { 3510Sstevel@tonic-gate register struct pollfd *pfd; 3520Sstevel@tonic-gate register int i; 3530Sstevel@tonic-gate pid_t pid; 3540Sstevel@tonic-gate int ret_val; 3550Sstevel@tonic-gate int timeout; 3560Sstevel@tonic-gate static time_t last_timeout = 0; 3570Sstevel@tonic-gate static int bad_error = 0; /* Count of POLL errors */ 3580Sstevel@tonic-gate 3590Sstevel@tonic-gate /* 3600Sstevel@tonic-gate * First time through we initialize last_timeout to now. 3610Sstevel@tonic-gate */ 3620Sstevel@tonic-gate if (last_timeout == 0) 3630Sstevel@tonic-gate last_timeout = time(NULL); 3640Sstevel@tonic-gate 3650Sstevel@tonic-gate /* 3660Sstevel@tonic-gate * Recalculate timeout - checking to see if time expired. 3670Sstevel@tonic-gate */ 3680Sstevel@tonic-gate 3690Sstevel@tonic-gate if ((timeout = Poll_timeout - (time(NULL) - last_timeout)) <= 0) { 3700Sstevel@tonic-gate timeout = Poll_timeout; 3710Sstevel@tonic-gate last_timeout = time(NULL); 3720Sstevel@tonic-gate scan_utmps(); 3730Sstevel@tonic-gate } 3740Sstevel@tonic-gate 3750Sstevel@tonic-gate fdtable[0].events = POLLRDNORM; 3760Sstevel@tonic-gate 3770Sstevel@tonic-gate for (i = 0; i < (timeout / WTMPX_ufreq); i++) { 3780Sstevel@tonic-gate 3790Sstevel@tonic-gate /* 3800Sstevel@tonic-gate * Loop here while getting EAGAIN 3810Sstevel@tonic-gate */ 3820Sstevel@tonic-gate 3830Sstevel@tonic-gate while ((ret_val = poll(fdtable, pidcnt, WTMPX_ufreq*1000)) < 0) 3840Sstevel@tonic-gate if (errno == EAGAIN) 3850Sstevel@tonic-gate (void) sleep(2); 3860Sstevel@tonic-gate else 3870Sstevel@tonic-gate fatal("poll"); 3880Sstevel@tonic-gate /* 3890Sstevel@tonic-gate * The results of pread(2) are discarded; we only want 3900Sstevel@tonic-gate * to update the access time of WTMPX_FILE. 3910Sstevel@tonic-gate * Periodically touching WTMPX helps determine when the 3920Sstevel@tonic-gate * OS became unavailable when the OS boots again . 3930Sstevel@tonic-gate * See PSARC 2004/462 for more information. 3940Sstevel@tonic-gate */ 3950Sstevel@tonic-gate 3960Sstevel@tonic-gate (void) pread(WTMPXfd, (void *)&pid, sizeof (pid), 0); 3970Sstevel@tonic-gate 3980Sstevel@tonic-gate if (ret_val) /* file descriptor(s) need attention */ 3990Sstevel@tonic-gate break; 4000Sstevel@tonic-gate } 4010Sstevel@tonic-gate 4020Sstevel@tonic-gate /* 4030Sstevel@tonic-gate * If ret_val == 0 the poll timed out - reset last_time and 4040Sstevel@tonic-gate * call scan_utmps 4050Sstevel@tonic-gate */ 4060Sstevel@tonic-gate if (ret_val == 0) { 4070Sstevel@tonic-gate last_timeout = time(NULL); 4080Sstevel@tonic-gate scan_utmps(); 4090Sstevel@tonic-gate return (1); 4100Sstevel@tonic-gate } 4110Sstevel@tonic-gate 4120Sstevel@tonic-gate /* 4130Sstevel@tonic-gate * Check the pipe file descriptor 4140Sstevel@tonic-gate */ 4150Sstevel@tonic-gate if (fdtable[0].revents & POLLRDNORM) { 4160Sstevel@tonic-gate drain_pipe(); 4170Sstevel@tonic-gate fdtable[0].revents = 0; 4180Sstevel@tonic-gate ret_val--; 4190Sstevel@tonic-gate } 4200Sstevel@tonic-gate 4210Sstevel@tonic-gate (void) sleep(5); /* Give parents time to cleanup children */ 4220Sstevel@tonic-gate 4230Sstevel@tonic-gate /* 4240Sstevel@tonic-gate * We got here because the status of one of the pids that 4250Sstevel@tonic-gate * we are polling on has changed, so search the table looking 4260Sstevel@tonic-gate * for the entry. 4270Sstevel@tonic-gate * 4280Sstevel@tonic-gate * The table is scanned backwards so that entries can be removed 4290Sstevel@tonic-gate * while we go since the table is compacted from high down to low 4300Sstevel@tonic-gate */ 4310Sstevel@tonic-gate for (i = pidcnt - 1; i > 0; i--) { 4320Sstevel@tonic-gate /* 4330Sstevel@tonic-gate * Break out of the loop if we've processed all the entries. 4340Sstevel@tonic-gate */ 4350Sstevel@tonic-gate if (ret_val == 0) 4360Sstevel@tonic-gate break; 4370Sstevel@tonic-gate 4380Sstevel@tonic-gate pfd = &fdtable[i]; 4390Sstevel@tonic-gate 4400Sstevel@tonic-gate if (pfd->fd < 0) { 4410Sstevel@tonic-gate rem_pid((pid_t)0, i, DONT_CLEAN); 4420Sstevel@tonic-gate continue; 4430Sstevel@tonic-gate } 4440Sstevel@tonic-gate /* 4450Sstevel@tonic-gate * POLLHUP - Process terminated 4460Sstevel@tonic-gate */ 4470Sstevel@tonic-gate if (pfd->revents & POLLHUP) { 4480Sstevel@tonic-gate psinfo_t psinfo; 4490Sstevel@tonic-gate 4500Sstevel@tonic-gate if (pread(pfd->fd, &psinfo, sizeof (psinfo), (off_t)0) 4510Sstevel@tonic-gate != sizeof (psinfo)) { 4520Sstevel@tonic-gate dprintf(("! %d: terminated, status 0x%.4x\n", \ 453350Sdp (int)pidtable[i].pl_pid, psinfo.pr_wstat)); 4540Sstevel@tonic-gate pidtable[i].pl_status = psinfo.pr_wstat; 4550Sstevel@tonic-gate 4560Sstevel@tonic-gate } else { 4570Sstevel@tonic-gate dprintf(("! %d: terminated\n", \ 458350Sdp (int)pidtable[i].pl_pid)); 4590Sstevel@tonic-gate pidtable[i].pl_status = 0; 4600Sstevel@tonic-gate } 4610Sstevel@tonic-gate /* 4620Sstevel@tonic-gate * PID gets removed when terminated only 4630Sstevel@tonic-gate */ 4640Sstevel@tonic-gate rem_pid((pid_t)0, i, CLEANIT); 4650Sstevel@tonic-gate ret_val--; 4660Sstevel@tonic-gate continue; 4670Sstevel@tonic-gate } 4680Sstevel@tonic-gate /* 4690Sstevel@tonic-gate * POLLNVAL and POLLERR 4700Sstevel@tonic-gate * These error's shouldn't occurr but until their fixed 4710Sstevel@tonic-gate * we perform some simple error recovery. 4720Sstevel@tonic-gate */ 4730Sstevel@tonic-gate if (pfd->revents & (POLLNVAL|POLLERR)) { 4740Sstevel@tonic-gate dprintf(("Poll Err = %d pid = %d i = %d\n", \ 475350Sdp pfd->revents, (int)pidtable[i].pl_pid, i)); 4760Sstevel@tonic-gate 4770Sstevel@tonic-gate pid = pidtable[i].pl_pid; /* Save pid for below */ 4780Sstevel@tonic-gate /* 4790Sstevel@tonic-gate * If its POLLNVAL we just remove the process for 4800Sstevel@tonic-gate * now, it will get picked up in the next scan. 4810Sstevel@tonic-gate * POLLERR pids get re-added after being deleted. 4820Sstevel@tonic-gate */ 4830Sstevel@tonic-gate if (pfd->revents & POLLNVAL) { 4840Sstevel@tonic-gate rem_pid((pid_t)0, i, DONT_CLEAN); 4850Sstevel@tonic-gate } else { /* Else... POLLERR */ 4860Sstevel@tonic-gate rem_pid((pid_t)0, i, DONT_CLEAN); 4870Sstevel@tonic-gate add_pid(pid); 4880Sstevel@tonic-gate } 4890Sstevel@tonic-gate 4900Sstevel@tonic-gate if (bad_error++ > MAX_POLL_ERRS) { 4910Sstevel@tonic-gate bad_error = 0; 4920Sstevel@tonic-gate return (0); /* 0 Indicates severe error */ 4930Sstevel@tonic-gate } 4940Sstevel@tonic-gate ret_val--; 4950Sstevel@tonic-gate continue; 4960Sstevel@tonic-gate } 4970Sstevel@tonic-gate 4980Sstevel@tonic-gate /* 4990Sstevel@tonic-gate * No more bits should be set in revents but check anyway 5000Sstevel@tonic-gate */ 5010Sstevel@tonic-gate if (pfd->revents != 0) { 5020Sstevel@tonic-gate dprintf(("%d: unknown err %d\n", \ 5030Sstevel@tonic-gate (int)pidtable[i].pl_pid, pfd->revents)); 5040Sstevel@tonic-gate 5050Sstevel@tonic-gate rem_pid((pid_t)0, i, DONT_CLEAN); 5060Sstevel@tonic-gate ret_val--; 5070Sstevel@tonic-gate 5080Sstevel@tonic-gate if (bad_error++ > MAX_POLL_ERRS) { 5090Sstevel@tonic-gate bad_error = 0; 5100Sstevel@tonic-gate return (0); /* 0 Indicates severe error */ 5110Sstevel@tonic-gate } 5120Sstevel@tonic-gate return (1); 5130Sstevel@tonic-gate } 5140Sstevel@tonic-gate } 5150Sstevel@tonic-gate return (1); /* 1 Indicates Everything okay */ 5160Sstevel@tonic-gate } 5170Sstevel@tonic-gate 5180Sstevel@tonic-gate /* 5190Sstevel@tonic-gate * *** The Scanner *** 5200Sstevel@tonic-gate * 5210Sstevel@tonic-gate * scan_utmps() - Scan the utmpx file. 5220Sstevel@tonic-gate * For each USER_PROCESS check 5230Sstevel@tonic-gate * if its alive or dead. If alive and its not in 5240Sstevel@tonic-gate * our table to be watched, put it there. If its 5250Sstevel@tonic-gate * dead, remove it from our table and clean it up. 5260Sstevel@tonic-gate */ 5270Sstevel@tonic-gate 5280Sstevel@tonic-gate static void 5290Sstevel@tonic-gate scan_utmps() 5300Sstevel@tonic-gate { 5310Sstevel@tonic-gate struct utmpx *utmpx; 5320Sstevel@tonic-gate int i; 5330Sstevel@tonic-gate 5340Sstevel@tonic-gate dprintf(("Scan utmps\n")); 5350Sstevel@tonic-gate /* 5360Sstevel@tonic-gate * Scan utmpx. 5370Sstevel@tonic-gate */ 5380Sstevel@tonic-gate setutxent(); 5390Sstevel@tonic-gate while ((utmpx = getutxent()) != NULL) { 5400Sstevel@tonic-gate if (utmpx->ut_type == USER_PROCESS) { 5410Sstevel@tonic-gate /* 5420Sstevel@tonic-gate * Is the process alive? 5430Sstevel@tonic-gate */ 5440Sstevel@tonic-gate if (proc_is_alive(utmpx->ut_pid)) { 5450Sstevel@tonic-gate /* 5460Sstevel@tonic-gate * Yes, the process is alive, so add it if we 5470Sstevel@tonic-gate * don't have it in our table. 5480Sstevel@tonic-gate */ 5490Sstevel@tonic-gate if (find_pid(utmpx->ut_pid, &i) == 0) 5500Sstevel@tonic-gate add_pid(utmpx->ut_pid); /* No, add it */ 5510Sstevel@tonic-gate } else { 5520Sstevel@tonic-gate /* 5530Sstevel@tonic-gate * No, the process is dead, so remove it if its 5540Sstevel@tonic-gate * in our table, otherwise just clean it. 5550Sstevel@tonic-gate */ 5560Sstevel@tonic-gate if (find_pid(utmpx->ut_pid, &i) == 1) 5570Sstevel@tonic-gate rem_pid(utmpx->ut_pid, i, CLEANIT); 5580Sstevel@tonic-gate else 5590Sstevel@tonic-gate clean_utmpx_ent(utmpx); 5600Sstevel@tonic-gate } 5610Sstevel@tonic-gate } 5620Sstevel@tonic-gate } 5630Sstevel@tonic-gate /* 5640Sstevel@tonic-gate * Close it to flush the buffer. 5650Sstevel@tonic-gate */ 5660Sstevel@tonic-gate endutxent(); 5670Sstevel@tonic-gate } 5680Sstevel@tonic-gate 5690Sstevel@tonic-gate 5700Sstevel@tonic-gate /* 5710Sstevel@tonic-gate * *** Receiver Routines *** 5720Sstevel@tonic-gate */ 5730Sstevel@tonic-gate 5740Sstevel@tonic-gate /* 5750Sstevel@tonic-gate * setup_pipe - Set up the pipe to read pids over 5760Sstevel@tonic-gate */ 5770Sstevel@tonic-gate 5780Sstevel@tonic-gate static void 5790Sstevel@tonic-gate setup_pipe() 5800Sstevel@tonic-gate { 5810Sstevel@tonic-gate 5820Sstevel@tonic-gate struct statvfs statvfs_buf; 5830Sstevel@tonic-gate /* 5840Sstevel@tonic-gate * This code & comments swiped from init and left stock since it works 5850Sstevel@tonic-gate */ 5860Sstevel@tonic-gate 5870Sstevel@tonic-gate if (Pfd < 0) { 5880Sstevel@tonic-gate if ((statvfs(UTMPPIPE_DIR, &statvfs_buf) == 0) && 5890Sstevel@tonic-gate ((statvfs_buf.f_flag & ST_RDONLY) == 0)) { 5900Sstevel@tonic-gate (void) unlink(UTMPPIPE); 5910Sstevel@tonic-gate (void) mknod(UTMPPIPE, S_IFIFO | 0600, 0); 5920Sstevel@tonic-gate } 5930Sstevel@tonic-gate Pfd = open(UTMPPIPE, O_RDWR | O_NDELAY); 5940Sstevel@tonic-gate } 5950Sstevel@tonic-gate if (Pfd < 0) 5960Sstevel@tonic-gate nonfatal(UTMPPIPE); 5970Sstevel@tonic-gate /* 5980Sstevel@tonic-gate * This code from init modified to be poll based instead of SIGPOLL, 5990Sstevel@tonic-gate * signal based. 6000Sstevel@tonic-gate */ 6010Sstevel@tonic-gate 6020Sstevel@tonic-gate if (Pfd >= 0) { 6030Sstevel@tonic-gate /* 6040Sstevel@tonic-gate * Read pipe in message discard mode. When read reads a 6050Sstevel@tonic-gate * pidrec size record, the remainder of the message will 6060Sstevel@tonic-gate * be discarded. Though there shouldn't be any it will 6070Sstevel@tonic-gate * help resynch if someone else wrote some garbage. 6080Sstevel@tonic-gate */ 6090Sstevel@tonic-gate (void) ioctl(Pfd, I_SRDOPT, RMSGD); 6100Sstevel@tonic-gate } 6110Sstevel@tonic-gate 6120Sstevel@tonic-gate /* 6130Sstevel@tonic-gate * My code. We use slot 0 in the table to hold the fd of the pipe 6140Sstevel@tonic-gate */ 6150Sstevel@tonic-gate add_pid(0); /* Proc 0 guaranteed to get slot 0 */ 6160Sstevel@tonic-gate fdtable[0].fd = Pfd; /* Pfd could be -1, should be okay */ 6170Sstevel@tonic-gate fdtable[0].events = POLLRDNORM; 6180Sstevel@tonic-gate } 6190Sstevel@tonic-gate 6200Sstevel@tonic-gate /* 6210Sstevel@tonic-gate * drain_pipe() - The receiver routine that reads the pipe 6220Sstevel@tonic-gate */ 6230Sstevel@tonic-gate 6240Sstevel@tonic-gate static void 6250Sstevel@tonic-gate drain_pipe() 6260Sstevel@tonic-gate { 6270Sstevel@tonic-gate struct pidrec prec; 6280Sstevel@tonic-gate register struct pidrec *p = ≺ 6290Sstevel@tonic-gate int bytes_read; 6300Sstevel@tonic-gate int i; 6310Sstevel@tonic-gate 6320Sstevel@tonic-gate for (;;) { 6330Sstevel@tonic-gate /* 6340Sstevel@tonic-gate * Important Note: Either read will really fail (in which case 6350Sstevel@tonic-gate * return is all we can do) or will get EAGAIN (Pfd was opened 6360Sstevel@tonic-gate * O_NDELAY), in which case we also want to return. 6370Sstevel@tonic-gate */ 6380Sstevel@tonic-gate 6390Sstevel@tonic-gate if ((bytes_read = read(Pfd, p, sizeof (struct pidrec))) != 6400Sstevel@tonic-gate sizeof (struct pidrec)) { 6410Sstevel@tonic-gate /* 6420Sstevel@tonic-gate * Something went wrong reading, so read until pipe 6430Sstevel@tonic-gate * is empty 6440Sstevel@tonic-gate */ 6450Sstevel@tonic-gate if (bytes_read > 0) 6460Sstevel@tonic-gate while (read(Pfd, p, sizeof (struct pidrec)) > 0) 6470Sstevel@tonic-gate ; 6480Sstevel@tonic-gate return; 6490Sstevel@tonic-gate } 6500Sstevel@tonic-gate 6510Sstevel@tonic-gate dprintf(("drain_pipe: Recd command %d, pid %d\n", 652350Sdp p->pd_type, (int)p->pd_pid)); 6530Sstevel@tonic-gate switch (p->pd_type) { 6540Sstevel@tonic-gate case ADDPID: 6550Sstevel@tonic-gate /* 6560Sstevel@tonic-gate * Check if we already have the process, adding it 6570Sstevel@tonic-gate * if we don't. 6580Sstevel@tonic-gate */ 6590Sstevel@tonic-gate if (find_pid(p->pd_pid, &i) == 0) 6600Sstevel@tonic-gate add_pid(p->pd_pid); 6610Sstevel@tonic-gate break; 6620Sstevel@tonic-gate 6630Sstevel@tonic-gate case REMPID: 6640Sstevel@tonic-gate rem_pid(p->pd_pid, -1, DONT_CLEAN); 6650Sstevel@tonic-gate break; 6660Sstevel@tonic-gate default: 6670Sstevel@tonic-gate nonfatal("Bad message on utmppipe\n"); 6680Sstevel@tonic-gate break; 6690Sstevel@tonic-gate } 6700Sstevel@tonic-gate } 6710Sstevel@tonic-gate } 6720Sstevel@tonic-gate 6730Sstevel@tonic-gate 6740Sstevel@tonic-gate /* 6750Sstevel@tonic-gate * *** Utilities for add and removing entries in the tables *** 6760Sstevel@tonic-gate */ 6770Sstevel@tonic-gate 6780Sstevel@tonic-gate /* 6790Sstevel@tonic-gate * add_pid - add a pid to the fd table and the pidtable. 6800Sstevel@tonic-gate * these tables are sorted tables for quick lookups. 6810Sstevel@tonic-gate * 6820Sstevel@tonic-gate */ 6830Sstevel@tonic-gate static void 6840Sstevel@tonic-gate add_pid(pid) 6850Sstevel@tonic-gate pid_t pid; 6860Sstevel@tonic-gate { 6870Sstevel@tonic-gate int fd = 0; 6880Sstevel@tonic-gate int i = 0, move_amt; 6890Sstevel@tonic-gate int j; 6900Sstevel@tonic-gate static int first_time = 1; 6910Sstevel@tonic-gate 6920Sstevel@tonic-gate /* 6930Sstevel@tonic-gate * Check to see if the pid is already in our table, or being passed 6940Sstevel@tonic-gate * pid zero. 6950Sstevel@tonic-gate */ 6960Sstevel@tonic-gate if (pidcnt != 0 && (find_pid(pid, &j) == 1 || pid == 0)) 6970Sstevel@tonic-gate return; 6980Sstevel@tonic-gate 6990Sstevel@tonic-gate if (pidcnt >= Max_fds) { 7000Sstevel@tonic-gate if (first_time == 1) { 7010Sstevel@tonic-gate /* 7020Sstevel@tonic-gate * Print this error only once 7030Sstevel@tonic-gate */ 7040Sstevel@tonic-gate nonfatal("File Descriptor limit exceeded"); 7050Sstevel@tonic-gate first_time = 0; 7060Sstevel@tonic-gate } 7070Sstevel@tonic-gate return; 7080Sstevel@tonic-gate } 7090Sstevel@tonic-gate /* 7100Sstevel@tonic-gate * Open the /proc file checking if there's still a valid proc file. 7110Sstevel@tonic-gate */ 7120Sstevel@tonic-gate if (pid != 0 && (fd = proc_to_fd(pid)) == -1) { 7130Sstevel@tonic-gate /* 7140Sstevel@tonic-gate * No so the process died before we got to watch for him 7150Sstevel@tonic-gate */ 7160Sstevel@tonic-gate return; 7170Sstevel@tonic-gate } 7180Sstevel@tonic-gate 7190Sstevel@tonic-gate /* 7200Sstevel@tonic-gate * We only do this code if we're not putting in the first element 7210Sstevel@tonic-gate * Which we know will be for proc zero which is used by setup_pipe 7220Sstevel@tonic-gate * for its pipe fd. 7230Sstevel@tonic-gate */ 7240Sstevel@tonic-gate if (pidcnt != 0) { 7250Sstevel@tonic-gate for (i = 0; i < pidcnt; i++) { 7260Sstevel@tonic-gate if (pid <= pidtable[i].pl_pid) 7270Sstevel@tonic-gate break; 7280Sstevel@tonic-gate } 7290Sstevel@tonic-gate 7300Sstevel@tonic-gate /* 7310Sstevel@tonic-gate * Handle the case where we're not sticking our entry on the 7320Sstevel@tonic-gate * the end, or overwriting an existing entry. 7330Sstevel@tonic-gate */ 7340Sstevel@tonic-gate if (i != pidcnt && pid != pidtable[i].pl_pid) { 7350Sstevel@tonic-gate 7360Sstevel@tonic-gate move_amt = pidcnt - i; 7370Sstevel@tonic-gate /* 7380Sstevel@tonic-gate * Move table down 7390Sstevel@tonic-gate */ 7400Sstevel@tonic-gate if (move_amt != 0) { 7410Sstevel@tonic-gate (void) memmove(&pidtable[i+1], &pidtable[i], 7420Sstevel@tonic-gate move_amt * sizeof (struct pidentry)); 7430Sstevel@tonic-gate (void) memmove(&fdtable[i+1], &fdtable[i], 7440Sstevel@tonic-gate move_amt * sizeof (pollfd_t)); 7450Sstevel@tonic-gate } 7460Sstevel@tonic-gate } 7470Sstevel@tonic-gate } 7480Sstevel@tonic-gate 7490Sstevel@tonic-gate /* 7500Sstevel@tonic-gate * Fill in the events field for poll and copy the entry into the array 7510Sstevel@tonic-gate */ 7520Sstevel@tonic-gate fdtable[i].events = 0; 7530Sstevel@tonic-gate fdtable[i].revents = 0; 7540Sstevel@tonic-gate fdtable[i].fd = fd; 7550Sstevel@tonic-gate 7560Sstevel@tonic-gate /* 7570Sstevel@tonic-gate * Likewise, setup pid field and pointer (index) to the fdtable entry 7580Sstevel@tonic-gate */ 7590Sstevel@tonic-gate pidtable[i].pl_pid = pid; 7600Sstevel@tonic-gate 7610Sstevel@tonic-gate pidcnt++; /* Bump the pid count */ 7620Sstevel@tonic-gate dprintf((" add_pid: pid = %d fd = %d index = %d pidcnt = %d\n", 7630Sstevel@tonic-gate (int)pid, fd, i, pidcnt)); 7640Sstevel@tonic-gate } 7650Sstevel@tonic-gate 7660Sstevel@tonic-gate 7670Sstevel@tonic-gate /* 7680Sstevel@tonic-gate * rem_pid - Remove an entry from the table and check to see if its 7690Sstevel@tonic-gate * not in the utmpx file. 7700Sstevel@tonic-gate * If i != -1 don't look up the pid, use i as index 7710Sstevel@tonic-gate */ 7720Sstevel@tonic-gate 7730Sstevel@tonic-gate static void 7740Sstevel@tonic-gate rem_pid(pid, i, clean_it) 7750Sstevel@tonic-gate pid_t pid; /* Pid of process to clean or 0 if we don't know it */ 7760Sstevel@tonic-gate int i; /* Index into table or -1 if we need to look it up */ 7770Sstevel@tonic-gate int clean_it; /* Clean the entry, or just remove from table? */ 7780Sstevel@tonic-gate { 7790Sstevel@tonic-gate int move_amt; 7800Sstevel@tonic-gate 7810Sstevel@tonic-gate dprintf((" rem_pid: pid = %d i = %d", (int)pid, i)); 7820Sstevel@tonic-gate 7830Sstevel@tonic-gate /* 7840Sstevel@tonic-gate * Don't allow slot 0 in the table to be removed - utmppipe fd 7850Sstevel@tonic-gate */ 7860Sstevel@tonic-gate if ((i == -1 && pid == 0) || (i == 0)) { 7870Sstevel@tonic-gate dprintf((" - attempted to remove proc 0\n")); 7880Sstevel@tonic-gate return; 7890Sstevel@tonic-gate } 7900Sstevel@tonic-gate 7910Sstevel@tonic-gate if (i != -1 || find_pid(pid, &i) == 1) { /* Found the entry */ 7920Sstevel@tonic-gate (void) close(fdtable[i].fd); /* We're done with the fd */ 7930Sstevel@tonic-gate 7940Sstevel@tonic-gate dprintf((" fd = %d\n", fdtable[i].fd)); 7950Sstevel@tonic-gate 7960Sstevel@tonic-gate if (clean_it == CLEANIT) 7970Sstevel@tonic-gate clean_entry(i); 7980Sstevel@tonic-gate 7990Sstevel@tonic-gate move_amt = (pidcnt - i) - 1; 8000Sstevel@tonic-gate /* 8010Sstevel@tonic-gate * Remove entries from the tables. 8020Sstevel@tonic-gate */ 8030Sstevel@tonic-gate (void) memmove(&pidtable[i], &pidtable[i+1], 8040Sstevel@tonic-gate move_amt * sizeof (struct pidentry)); 8050Sstevel@tonic-gate 8060Sstevel@tonic-gate (void) memmove(&fdtable[i], &fdtable[i+1], 8070Sstevel@tonic-gate move_amt * sizeof (pollfd_t)); 8080Sstevel@tonic-gate 8090Sstevel@tonic-gate /* 8100Sstevel@tonic-gate * decrement the pid count - one less pid to worry about 8110Sstevel@tonic-gate */ 8120Sstevel@tonic-gate pidcnt--; 8130Sstevel@tonic-gate } 8140Sstevel@tonic-gate if (i == -1) 8150Sstevel@tonic-gate dprintf((" - entry not found \n")); 8160Sstevel@tonic-gate } 8170Sstevel@tonic-gate 8180Sstevel@tonic-gate 8190Sstevel@tonic-gate /* 8200Sstevel@tonic-gate * find_pid - Returns an index into the pidtable of the specifed pid, 8210Sstevel@tonic-gate * else -1 if not found 8220Sstevel@tonic-gate */ 8230Sstevel@tonic-gate 8240Sstevel@tonic-gate static int 8250Sstevel@tonic-gate find_pid(pid, i) 8260Sstevel@tonic-gate pid_t pid; 8270Sstevel@tonic-gate int *i; 8280Sstevel@tonic-gate { 8290Sstevel@tonic-gate struct pidentry pe; 8300Sstevel@tonic-gate struct pidentry *p; 8310Sstevel@tonic-gate 8320Sstevel@tonic-gate pe.pl_pid = pid; 8330Sstevel@tonic-gate p = bsearch(&pe, pidtable, pidcnt, sizeof (struct pidentry), pidcmp); 8340Sstevel@tonic-gate 8350Sstevel@tonic-gate if (p == NULL) 8360Sstevel@tonic-gate return (0); 8370Sstevel@tonic-gate else { 8380Sstevel@tonic-gate *i = p - (struct pidentry *)pidtable; 8390Sstevel@tonic-gate return (1); 8400Sstevel@tonic-gate } 8410Sstevel@tonic-gate } 8420Sstevel@tonic-gate 8430Sstevel@tonic-gate 8440Sstevel@tonic-gate /* 8450Sstevel@tonic-gate * Pidcmp - Used by besearch for sorting and finding process IDs. 8460Sstevel@tonic-gate */ 8470Sstevel@tonic-gate 8480Sstevel@tonic-gate static int 8490Sstevel@tonic-gate pidcmp(a, b) 8500Sstevel@tonic-gate struct pidentry *a, *b; 8510Sstevel@tonic-gate { 8520Sstevel@tonic-gate if (b == NULL || a == NULL) 8530Sstevel@tonic-gate return (0); 8540Sstevel@tonic-gate return (a->pl_pid - b->pl_pid); 8550Sstevel@tonic-gate } 8560Sstevel@tonic-gate 8570Sstevel@tonic-gate 8580Sstevel@tonic-gate /* 8590Sstevel@tonic-gate * proc_to_fd - Take a process ID and return an open file descriptor to the 8600Sstevel@tonic-gate * /proc file for the specified process. 8610Sstevel@tonic-gate */ 8620Sstevel@tonic-gate static int 8630Sstevel@tonic-gate proc_to_fd(pid) 8640Sstevel@tonic-gate pid_t pid; 8650Sstevel@tonic-gate { 8660Sstevel@tonic-gate char procname[64]; 8670Sstevel@tonic-gate int fd, dfd; 8680Sstevel@tonic-gate 8690Sstevel@tonic-gate (void) sprintf(procname, "/proc/%d/psinfo", (int)pid); 8700Sstevel@tonic-gate 8710Sstevel@tonic-gate if ((fd = open(procname, O_RDONLY)) >= 0) { 8720Sstevel@tonic-gate /* 8730Sstevel@tonic-gate * dup the fd above the low order values to assure 8740Sstevel@tonic-gate * stdio works for other fds - paranoia. 8750Sstevel@tonic-gate */ 8760Sstevel@tonic-gate if (fd < EXTRA_MARGIN) { 8770Sstevel@tonic-gate dfd = fcntl(fd, F_DUPFD, EXTRA_MARGIN); 8780Sstevel@tonic-gate if (dfd > 0) { 8790Sstevel@tonic-gate (void) close(fd); 8800Sstevel@tonic-gate fd = dfd; 8810Sstevel@tonic-gate } 8820Sstevel@tonic-gate } 8830Sstevel@tonic-gate /* 8840Sstevel@tonic-gate * More paranoia - set the close on exec flag 8850Sstevel@tonic-gate */ 8860Sstevel@tonic-gate (void) fcntl(fd, F_SETFD, 1); 8870Sstevel@tonic-gate return (fd); 8880Sstevel@tonic-gate } 8890Sstevel@tonic-gate if (errno == ENOENT) 8900Sstevel@tonic-gate return (-1); 8910Sstevel@tonic-gate 8920Sstevel@tonic-gate if (errno == EMFILE) { 8930Sstevel@tonic-gate /* 8940Sstevel@tonic-gate * This is fatal, since libc won't be able to allocate 8950Sstevel@tonic-gate * any fds for the pututxline() routines 8960Sstevel@tonic-gate */ 8970Sstevel@tonic-gate fatal("Out of file descriptors"); 8980Sstevel@tonic-gate } 8990Sstevel@tonic-gate fatal(procname); /* Only get here on error */ 9000Sstevel@tonic-gate return (-1); 9010Sstevel@tonic-gate } 9020Sstevel@tonic-gate 9030Sstevel@tonic-gate 9040Sstevel@tonic-gate /* 9050Sstevel@tonic-gate * *** Utmpx Cleaning Utilities *** 9060Sstevel@tonic-gate */ 9070Sstevel@tonic-gate 9080Sstevel@tonic-gate /* 9090Sstevel@tonic-gate * Clean_entry - Cleans the specified entry - where i is an index 9100Sstevel@tonic-gate * into the pid_table. 9110Sstevel@tonic-gate */ 9120Sstevel@tonic-gate static void 9130Sstevel@tonic-gate clean_entry(i) 9140Sstevel@tonic-gate int i; 9150Sstevel@tonic-gate { 9160Sstevel@tonic-gate struct utmpx *u; 9170Sstevel@tonic-gate 9180Sstevel@tonic-gate if (pidcnt == 0) 9190Sstevel@tonic-gate return; 9200Sstevel@tonic-gate 9210Sstevel@tonic-gate dprintf((" Cleaning %d\n", (int)pidtable[i].pl_pid)); 9220Sstevel@tonic-gate 9230Sstevel@tonic-gate /* 9240Sstevel@tonic-gate * Double check if the process is dead. 9250Sstevel@tonic-gate */ 9260Sstevel@tonic-gate if (proc_is_alive(pidtable[i].pl_pid)) { 9270Sstevel@tonic-gate dprintf((" Bad attempt to clean %d\n", \ 9280Sstevel@tonic-gate (int)pidtable[i].pl_pid)); 9290Sstevel@tonic-gate return; 9300Sstevel@tonic-gate } 9310Sstevel@tonic-gate 9320Sstevel@tonic-gate /* 9330Sstevel@tonic-gate * Find the entry that corresponds to this pid. 9340Sstevel@tonic-gate * Do nothing if entry not found in utmpx file. 9350Sstevel@tonic-gate */ 9360Sstevel@tonic-gate setutxent(); 9370Sstevel@tonic-gate while ((u = getutxent()) != NULL) { 9380Sstevel@tonic-gate if (u->ut_pid == pidtable[i].pl_pid) { 9390Sstevel@tonic-gate if (u->ut_type == USER_PROCESS) { 9400Sstevel@tonic-gate clean_utmpx_ent(u); 9410Sstevel@tonic-gate } 9420Sstevel@tonic-gate } 9430Sstevel@tonic-gate } 9440Sstevel@tonic-gate endutxent(); 9450Sstevel@tonic-gate } 9460Sstevel@tonic-gate 9470Sstevel@tonic-gate 9480Sstevel@tonic-gate /* 9490Sstevel@tonic-gate * clean_utmpx_ent - Clean a utmpx entry 9500Sstevel@tonic-gate */ 9510Sstevel@tonic-gate 9520Sstevel@tonic-gate static void 9530Sstevel@tonic-gate clean_utmpx_ent(u) 9540Sstevel@tonic-gate struct utmpx *u; 9550Sstevel@tonic-gate { 9560Sstevel@tonic-gate dprintf((" clean_utmpx_ent: %d\n", (int)u->ut_pid)); 9570Sstevel@tonic-gate u->ut_type = DEAD_PROCESS; 9580Sstevel@tonic-gate (void) time(&u->ut_xtime); 9590Sstevel@tonic-gate (void) pututxline(u); 9600Sstevel@tonic-gate updwtmpx(WTMPX_FILE, u); 9610Sstevel@tonic-gate /* 9620Sstevel@tonic-gate * XXX update wtmp for ! nonuser entries? 9630Sstevel@tonic-gate */ 9640Sstevel@tonic-gate } 9650Sstevel@tonic-gate 9660Sstevel@tonic-gate /* 9670Sstevel@tonic-gate * *** Error Handling and Debugging Routines *** 9680Sstevel@tonic-gate */ 9690Sstevel@tonic-gate 9700Sstevel@tonic-gate /* 9710Sstevel@tonic-gate * fatal - Catastrophic failure 9720Sstevel@tonic-gate */ 9730Sstevel@tonic-gate 9740Sstevel@tonic-gate static void 9750Sstevel@tonic-gate fatal(char *str) 9760Sstevel@tonic-gate { 9770Sstevel@tonic-gate int oerrno = errno; 9780Sstevel@tonic-gate 979350Sdp syslog(LOG_ALERT, "%s", str); 9800Sstevel@tonic-gate if (Debug == 1) { 9810Sstevel@tonic-gate if ((errno = oerrno) != 0) 9820Sstevel@tonic-gate perror(prog_name); 9830Sstevel@tonic-gate dprintf(("%s\n", str)); 9840Sstevel@tonic-gate } 985350Sdp exit(1); 9860Sstevel@tonic-gate } 9870Sstevel@tonic-gate 9880Sstevel@tonic-gate /* 9890Sstevel@tonic-gate * nonfatal - Non-Catastrophic failure - print message and errno 9900Sstevel@tonic-gate */ 9910Sstevel@tonic-gate 9920Sstevel@tonic-gate static void 9930Sstevel@tonic-gate nonfatal(char *str) 9940Sstevel@tonic-gate { 995350Sdp syslog(LOG_WARNING, "%s", str); 9960Sstevel@tonic-gate 9970Sstevel@tonic-gate if (Debug == 1) { 9980Sstevel@tonic-gate if (errno != 0) 9990Sstevel@tonic-gate perror(prog_name); 10000Sstevel@tonic-gate dprintf(("%c%s\n", 7, str)); 10010Sstevel@tonic-gate print_tables(); 10020Sstevel@tonic-gate (void) sleep(5); /* Time to read debug messages */ 10030Sstevel@tonic-gate } 10040Sstevel@tonic-gate } 10050Sstevel@tonic-gate 10060Sstevel@tonic-gate /* 10070Sstevel@tonic-gate * print_tables - Print internal tables - for debugging 10080Sstevel@tonic-gate */ 10090Sstevel@tonic-gate 10100Sstevel@tonic-gate static void 10110Sstevel@tonic-gate print_tables() 10120Sstevel@tonic-gate { 10130Sstevel@tonic-gate int i; 10140Sstevel@tonic-gate 10150Sstevel@tonic-gate if (Debug == 0) 10160Sstevel@tonic-gate return; 10170Sstevel@tonic-gate 10180Sstevel@tonic-gate dprintf(("pidtable: ")); 10190Sstevel@tonic-gate for (i = 0; i < pidcnt; i++) 10200Sstevel@tonic-gate dprintf(("%d: %d ", i, (int)pidtable[i].pl_pid)); 10210Sstevel@tonic-gate dprintf(("\n")); 10220Sstevel@tonic-gate dprintf(("fdtable: ")); 10230Sstevel@tonic-gate for (i = 0; i < pidcnt; i++) 10240Sstevel@tonic-gate dprintf(("%d: %d ", i, fdtable[i].fd)); 10250Sstevel@tonic-gate dprintf(("\n")); 10260Sstevel@tonic-gate } 10270Sstevel@tonic-gate 10280Sstevel@tonic-gate /* 10290Sstevel@tonic-gate * proc_is_alive - Check to see if a process is alive AND its 10300Sstevel@tonic-gate * not a zombie. Returns 1 if process is alive 10310Sstevel@tonic-gate * and zero if it is dead or a zombie. 10320Sstevel@tonic-gate */ 10330Sstevel@tonic-gate 10340Sstevel@tonic-gate static int 10350Sstevel@tonic-gate proc_is_alive(pid) 10360Sstevel@tonic-gate pid_t pid; 10370Sstevel@tonic-gate { 10380Sstevel@tonic-gate char psinfoname[64]; 10390Sstevel@tonic-gate int fd; 10400Sstevel@tonic-gate psinfo_t psinfo; 10410Sstevel@tonic-gate 10420Sstevel@tonic-gate if (kill(pid, 0) != 0) 10430Sstevel@tonic-gate return (0); /* Kill failed - no process */ 10440Sstevel@tonic-gate 10450Sstevel@tonic-gate /* 10460Sstevel@tonic-gate * The process exists, so check if it's a zombie. 10470Sstevel@tonic-gate */ 10480Sstevel@tonic-gate (void) sprintf(psinfoname, "/proc/%d/psinfo", (int)pid); 10490Sstevel@tonic-gate 10500Sstevel@tonic-gate if ((fd = open(psinfoname, O_RDONLY)) < 0 || 10510Sstevel@tonic-gate read(fd, &psinfo, sizeof (psinfo)) != sizeof (psinfo)) { 10520Sstevel@tonic-gate /* 10530Sstevel@tonic-gate * We either couldn't open the proc, or we did but the 10540Sstevel@tonic-gate * read of the psinfo file failed, so pid is nonexistent. 10550Sstevel@tonic-gate */ 10560Sstevel@tonic-gate psinfo.pr_nlwp = 0; 10570Sstevel@tonic-gate } 10580Sstevel@tonic-gate if (fd >= 0) 10590Sstevel@tonic-gate (void) close(fd); 10600Sstevel@tonic-gate 10610Sstevel@tonic-gate /* if pr_nlwp == 0, process is a zombie */ 10620Sstevel@tonic-gate return (psinfo.pr_nlwp != 0); 10630Sstevel@tonic-gate } 10640Sstevel@tonic-gate 10650Sstevel@tonic-gate /* 10660Sstevel@tonic-gate * warn_utmp - /var/adm/utmp has been deprecated. It should no longer 10670Sstevel@tonic-gate * be used. Applications that try to directly manipulate 10680Sstevel@tonic-gate * it may cause problems. Since the file is no longer 10690Sstevel@tonic-gate * shipped, if it appears on a system it's because an 10700Sstevel@tonic-gate * old application created it. We'll have utmpd 10710Sstevel@tonic-gate * complain about it periodically. 10720Sstevel@tonic-gate */ 10730Sstevel@tonic-gate 10740Sstevel@tonic-gate static void 10750Sstevel@tonic-gate warn_utmp() 10760Sstevel@tonic-gate { 10770Sstevel@tonic-gate struct stat s; 10780Sstevel@tonic-gate 10790Sstevel@tonic-gate if (lstat(UTMP_FILE, &s) == 0 && 10800Sstevel@tonic-gate s.st_size % sizeof (struct utmp) == 0) { 10810Sstevel@tonic-gate nonfatal("WARNING: /var/adm/utmp exists!\nSee " 10820Sstevel@tonic-gate "utmp(4) for more information"); 10830Sstevel@tonic-gate } 10840Sstevel@tonic-gate } 1085