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
57c3666b4Skk112340 * Common Development and Distribution License (the "License").
67c3666b4Skk112340 * 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 */
21657b1f3dSraf
227c478bd9Sstevel@tonic-gate /*
23*8fd04b83SRoger A. Faulkner * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate * This file contains a set of generic routines for periodically
297c478bd9Sstevel@tonic-gate * sampling the state of another process, or tree of processes.
307c478bd9Sstevel@tonic-gate *
317c478bd9Sstevel@tonic-gate * It is built upon the infrastructure provided by libproc.
327c478bd9Sstevel@tonic-gate */
337c478bd9Sstevel@tonic-gate
347c478bd9Sstevel@tonic-gate #include <sys/wait.h>
357c478bd9Sstevel@tonic-gate #include <sys/syscall.h>
367c478bd9Sstevel@tonic-gate #include <sys/time.h>
377c478bd9Sstevel@tonic-gate #include <libproc.h>
387c478bd9Sstevel@tonic-gate #include <stdio.h>
397c478bd9Sstevel@tonic-gate #include <stdlib.h>
407c478bd9Sstevel@tonic-gate #include <errno.h>
417c478bd9Sstevel@tonic-gate #include <unistd.h>
427c478bd9Sstevel@tonic-gate #include <signal.h>
437c478bd9Sstevel@tonic-gate #include <string.h>
447c478bd9Sstevel@tonic-gate #include <strings.h>
457c478bd9Sstevel@tonic-gate #include <limits.h>
467c478bd9Sstevel@tonic-gate #include <ctype.h>
477c478bd9Sstevel@tonic-gate #include <libintl.h>
487c478bd9Sstevel@tonic-gate #include <libcpc.h>
497c478bd9Sstevel@tonic-gate #include <sys/cpc_impl.h>
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gate #include "libpctx.h"
527c478bd9Sstevel@tonic-gate
537c478bd9Sstevel@tonic-gate struct __pctx {
547c478bd9Sstevel@tonic-gate pctx_errfn_t *errfn;
557c478bd9Sstevel@tonic-gate struct ps_prochandle *Pr;
567c478bd9Sstevel@tonic-gate void *uarg;
577c478bd9Sstevel@tonic-gate pctx_sysc_execfn_t *exec;
587c478bd9Sstevel@tonic-gate pctx_sysc_forkfn_t *fork;
597c478bd9Sstevel@tonic-gate pctx_sysc_exitfn_t *exit;
607c478bd9Sstevel@tonic-gate pctx_sysc_lwp_createfn_t *lwp_create;
617c478bd9Sstevel@tonic-gate pctx_init_lwpfn_t *init_lwp;
627c478bd9Sstevel@tonic-gate pctx_fini_lwpfn_t *fini_lwp;
637c478bd9Sstevel@tonic-gate pctx_sysc_lwp_exitfn_t *lwp_exit;
647c478bd9Sstevel@tonic-gate int verbose;
657c478bd9Sstevel@tonic-gate int created;
667c478bd9Sstevel@tonic-gate int sigblocked;
67b885580bSAlexander Kolbasov int terminate;
687c478bd9Sstevel@tonic-gate sigset_t savedset;
697c478bd9Sstevel@tonic-gate cpc_t *cpc;
707c478bd9Sstevel@tonic-gate };
717c478bd9Sstevel@tonic-gate
727c478bd9Sstevel@tonic-gate static void (*pctx_cpc_callback)(cpc_t *cpc, struct __pctx *pctx);
737c478bd9Sstevel@tonic-gate
747c478bd9Sstevel@tonic-gate static void
pctx_default_errfn(const char * fn,const char * fmt,va_list ap)757c478bd9Sstevel@tonic-gate pctx_default_errfn(const char *fn, const char *fmt, va_list ap)
767c478bd9Sstevel@tonic-gate {
777c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "libpctx: pctx_%s: ", fn);
787c478bd9Sstevel@tonic-gate (void) vfprintf(stderr, fmt, ap);
797c478bd9Sstevel@tonic-gate }
807c478bd9Sstevel@tonic-gate
817c478bd9Sstevel@tonic-gate /*PRINTFLIKE3*/
827c478bd9Sstevel@tonic-gate static void
pctx_error(pctx_t * pctx,const char * fn,const char * fmt,...)837c478bd9Sstevel@tonic-gate pctx_error(pctx_t *pctx, const char *fn, const char *fmt, ...)
847c478bd9Sstevel@tonic-gate {
857c478bd9Sstevel@tonic-gate va_list ap;
867c478bd9Sstevel@tonic-gate
877c478bd9Sstevel@tonic-gate va_start(ap, fmt);
887c478bd9Sstevel@tonic-gate pctx->errfn(fn, fmt, ap);
897c478bd9Sstevel@tonic-gate va_end(ap);
907c478bd9Sstevel@tonic-gate }
917c478bd9Sstevel@tonic-gate
927c478bd9Sstevel@tonic-gate /*
937c478bd9Sstevel@tonic-gate * Create a new process and bind the user args for it
947c478bd9Sstevel@tonic-gate */
957c478bd9Sstevel@tonic-gate pctx_t *
pctx_create(const char * filename,char * const * argv,void * arg,int verbose,pctx_errfn_t * errfn)967c478bd9Sstevel@tonic-gate pctx_create(
977c478bd9Sstevel@tonic-gate const char *filename,
987c478bd9Sstevel@tonic-gate char *const *argv,
997c478bd9Sstevel@tonic-gate void *arg,
1007c478bd9Sstevel@tonic-gate int verbose,
1017c478bd9Sstevel@tonic-gate pctx_errfn_t *errfn)
1027c478bd9Sstevel@tonic-gate {
1037c478bd9Sstevel@tonic-gate static const char fn[] = "create";
1047c478bd9Sstevel@tonic-gate int err;
1057c478bd9Sstevel@tonic-gate pctx_t *pctx;
1067c478bd9Sstevel@tonic-gate
1077c478bd9Sstevel@tonic-gate pctx = calloc(1, sizeof (*pctx));
1087c478bd9Sstevel@tonic-gate pctx->uarg = arg;
1097c478bd9Sstevel@tonic-gate pctx->verbose = verbose;
110b885580bSAlexander Kolbasov pctx->terminate = 0;
1117c478bd9Sstevel@tonic-gate pctx->errfn = errfn ? errfn : pctx_default_errfn;
1127c478bd9Sstevel@tonic-gate
1137c478bd9Sstevel@tonic-gate if ((pctx->Pr = Pcreate(filename, argv, &err, 0, 0)) == NULL) {
1147c478bd9Sstevel@tonic-gate switch (err) {
1157c478bd9Sstevel@tonic-gate case C_PERM:
1167c478bd9Sstevel@tonic-gate pctx_error(pctx, fn, gettext("cannot trace set-id or "
1177c478bd9Sstevel@tonic-gate "unreadable program '%s'\n"), filename);
1187c478bd9Sstevel@tonic-gate break;
1197c478bd9Sstevel@tonic-gate case C_LP64:
1207c478bd9Sstevel@tonic-gate pctx_error(pctx, fn, gettext("cannot control LP64 "
1217c478bd9Sstevel@tonic-gate "program '%s'\n"), filename);
1227c478bd9Sstevel@tonic-gate break;
1237c478bd9Sstevel@tonic-gate case C_NOEXEC:
1247c478bd9Sstevel@tonic-gate pctx_error(pctx, fn, gettext("cannot execute "
1257c478bd9Sstevel@tonic-gate "program '%s'\n"), filename);
1267c478bd9Sstevel@tonic-gate break;
1277c478bd9Sstevel@tonic-gate case C_NOENT:
1287c478bd9Sstevel@tonic-gate pctx_error(pctx, fn, gettext("cannot find"
1297c478bd9Sstevel@tonic-gate "program '%s'\n"), filename);
1307c478bd9Sstevel@tonic-gate break;
1317c478bd9Sstevel@tonic-gate case C_FORK:
1327c478bd9Sstevel@tonic-gate pctx_error(pctx, fn, gettext("cannot fork, "
1337c478bd9Sstevel@tonic-gate "program '%s'\n"), filename);
1347c478bd9Sstevel@tonic-gate break;
1357c478bd9Sstevel@tonic-gate default:
1367c478bd9Sstevel@tonic-gate pctx_error(pctx, fn, gettext("%s, program '%s'\n"),
1377c478bd9Sstevel@tonic-gate Pcreate_error(err), filename);
1387c478bd9Sstevel@tonic-gate break;
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate free(pctx);
1417c478bd9Sstevel@tonic-gate return (NULL);
1427c478bd9Sstevel@tonic-gate }
1437c478bd9Sstevel@tonic-gate
1447c478bd9Sstevel@tonic-gate if (Psysentry(pctx->Pr, SYS_exit, 1) == -1) {
1457c478bd9Sstevel@tonic-gate pctx_error(pctx, fn,
1467c478bd9Sstevel@tonic-gate gettext("can't stop-on-exit() program '%s'\n"), filename);
1477c478bd9Sstevel@tonic-gate Prelease(pctx->Pr, PRELEASE_KILL);
1487c478bd9Sstevel@tonic-gate free(pctx);
1497c478bd9Sstevel@tonic-gate return (NULL);
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate /*
1527c478bd9Sstevel@tonic-gate * Set kill-on-last-close so the controlled process
1537c478bd9Sstevel@tonic-gate * dies if we die.
1547c478bd9Sstevel@tonic-gate */
1557c478bd9Sstevel@tonic-gate pctx->created = 1;
1567c478bd9Sstevel@tonic-gate (void) Psetflags(pctx->Pr, PR_KLC);
1577c478bd9Sstevel@tonic-gate (void) pctx_set_events(pctx, PCTX_NULL_EVENT);
1587c478bd9Sstevel@tonic-gate
1597c478bd9Sstevel@tonic-gate return (pctx);
1607c478bd9Sstevel@tonic-gate }
1617c478bd9Sstevel@tonic-gate
1627c478bd9Sstevel@tonic-gate /*
1637c478bd9Sstevel@tonic-gate * Capture an existing process and bind the user args for it
1647c478bd9Sstevel@tonic-gate */
1657c478bd9Sstevel@tonic-gate pctx_t *
pctx_capture(pid_t pid,void * arg,int verbose,pctx_errfn_t * errfn)1667c478bd9Sstevel@tonic-gate pctx_capture(pid_t pid, void *arg, int verbose, pctx_errfn_t *errfn)
1677c478bd9Sstevel@tonic-gate {
1687c478bd9Sstevel@tonic-gate static const char fn[] = "capture";
1697c478bd9Sstevel@tonic-gate int err;
1707c478bd9Sstevel@tonic-gate pctx_t *pctx;
1717c478bd9Sstevel@tonic-gate
1727c478bd9Sstevel@tonic-gate pctx = calloc(1, sizeof (*pctx));
1737c478bd9Sstevel@tonic-gate pctx->uarg = arg;
1747c478bd9Sstevel@tonic-gate pctx->verbose = verbose;
1757c478bd9Sstevel@tonic-gate pctx->errfn = errfn ? errfn : pctx_default_errfn;
1767c478bd9Sstevel@tonic-gate
1777c478bd9Sstevel@tonic-gate if ((pctx->Pr = Pgrab(pid, 0, &err)) == NULL) {
1787c478bd9Sstevel@tonic-gate switch (err) {
1797c478bd9Sstevel@tonic-gate case G_NOPROC:
1807c478bd9Sstevel@tonic-gate pctx_error(pctx, fn,
1817c478bd9Sstevel@tonic-gate gettext("pid %d doesn't exist\n"), (int)pid);
1827c478bd9Sstevel@tonic-gate break;
1837c478bd9Sstevel@tonic-gate case G_ZOMB:
1847c478bd9Sstevel@tonic-gate pctx_error(pctx, fn,
1857c478bd9Sstevel@tonic-gate gettext("pid %d is a zombie\n"), (int)pid);
1867c478bd9Sstevel@tonic-gate break;
1877c478bd9Sstevel@tonic-gate case G_PERM:
1887c478bd9Sstevel@tonic-gate pctx_error(pctx, fn,
1897c478bd9Sstevel@tonic-gate gettext("pid %d: permission denied\n"), (int)pid);
1907c478bd9Sstevel@tonic-gate break;
1917c478bd9Sstevel@tonic-gate case G_BUSY:
1927c478bd9Sstevel@tonic-gate pctx_error(pctx, fn,
1937c478bd9Sstevel@tonic-gate gettext("pid %d is already being traced\n"),
1947c478bd9Sstevel@tonic-gate (int)pid);
1957c478bd9Sstevel@tonic-gate break;
1967c478bd9Sstevel@tonic-gate case G_SYS:
1977c478bd9Sstevel@tonic-gate pctx_error(pctx, fn,
1987c478bd9Sstevel@tonic-gate gettext("pid %d is a system process\n"), (int)pid);
1997c478bd9Sstevel@tonic-gate break;
2007c478bd9Sstevel@tonic-gate case G_SELF:
2017c478bd9Sstevel@tonic-gate pctx_error(pctx, fn,
2027c478bd9Sstevel@tonic-gate gettext("cannot capture self!\n"));
2037c478bd9Sstevel@tonic-gate break;
2047c478bd9Sstevel@tonic-gate case G_LP64:
2057c478bd9Sstevel@tonic-gate pctx_error(pctx, fn, gettext("cannot control LP64 "
2067c478bd9Sstevel@tonic-gate "process, pid %d\n"), (int)pid);
2077c478bd9Sstevel@tonic-gate break;
2087c478bd9Sstevel@tonic-gate default:
2097c478bd9Sstevel@tonic-gate pctx_error(pctx, fn, gettext("%s: pid %d\n"),
2107c478bd9Sstevel@tonic-gate Pgrab_error(err), (int)pid);
2117c478bd9Sstevel@tonic-gate break;
2127c478bd9Sstevel@tonic-gate }
2137c478bd9Sstevel@tonic-gate free(pctx);
2147c478bd9Sstevel@tonic-gate return (NULL);
2157c478bd9Sstevel@tonic-gate }
2167c478bd9Sstevel@tonic-gate
2177c478bd9Sstevel@tonic-gate if (Psysentry(pctx->Pr, SYS_exit, 1) == -1) {
2187c478bd9Sstevel@tonic-gate pctx_error(pctx, fn,
2197c478bd9Sstevel@tonic-gate gettext("can't stop-on-exit() pid %d\n"), (int)pid);
2207c478bd9Sstevel@tonic-gate Prelease(pctx->Pr, PRELEASE_CLEAR);
2217c478bd9Sstevel@tonic-gate free(pctx);
2227c478bd9Sstevel@tonic-gate return (NULL);
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate
2257c478bd9Sstevel@tonic-gate /*
2267c478bd9Sstevel@tonic-gate * Set run-on-last-close so the controlled process
2277c478bd9Sstevel@tonic-gate * runs even if we die on a signal. This is because
2287c478bd9Sstevel@tonic-gate * we grabbed an existing process - it would be impolite
2297c478bd9Sstevel@tonic-gate * to cause it to die if we exit prematurely.
2307c478bd9Sstevel@tonic-gate */
2317c478bd9Sstevel@tonic-gate pctx->created = 0;
2327c478bd9Sstevel@tonic-gate (void) Psetflags(pctx->Pr, PR_RLC);
2337c478bd9Sstevel@tonic-gate (void) pctx_set_events(pctx, PCTX_NULL_EVENT);
2347c478bd9Sstevel@tonic-gate
2357c478bd9Sstevel@tonic-gate return (pctx);
2367c478bd9Sstevel@tonic-gate }
2377c478bd9Sstevel@tonic-gate
2387c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2397c478bd9Sstevel@tonic-gate static void
default_void(pctx_t * pctx)2407c478bd9Sstevel@tonic-gate default_void(pctx_t *pctx)
2417c478bd9Sstevel@tonic-gate {}
2427c478bd9Sstevel@tonic-gate
2437c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2447c478bd9Sstevel@tonic-gate static int
default_int(pctx_t * pctx)2457c478bd9Sstevel@tonic-gate default_int(pctx_t *pctx)
2467c478bd9Sstevel@tonic-gate {
2477c478bd9Sstevel@tonic-gate return (0);
2487c478bd9Sstevel@tonic-gate }
2497c478bd9Sstevel@tonic-gate
2507c478bd9Sstevel@tonic-gate int
pctx_set_events(pctx_t * pctx,...)2517c478bd9Sstevel@tonic-gate pctx_set_events(pctx_t *pctx, ...)
2527c478bd9Sstevel@tonic-gate {
2537c478bd9Sstevel@tonic-gate static const char fn[] = "set_events";
2547c478bd9Sstevel@tonic-gate va_list pvar;
2557c478bd9Sstevel@tonic-gate int error = 0;
2567c478bd9Sstevel@tonic-gate pctx_event_t event;
2577c478bd9Sstevel@tonic-gate
2587c478bd9Sstevel@tonic-gate va_start(pvar, pctx);
2597c478bd9Sstevel@tonic-gate do {
2607c478bd9Sstevel@tonic-gate switch (event = (pctx_event_t)va_arg(pvar, pctx_event_t)) {
2617c478bd9Sstevel@tonic-gate case PCTX_NULL_EVENT:
2627c478bd9Sstevel@tonic-gate break;
2637c478bd9Sstevel@tonic-gate case PCTX_SYSC_EXEC_EVENT:
2647c478bd9Sstevel@tonic-gate pctx->exec = (pctx_sysc_execfn_t *)
2657c478bd9Sstevel@tonic-gate va_arg(pvar, pctx_sysc_execfn_t *);
2667c478bd9Sstevel@tonic-gate break;
2677c478bd9Sstevel@tonic-gate case PCTX_SYSC_FORK_EVENT:
2687c478bd9Sstevel@tonic-gate pctx->fork = (pctx_sysc_forkfn_t *)
2697c478bd9Sstevel@tonic-gate va_arg(pvar, pctx_sysc_forkfn_t *);
2707c478bd9Sstevel@tonic-gate break;
2717c478bd9Sstevel@tonic-gate case PCTX_SYSC_EXIT_EVENT: /* always intercepted */
2727c478bd9Sstevel@tonic-gate pctx->exit = (pctx_sysc_exitfn_t *)
2737c478bd9Sstevel@tonic-gate va_arg(pvar, pctx_sysc_exitfn_t *);
2747c478bd9Sstevel@tonic-gate break;
2757c478bd9Sstevel@tonic-gate case PCTX_SYSC_LWP_CREATE_EVENT:
2767c478bd9Sstevel@tonic-gate pctx->lwp_create = (pctx_sysc_lwp_createfn_t *)
2777c478bd9Sstevel@tonic-gate va_arg(pvar, pctx_sysc_lwp_createfn_t *);
2787c478bd9Sstevel@tonic-gate break;
2797c478bd9Sstevel@tonic-gate case PCTX_INIT_LWP_EVENT:
2807c478bd9Sstevel@tonic-gate pctx->init_lwp = (pctx_init_lwpfn_t *)
2817c478bd9Sstevel@tonic-gate va_arg(pvar, pctx_init_lwpfn_t *);
2827c478bd9Sstevel@tonic-gate break;
2837c478bd9Sstevel@tonic-gate case PCTX_FINI_LWP_EVENT:
2847c478bd9Sstevel@tonic-gate pctx->fini_lwp = (pctx_fini_lwpfn_t *)
2857c478bd9Sstevel@tonic-gate va_arg(pvar, pctx_fini_lwpfn_t *);
2867c478bd9Sstevel@tonic-gate break;
2877c478bd9Sstevel@tonic-gate case PCTX_SYSC_LWP_EXIT_EVENT:
2887c478bd9Sstevel@tonic-gate pctx->lwp_exit = (pctx_sysc_lwp_exitfn_t *)
2897c478bd9Sstevel@tonic-gate va_arg(pvar, pctx_sysc_lwp_exitfn_t *);
2907c478bd9Sstevel@tonic-gate break;
2917c478bd9Sstevel@tonic-gate default:
2927c478bd9Sstevel@tonic-gate pctx_error(pctx, fn,
2937c478bd9Sstevel@tonic-gate gettext("unknown event type %x\n"), event);
2947c478bd9Sstevel@tonic-gate error = -1;
2957c478bd9Sstevel@tonic-gate break;
2967c478bd9Sstevel@tonic-gate }
2977c478bd9Sstevel@tonic-gate } while (event != PCTX_NULL_EVENT && error == 0);
2987c478bd9Sstevel@tonic-gate va_end(pvar);
2997c478bd9Sstevel@tonic-gate
3007c478bd9Sstevel@tonic-gate if (error != 0)
3017c478bd9Sstevel@tonic-gate return (error);
3027c478bd9Sstevel@tonic-gate
3037c478bd9Sstevel@tonic-gate if (pctx->exec == NULL)
3047c478bd9Sstevel@tonic-gate pctx->exec = (pctx_sysc_execfn_t *)default_int;
3057c478bd9Sstevel@tonic-gate if (pctx->fork == NULL)
3067c478bd9Sstevel@tonic-gate pctx->fork = (pctx_sysc_forkfn_t *)default_void;
3077c478bd9Sstevel@tonic-gate if (pctx->exit == NULL)
3087c478bd9Sstevel@tonic-gate pctx->exit = (pctx_sysc_exitfn_t *)default_void;
3097c478bd9Sstevel@tonic-gate if (pctx->lwp_create == NULL)
3107c478bd9Sstevel@tonic-gate pctx->lwp_create = (pctx_sysc_lwp_createfn_t *)default_int;
3117c478bd9Sstevel@tonic-gate if (pctx->init_lwp == NULL)
3127c478bd9Sstevel@tonic-gate pctx->init_lwp = (pctx_init_lwpfn_t *)default_int;
3137c478bd9Sstevel@tonic-gate if (pctx->fini_lwp == NULL)
3147c478bd9Sstevel@tonic-gate pctx->fini_lwp = (pctx_fini_lwpfn_t *)default_int;
3157c478bd9Sstevel@tonic-gate if (pctx->lwp_exit == NULL)
3167c478bd9Sstevel@tonic-gate pctx->lwp_exit = (pctx_sysc_lwp_exitfn_t *)default_int;
3177c478bd9Sstevel@tonic-gate
3187c478bd9Sstevel@tonic-gate if (pctx->fork != (pctx_sysc_forkfn_t *)default_void) {
3197c478bd9Sstevel@tonic-gate (void) Psysexit(pctx->Pr, SYS_vfork, 1);
320657b1f3dSraf (void) Psysexit(pctx->Pr, SYS_forksys, 1);
3217c478bd9Sstevel@tonic-gate if (Psetflags(pctx->Pr, PR_FORK) == -1)
3227c478bd9Sstevel@tonic-gate error = -1;
3237c478bd9Sstevel@tonic-gate } else {
3247c478bd9Sstevel@tonic-gate (void) Psysexit(pctx->Pr, SYS_vfork, 0);
325657b1f3dSraf (void) Psysexit(pctx->Pr, SYS_forksys, 0);
3267c478bd9Sstevel@tonic-gate if (Punsetflags(pctx->Pr, PR_FORK) == -1)
3277c478bd9Sstevel@tonic-gate error = -1;
3287c478bd9Sstevel@tonic-gate }
3297c478bd9Sstevel@tonic-gate
3307c478bd9Sstevel@tonic-gate /*
3317c478bd9Sstevel@tonic-gate * exec causes termination of all but the exec-ing lwp,
3327c478bd9Sstevel@tonic-gate * and resets the lwpid to one in the new address space.
3337c478bd9Sstevel@tonic-gate */
3347c478bd9Sstevel@tonic-gate if (pctx->exec != (pctx_sysc_execfn_t *)default_int ||
3357c478bd9Sstevel@tonic-gate pctx->fini_lwp != (pctx_fini_lwpfn_t *)default_int ||
3367c478bd9Sstevel@tonic-gate pctx->init_lwp != (pctx_init_lwpfn_t *)default_int) {
3377c478bd9Sstevel@tonic-gate (void) Psysexit(pctx->Pr, SYS_execve, 1);
3387c478bd9Sstevel@tonic-gate (void) Psysentry(pctx->Pr, SYS_execve, 1);
3397c478bd9Sstevel@tonic-gate } else {
3407c478bd9Sstevel@tonic-gate (void) Psysexit(pctx->Pr, SYS_execve, 0);
3417c478bd9Sstevel@tonic-gate (void) Psysentry(pctx->Pr, SYS_execve, 0);
3427c478bd9Sstevel@tonic-gate }
3437c478bd9Sstevel@tonic-gate
3447c478bd9Sstevel@tonic-gate (void) Psysexit(pctx->Pr, SYS_lwp_create,
3457c478bd9Sstevel@tonic-gate pctx->lwp_create != (pctx_sysc_lwp_createfn_t *)default_int ||
3467c478bd9Sstevel@tonic-gate pctx->init_lwp != (pctx_init_lwpfn_t *)default_int);
3477c478bd9Sstevel@tonic-gate
3487c478bd9Sstevel@tonic-gate (void) Psysentry(pctx->Pr, SYS_lwp_exit,
3497c478bd9Sstevel@tonic-gate pctx->lwp_exit != (pctx_sysc_lwp_exitfn_t *)default_int ||
3507c478bd9Sstevel@tonic-gate pctx->fini_lwp != (pctx_fini_lwpfn_t *)default_int);
3517c478bd9Sstevel@tonic-gate
3527c478bd9Sstevel@tonic-gate return (0);
3537c478bd9Sstevel@tonic-gate }
3547c478bd9Sstevel@tonic-gate
3557c478bd9Sstevel@tonic-gate static sigset_t termsig;
3567c478bd9Sstevel@tonic-gate
3577c478bd9Sstevel@tonic-gate static void
__libpctx_init(void)3587c478bd9Sstevel@tonic-gate __libpctx_init(void)
3597c478bd9Sstevel@tonic-gate {
3607c478bd9Sstevel@tonic-gate /*
3617c478bd9Sstevel@tonic-gate * Initialize the signal set used to shield ourselves from
3627c478bd9Sstevel@tonic-gate * death-by-terminal-signal while the agent lwp is running.
3637c478bd9Sstevel@tonic-gate */
3647c478bd9Sstevel@tonic-gate (void) sigemptyset(&termsig);
3657c478bd9Sstevel@tonic-gate (void) sigaddset(&termsig, SIGHUP);
3667c478bd9Sstevel@tonic-gate (void) sigaddset(&termsig, SIGTERM);
3677c478bd9Sstevel@tonic-gate (void) sigaddset(&termsig, SIGINT);
3687c478bd9Sstevel@tonic-gate (void) sigaddset(&termsig, SIGQUIT);
3697c478bd9Sstevel@tonic-gate }
3707c478bd9Sstevel@tonic-gate
3717c478bd9Sstevel@tonic-gate #pragma init(__libpctx_init)
3727c478bd9Sstevel@tonic-gate
3737c478bd9Sstevel@tonic-gate static void
pctx_begin_syscalls(pctx_t * pctx)3747c478bd9Sstevel@tonic-gate pctx_begin_syscalls(pctx_t *pctx)
3757c478bd9Sstevel@tonic-gate {
3767c478bd9Sstevel@tonic-gate if (pctx->Pr == NULL)
3777c478bd9Sstevel@tonic-gate return;
3787c478bd9Sstevel@tonic-gate if (pctx->sigblocked++ == 0) {
3797c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_BLOCK, &termsig, &pctx->savedset);
3807c478bd9Sstevel@tonic-gate (void) Pcreate_agent(pctx->Pr);
3817c478bd9Sstevel@tonic-gate }
3827c478bd9Sstevel@tonic-gate }
3837c478bd9Sstevel@tonic-gate
3847c478bd9Sstevel@tonic-gate static void
pctx_end_syscalls(pctx_t * pctx)3857c478bd9Sstevel@tonic-gate pctx_end_syscalls(pctx_t *pctx)
3867c478bd9Sstevel@tonic-gate {
3877c478bd9Sstevel@tonic-gate if (pctx->Pr == NULL)
3887c478bd9Sstevel@tonic-gate return;
3897c478bd9Sstevel@tonic-gate if (--pctx->sigblocked == 0) {
3907c478bd9Sstevel@tonic-gate (void) Pdestroy_agent(pctx->Pr);
3917c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_SETMASK, &pctx->savedset, NULL);
3927c478bd9Sstevel@tonic-gate }
3937c478bd9Sstevel@tonic-gate }
3947c478bd9Sstevel@tonic-gate
3957c478bd9Sstevel@tonic-gate /*
3967c478bd9Sstevel@tonic-gate * Iterate over the valid lwpids in the process, invoking the
3977c478bd9Sstevel@tonic-gate * action function on each one.
3987c478bd9Sstevel@tonic-gate */
3997c478bd9Sstevel@tonic-gate static int
pctx_lwpiterate(pctx_t * pctx,int (* action)(pctx_t *,pid_t,id_t,void *))4007c478bd9Sstevel@tonic-gate pctx_lwpiterate(pctx_t *pctx, int (*action)(pctx_t *, pid_t, id_t, void *))
4017c478bd9Sstevel@tonic-gate {
4027c478bd9Sstevel@tonic-gate const pstatus_t *pstatus;
4037c478bd9Sstevel@tonic-gate char lstatus[64];
4047c478bd9Sstevel@tonic-gate struct stat statb;
4057c478bd9Sstevel@tonic-gate lwpstatus_t *lwps;
4067c478bd9Sstevel@tonic-gate prheader_t *prh;
4077c478bd9Sstevel@tonic-gate int fd, nlwp;
4087c478bd9Sstevel@tonic-gate int ret = 0;
4097c478bd9Sstevel@tonic-gate
4107c478bd9Sstevel@tonic-gate if (action == (int (*)(pctx_t *, pid_t, id_t, void *))default_int)
4117c478bd9Sstevel@tonic-gate return (0);
4127c478bd9Sstevel@tonic-gate
4137c478bd9Sstevel@tonic-gate pstatus = Pstatus(pctx->Pr);
4147c478bd9Sstevel@tonic-gate if (pstatus->pr_nlwp <= 1) {
4157c478bd9Sstevel@tonic-gate pctx_begin_syscalls(pctx);
4167c478bd9Sstevel@tonic-gate ret = action(pctx, pstatus->pr_pid, 1, pctx->uarg);
4177c478bd9Sstevel@tonic-gate pctx_end_syscalls(pctx);
4187c478bd9Sstevel@tonic-gate return (ret);
4197c478bd9Sstevel@tonic-gate }
4207c478bd9Sstevel@tonic-gate
4217c478bd9Sstevel@tonic-gate (void) snprintf(lstatus, sizeof (lstatus),
4227c478bd9Sstevel@tonic-gate "/proc/%d/lstatus", (int)pstatus->pr_pid);
4237c478bd9Sstevel@tonic-gate
4247c478bd9Sstevel@tonic-gate if ((fd = open(lstatus, O_RDONLY)) < 0 ||
4257c478bd9Sstevel@tonic-gate fstat(fd, &statb) != 0) {
4267c478bd9Sstevel@tonic-gate if (fd >= 0)
4277c478bd9Sstevel@tonic-gate (void) close(fd);
4287c478bd9Sstevel@tonic-gate return (-1);
4297c478bd9Sstevel@tonic-gate }
4307c478bd9Sstevel@tonic-gate
4317c478bd9Sstevel@tonic-gate prh = malloc(statb.st_size);
4327c478bd9Sstevel@tonic-gate if (read(fd, prh, statb.st_size) <
4337c478bd9Sstevel@tonic-gate sizeof (prheader_t) + sizeof (lwpstatus_t)) {
4347c478bd9Sstevel@tonic-gate (void) close(fd);
4357c478bd9Sstevel@tonic-gate free(prh);
4367c478bd9Sstevel@tonic-gate return (-1);
4377c478bd9Sstevel@tonic-gate }
4387c478bd9Sstevel@tonic-gate (void) close(fd);
4397c478bd9Sstevel@tonic-gate
4407c478bd9Sstevel@tonic-gate /* LINTED pointer cast may result in improper alignment */
4417c478bd9Sstevel@tonic-gate lwps = (lwpstatus_t *)(prh + 1);
4427c478bd9Sstevel@tonic-gate pctx_begin_syscalls(pctx);
4437c478bd9Sstevel@tonic-gate for (nlwp = prh->pr_nent; nlwp > 0; nlwp--) {
4447c478bd9Sstevel@tonic-gate if (action(pctx,
4457c478bd9Sstevel@tonic-gate pstatus->pr_pid, lwps->pr_lwpid, pctx->uarg) != 0)
4467c478bd9Sstevel@tonic-gate ret = -1;
4477c478bd9Sstevel@tonic-gate /* LINTED pointer cast may result in improper alignment */
4487c478bd9Sstevel@tonic-gate lwps = (lwpstatus_t *)((char *)lwps + prh->pr_entsize);
4497c478bd9Sstevel@tonic-gate }
4507c478bd9Sstevel@tonic-gate pctx_end_syscalls(pctx);
4517c478bd9Sstevel@tonic-gate free(prh);
4527c478bd9Sstevel@tonic-gate return (ret);
4537c478bd9Sstevel@tonic-gate }
4547c478bd9Sstevel@tonic-gate
4557c478bd9Sstevel@tonic-gate /*
4567c478bd9Sstevel@tonic-gate * Free any associated state, but leave the process stopped if it
4577c478bd9Sstevel@tonic-gate * is still under our control. (If it isn't under our control,
4587c478bd9Sstevel@tonic-gate * it should just run to completion when we do our last close)
4597c478bd9Sstevel@tonic-gate */
4607c478bd9Sstevel@tonic-gate static void
pctx_free(pctx_t * pctx)4617c478bd9Sstevel@tonic-gate pctx_free(pctx_t *pctx)
4627c478bd9Sstevel@tonic-gate {
4637c478bd9Sstevel@tonic-gate if (pctx->cpc != NULL && pctx_cpc_callback != NULL)
4647c478bd9Sstevel@tonic-gate (*pctx_cpc_callback)(pctx->cpc, pctx);
4657c478bd9Sstevel@tonic-gate if (pctx->Pr) {
4667c478bd9Sstevel@tonic-gate Pfree(pctx->Pr);
4677c478bd9Sstevel@tonic-gate pctx->Pr = NULL;
4687c478bd9Sstevel@tonic-gate }
4697c478bd9Sstevel@tonic-gate pctx->errfn = pctx_default_errfn;
4707c478bd9Sstevel@tonic-gate }
4717c478bd9Sstevel@tonic-gate
4727c478bd9Sstevel@tonic-gate /*
4737c478bd9Sstevel@tonic-gate * Completely release the process from our control and discard all our state
4747c478bd9Sstevel@tonic-gate */
4757c478bd9Sstevel@tonic-gate void
pctx_release(pctx_t * pctx)4767c478bd9Sstevel@tonic-gate pctx_release(pctx_t *pctx)
4777c478bd9Sstevel@tonic-gate {
4787c478bd9Sstevel@tonic-gate if (pctx->Pr) {
4797c478bd9Sstevel@tonic-gate Prelease(pctx->Pr, PRELEASE_CLEAR);
4807c478bd9Sstevel@tonic-gate pctx->Pr = NULL;
4817c478bd9Sstevel@tonic-gate }
482b885580bSAlexander Kolbasov
4837c478bd9Sstevel@tonic-gate pctx_free(pctx);
4847c478bd9Sstevel@tonic-gate bzero(pctx, sizeof (*pctx));
4857c478bd9Sstevel@tonic-gate free(pctx);
4867c478bd9Sstevel@tonic-gate }
4877c478bd9Sstevel@tonic-gate
4887c478bd9Sstevel@tonic-gate static void
msincr(struct timeval * tv,uint_t msec)4897c478bd9Sstevel@tonic-gate msincr(struct timeval *tv, uint_t msec)
4907c478bd9Sstevel@tonic-gate {
4917c478bd9Sstevel@tonic-gate tv->tv_sec += msec / MILLISEC;
4927c478bd9Sstevel@tonic-gate tv->tv_usec += (msec % MILLISEC) * MILLISEC;
4937c478bd9Sstevel@tonic-gate if (tv->tv_usec > MICROSEC) {
4947c478bd9Sstevel@tonic-gate tv->tv_sec++;
4957c478bd9Sstevel@tonic-gate tv->tv_usec -= MICROSEC;
4967c478bd9Sstevel@tonic-gate }
4977c478bd9Sstevel@tonic-gate }
4987c478bd9Sstevel@tonic-gate
4997c478bd9Sstevel@tonic-gate static uint_t
msdiff(struct timeval * tva,struct timeval * tvb)5007c478bd9Sstevel@tonic-gate msdiff(struct timeval *tva, struct timeval *tvb)
5017c478bd9Sstevel@tonic-gate {
5027c478bd9Sstevel@tonic-gate time_t sdiff = tva->tv_sec - tvb->tv_sec;
5037c478bd9Sstevel@tonic-gate suseconds_t udiff = tva->tv_usec - tvb->tv_usec;
5047c478bd9Sstevel@tonic-gate
5057c478bd9Sstevel@tonic-gate if (sdiff < 0)
5067c478bd9Sstevel@tonic-gate return (0);
5077c478bd9Sstevel@tonic-gate if (udiff < 0) {
5087c478bd9Sstevel@tonic-gate udiff += MICROSEC;
5097c478bd9Sstevel@tonic-gate sdiff--;
5107c478bd9Sstevel@tonic-gate }
5117c478bd9Sstevel@tonic-gate if (sdiff < 0)
5127c478bd9Sstevel@tonic-gate return (0);
5137c478bd9Sstevel@tonic-gate if (sdiff >= (INT_MAX / MILLISEC))
5147c478bd9Sstevel@tonic-gate return ((uint_t)INT_MAX);
5157c478bd9Sstevel@tonic-gate return ((uint_t)(sdiff * MILLISEC + udiff / MILLISEC));
5167c478bd9Sstevel@tonic-gate }
5177c478bd9Sstevel@tonic-gate
5187c478bd9Sstevel@tonic-gate int
pctx_run(pctx_t * pctx,uint_t msec,uint_t nsamples,int (* tick)(pctx_t *,pid_t,id_t,void *))5197c478bd9Sstevel@tonic-gate pctx_run(
5207c478bd9Sstevel@tonic-gate pctx_t *pctx,
5217c478bd9Sstevel@tonic-gate uint_t msec,
5227c478bd9Sstevel@tonic-gate uint_t nsamples,
5237c478bd9Sstevel@tonic-gate int (*tick)(pctx_t *, pid_t, id_t, void *))
5247c478bd9Sstevel@tonic-gate {
5257c478bd9Sstevel@tonic-gate static const char fn[] = "run";
5267c478bd9Sstevel@tonic-gate struct timeval tvgoal, tvnow;
5277c478bd9Sstevel@tonic-gate uint_t mswait = 0;
5287c478bd9Sstevel@tonic-gate int running = 1;
5297c478bd9Sstevel@tonic-gate const pstatus_t *pstatus;
5307c478bd9Sstevel@tonic-gate psinfo_t psinfo;
5317c478bd9Sstevel@tonic-gate void (*sigsaved)();
5327c478bd9Sstevel@tonic-gate id_t lwpid;
5337c478bd9Sstevel@tonic-gate pid_t pid = Pstatus(pctx->Pr)->pr_pid;
5347c478bd9Sstevel@tonic-gate int pstate;
5357c478bd9Sstevel@tonic-gate
5367c478bd9Sstevel@tonic-gate if (msec == 0)
5377c478bd9Sstevel@tonic-gate nsamples = 0;
5387c478bd9Sstevel@tonic-gate if (nsamples == 0)
5397c478bd9Sstevel@tonic-gate nsamples = UINT_MAX;
5407c478bd9Sstevel@tonic-gate
5417c478bd9Sstevel@tonic-gate /*
5427c478bd9Sstevel@tonic-gate * Casually discard any knowledge of the children we create
5437c478bd9Sstevel@tonic-gate */
5447c478bd9Sstevel@tonic-gate sigsaved = signal(SIGCHLD, SIG_IGN);
5457c478bd9Sstevel@tonic-gate
5467c478bd9Sstevel@tonic-gate /*
5477c478bd9Sstevel@tonic-gate * Since we've just "discovered" this process which might have
5487c478bd9Sstevel@tonic-gate * been running for weeks, deliver some init_lwp events so
5497c478bd9Sstevel@tonic-gate * that our caller gets a handle on the process.
5507c478bd9Sstevel@tonic-gate */
5517c478bd9Sstevel@tonic-gate if (pctx_lwpiterate(pctx, pctx->init_lwp) != 0) {
5527c478bd9Sstevel@tonic-gate if (pctx->verbose)
5537c478bd9Sstevel@tonic-gate pctx_error(pctx, fn,
5547c478bd9Sstevel@tonic-gate gettext("%d: lwp discovery failed\n"), (int)pid);
5557c478bd9Sstevel@tonic-gate goto bailout;
5567c478bd9Sstevel@tonic-gate }
5577c478bd9Sstevel@tonic-gate
5587c478bd9Sstevel@tonic-gate if (msec != 0) {
5597c478bd9Sstevel@tonic-gate /*
5607c478bd9Sstevel@tonic-gate * tvgoal represents the time at which the sample
5617c478bd9Sstevel@tonic-gate * should next be taken.
5627c478bd9Sstevel@tonic-gate */
5637c478bd9Sstevel@tonic-gate (void) gettimeofday(&tvgoal, 0);
5647c478bd9Sstevel@tonic-gate msincr(&tvgoal, msec);
5657c478bd9Sstevel@tonic-gate }
5667c478bd9Sstevel@tonic-gate
5677c3666b4Skk112340 /*
5687c3666b4Skk112340 * The event handling loop continues while running is 1.
5697c3666b4Skk112340 * running becomes 0 when either the controlled process has
5707c3666b4Skk112340 * exited successfully or the number of time samples has expired.
5717c3666b4Skk112340 * Otherwise, if an error has occurred, running becomes -1.
5727c3666b4Skk112340 */
573b885580bSAlexander Kolbasov while (running == 1 && !pctx->terminate) {
5747c478bd9Sstevel@tonic-gate
5757c478bd9Sstevel@tonic-gate if (Psetrun(pctx->Pr, 0, 0) != 0) {
5767c478bd9Sstevel@tonic-gate if (pctx->verbose)
5777c478bd9Sstevel@tonic-gate pctx_error(pctx, fn,
5787c478bd9Sstevel@tonic-gate gettext("%d: Psetrun\n"), (int)pid);
5797c478bd9Sstevel@tonic-gate break;
5807c478bd9Sstevel@tonic-gate }
5817c478bd9Sstevel@tonic-gate
5827c478bd9Sstevel@tonic-gate if (msec != 0) {
5837c478bd9Sstevel@tonic-gate /*
5847c478bd9Sstevel@tonic-gate * This timing loop attempts to estimate the number
5857c478bd9Sstevel@tonic-gate * of milliseconds between our "goal" time (when
5867c478bd9Sstevel@tonic-gate * we should stop the process and run the tick
5877c478bd9Sstevel@tonic-gate * routine) and the current time.
5887c478bd9Sstevel@tonic-gate *
5897c478bd9Sstevel@tonic-gate * If we ever find ourselves running behind i.e. we
5907c478bd9Sstevel@tonic-gate * missed our goal, then we skip ahead to the next
5917c478bd9Sstevel@tonic-gate * goal instead.
5927c478bd9Sstevel@tonic-gate */
5937c478bd9Sstevel@tonic-gate do {
5947c478bd9Sstevel@tonic-gate (void) gettimeofday(&tvnow, 0);
5957c478bd9Sstevel@tonic-gate if ((mswait = msdiff(&tvgoal, &tvnow)) == 0) {
5967c478bd9Sstevel@tonic-gate msincr(&tvgoal, msec);
5977c478bd9Sstevel@tonic-gate /*
5987c478bd9Sstevel@tonic-gate * Skip ahead to the next goal, unless
5997c478bd9Sstevel@tonic-gate * there is only one more sample left
6007c478bd9Sstevel@tonic-gate * to take.
6017c478bd9Sstevel@tonic-gate */
6027c478bd9Sstevel@tonic-gate if (nsamples != 1)
6037c478bd9Sstevel@tonic-gate nsamples--;
6047c478bd9Sstevel@tonic-gate }
605b885580bSAlexander Kolbasov } while (mswait == 0 && !pctx->terminate);
6067c478bd9Sstevel@tonic-gate }
6077c478bd9Sstevel@tonic-gate
608b885580bSAlexander Kolbasov if (pctx->terminate)
609b885580bSAlexander Kolbasov goto bailout;
610b885580bSAlexander Kolbasov else
6117c478bd9Sstevel@tonic-gate (void) Pwait(pctx->Pr, mswait);
6127c478bd9Sstevel@tonic-gate
6137c478bd9Sstevel@tonic-gate checkstate:
6147c478bd9Sstevel@tonic-gate switch (pstate = Pstate(pctx->Pr)) {
6157c478bd9Sstevel@tonic-gate case PS_RUN:
6167c478bd9Sstevel@tonic-gate /*
6177c478bd9Sstevel@tonic-gate * Try again, but wait for up to 5 seconds.
6187c478bd9Sstevel@tonic-gate */
6197c478bd9Sstevel@tonic-gate if (Pstop(pctx->Pr, 5 * MILLISEC) == -1 ||
6207c478bd9Sstevel@tonic-gate (pstate = Pstate(pctx->Pr)) != PS_STOP) {
6217c478bd9Sstevel@tonic-gate pctx_error(pctx, fn,
6227c478bd9Sstevel@tonic-gate gettext("%d: won't stop\n"), (int)pid);
6237c478bd9Sstevel@tonic-gate }
6247c478bd9Sstevel@tonic-gate break;
6257c478bd9Sstevel@tonic-gate case PS_STOP:
6267c478bd9Sstevel@tonic-gate break;
6277c478bd9Sstevel@tonic-gate case PS_LOST:
6287c478bd9Sstevel@tonic-gate /*
6297c478bd9Sstevel@tonic-gate * Lost control - probably execed a setuid/setgid
6307c478bd9Sstevel@tonic-gate * executable. Try and get control back again,
6317c478bd9Sstevel@tonic-gate * else bail ..
6327c478bd9Sstevel@tonic-gate */
6337c478bd9Sstevel@tonic-gate (void) Preopen(pctx->Pr);
6347c478bd9Sstevel@tonic-gate if ((pstate = Pstate(pctx->Pr)) != PS_LOST)
6357c478bd9Sstevel@tonic-gate goto checkstate;
6367c478bd9Sstevel@tonic-gate pctx_error(pctx, fn,
6377c478bd9Sstevel@tonic-gate gettext("%d: execed a program that cannot "
6387c478bd9Sstevel@tonic-gate "be tracked\n"), (int)pid);
6397c3666b4Skk112340 running = -1;
6407c478bd9Sstevel@tonic-gate break;
6417c478bd9Sstevel@tonic-gate case PS_UNDEAD:
6427c478bd9Sstevel@tonic-gate case PS_DEAD:
6437c478bd9Sstevel@tonic-gate if (pctx->verbose)
6447c478bd9Sstevel@tonic-gate pctx_error(pctx, fn,
6457c478bd9Sstevel@tonic-gate gettext("%d: process terminated\n"),
6467c478bd9Sstevel@tonic-gate (int)pid);
6477c3666b4Skk112340 running = -1;
6487c478bd9Sstevel@tonic-gate break;
6497c478bd9Sstevel@tonic-gate default:
6507c478bd9Sstevel@tonic-gate if (pctx->verbose)
6517c478bd9Sstevel@tonic-gate pctx_error(pctx, fn,
6527c478bd9Sstevel@tonic-gate gettext("%d: process state 0x%x?\n"),
6537c478bd9Sstevel@tonic-gate (int)pid, pstate);
6547c478bd9Sstevel@tonic-gate break;
6557c478bd9Sstevel@tonic-gate }
6567c478bd9Sstevel@tonic-gate
6577c478bd9Sstevel@tonic-gate if (pstate != PS_STOP)
6587c478bd9Sstevel@tonic-gate break;
6597c478bd9Sstevel@tonic-gate
6607c478bd9Sstevel@tonic-gate pstatus = Pstatus(pctx->Pr);
6617c478bd9Sstevel@tonic-gate lwpid = pstatus->pr_lwp.pr_lwpid;
6627c478bd9Sstevel@tonic-gate switch (pstatus->pr_lwp.pr_why) {
6637c478bd9Sstevel@tonic-gate case PR_REQUESTED:
6647c478bd9Sstevel@tonic-gate msincr(&tvgoal, msec);
6657c478bd9Sstevel@tonic-gate if (pstatus->pr_flags & PR_VFORKP) {
6667c478bd9Sstevel@tonic-gate /*
6677c478bd9Sstevel@tonic-gate * The process is in a vfork stupor until
6687c478bd9Sstevel@tonic-gate * its child releases it via an exec.
6697c478bd9Sstevel@tonic-gate * Don't sample it while it's in this state
6707c478bd9Sstevel@tonic-gate * - we won't be able to create the agent.
6717c478bd9Sstevel@tonic-gate */
6727c478bd9Sstevel@tonic-gate break;
6737c478bd9Sstevel@tonic-gate }
6747c478bd9Sstevel@tonic-gate if (pctx_lwpiterate(pctx, tick) != 0)
6757c3666b4Skk112340 running = -1;
6767c3666b4Skk112340 if (running == 1 && --nsamples == 0)
6777c478bd9Sstevel@tonic-gate running = 0;
6787c478bd9Sstevel@tonic-gate break;
6797c478bd9Sstevel@tonic-gate case PR_SYSENTRY:
6807c478bd9Sstevel@tonic-gate switch (pstatus->pr_lwp.pr_what) {
6817c478bd9Sstevel@tonic-gate case SYS_lwp_exit:
6827c478bd9Sstevel@tonic-gate pctx_begin_syscalls(pctx);
6837c478bd9Sstevel@tonic-gate (void) pctx->fini_lwp(pctx,
6847c478bd9Sstevel@tonic-gate pid, lwpid, pctx->uarg);
6857c478bd9Sstevel@tonic-gate (void) pctx->lwp_exit(pctx,
6867c478bd9Sstevel@tonic-gate pid, lwpid, pctx->uarg);
6877c478bd9Sstevel@tonic-gate pctx_end_syscalls(pctx);
6887c478bd9Sstevel@tonic-gate break;
6897c478bd9Sstevel@tonic-gate case SYS_exit:
6907c3666b4Skk112340 if (pctx_lwpiterate(pctx, pctx->fini_lwp)
6917c3666b4Skk112340 != 0)
6927c3666b4Skk112340 running = -1;
6937c478bd9Sstevel@tonic-gate pctx->exit(pctx, pid, lwpid,
6947c478bd9Sstevel@tonic-gate (int)pstatus->pr_lwp.pr_sysarg[0],
6957c478bd9Sstevel@tonic-gate pctx->uarg);
6967c3666b4Skk112340 if (running == 1)
6977c478bd9Sstevel@tonic-gate running = 0;
6987c478bd9Sstevel@tonic-gate break;
6997c478bd9Sstevel@tonic-gate case SYS_execve:
7007c478bd9Sstevel@tonic-gate (void) pctx_lwpiterate(pctx, pctx->fini_lwp);
7017c478bd9Sstevel@tonic-gate break;
7027c478bd9Sstevel@tonic-gate default:
7037c478bd9Sstevel@tonic-gate pctx_error(pctx, fn,
7047c478bd9Sstevel@tonic-gate "warning - pid %d sysentry(%d)\n",
7057c478bd9Sstevel@tonic-gate (int)pid, pstatus->pr_lwp.pr_what);
7067c478bd9Sstevel@tonic-gate break;
7077c478bd9Sstevel@tonic-gate }
7087c478bd9Sstevel@tonic-gate break;
7097c478bd9Sstevel@tonic-gate case PR_SYSEXIT:
7107c478bd9Sstevel@tonic-gate switch (pstatus->pr_lwp.pr_what) {
7117c478bd9Sstevel@tonic-gate case SYS_execve:
7127c478bd9Sstevel@tonic-gate if (pstatus->pr_lwp.pr_errno) {
7137c478bd9Sstevel@tonic-gate /*
7147c478bd9Sstevel@tonic-gate * The exec failed completely.
7157c478bd9Sstevel@tonic-gate * Reinstate the lwps we fini'd
7167c478bd9Sstevel@tonic-gate * at exec entrance
7177c478bd9Sstevel@tonic-gate */
7187c3666b4Skk112340 if (pctx_lwpiterate(pctx,
7197c3666b4Skk112340 pctx->init_lwp) == 0)
7207c3666b4Skk112340 running = 1;
7217c3666b4Skk112340 else
7227c3666b4Skk112340 running = -1;
7237c478bd9Sstevel@tonic-gate break;
7247c478bd9Sstevel@tonic-gate }
7257c478bd9Sstevel@tonic-gate if (pctx->exec == (pctx_sysc_execfn_t *)
7267c478bd9Sstevel@tonic-gate default_int) {
7277c478bd9Sstevel@tonic-gate running = 0;
7287c478bd9Sstevel@tonic-gate break;
7297c478bd9Sstevel@tonic-gate }
7307c478bd9Sstevel@tonic-gate (void) memcpy(&psinfo,
7317c478bd9Sstevel@tonic-gate Ppsinfo(pctx->Pr), sizeof (psinfo));
7327c478bd9Sstevel@tonic-gate proc_unctrl_psinfo(&psinfo);
7337c478bd9Sstevel@tonic-gate pctx_begin_syscalls(pctx);
7347c3666b4Skk112340 if (pctx->exec(pctx, pid, lwpid,
7357c3666b4Skk112340 psinfo.pr_psargs, pctx->uarg) != 0)
7367c3666b4Skk112340 running = -1;
7377c3666b4Skk112340 if (running == 1 && pctx->init_lwp(pctx,
7387c3666b4Skk112340 pid, 1, pctx->uarg) != 0)
7397c3666b4Skk112340 running = -1;
7407c478bd9Sstevel@tonic-gate pctx_end_syscalls(pctx);
7417c478bd9Sstevel@tonic-gate break;
7427c478bd9Sstevel@tonic-gate case SYS_lwp_create:
7437c478bd9Sstevel@tonic-gate if (pstatus->pr_lwp.pr_errno ||
7447c478bd9Sstevel@tonic-gate pstatus->pr_lwp.pr_rval1)
7457c478bd9Sstevel@tonic-gate break;
7467c478bd9Sstevel@tonic-gate pctx_begin_syscalls(pctx);
7477c3666b4Skk112340 if (pctx->init_lwp(pctx, pid, lwpid,
7487c3666b4Skk112340 pctx->uarg) != 0)
7497c3666b4Skk112340 running = -1;
7507c3666b4Skk112340 if (running == 1 && pctx->lwp_create(pctx,
7517c3666b4Skk112340 pid, lwpid, pctx->uarg) != 0)
7527c3666b4Skk112340 running = -1;
7537c478bd9Sstevel@tonic-gate pctx_end_syscalls(pctx);
7547c478bd9Sstevel@tonic-gate break;
7557c478bd9Sstevel@tonic-gate case SYS_vfork:
756657b1f3dSraf case SYS_forksys:
7577c478bd9Sstevel@tonic-gate if (pstatus->pr_lwp.pr_errno)
7587c478bd9Sstevel@tonic-gate break;
7597c478bd9Sstevel@tonic-gate (void) fflush(NULL);
7607c478bd9Sstevel@tonic-gate switch (fork1()) {
7617c478bd9Sstevel@tonic-gate pid_t ppid;
7627c478bd9Sstevel@tonic-gate int wascreated;
7637c478bd9Sstevel@tonic-gate pctx_sysc_forkfn_t *forkfn;
7647c478bd9Sstevel@tonic-gate case 0:
7657c478bd9Sstevel@tonic-gate ppid = pid;
7667c478bd9Sstevel@tonic-gate pid = pstatus->pr_lwp.pr_rval1;
7677c478bd9Sstevel@tonic-gate wascreated = pctx->created;
7687c478bd9Sstevel@tonic-gate forkfn = pctx->fork;
7697c478bd9Sstevel@tonic-gate pctx_free(pctx);
7707c478bd9Sstevel@tonic-gate pctx = pctx_capture(pid, pctx->uarg,
7717c478bd9Sstevel@tonic-gate pctx->verbose, pctx->errfn);
7727c478bd9Sstevel@tonic-gate if (pctx != NULL) {
7737c478bd9Sstevel@tonic-gate if (wascreated) {
7747c478bd9Sstevel@tonic-gate /*
7757c478bd9Sstevel@tonic-gate * Set kill on last
7767c478bd9Sstevel@tonic-gate * close so -all-
7777c478bd9Sstevel@tonic-gate * children die.
7787c478bd9Sstevel@tonic-gate */
7797c478bd9Sstevel@tonic-gate pctx->created = 1;
7807c478bd9Sstevel@tonic-gate (void) Psetflags(
7817c478bd9Sstevel@tonic-gate pctx->Pr, PR_KLC);
7827c478bd9Sstevel@tonic-gate }
7837c478bd9Sstevel@tonic-gate (*forkfn)(pctx, ppid, pid,
7847c478bd9Sstevel@tonic-gate lwpid, pctx->uarg);
7857c478bd9Sstevel@tonic-gate pctx_release(pctx);
7867c478bd9Sstevel@tonic-gate _exit(0);
7877c3666b4Skk112340 } else {
7887c3666b4Skk112340 _exit(1);
7897c3666b4Skk112340 }
7907c478bd9Sstevel@tonic-gate /*NOTREACHED*/
7917c478bd9Sstevel@tonic-gate case -1:
7927c478bd9Sstevel@tonic-gate pctx_error(pctx, fn,
7937c478bd9Sstevel@tonic-gate "cannot follow pid %d: %s\n",
7947c478bd9Sstevel@tonic-gate (int)pstatus->pr_lwp.pr_rval1,
7957c478bd9Sstevel@tonic-gate strerror(errno));
7967c478bd9Sstevel@tonic-gate break;
7977c478bd9Sstevel@tonic-gate default:
7987c478bd9Sstevel@tonic-gate break;
7997c478bd9Sstevel@tonic-gate }
8007c478bd9Sstevel@tonic-gate break;
8017c478bd9Sstevel@tonic-gate default:
8027c478bd9Sstevel@tonic-gate pctx_error(pctx, fn, gettext(
8037c478bd9Sstevel@tonic-gate "warning - pid %d sysexit(%d)\n"),
8047c478bd9Sstevel@tonic-gate (int)pid, pstatus->pr_lwp.pr_what);
8057c478bd9Sstevel@tonic-gate break;
8067c478bd9Sstevel@tonic-gate }
8077c478bd9Sstevel@tonic-gate break;
8087c478bd9Sstevel@tonic-gate case PR_SIGNALLED:
8097c478bd9Sstevel@tonic-gate if (pctx->verbose)
8107c478bd9Sstevel@tonic-gate pctx_error(pctx, fn,
8117c478bd9Sstevel@tonic-gate gettext("pid %d - signalled\n"), (int)pid);
8127c478bd9Sstevel@tonic-gate break;
8137c478bd9Sstevel@tonic-gate case PR_JOBCONTROL:
8147c478bd9Sstevel@tonic-gate if (pctx->verbose)
8157c478bd9Sstevel@tonic-gate pctx_error(pctx, fn,
8167c478bd9Sstevel@tonic-gate gettext("pid %d - job control stop\n"),
8177c478bd9Sstevel@tonic-gate (int)pid);
8187c3666b4Skk112340 running = -1;
8197c478bd9Sstevel@tonic-gate break;
8207c478bd9Sstevel@tonic-gate case PR_FAULTED:
8217c478bd9Sstevel@tonic-gate if (pctx->verbose)
8227c478bd9Sstevel@tonic-gate pctx_error(pctx, fn,
8237c478bd9Sstevel@tonic-gate gettext("pid %d - faulted\n"), (int)pid);
8247c478bd9Sstevel@tonic-gate break;
8257c478bd9Sstevel@tonic-gate case PR_SUSPENDED:
8267c478bd9Sstevel@tonic-gate if (pctx->verbose)
8277c478bd9Sstevel@tonic-gate pctx_error(pctx, fn,
8287c478bd9Sstevel@tonic-gate gettext("pid %d - suspended\n"), (int)pid);
8297c478bd9Sstevel@tonic-gate break;
8307c478bd9Sstevel@tonic-gate case PR_CHECKPOINT:
8317c478bd9Sstevel@tonic-gate if (pctx->verbose)
8327c478bd9Sstevel@tonic-gate pctx_error(pctx, fn,
8337c478bd9Sstevel@tonic-gate gettext("pid %d - checkpoint\n"),
8347c478bd9Sstevel@tonic-gate (int)pid);
8357c478bd9Sstevel@tonic-gate break;
8367c478bd9Sstevel@tonic-gate default:
8377c478bd9Sstevel@tonic-gate if (pctx->verbose)
8387c478bd9Sstevel@tonic-gate pctx_error(pctx, fn,
8397c478bd9Sstevel@tonic-gate gettext("pid %d - reason %d\n"),
8407c478bd9Sstevel@tonic-gate (int)pid, pstatus->pr_lwp.pr_why);
8417c3666b4Skk112340 running = -1;
8427c478bd9Sstevel@tonic-gate break;
8437c478bd9Sstevel@tonic-gate }
8447c478bd9Sstevel@tonic-gate }
8457c478bd9Sstevel@tonic-gate
8467c478bd9Sstevel@tonic-gate bailout:
8477c478bd9Sstevel@tonic-gate (void) signal(SIGCHLD, sigsaved);
8487c478bd9Sstevel@tonic-gate
849b885580bSAlexander Kolbasov if (pctx->terminate)
850b885580bSAlexander Kolbasov return (0);
851b885580bSAlexander Kolbasov
8527c3666b4Skk112340 switch (running) {
8537c3666b4Skk112340 case 0:
8547c3666b4Skk112340 return (0);
8557c3666b4Skk112340 case -1:
8567c3666b4Skk112340 return (-1);
8577c3666b4Skk112340 default:
8587c3666b4Skk112340 pctx_error(pctx, fn, gettext("lost control of pid %d\n"),
8597c3666b4Skk112340 (int)pid);
8607c478bd9Sstevel@tonic-gate pctx_free(pctx);
8617c478bd9Sstevel@tonic-gate return (-1);
8627c478bd9Sstevel@tonic-gate }
8637c3666b4Skk112340 }
8647c478bd9Sstevel@tonic-gate
8657c478bd9Sstevel@tonic-gate /*
8667c478bd9Sstevel@tonic-gate * Execute the private 'cpc' system call in the context of the
8677c478bd9Sstevel@tonic-gate * controlled process.
8687c478bd9Sstevel@tonic-gate */
8697c478bd9Sstevel@tonic-gate int
__pctx_cpc(pctx_t * pctx,cpc_t * cpc,int cmd,id_t lwpid,void * data1,void * data2,void * data3,int bufsize)8707c478bd9Sstevel@tonic-gate __pctx_cpc(pctx_t *pctx, cpc_t *cpc,
8717c478bd9Sstevel@tonic-gate int cmd, id_t lwpid, void *data1, void *data2, void *data3, int bufsize)
8727c478bd9Sstevel@tonic-gate {
8737c478bd9Sstevel@tonic-gate sysret_t rval;
8747c478bd9Sstevel@tonic-gate argdes_t argd[5];
8757c478bd9Sstevel@tonic-gate argdes_t *adp = &argd[0];
8767c478bd9Sstevel@tonic-gate int error;
8777c478bd9Sstevel@tonic-gate
8787c478bd9Sstevel@tonic-gate /*
8797c478bd9Sstevel@tonic-gate * Keep track of the relationship between cpc_t and pctx_t here.
8807c478bd9Sstevel@tonic-gate * We store the last cpc_t used by libpctx, so that when this pctx is
8817c478bd9Sstevel@tonic-gate * destroyed, libpctx can notify libcpc.
8827c478bd9Sstevel@tonic-gate */
883b885580bSAlexander Kolbasov
8847c478bd9Sstevel@tonic-gate if (pctx->cpc != NULL && pctx->cpc != cpc && pctx_cpc_callback != NULL)
8857c478bd9Sstevel@tonic-gate (*pctx_cpc_callback)(pctx->cpc, pctx);
8867c478bd9Sstevel@tonic-gate pctx->cpc = cpc;
8877c478bd9Sstevel@tonic-gate
8887c478bd9Sstevel@tonic-gate /*
8897c478bd9Sstevel@tonic-gate * cmd and lwpid are passed in by value no matter what the command is.
8907c478bd9Sstevel@tonic-gate */
8917c478bd9Sstevel@tonic-gate adp->arg_value = cmd;
8927c478bd9Sstevel@tonic-gate adp->arg_object = NULL;
8937c478bd9Sstevel@tonic-gate adp->arg_type = AT_BYVAL;
8947c478bd9Sstevel@tonic-gate adp->arg_inout = AI_INPUT;
8957c478bd9Sstevel@tonic-gate adp->arg_size = 0;
8967c478bd9Sstevel@tonic-gate adp++;
8977c478bd9Sstevel@tonic-gate
8987c478bd9Sstevel@tonic-gate adp->arg_value = lwpid;
8997c478bd9Sstevel@tonic-gate adp->arg_object = NULL;
9007c478bd9Sstevel@tonic-gate adp->arg_type = AT_BYVAL;
9017c478bd9Sstevel@tonic-gate adp->arg_inout = AI_INPUT;
9027c478bd9Sstevel@tonic-gate adp->arg_size = 0;
9037c478bd9Sstevel@tonic-gate adp++;
9047c478bd9Sstevel@tonic-gate
9057c478bd9Sstevel@tonic-gate switch (cmd) {
9067c478bd9Sstevel@tonic-gate case CPC_BIND:
9077c478bd9Sstevel@tonic-gate adp->arg_value = 0;
9087c478bd9Sstevel@tonic-gate adp->arg_object = data1;
9097c478bd9Sstevel@tonic-gate adp->arg_type = AT_BYREF;
9107c478bd9Sstevel@tonic-gate adp->arg_inout = AI_INPUT;
9117c478bd9Sstevel@tonic-gate adp->arg_size = (size_t)data2;
9127c478bd9Sstevel@tonic-gate adp++;
9137c478bd9Sstevel@tonic-gate
9147c478bd9Sstevel@tonic-gate adp->arg_value = (size_t)data2;
9157c478bd9Sstevel@tonic-gate adp->arg_object = NULL;
9167c478bd9Sstevel@tonic-gate adp->arg_type = AT_BYVAL;
9177c478bd9Sstevel@tonic-gate adp->arg_inout = AI_INPUT;
9187c478bd9Sstevel@tonic-gate adp->arg_size = 0;
9197c478bd9Sstevel@tonic-gate adp++;
9207c478bd9Sstevel@tonic-gate
9217c478bd9Sstevel@tonic-gate adp->arg_value = 0;
9227c478bd9Sstevel@tonic-gate adp->arg_object = data3;
9237c478bd9Sstevel@tonic-gate adp->arg_type = AT_BYREF;
9247c478bd9Sstevel@tonic-gate adp->arg_inout = AI_INOUT;
9257c478bd9Sstevel@tonic-gate adp->arg_size = sizeof (int);
9267c478bd9Sstevel@tonic-gate
9277c478bd9Sstevel@tonic-gate break;
9287c478bd9Sstevel@tonic-gate case CPC_SAMPLE:
9297c478bd9Sstevel@tonic-gate adp->arg_value = 0;
9307c478bd9Sstevel@tonic-gate adp->arg_object = data1;
9317c478bd9Sstevel@tonic-gate adp->arg_type = AT_BYREF;
9327c478bd9Sstevel@tonic-gate adp->arg_inout = AI_OUTPUT;
9337c478bd9Sstevel@tonic-gate adp->arg_size = bufsize;
9347c478bd9Sstevel@tonic-gate adp++;
9357c478bd9Sstevel@tonic-gate
9367c478bd9Sstevel@tonic-gate adp->arg_value = 0;
9377c478bd9Sstevel@tonic-gate adp->arg_object = data2;
9387c478bd9Sstevel@tonic-gate adp->arg_type = AT_BYREF;
9397c478bd9Sstevel@tonic-gate adp->arg_inout = AI_OUTPUT;
9407c478bd9Sstevel@tonic-gate adp->arg_size = sizeof (hrtime_t);
9417c478bd9Sstevel@tonic-gate adp++;
9427c478bd9Sstevel@tonic-gate
9437c478bd9Sstevel@tonic-gate adp->arg_value = 0;
9447c478bd9Sstevel@tonic-gate adp->arg_object = data3;
9457c478bd9Sstevel@tonic-gate adp->arg_type = AT_BYREF;
9467c478bd9Sstevel@tonic-gate adp->arg_inout = AI_OUTPUT;
9477c478bd9Sstevel@tonic-gate adp->arg_size = sizeof (uint64_t);
9487c478bd9Sstevel@tonic-gate
9497c478bd9Sstevel@tonic-gate break;
9507c478bd9Sstevel@tonic-gate default:
9517c478bd9Sstevel@tonic-gate adp->arg_value = 0;
9527c478bd9Sstevel@tonic-gate adp->arg_object = 0;
9537c478bd9Sstevel@tonic-gate adp->arg_type = AT_BYVAL;
9547c478bd9Sstevel@tonic-gate adp->arg_inout = AI_INPUT;
9557c478bd9Sstevel@tonic-gate adp->arg_size = 0;
9567c478bd9Sstevel@tonic-gate adp++;
9577c478bd9Sstevel@tonic-gate
9587c478bd9Sstevel@tonic-gate adp->arg_value = 0;
9597c478bd9Sstevel@tonic-gate adp->arg_object = 0;
9607c478bd9Sstevel@tonic-gate adp->arg_type = AT_BYVAL;
9617c478bd9Sstevel@tonic-gate adp->arg_inout = AI_INPUT;
9627c478bd9Sstevel@tonic-gate adp->arg_size = 0;
9637c478bd9Sstevel@tonic-gate adp++;
9647c478bd9Sstevel@tonic-gate
9657c478bd9Sstevel@tonic-gate adp->arg_value = 0;
9667c478bd9Sstevel@tonic-gate adp->arg_object = 0;
9677c478bd9Sstevel@tonic-gate adp->arg_type = AT_BYVAL;
9687c478bd9Sstevel@tonic-gate adp->arg_inout = AI_INPUT;
9697c478bd9Sstevel@tonic-gate adp->arg_size = 0;
9707c478bd9Sstevel@tonic-gate
9717c478bd9Sstevel@tonic-gate break;
9727c478bd9Sstevel@tonic-gate }
9737c478bd9Sstevel@tonic-gate
9747c478bd9Sstevel@tonic-gate error = Psyscall(pctx->Pr, &rval, SYS_cpc, 5, &argd[0]);
9757c478bd9Sstevel@tonic-gate
9767c478bd9Sstevel@tonic-gate if (error) {
9777c478bd9Sstevel@tonic-gate errno = error > 0 ? error : ENOSYS;
9787c478bd9Sstevel@tonic-gate return (-1);
9797c478bd9Sstevel@tonic-gate }
9807c478bd9Sstevel@tonic-gate return (rval.sys_rval1);
9817c478bd9Sstevel@tonic-gate }
9827c478bd9Sstevel@tonic-gate
9837c478bd9Sstevel@tonic-gate /*
9847c478bd9Sstevel@tonic-gate * libcpc-private hook used to register a callback. The callback is used to
9857c478bd9Sstevel@tonic-gate * notify libcpc when a pctx handle is invalidated.
9867c478bd9Sstevel@tonic-gate */
9877c478bd9Sstevel@tonic-gate void
__pctx_cpc_register_callback(void (* arg)(struct __cpc *,struct __pctx *))9887c478bd9Sstevel@tonic-gate __pctx_cpc_register_callback(void (*arg)(struct __cpc *, struct __pctx *))
9897c478bd9Sstevel@tonic-gate {
9907c478bd9Sstevel@tonic-gate pctx_cpc_callback = arg;
9917c478bd9Sstevel@tonic-gate }
992b885580bSAlexander Kolbasov
993b885580bSAlexander Kolbasov /*
994b885580bSAlexander Kolbasov * Tell pctx_run to bail out immediately
995b885580bSAlexander Kolbasov */
996b885580bSAlexander Kolbasov void
pctx_terminate(struct __pctx * pctx)997b885580bSAlexander Kolbasov pctx_terminate(struct __pctx *pctx)
998b885580bSAlexander Kolbasov {
999b885580bSAlexander Kolbasov pctx->terminate = 1;
1000b885580bSAlexander Kolbasov }
1001