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