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
57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate * with the License.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate * and limitations under the License.
137c478bd9Sstevel@tonic-gate *
147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * CDDL HEADER END
217c478bd9Sstevel@tonic-gate */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate #include <stdio.h>
287c478bd9Sstevel@tonic-gate #include <stdlib.h>
297c478bd9Sstevel@tonic-gate #include <unistd.h>
307c478bd9Sstevel@tonic-gate #include <fcntl.h>
317c478bd9Sstevel@tonic-gate #include <ctype.h>
327c478bd9Sstevel@tonic-gate #include <string.h>
337c478bd9Sstevel@tonic-gate #include <signal.h>
347c478bd9Sstevel@tonic-gate #include <errno.h>
357c478bd9Sstevel@tonic-gate #include <sys/types.h>
367c478bd9Sstevel@tonic-gate #include <sys/wait.h>
377c478bd9Sstevel@tonic-gate #include <libproc.h>
387c478bd9Sstevel@tonic-gate
397c478bd9Sstevel@tonic-gate #define NOREAP_TIME 60 /* wait 60 seconds before allow a reap */
407c478bd9Sstevel@tonic-gate
417c478bd9Sstevel@tonic-gate static volatile int interrupt;
427c478bd9Sstevel@tonic-gate static int Fflag;
437c478bd9Sstevel@tonic-gate static char *command;
447c478bd9Sstevel@tonic-gate
457c478bd9Sstevel@tonic-gate static void
intr(int sig)467c478bd9Sstevel@tonic-gate intr(int sig)
477c478bd9Sstevel@tonic-gate {
487c478bd9Sstevel@tonic-gate interrupt = sig;
497c478bd9Sstevel@tonic-gate }
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gate static int
open_usage(pid_t pid,int * perr)527c478bd9Sstevel@tonic-gate open_usage(pid_t pid, int *perr)
537c478bd9Sstevel@tonic-gate {
547c478bd9Sstevel@tonic-gate char path[64];
557c478bd9Sstevel@tonic-gate struct stat64 st;
567c478bd9Sstevel@tonic-gate int fd;
577c478bd9Sstevel@tonic-gate
587c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "/proc/%d/usage", (int)pid);
597c478bd9Sstevel@tonic-gate
607c478bd9Sstevel@tonic-gate /*
617c478bd9Sstevel@tonic-gate * Attempt to open the usage file, and return the fd if we can
627c478bd9Sstevel@tonic-gate * confirm this is a regular file provided by /proc.
637c478bd9Sstevel@tonic-gate */
647c478bd9Sstevel@tonic-gate if ((fd = open64(path, O_RDONLY)) >= 0) {
657c478bd9Sstevel@tonic-gate if (fstat64(fd, &st) != 0 || !S_ISREG(st.st_mode) ||
667c478bd9Sstevel@tonic-gate strcmp(st.st_fstype, "proc") != 0) {
677c478bd9Sstevel@tonic-gate (void) close(fd);
687c478bd9Sstevel@tonic-gate fd = -1;
697c478bd9Sstevel@tonic-gate }
707c478bd9Sstevel@tonic-gate } else if (errno == EACCES || errno == EPERM)
717c478bd9Sstevel@tonic-gate *perr = G_PERM;
727c478bd9Sstevel@tonic-gate
737c478bd9Sstevel@tonic-gate return (fd);
747c478bd9Sstevel@tonic-gate }
757c478bd9Sstevel@tonic-gate
767c478bd9Sstevel@tonic-gate static int
proc_usage(pid_t pid,prusage_t * pup,int * perr)777c478bd9Sstevel@tonic-gate proc_usage(pid_t pid, prusage_t *pup, int *perr)
787c478bd9Sstevel@tonic-gate {
797c478bd9Sstevel@tonic-gate int fd;
807c478bd9Sstevel@tonic-gate
817c478bd9Sstevel@tonic-gate *perr = G_NOPROC;
827c478bd9Sstevel@tonic-gate
837c478bd9Sstevel@tonic-gate if ((fd = open_usage(pid, perr)) != -1) {
847c478bd9Sstevel@tonic-gate if (read(fd, pup, sizeof (prusage_t)) == sizeof (prusage_t)) {
857c478bd9Sstevel@tonic-gate *perr = 0;
867c478bd9Sstevel@tonic-gate (void) close(fd);
877c478bd9Sstevel@tonic-gate return (0);
887c478bd9Sstevel@tonic-gate }
897c478bd9Sstevel@tonic-gate
907c478bd9Sstevel@tonic-gate /*
917c478bd9Sstevel@tonic-gate * If the read failed, the process may have gone away.
927c478bd9Sstevel@tonic-gate */
937c478bd9Sstevel@tonic-gate (void) close(fd);
947c478bd9Sstevel@tonic-gate }
957c478bd9Sstevel@tonic-gate return (-1);
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate /*
997c478bd9Sstevel@tonic-gate * Force the parent process (ppid) to wait for its child process (pid).
1007c478bd9Sstevel@tonic-gate */
1017c478bd9Sstevel@tonic-gate static int
reap(char * arg,pid_t * reap_pid,int * exit_status)1027c478bd9Sstevel@tonic-gate reap(char *arg, pid_t *reap_pid, int *exit_status)
1037c478bd9Sstevel@tonic-gate {
1047c478bd9Sstevel@tonic-gate struct ps_prochandle *Pr;
1057c478bd9Sstevel@tonic-gate siginfo_t siginfo;
1067c478bd9Sstevel@tonic-gate psinfo_t psinfo;
1077c478bd9Sstevel@tonic-gate prusage_t usage;
1087c478bd9Sstevel@tonic-gate pid_t pid, ppid;
1097c478bd9Sstevel@tonic-gate time_t elapsed;
1107c478bd9Sstevel@tonic-gate int gret;
1117c478bd9Sstevel@tonic-gate
1127c478bd9Sstevel@tonic-gate /*
1137c478bd9Sstevel@tonic-gate * get the specified pid and the psinfo struct
1147c478bd9Sstevel@tonic-gate */
1157c478bd9Sstevel@tonic-gate if ((pid = proc_arg_psinfo(arg, PR_ARG_PIDS, &psinfo, &gret)) == -1) {
1167c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: cannot examine %s: %s\n",
1177c478bd9Sstevel@tonic-gate command, arg, Pgrab_error(gret));
1187c478bd9Sstevel@tonic-gate return (1);
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate
1217c478bd9Sstevel@tonic-gate if (psinfo.pr_nlwp != 0) {
1227c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: process not defunct: %d\n",
1237c478bd9Sstevel@tonic-gate command, (int)pid);
1247c478bd9Sstevel@tonic-gate return (1);
1257c478bd9Sstevel@tonic-gate }
1267c478bd9Sstevel@tonic-gate
1277c478bd9Sstevel@tonic-gate *exit_status = psinfo.pr_wstat;
1287c478bd9Sstevel@tonic-gate *reap_pid = psinfo.pr_pid;
1297c478bd9Sstevel@tonic-gate ppid = psinfo.pr_ppid;
1307c478bd9Sstevel@tonic-gate
1317c478bd9Sstevel@tonic-gate if (ppid == 1) {
1327c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: Failed to reap %d: the only "
1337c478bd9Sstevel@tonic-gate "non-defunct ancestor is 'init'\n", command,
1347c478bd9Sstevel@tonic-gate (int)pid);
1357c478bd9Sstevel@tonic-gate return (1);
1367c478bd9Sstevel@tonic-gate }
1377c478bd9Sstevel@tonic-gate
1387c478bd9Sstevel@tonic-gate if (proc_usage(pid, &usage, &gret) == 0) {
1397c478bd9Sstevel@tonic-gate elapsed = usage.pr_tstamp.tv_sec - usage.pr_term.tv_sec;
1407c478bd9Sstevel@tonic-gate } else {
1417c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: cannot examine %d: %s\n",
1427c478bd9Sstevel@tonic-gate command, (int)pid, Pgrab_error(gret));
1437c478bd9Sstevel@tonic-gate return (1);
1447c478bd9Sstevel@tonic-gate }
1457c478bd9Sstevel@tonic-gate
1467c478bd9Sstevel@tonic-gate if ((Fflag == 0) && (elapsed < NOREAP_TIME)) {
1477c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: unsafe to reap %d; it has been "
1487c478bd9Sstevel@tonic-gate "defunct less than %d seconds\n", command, (int)pid,
1497c478bd9Sstevel@tonic-gate NOREAP_TIME);
1507c478bd9Sstevel@tonic-gate return (1);
1517c478bd9Sstevel@tonic-gate }
1527c478bd9Sstevel@tonic-gate
1537c478bd9Sstevel@tonic-gate if ((Pr = Pgrab(ppid, Fflag | PGRAB_NOSTOP, &gret)) == NULL) {
1547c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: cannot examine %d: %s\n", command,
1557c478bd9Sstevel@tonic-gate (int)ppid, Pgrab_error(gret));
1567c478bd9Sstevel@tonic-gate return (1);
1577c478bd9Sstevel@tonic-gate }
1587c478bd9Sstevel@tonic-gate
1597c478bd9Sstevel@tonic-gate if ((Fflag == 0) && (Pstate(Pr) == PS_STOP)) {
1607c478bd9Sstevel@tonic-gate Prelease(Pr, 0);
1617c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: unsafe to reap %d; parent is "
1627c478bd9Sstevel@tonic-gate "stopped and may reap status upon restart\n", command,
1637c478bd9Sstevel@tonic-gate (int)pid);
1647c478bd9Sstevel@tonic-gate return (1);
1657c478bd9Sstevel@tonic-gate }
1667c478bd9Sstevel@tonic-gate
1677c478bd9Sstevel@tonic-gate /*
1687c478bd9Sstevel@tonic-gate * Pstop() will fail if the process to be stopped has become a zombie.
1697c478bd9Sstevel@tonic-gate * This means that we can say with certainty that the child of this
1707c478bd9Sstevel@tonic-gate * process has not changed parents (i.e. been reparented to init) once
1717c478bd9Sstevel@tonic-gate * the Pstop() succeeds.
1727c478bd9Sstevel@tonic-gate */
1737c478bd9Sstevel@tonic-gate if (Pstop(Pr, 1000) != 0) {
1747c478bd9Sstevel@tonic-gate Prelease(Pr, 0);
1757c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: failed to stop %d: %s", command,
1767c478bd9Sstevel@tonic-gate (int)ppid, strerror(errno));
1777c478bd9Sstevel@tonic-gate return (1);
1787c478bd9Sstevel@tonic-gate }
1797c478bd9Sstevel@tonic-gate
1807c478bd9Sstevel@tonic-gate if (pr_waitid(Pr, P_PID, pid, &siginfo, WEXITED|WNOHANG) != 0) {
1817c478bd9Sstevel@tonic-gate Prelease(Pr, 0);
1827c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: waitid() in process %d failed: %s",
1837c478bd9Sstevel@tonic-gate command, (int)ppid, strerror(errno));
1847c478bd9Sstevel@tonic-gate return (1);
1857c478bd9Sstevel@tonic-gate }
1867c478bd9Sstevel@tonic-gate
1877c478bd9Sstevel@tonic-gate Prelease(Pr, 0);
1887c478bd9Sstevel@tonic-gate return (0);
1897c478bd9Sstevel@tonic-gate }
1907c478bd9Sstevel@tonic-gate
1917c478bd9Sstevel@tonic-gate static void
print_exit_status(pid_t pid,int wstat)1927c478bd9Sstevel@tonic-gate print_exit_status(pid_t pid, int wstat)
1937c478bd9Sstevel@tonic-gate {
1947c478bd9Sstevel@tonic-gate (void) printf("%d: ", (int)pid);
1957c478bd9Sstevel@tonic-gate if (WIFSIGNALED(wstat)) {
1967c478bd9Sstevel@tonic-gate char buf[SIG2STR_MAX];
1977c478bd9Sstevel@tonic-gate int sig = WTERMSIG(wstat);
1987c478bd9Sstevel@tonic-gate
1997c478bd9Sstevel@tonic-gate if (sig2str(sig, buf) == 0)
2007c478bd9Sstevel@tonic-gate (void) printf("killed by signal %s", buf);
2017c478bd9Sstevel@tonic-gate else
2027c478bd9Sstevel@tonic-gate (void) printf("killed by signal %d", sig);
2037c478bd9Sstevel@tonic-gate
2047c478bd9Sstevel@tonic-gate if (WCOREDUMP(wstat))
2057c478bd9Sstevel@tonic-gate (void) printf(" (core dumped)");
2067c478bd9Sstevel@tonic-gate } else {
2077c478bd9Sstevel@tonic-gate (void) printf("exited with status %d", WEXITSTATUS(wstat));
2087c478bd9Sstevel@tonic-gate }
2097c478bd9Sstevel@tonic-gate (void) printf("\n");
2107c478bd9Sstevel@tonic-gate }
2117c478bd9Sstevel@tonic-gate
2127c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])2137c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
2147c478bd9Sstevel@tonic-gate {
2157c478bd9Sstevel@tonic-gate int retc = 0;
2167c478bd9Sstevel@tonic-gate int opt;
2177c478bd9Sstevel@tonic-gate int errflg = 0;
2187c478bd9Sstevel@tonic-gate
2197c478bd9Sstevel@tonic-gate if ((command = strrchr(argv[0], '/')) != NULL)
2207c478bd9Sstevel@tonic-gate command++;
2217c478bd9Sstevel@tonic-gate else
2227c478bd9Sstevel@tonic-gate command = argv[0];
2237c478bd9Sstevel@tonic-gate
2247c478bd9Sstevel@tonic-gate while ((opt = getopt(argc, argv, "F")) != EOF) {
2257c478bd9Sstevel@tonic-gate switch (opt) {
2267c478bd9Sstevel@tonic-gate case 'F': /* force grabbing (no O_EXCL) */
2277c478bd9Sstevel@tonic-gate Fflag = PGRAB_FORCE;
2287c478bd9Sstevel@tonic-gate break;
2297c478bd9Sstevel@tonic-gate default:
2307c478bd9Sstevel@tonic-gate errflg = 1;
2317c478bd9Sstevel@tonic-gate break;
2327c478bd9Sstevel@tonic-gate }
2337c478bd9Sstevel@tonic-gate }
2347c478bd9Sstevel@tonic-gate
2357c478bd9Sstevel@tonic-gate argc -= optind;
2367c478bd9Sstevel@tonic-gate argv += optind;
2377c478bd9Sstevel@tonic-gate
2387c478bd9Sstevel@tonic-gate if (errflg || argc <= 0) {
2397c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "usage: %s pid ...\n", command);
2407c478bd9Sstevel@tonic-gate (void) fprintf(stderr, " (Reap a defunct process by forcing "
241*4b9db4f6SChris Fraire "its parent to wait(3C) for it)\n");
2427c478bd9Sstevel@tonic-gate exit(2);
2437c478bd9Sstevel@tonic-gate }
2447c478bd9Sstevel@tonic-gate
2457c478bd9Sstevel@tonic-gate /* catch signals from terminal */
2467c478bd9Sstevel@tonic-gate if (sigset(SIGHUP, SIG_IGN) == SIG_DFL)
2477c478bd9Sstevel@tonic-gate (void) sigset(SIGHUP, intr);
2487c478bd9Sstevel@tonic-gate if (sigset(SIGINT, SIG_IGN) == SIG_DFL)
2497c478bd9Sstevel@tonic-gate (void) sigset(SIGINT, intr);
2507c478bd9Sstevel@tonic-gate if (sigset(SIGPIPE, SIG_IGN) == SIG_DFL)
2517c478bd9Sstevel@tonic-gate (void) sigset(SIGPIPE, intr);
2527c478bd9Sstevel@tonic-gate if (sigset(SIGQUIT, SIG_IGN) == SIG_DFL)
2537c478bd9Sstevel@tonic-gate (void) sigset(SIGQUIT, intr);
2547c478bd9Sstevel@tonic-gate (void) sigset(SIGTERM, intr);
2557c478bd9Sstevel@tonic-gate
2567c478bd9Sstevel@tonic-gate while (--argc >= 0 && !interrupt) {
2577c478bd9Sstevel@tonic-gate pid_t pid;
2587c478bd9Sstevel@tonic-gate int wstat, r;
2597c478bd9Sstevel@tonic-gate
2607c478bd9Sstevel@tonic-gate retc += r = reap(*argv++, &pid, &wstat);
2617c478bd9Sstevel@tonic-gate
2627c478bd9Sstevel@tonic-gate if (r == 0)
2637c478bd9Sstevel@tonic-gate print_exit_status(pid, wstat);
2647c478bd9Sstevel@tonic-gate }
2657c478bd9Sstevel@tonic-gate
2667c478bd9Sstevel@tonic-gate if (interrupt && retc == 0)
2677c478bd9Sstevel@tonic-gate retc++;
2687c478bd9Sstevel@tonic-gate return (retc == 0 ? 0 : 1);
2697c478bd9Sstevel@tonic-gate }
270