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