xref: /titanic_53/usr/src/lib/libpctx/common/libpctx.c (revision b885580b43755ee4ea1e280b85428893d2ba9291)
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*b885580bSAlexander Kolbasov  * Copyright 2009 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;
67*b885580bSAlexander 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
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
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 *
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;
110*b885580bSAlexander 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 *
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
2407c478bd9Sstevel@tonic-gate default_void(pctx_t *pctx)
2417c478bd9Sstevel@tonic-gate {}
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2447c478bd9Sstevel@tonic-gate static int
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
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_forkall, 1);
3207c478bd9Sstevel@tonic-gate 		(void) Psysexit(pctx->Pr, SYS_vfork, 1);
3217c478bd9Sstevel@tonic-gate 		(void) Psysexit(pctx->Pr, SYS_fork1, 1);
322657b1f3dSraf 		(void) Psysexit(pctx->Pr, SYS_forksys, 1);
3237c478bd9Sstevel@tonic-gate 		if (Psetflags(pctx->Pr, PR_FORK) == -1)
3247c478bd9Sstevel@tonic-gate 			error = -1;
3257c478bd9Sstevel@tonic-gate 	} else {
3267c478bd9Sstevel@tonic-gate 		(void) Psysexit(pctx->Pr, SYS_forkall, 0);
3277c478bd9Sstevel@tonic-gate 		(void) Psysexit(pctx->Pr, SYS_vfork, 0);
3287c478bd9Sstevel@tonic-gate 		(void) Psysexit(pctx->Pr, SYS_fork1, 0);
329657b1f3dSraf 		(void) Psysexit(pctx->Pr, SYS_forksys, 0);
3307c478bd9Sstevel@tonic-gate 		if (Punsetflags(pctx->Pr, PR_FORK) == -1)
3317c478bd9Sstevel@tonic-gate 			error = -1;
3327c478bd9Sstevel@tonic-gate 	}
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	/*
3357c478bd9Sstevel@tonic-gate 	 * exec causes termination of all but the exec-ing lwp,
3367c478bd9Sstevel@tonic-gate 	 * and resets the lwpid to one in the new address space.
3377c478bd9Sstevel@tonic-gate 	 */
3387c478bd9Sstevel@tonic-gate 	if (pctx->exec != (pctx_sysc_execfn_t *)default_int ||
3397c478bd9Sstevel@tonic-gate 	    pctx->fini_lwp != (pctx_fini_lwpfn_t *)default_int ||
3407c478bd9Sstevel@tonic-gate 	    pctx->init_lwp != (pctx_init_lwpfn_t *)default_int) {
3417c478bd9Sstevel@tonic-gate 		(void) Psysexit(pctx->Pr, SYS_exec, 1);
3427c478bd9Sstevel@tonic-gate 		(void) Psysexit(pctx->Pr, SYS_execve, 1);
3437c478bd9Sstevel@tonic-gate 		(void) Psysentry(pctx->Pr, SYS_exec, 1);
3447c478bd9Sstevel@tonic-gate 		(void) Psysentry(pctx->Pr, SYS_execve, 1);
3457c478bd9Sstevel@tonic-gate 	} else {
3467c478bd9Sstevel@tonic-gate 		(void) Psysexit(pctx->Pr, SYS_exec, 0);
3477c478bd9Sstevel@tonic-gate 		(void) Psysexit(pctx->Pr, SYS_execve, 0);
3487c478bd9Sstevel@tonic-gate 		(void) Psysentry(pctx->Pr, SYS_exec, 0);
3497c478bd9Sstevel@tonic-gate 		(void) Psysentry(pctx->Pr, SYS_execve, 0);
3507c478bd9Sstevel@tonic-gate 	}
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	(void) Psysexit(pctx->Pr, SYS_lwp_create,
3537c478bd9Sstevel@tonic-gate 	    pctx->lwp_create != (pctx_sysc_lwp_createfn_t *)default_int ||
3547c478bd9Sstevel@tonic-gate 	    pctx->init_lwp != (pctx_init_lwpfn_t *)default_int);
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	(void) Psysentry(pctx->Pr, SYS_lwp_exit,
3577c478bd9Sstevel@tonic-gate 	    pctx->lwp_exit != (pctx_sysc_lwp_exitfn_t *)default_int ||
3587c478bd9Sstevel@tonic-gate 	    pctx->fini_lwp != (pctx_fini_lwpfn_t *)default_int);
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	return (0);
3617c478bd9Sstevel@tonic-gate }
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate static sigset_t termsig;
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate static void
3667c478bd9Sstevel@tonic-gate __libpctx_init(void)
3677c478bd9Sstevel@tonic-gate {
3687c478bd9Sstevel@tonic-gate 	/*
3697c478bd9Sstevel@tonic-gate 	 * Initialize the signal set used to shield ourselves from
3707c478bd9Sstevel@tonic-gate 	 * death-by-terminal-signal while the agent lwp is running.
3717c478bd9Sstevel@tonic-gate 	 */
3727c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&termsig);
3737c478bd9Sstevel@tonic-gate 	(void) sigaddset(&termsig, SIGHUP);
3747c478bd9Sstevel@tonic-gate 	(void) sigaddset(&termsig, SIGTERM);
3757c478bd9Sstevel@tonic-gate 	(void) sigaddset(&termsig, SIGINT);
3767c478bd9Sstevel@tonic-gate 	(void) sigaddset(&termsig, SIGQUIT);
3777c478bd9Sstevel@tonic-gate }
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate #pragma init(__libpctx_init)
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate static void
3827c478bd9Sstevel@tonic-gate pctx_begin_syscalls(pctx_t *pctx)
3837c478bd9Sstevel@tonic-gate {
3847c478bd9Sstevel@tonic-gate 	if (pctx->Pr == NULL)
3857c478bd9Sstevel@tonic-gate 		return;
3867c478bd9Sstevel@tonic-gate 	if (pctx->sigblocked++ == 0) {
3877c478bd9Sstevel@tonic-gate 		(void) sigprocmask(SIG_BLOCK, &termsig, &pctx->savedset);
3887c478bd9Sstevel@tonic-gate 		(void) Pcreate_agent(pctx->Pr);
3897c478bd9Sstevel@tonic-gate 	}
3907c478bd9Sstevel@tonic-gate }
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate static void
3937c478bd9Sstevel@tonic-gate pctx_end_syscalls(pctx_t *pctx)
3947c478bd9Sstevel@tonic-gate {
3957c478bd9Sstevel@tonic-gate 	if (pctx->Pr == NULL)
3967c478bd9Sstevel@tonic-gate 		return;
3977c478bd9Sstevel@tonic-gate 	if (--pctx->sigblocked == 0) {
3987c478bd9Sstevel@tonic-gate 		(void) Pdestroy_agent(pctx->Pr);
3997c478bd9Sstevel@tonic-gate 		(void) sigprocmask(SIG_SETMASK, &pctx->savedset, NULL);
4007c478bd9Sstevel@tonic-gate 	}
4017c478bd9Sstevel@tonic-gate }
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate /*
4047c478bd9Sstevel@tonic-gate  * Iterate over the valid lwpids in the process, invoking the
4057c478bd9Sstevel@tonic-gate  * action function on each one.
4067c478bd9Sstevel@tonic-gate  */
4077c478bd9Sstevel@tonic-gate static int
4087c478bd9Sstevel@tonic-gate pctx_lwpiterate(pctx_t *pctx, int (*action)(pctx_t *, pid_t, id_t, void *))
4097c478bd9Sstevel@tonic-gate {
4107c478bd9Sstevel@tonic-gate 	const pstatus_t *pstatus;
4117c478bd9Sstevel@tonic-gate 	char lstatus[64];
4127c478bd9Sstevel@tonic-gate 	struct stat statb;
4137c478bd9Sstevel@tonic-gate 	lwpstatus_t *lwps;
4147c478bd9Sstevel@tonic-gate 	prheader_t *prh;
4157c478bd9Sstevel@tonic-gate 	int fd, nlwp;
4167c478bd9Sstevel@tonic-gate 	int ret = 0;
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	if (action == (int (*)(pctx_t *, pid_t, id_t, void *))default_int)
4197c478bd9Sstevel@tonic-gate 		return (0);
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 	pstatus = Pstatus(pctx->Pr);
4227c478bd9Sstevel@tonic-gate 	if (pstatus->pr_nlwp <= 1) {
4237c478bd9Sstevel@tonic-gate 		pctx_begin_syscalls(pctx);
4247c478bd9Sstevel@tonic-gate 		ret = action(pctx, pstatus->pr_pid, 1, pctx->uarg);
4257c478bd9Sstevel@tonic-gate 		pctx_end_syscalls(pctx);
4267c478bd9Sstevel@tonic-gate 		return (ret);
4277c478bd9Sstevel@tonic-gate 	}
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	(void) snprintf(lstatus, sizeof (lstatus),
4307c478bd9Sstevel@tonic-gate 	    "/proc/%d/lstatus", (int)pstatus->pr_pid);
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 	if ((fd = open(lstatus, O_RDONLY)) < 0 ||
4337c478bd9Sstevel@tonic-gate 	    fstat(fd, &statb) != 0) {
4347c478bd9Sstevel@tonic-gate 		if (fd >= 0)
4357c478bd9Sstevel@tonic-gate 			(void) close(fd);
4367c478bd9Sstevel@tonic-gate 		return (-1);
4377c478bd9Sstevel@tonic-gate 	}
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 	prh = malloc(statb.st_size);
4407c478bd9Sstevel@tonic-gate 	if (read(fd, prh, statb.st_size) <
4417c478bd9Sstevel@tonic-gate 	    sizeof (prheader_t) + sizeof (lwpstatus_t)) {
4427c478bd9Sstevel@tonic-gate 		(void) close(fd);
4437c478bd9Sstevel@tonic-gate 		free(prh);
4447c478bd9Sstevel@tonic-gate 		return (-1);
4457c478bd9Sstevel@tonic-gate 	}
4467c478bd9Sstevel@tonic-gate 	(void) close(fd);
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	/* LINTED pointer cast may result in improper alignment */
4497c478bd9Sstevel@tonic-gate 	lwps = (lwpstatus_t *)(prh + 1);
4507c478bd9Sstevel@tonic-gate 	pctx_begin_syscalls(pctx);
4517c478bd9Sstevel@tonic-gate 	for (nlwp = prh->pr_nent; nlwp > 0; nlwp--) {
4527c478bd9Sstevel@tonic-gate 		if (action(pctx,
4537c478bd9Sstevel@tonic-gate 		    pstatus->pr_pid, lwps->pr_lwpid, pctx->uarg) != 0)
4547c478bd9Sstevel@tonic-gate 			ret = -1;
4557c478bd9Sstevel@tonic-gate 		/* LINTED pointer cast may result in improper alignment */
4567c478bd9Sstevel@tonic-gate 		lwps = (lwpstatus_t *)((char *)lwps + prh->pr_entsize);
4577c478bd9Sstevel@tonic-gate 	}
4587c478bd9Sstevel@tonic-gate 	pctx_end_syscalls(pctx);
4597c478bd9Sstevel@tonic-gate 	free(prh);
4607c478bd9Sstevel@tonic-gate 	return (ret);
4617c478bd9Sstevel@tonic-gate }
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate /*
4647c478bd9Sstevel@tonic-gate  * Free any associated state, but leave the process stopped if it
4657c478bd9Sstevel@tonic-gate  * is still under our control.  (If it isn't under our control,
4667c478bd9Sstevel@tonic-gate  * it should just run to completion when we do our last close)
4677c478bd9Sstevel@tonic-gate  */
4687c478bd9Sstevel@tonic-gate static void
4697c478bd9Sstevel@tonic-gate pctx_free(pctx_t *pctx)
4707c478bd9Sstevel@tonic-gate {
4717c478bd9Sstevel@tonic-gate 	if (pctx->cpc != NULL && pctx_cpc_callback != NULL)
4727c478bd9Sstevel@tonic-gate 		(*pctx_cpc_callback)(pctx->cpc, pctx);
4737c478bd9Sstevel@tonic-gate 	if (pctx->Pr) {
4747c478bd9Sstevel@tonic-gate 		Pfree(pctx->Pr);
4757c478bd9Sstevel@tonic-gate 		pctx->Pr = NULL;
4767c478bd9Sstevel@tonic-gate 	}
4777c478bd9Sstevel@tonic-gate 	pctx->errfn = pctx_default_errfn;
4787c478bd9Sstevel@tonic-gate }
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate /*
4817c478bd9Sstevel@tonic-gate  * Completely release the process from our control and discard all our state
4827c478bd9Sstevel@tonic-gate  */
4837c478bd9Sstevel@tonic-gate void
4847c478bd9Sstevel@tonic-gate pctx_release(pctx_t *pctx)
4857c478bd9Sstevel@tonic-gate {
4867c478bd9Sstevel@tonic-gate 	if (pctx->Pr) {
4877c478bd9Sstevel@tonic-gate 		Prelease(pctx->Pr, PRELEASE_CLEAR);
4887c478bd9Sstevel@tonic-gate 		pctx->Pr = NULL;
4897c478bd9Sstevel@tonic-gate 	}
490*b885580bSAlexander Kolbasov 
4917c478bd9Sstevel@tonic-gate 	pctx_free(pctx);
4927c478bd9Sstevel@tonic-gate 	bzero(pctx, sizeof (*pctx));
4937c478bd9Sstevel@tonic-gate 	free(pctx);
4947c478bd9Sstevel@tonic-gate }
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate static void
4977c478bd9Sstevel@tonic-gate msincr(struct timeval *tv, uint_t msec)
4987c478bd9Sstevel@tonic-gate {
4997c478bd9Sstevel@tonic-gate 	tv->tv_sec += msec / MILLISEC;
5007c478bd9Sstevel@tonic-gate 	tv->tv_usec += (msec % MILLISEC) * MILLISEC;
5017c478bd9Sstevel@tonic-gate 	if (tv->tv_usec > MICROSEC) {
5027c478bd9Sstevel@tonic-gate 		tv->tv_sec++;
5037c478bd9Sstevel@tonic-gate 		tv->tv_usec -= MICROSEC;
5047c478bd9Sstevel@tonic-gate 	}
5057c478bd9Sstevel@tonic-gate }
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate static uint_t
5087c478bd9Sstevel@tonic-gate msdiff(struct timeval *tva, struct timeval *tvb)
5097c478bd9Sstevel@tonic-gate {
5107c478bd9Sstevel@tonic-gate 	time_t sdiff = tva->tv_sec - tvb->tv_sec;
5117c478bd9Sstevel@tonic-gate 	suseconds_t udiff = tva->tv_usec - tvb->tv_usec;
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 	if (sdiff < 0)
5147c478bd9Sstevel@tonic-gate 		return (0);
5157c478bd9Sstevel@tonic-gate 	if (udiff < 0) {
5167c478bd9Sstevel@tonic-gate 		udiff += MICROSEC;
5177c478bd9Sstevel@tonic-gate 		sdiff--;
5187c478bd9Sstevel@tonic-gate 	}
5197c478bd9Sstevel@tonic-gate 	if (sdiff < 0)
5207c478bd9Sstevel@tonic-gate 		return (0);
5217c478bd9Sstevel@tonic-gate 	if (sdiff >= (INT_MAX / MILLISEC))
5227c478bd9Sstevel@tonic-gate 		return ((uint_t)INT_MAX);
5237c478bd9Sstevel@tonic-gate 	return ((uint_t)(sdiff * MILLISEC + udiff / MILLISEC));
5247c478bd9Sstevel@tonic-gate }
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate int
5277c478bd9Sstevel@tonic-gate pctx_run(
5287c478bd9Sstevel@tonic-gate 	pctx_t *pctx,
5297c478bd9Sstevel@tonic-gate 	uint_t msec,
5307c478bd9Sstevel@tonic-gate 	uint_t nsamples,
5317c478bd9Sstevel@tonic-gate 	int (*tick)(pctx_t *, pid_t, id_t, void *))
5327c478bd9Sstevel@tonic-gate {
5337c478bd9Sstevel@tonic-gate 	static const char fn[] = "run";
5347c478bd9Sstevel@tonic-gate 	struct timeval tvgoal, tvnow;
5357c478bd9Sstevel@tonic-gate 	uint_t mswait = 0;
5367c478bd9Sstevel@tonic-gate 	int running = 1;
5377c478bd9Sstevel@tonic-gate 	const pstatus_t *pstatus;
5387c478bd9Sstevel@tonic-gate 	psinfo_t psinfo;
5397c478bd9Sstevel@tonic-gate 	void (*sigsaved)();
5407c478bd9Sstevel@tonic-gate 	id_t lwpid;
5417c478bd9Sstevel@tonic-gate 	pid_t pid = Pstatus(pctx->Pr)->pr_pid;
5427c478bd9Sstevel@tonic-gate 	int pstate;
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 	if (msec == 0)
5457c478bd9Sstevel@tonic-gate 		nsamples = 0;
5467c478bd9Sstevel@tonic-gate 	if (nsamples == 0)
5477c478bd9Sstevel@tonic-gate 		nsamples = UINT_MAX;
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 	/*
5507c478bd9Sstevel@tonic-gate 	 * Casually discard any knowledge of the children we create
5517c478bd9Sstevel@tonic-gate 	 */
5527c478bd9Sstevel@tonic-gate 	sigsaved = signal(SIGCHLD, SIG_IGN);
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	/*
5557c478bd9Sstevel@tonic-gate 	 * Since we've just "discovered" this process which might have
5567c478bd9Sstevel@tonic-gate 	 * been running for weeks, deliver some init_lwp events so
5577c478bd9Sstevel@tonic-gate 	 * that our caller gets a handle on the process.
5587c478bd9Sstevel@tonic-gate 	 */
5597c478bd9Sstevel@tonic-gate 	if (pctx_lwpiterate(pctx, pctx->init_lwp) != 0) {
5607c478bd9Sstevel@tonic-gate 		if (pctx->verbose)
5617c478bd9Sstevel@tonic-gate 			pctx_error(pctx, fn,
5627c478bd9Sstevel@tonic-gate 			    gettext("%d: lwp discovery failed\n"), (int)pid);
5637c478bd9Sstevel@tonic-gate 		goto bailout;
5647c478bd9Sstevel@tonic-gate 	}
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 	if (msec != 0) {
5677c478bd9Sstevel@tonic-gate 		/*
5687c478bd9Sstevel@tonic-gate 		 * tvgoal represents the time at which the sample
5697c478bd9Sstevel@tonic-gate 		 * should next be taken.
5707c478bd9Sstevel@tonic-gate 		 */
5717c478bd9Sstevel@tonic-gate 		(void) gettimeofday(&tvgoal, 0);
5727c478bd9Sstevel@tonic-gate 		msincr(&tvgoal, msec);
5737c478bd9Sstevel@tonic-gate 	}
5747c478bd9Sstevel@tonic-gate 
5757c3666b4Skk112340 	/*
5767c3666b4Skk112340 	 * The event handling loop continues while running is 1.
5777c3666b4Skk112340 	 * running becomes 0 when either the controlled process has
5787c3666b4Skk112340 	 * exited successfully or the number of time samples has expired.
5797c3666b4Skk112340 	 * Otherwise, if an error has occurred, running becomes -1.
5807c3666b4Skk112340 	 */
581*b885580bSAlexander Kolbasov 	while (running == 1 && !pctx->terminate) {
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 		if (Psetrun(pctx->Pr, 0, 0) != 0) {
5847c478bd9Sstevel@tonic-gate 			if (pctx->verbose)
5857c478bd9Sstevel@tonic-gate 				pctx_error(pctx, fn,
5867c478bd9Sstevel@tonic-gate 				    gettext("%d: Psetrun\n"), (int)pid);
5877c478bd9Sstevel@tonic-gate 			break;
5887c478bd9Sstevel@tonic-gate 		}
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 		if (msec != 0) {
5917c478bd9Sstevel@tonic-gate 			/*
5927c478bd9Sstevel@tonic-gate 			 * This timing loop attempts to estimate the number
5937c478bd9Sstevel@tonic-gate 			 * of milliseconds between our "goal" time (when
5947c478bd9Sstevel@tonic-gate 			 * we should stop the process and run the tick
5957c478bd9Sstevel@tonic-gate 			 * routine) and the current time.
5967c478bd9Sstevel@tonic-gate 			 *
5977c478bd9Sstevel@tonic-gate 			 * If we ever find ourselves running behind i.e. we
5987c478bd9Sstevel@tonic-gate 			 * missed our goal, then we skip ahead to the next
5997c478bd9Sstevel@tonic-gate 			 * goal instead.
6007c478bd9Sstevel@tonic-gate 			 */
6017c478bd9Sstevel@tonic-gate 			do {
6027c478bd9Sstevel@tonic-gate 				(void) gettimeofday(&tvnow, 0);
6037c478bd9Sstevel@tonic-gate 				if ((mswait = msdiff(&tvgoal, &tvnow)) == 0) {
6047c478bd9Sstevel@tonic-gate 					msincr(&tvgoal, msec);
6057c478bd9Sstevel@tonic-gate 					/*
6067c478bd9Sstevel@tonic-gate 					 * Skip ahead to the next goal, unless
6077c478bd9Sstevel@tonic-gate 					 * there is only one more sample left
6087c478bd9Sstevel@tonic-gate 					 * to take.
6097c478bd9Sstevel@tonic-gate 					 */
6107c478bd9Sstevel@tonic-gate 					if (nsamples != 1)
6117c478bd9Sstevel@tonic-gate 						nsamples--;
6127c478bd9Sstevel@tonic-gate 				}
613*b885580bSAlexander Kolbasov 			} while (mswait == 0 && !pctx->terminate);
6147c478bd9Sstevel@tonic-gate 		}
6157c478bd9Sstevel@tonic-gate 
616*b885580bSAlexander Kolbasov 		if (pctx->terminate)
617*b885580bSAlexander Kolbasov 			goto bailout;
618*b885580bSAlexander Kolbasov 		else
6197c478bd9Sstevel@tonic-gate 			(void) Pwait(pctx->Pr, mswait);
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate checkstate:
6227c478bd9Sstevel@tonic-gate 		switch (pstate = Pstate(pctx->Pr)) {
6237c478bd9Sstevel@tonic-gate 		case PS_RUN:
6247c478bd9Sstevel@tonic-gate 			/*
6257c478bd9Sstevel@tonic-gate 			 * Try again, but wait for up to 5 seconds.
6267c478bd9Sstevel@tonic-gate 			 */
6277c478bd9Sstevel@tonic-gate 			if (Pstop(pctx->Pr, 5 * MILLISEC) == -1 ||
6287c478bd9Sstevel@tonic-gate 			    (pstate = Pstate(pctx->Pr)) != PS_STOP) {
6297c478bd9Sstevel@tonic-gate 				pctx_error(pctx, fn,
6307c478bd9Sstevel@tonic-gate 				    gettext("%d: won't stop\n"), (int)pid);
6317c478bd9Sstevel@tonic-gate 			}
6327c478bd9Sstevel@tonic-gate 			break;
6337c478bd9Sstevel@tonic-gate 		case PS_STOP:
6347c478bd9Sstevel@tonic-gate 			break;
6357c478bd9Sstevel@tonic-gate 		case PS_LOST:
6367c478bd9Sstevel@tonic-gate 			/*
6377c478bd9Sstevel@tonic-gate 			 * Lost control - probably execed a setuid/setgid
6387c478bd9Sstevel@tonic-gate 			 * executable.  Try and get control back again,
6397c478bd9Sstevel@tonic-gate 			 * else bail ..
6407c478bd9Sstevel@tonic-gate 			 */
6417c478bd9Sstevel@tonic-gate 			(void) Preopen(pctx->Pr);
6427c478bd9Sstevel@tonic-gate 			if ((pstate = Pstate(pctx->Pr)) != PS_LOST)
6437c478bd9Sstevel@tonic-gate 				goto checkstate;
6447c478bd9Sstevel@tonic-gate 			pctx_error(pctx, fn,
6457c478bd9Sstevel@tonic-gate 			    gettext("%d: execed a program that cannot "
6467c478bd9Sstevel@tonic-gate 			    "be tracked\n"), (int)pid);
6477c3666b4Skk112340 			running = -1;
6487c478bd9Sstevel@tonic-gate 			break;
6497c478bd9Sstevel@tonic-gate 		case PS_UNDEAD:
6507c478bd9Sstevel@tonic-gate 		case PS_DEAD:
6517c478bd9Sstevel@tonic-gate 			if (pctx->verbose)
6527c478bd9Sstevel@tonic-gate 				pctx_error(pctx, fn,
6537c478bd9Sstevel@tonic-gate 				    gettext("%d: process terminated\n"),
6547c478bd9Sstevel@tonic-gate 				    (int)pid);
6557c3666b4Skk112340 			running = -1;
6567c478bd9Sstevel@tonic-gate 			break;
6577c478bd9Sstevel@tonic-gate 		default:
6587c478bd9Sstevel@tonic-gate 			if (pctx->verbose)
6597c478bd9Sstevel@tonic-gate 				pctx_error(pctx, fn,
6607c478bd9Sstevel@tonic-gate 				    gettext("%d: process state 0x%x?\n"),
6617c478bd9Sstevel@tonic-gate 				    (int)pid, pstate);
6627c478bd9Sstevel@tonic-gate 			break;
6637c478bd9Sstevel@tonic-gate 		}
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 		if (pstate != PS_STOP)
6667c478bd9Sstevel@tonic-gate 			break;
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate 		pstatus = Pstatus(pctx->Pr);
6697c478bd9Sstevel@tonic-gate 		lwpid = pstatus->pr_lwp.pr_lwpid;
6707c478bd9Sstevel@tonic-gate 		switch (pstatus->pr_lwp.pr_why) {
6717c478bd9Sstevel@tonic-gate 		case PR_REQUESTED:
6727c478bd9Sstevel@tonic-gate 			msincr(&tvgoal, msec);
6737c478bd9Sstevel@tonic-gate 			if (pstatus->pr_flags & PR_VFORKP) {
6747c478bd9Sstevel@tonic-gate 				/*
6757c478bd9Sstevel@tonic-gate 				 * The process is in a vfork stupor until
6767c478bd9Sstevel@tonic-gate 				 * its child releases it via an exec.
6777c478bd9Sstevel@tonic-gate 				 * Don't sample it while it's in this state
6787c478bd9Sstevel@tonic-gate 				 * - we won't be able to create the agent.
6797c478bd9Sstevel@tonic-gate 				 */
6807c478bd9Sstevel@tonic-gate 				break;
6817c478bd9Sstevel@tonic-gate 			}
6827c478bd9Sstevel@tonic-gate 			if (pctx_lwpiterate(pctx, tick) != 0)
6837c3666b4Skk112340 				running = -1;
6847c3666b4Skk112340 			if (running == 1 && --nsamples == 0)
6857c478bd9Sstevel@tonic-gate 				running = 0;
6867c478bd9Sstevel@tonic-gate 			break;
6877c478bd9Sstevel@tonic-gate 		case PR_SYSENTRY:
6887c478bd9Sstevel@tonic-gate 			switch (pstatus->pr_lwp.pr_what) {
6897c478bd9Sstevel@tonic-gate 			case SYS_lwp_exit:
6907c478bd9Sstevel@tonic-gate 				pctx_begin_syscalls(pctx);
6917c478bd9Sstevel@tonic-gate 				(void) pctx->fini_lwp(pctx,
6927c478bd9Sstevel@tonic-gate 				    pid, lwpid, pctx->uarg);
6937c478bd9Sstevel@tonic-gate 				(void) pctx->lwp_exit(pctx,
6947c478bd9Sstevel@tonic-gate 				    pid, lwpid, pctx->uarg);
6957c478bd9Sstevel@tonic-gate 				pctx_end_syscalls(pctx);
6967c478bd9Sstevel@tonic-gate 				break;
6977c478bd9Sstevel@tonic-gate 			case SYS_exit:
6987c3666b4Skk112340 				if (pctx_lwpiterate(pctx, pctx->fini_lwp)
6997c3666b4Skk112340 				    != 0)
7007c3666b4Skk112340 					running = -1;
7017c478bd9Sstevel@tonic-gate 				pctx->exit(pctx, pid, lwpid,
7027c478bd9Sstevel@tonic-gate 				    (int)pstatus->pr_lwp.pr_sysarg[0],
7037c478bd9Sstevel@tonic-gate 				    pctx->uarg);
7047c3666b4Skk112340 				if (running == 1)
7057c478bd9Sstevel@tonic-gate 					running = 0;
7067c478bd9Sstevel@tonic-gate 				break;
7077c478bd9Sstevel@tonic-gate 			case SYS_exec:
7087c478bd9Sstevel@tonic-gate 			case SYS_execve:
7097c478bd9Sstevel@tonic-gate 				(void) pctx_lwpiterate(pctx, pctx->fini_lwp);
7107c478bd9Sstevel@tonic-gate 				break;
7117c478bd9Sstevel@tonic-gate 			default:
7127c478bd9Sstevel@tonic-gate 				pctx_error(pctx, fn,
7137c478bd9Sstevel@tonic-gate 				    "warning - pid %d sysentry(%d)\n",
7147c478bd9Sstevel@tonic-gate 				    (int)pid, pstatus->pr_lwp.pr_what);
7157c478bd9Sstevel@tonic-gate 				break;
7167c478bd9Sstevel@tonic-gate 			}
7177c478bd9Sstevel@tonic-gate 			break;
7187c478bd9Sstevel@tonic-gate 		case PR_SYSEXIT:
7197c478bd9Sstevel@tonic-gate 			switch (pstatus->pr_lwp.pr_what) {
7207c478bd9Sstevel@tonic-gate 			case SYS_exec:
7217c478bd9Sstevel@tonic-gate 			case SYS_execve:
7227c478bd9Sstevel@tonic-gate 				if (pstatus->pr_lwp.pr_errno) {
7237c478bd9Sstevel@tonic-gate 					/*
7247c478bd9Sstevel@tonic-gate 					 * The exec failed completely.
7257c478bd9Sstevel@tonic-gate 					 * Reinstate the lwps we fini'd
7267c478bd9Sstevel@tonic-gate 					 * at exec entrance
7277c478bd9Sstevel@tonic-gate 					 */
7287c3666b4Skk112340 					if (pctx_lwpiterate(pctx,
7297c3666b4Skk112340 					    pctx->init_lwp) == 0)
7307c3666b4Skk112340 						running = 1;
7317c3666b4Skk112340 					else
7327c3666b4Skk112340 						running = -1;
7337c478bd9Sstevel@tonic-gate 					break;
7347c478bd9Sstevel@tonic-gate 				}
7357c478bd9Sstevel@tonic-gate 				if (pctx->exec == (pctx_sysc_execfn_t *)
7367c478bd9Sstevel@tonic-gate 				    default_int) {
7377c478bd9Sstevel@tonic-gate 					running = 0;
7387c478bd9Sstevel@tonic-gate 					break;
7397c478bd9Sstevel@tonic-gate 				}
7407c478bd9Sstevel@tonic-gate 				(void) memcpy(&psinfo,
7417c478bd9Sstevel@tonic-gate 				    Ppsinfo(pctx->Pr), sizeof (psinfo));
7427c478bd9Sstevel@tonic-gate 				proc_unctrl_psinfo(&psinfo);
7437c478bd9Sstevel@tonic-gate 				pctx_begin_syscalls(pctx);
7447c3666b4Skk112340 				if (pctx->exec(pctx, pid, lwpid,
7457c3666b4Skk112340 				    psinfo.pr_psargs, pctx->uarg) != 0)
7467c3666b4Skk112340 					running = -1;
7477c3666b4Skk112340 				if (running == 1 && pctx->init_lwp(pctx,
7487c3666b4Skk112340 				    pid, 1, pctx->uarg) != 0)
7497c3666b4Skk112340 					running = -1;
7507c478bd9Sstevel@tonic-gate 				pctx_end_syscalls(pctx);
7517c478bd9Sstevel@tonic-gate 				break;
7527c478bd9Sstevel@tonic-gate 			case SYS_lwp_create:
7537c478bd9Sstevel@tonic-gate 				if (pstatus->pr_lwp.pr_errno ||
7547c478bd9Sstevel@tonic-gate 				    pstatus->pr_lwp.pr_rval1)
7557c478bd9Sstevel@tonic-gate 					break;
7567c478bd9Sstevel@tonic-gate 				pctx_begin_syscalls(pctx);
7577c3666b4Skk112340 				if (pctx->init_lwp(pctx, pid, lwpid,
7587c3666b4Skk112340 				    pctx->uarg) != 0)
7597c3666b4Skk112340 					running = -1;
7607c3666b4Skk112340 				if (running == 1 && pctx->lwp_create(pctx,
7617c3666b4Skk112340 				    pid, lwpid, pctx->uarg) != 0)
7627c3666b4Skk112340 					running = -1;
7637c478bd9Sstevel@tonic-gate 				pctx_end_syscalls(pctx);
7647c478bd9Sstevel@tonic-gate 				break;
7657c478bd9Sstevel@tonic-gate 			case SYS_forkall:
7667c478bd9Sstevel@tonic-gate 			case SYS_vfork:
7677c478bd9Sstevel@tonic-gate 			case SYS_fork1:
768657b1f3dSraf 			case SYS_forksys:
7697c478bd9Sstevel@tonic-gate 				if (pstatus->pr_lwp.pr_errno)
7707c478bd9Sstevel@tonic-gate 					break;
7717c478bd9Sstevel@tonic-gate 				(void) fflush(NULL);
7727c478bd9Sstevel@tonic-gate 				switch (fork1()) {
7737c478bd9Sstevel@tonic-gate 					pid_t ppid;
7747c478bd9Sstevel@tonic-gate 					int wascreated;
7757c478bd9Sstevel@tonic-gate 					pctx_sysc_forkfn_t *forkfn;
7767c478bd9Sstevel@tonic-gate 				case 0:
7777c478bd9Sstevel@tonic-gate 					ppid = pid;
7787c478bd9Sstevel@tonic-gate 					pid = pstatus->pr_lwp.pr_rval1;
7797c478bd9Sstevel@tonic-gate 					wascreated = pctx->created;
7807c478bd9Sstevel@tonic-gate 					forkfn = pctx->fork;
7817c478bd9Sstevel@tonic-gate 					pctx_free(pctx);
7827c478bd9Sstevel@tonic-gate 					pctx = pctx_capture(pid, pctx->uarg,
7837c478bd9Sstevel@tonic-gate 					    pctx->verbose, pctx->errfn);
7847c478bd9Sstevel@tonic-gate 					if (pctx != NULL) {
7857c478bd9Sstevel@tonic-gate 						if (wascreated) {
7867c478bd9Sstevel@tonic-gate 							/*
7877c478bd9Sstevel@tonic-gate 							 * Set kill on last
7887c478bd9Sstevel@tonic-gate 							 * close so -all-
7897c478bd9Sstevel@tonic-gate 							 * children die.
7907c478bd9Sstevel@tonic-gate 							 */
7917c478bd9Sstevel@tonic-gate 							pctx->created = 1;
7927c478bd9Sstevel@tonic-gate 							(void) Psetflags(
7937c478bd9Sstevel@tonic-gate 							    pctx->Pr, PR_KLC);
7947c478bd9Sstevel@tonic-gate 						}
7957c478bd9Sstevel@tonic-gate 						(*forkfn)(pctx, ppid, pid,
7967c478bd9Sstevel@tonic-gate 						    lwpid, pctx->uarg);
7977c478bd9Sstevel@tonic-gate 						pctx_release(pctx);
7987c478bd9Sstevel@tonic-gate 						_exit(0);
7997c3666b4Skk112340 					} else {
8007c3666b4Skk112340 						_exit(1);
8017c3666b4Skk112340 					}
8027c478bd9Sstevel@tonic-gate 					/*NOTREACHED*/
8037c478bd9Sstevel@tonic-gate 				case -1:
8047c478bd9Sstevel@tonic-gate 					pctx_error(pctx, fn,
8057c478bd9Sstevel@tonic-gate 					    "cannot follow pid %d: %s\n",
8067c478bd9Sstevel@tonic-gate 					    (int)pstatus->pr_lwp.pr_rval1,
8077c478bd9Sstevel@tonic-gate 					    strerror(errno));
8087c478bd9Sstevel@tonic-gate 					break;
8097c478bd9Sstevel@tonic-gate 				default:
8107c478bd9Sstevel@tonic-gate 					break;
8117c478bd9Sstevel@tonic-gate 				}
8127c478bd9Sstevel@tonic-gate 				break;
8137c478bd9Sstevel@tonic-gate 			default:
8147c478bd9Sstevel@tonic-gate 				pctx_error(pctx, fn, gettext(
8157c478bd9Sstevel@tonic-gate 				    "warning - pid %d sysexit(%d)\n"),
8167c478bd9Sstevel@tonic-gate 				    (int)pid, pstatus->pr_lwp.pr_what);
8177c478bd9Sstevel@tonic-gate 				break;
8187c478bd9Sstevel@tonic-gate 			}
8197c478bd9Sstevel@tonic-gate 			break;
8207c478bd9Sstevel@tonic-gate 		case PR_SIGNALLED:
8217c478bd9Sstevel@tonic-gate 			if (pctx->verbose)
8227c478bd9Sstevel@tonic-gate 				pctx_error(pctx, fn,
8237c478bd9Sstevel@tonic-gate 				    gettext("pid %d - signalled\n"), (int)pid);
8247c478bd9Sstevel@tonic-gate 			break;
8257c478bd9Sstevel@tonic-gate 		case PR_JOBCONTROL:
8267c478bd9Sstevel@tonic-gate 			if (pctx->verbose)
8277c478bd9Sstevel@tonic-gate 				pctx_error(pctx, fn,
8287c478bd9Sstevel@tonic-gate 				    gettext("pid %d - job control stop\n"),
8297c478bd9Sstevel@tonic-gate 				    (int)pid);
8307c3666b4Skk112340 			running = -1;
8317c478bd9Sstevel@tonic-gate 			break;
8327c478bd9Sstevel@tonic-gate 		case PR_FAULTED:
8337c478bd9Sstevel@tonic-gate 			if (pctx->verbose)
8347c478bd9Sstevel@tonic-gate 				pctx_error(pctx, fn,
8357c478bd9Sstevel@tonic-gate 				    gettext("pid %d - faulted\n"), (int)pid);
8367c478bd9Sstevel@tonic-gate 			break;
8377c478bd9Sstevel@tonic-gate 		case PR_SUSPENDED:
8387c478bd9Sstevel@tonic-gate 			if (pctx->verbose)
8397c478bd9Sstevel@tonic-gate 				pctx_error(pctx, fn,
8407c478bd9Sstevel@tonic-gate 				    gettext("pid %d - suspended\n"), (int)pid);
8417c478bd9Sstevel@tonic-gate 			break;
8427c478bd9Sstevel@tonic-gate 		case PR_CHECKPOINT:
8437c478bd9Sstevel@tonic-gate 			if (pctx->verbose)
8447c478bd9Sstevel@tonic-gate 				pctx_error(pctx, fn,
8457c478bd9Sstevel@tonic-gate 				    gettext("pid %d - checkpoint\n"),
8467c478bd9Sstevel@tonic-gate 				    (int)pid);
8477c478bd9Sstevel@tonic-gate 			break;
8487c478bd9Sstevel@tonic-gate 		default:
8497c478bd9Sstevel@tonic-gate 			if (pctx->verbose)
8507c478bd9Sstevel@tonic-gate 				pctx_error(pctx, fn,
8517c478bd9Sstevel@tonic-gate 				    gettext("pid %d - reason %d\n"),
8527c478bd9Sstevel@tonic-gate 				    (int)pid, pstatus->pr_lwp.pr_why);
8537c3666b4Skk112340 			running = -1;
8547c478bd9Sstevel@tonic-gate 			break;
8557c478bd9Sstevel@tonic-gate 		}
8567c478bd9Sstevel@tonic-gate 	}
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate bailout:
8597c478bd9Sstevel@tonic-gate 	(void) signal(SIGCHLD, sigsaved);
8607c478bd9Sstevel@tonic-gate 
861*b885580bSAlexander Kolbasov 	if (pctx->terminate)
862*b885580bSAlexander Kolbasov 		return (0);
863*b885580bSAlexander Kolbasov 
8647c3666b4Skk112340 	switch (running) {
8657c3666b4Skk112340 	case 0:
8667c3666b4Skk112340 		return (0);
8677c3666b4Skk112340 	case -1:
8687c3666b4Skk112340 		return (-1);
8697c3666b4Skk112340 	default:
8707c3666b4Skk112340 		pctx_error(pctx, fn, gettext("lost control of pid %d\n"),
8717c3666b4Skk112340 		    (int)pid);
8727c478bd9Sstevel@tonic-gate 		pctx_free(pctx);
8737c478bd9Sstevel@tonic-gate 		return (-1);
8747c478bd9Sstevel@tonic-gate 	}
8757c3666b4Skk112340 }
8767c478bd9Sstevel@tonic-gate 
8777c478bd9Sstevel@tonic-gate /*
8787c478bd9Sstevel@tonic-gate  * Execute the private 'cpc' system call in the context of the
8797c478bd9Sstevel@tonic-gate  * controlled process.
8807c478bd9Sstevel@tonic-gate  */
8817c478bd9Sstevel@tonic-gate int
8827c478bd9Sstevel@tonic-gate __pctx_cpc(pctx_t *pctx, cpc_t *cpc,
8837c478bd9Sstevel@tonic-gate     int cmd, id_t lwpid, void *data1, void *data2, void *data3, int bufsize)
8847c478bd9Sstevel@tonic-gate {
8857c478bd9Sstevel@tonic-gate 	sysret_t rval;
8867c478bd9Sstevel@tonic-gate 	argdes_t argd[5];
8877c478bd9Sstevel@tonic-gate 	argdes_t *adp = &argd[0];
8887c478bd9Sstevel@tonic-gate 	int error;
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate 	/*
8917c478bd9Sstevel@tonic-gate 	 * Keep track of the relationship between cpc_t and pctx_t here.
8927c478bd9Sstevel@tonic-gate 	 * We store the last cpc_t used by libpctx, so that when this pctx is
8937c478bd9Sstevel@tonic-gate 	 * destroyed, libpctx can notify libcpc.
8947c478bd9Sstevel@tonic-gate 	 */
895*b885580bSAlexander Kolbasov 
8967c478bd9Sstevel@tonic-gate 	if (pctx->cpc != NULL && pctx->cpc != cpc && pctx_cpc_callback != NULL)
8977c478bd9Sstevel@tonic-gate 		(*pctx_cpc_callback)(pctx->cpc, pctx);
8987c478bd9Sstevel@tonic-gate 	pctx->cpc = cpc;
8997c478bd9Sstevel@tonic-gate 
9007c478bd9Sstevel@tonic-gate 	/*
9017c478bd9Sstevel@tonic-gate 	 * cmd and lwpid are passed in by value no matter what the command is.
9027c478bd9Sstevel@tonic-gate 	 */
9037c478bd9Sstevel@tonic-gate 	adp->arg_value = cmd;
9047c478bd9Sstevel@tonic-gate 	adp->arg_object = NULL;
9057c478bd9Sstevel@tonic-gate 	adp->arg_type = AT_BYVAL;
9067c478bd9Sstevel@tonic-gate 	adp->arg_inout = AI_INPUT;
9077c478bd9Sstevel@tonic-gate 	adp->arg_size = 0;
9087c478bd9Sstevel@tonic-gate 	adp++;
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate 	adp->arg_value = lwpid;
9117c478bd9Sstevel@tonic-gate 	adp->arg_object = NULL;
9127c478bd9Sstevel@tonic-gate 	adp->arg_type = AT_BYVAL;
9137c478bd9Sstevel@tonic-gate 	adp->arg_inout = AI_INPUT;
9147c478bd9Sstevel@tonic-gate 	adp->arg_size = 0;
9157c478bd9Sstevel@tonic-gate 	adp++;
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate 	switch (cmd) {
9187c478bd9Sstevel@tonic-gate 	case CPC_BIND:
9197c478bd9Sstevel@tonic-gate 		adp->arg_value = 0;
9207c478bd9Sstevel@tonic-gate 		adp->arg_object = data1;
9217c478bd9Sstevel@tonic-gate 		adp->arg_type = AT_BYREF;
9227c478bd9Sstevel@tonic-gate 		adp->arg_inout = AI_INPUT;
9237c478bd9Sstevel@tonic-gate 		adp->arg_size = (size_t)data2;
9247c478bd9Sstevel@tonic-gate 		adp++;
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate 		adp->arg_value = (size_t)data2;
9277c478bd9Sstevel@tonic-gate 		adp->arg_object = NULL;
9287c478bd9Sstevel@tonic-gate 		adp->arg_type = AT_BYVAL;
9297c478bd9Sstevel@tonic-gate 		adp->arg_inout = AI_INPUT;
9307c478bd9Sstevel@tonic-gate 		adp->arg_size = 0;
9317c478bd9Sstevel@tonic-gate 		adp++;
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate 		adp->arg_value = 0;
9347c478bd9Sstevel@tonic-gate 		adp->arg_object = data3;
9357c478bd9Sstevel@tonic-gate 		adp->arg_type = AT_BYREF;
9367c478bd9Sstevel@tonic-gate 		adp->arg_inout = AI_INOUT;
9377c478bd9Sstevel@tonic-gate 		adp->arg_size = sizeof (int);
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate 		break;
9407c478bd9Sstevel@tonic-gate 	case CPC_SAMPLE:
9417c478bd9Sstevel@tonic-gate 		adp->arg_value = 0;
9427c478bd9Sstevel@tonic-gate 		adp->arg_object = data1;
9437c478bd9Sstevel@tonic-gate 		adp->arg_type = AT_BYREF;
9447c478bd9Sstevel@tonic-gate 		adp->arg_inout = AI_OUTPUT;
9457c478bd9Sstevel@tonic-gate 		adp->arg_size = bufsize;
9467c478bd9Sstevel@tonic-gate 		adp++;
9477c478bd9Sstevel@tonic-gate 
9487c478bd9Sstevel@tonic-gate 		adp->arg_value = 0;
9497c478bd9Sstevel@tonic-gate 		adp->arg_object = data2;
9507c478bd9Sstevel@tonic-gate 		adp->arg_type = AT_BYREF;
9517c478bd9Sstevel@tonic-gate 		adp->arg_inout = AI_OUTPUT;
9527c478bd9Sstevel@tonic-gate 		adp->arg_size = sizeof (hrtime_t);
9537c478bd9Sstevel@tonic-gate 		adp++;
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate 		adp->arg_value = 0;
9567c478bd9Sstevel@tonic-gate 		adp->arg_object = data3;
9577c478bd9Sstevel@tonic-gate 		adp->arg_type = AT_BYREF;
9587c478bd9Sstevel@tonic-gate 		adp->arg_inout = AI_OUTPUT;
9597c478bd9Sstevel@tonic-gate 		adp->arg_size = sizeof (uint64_t);
9607c478bd9Sstevel@tonic-gate 
9617c478bd9Sstevel@tonic-gate 		break;
9627c478bd9Sstevel@tonic-gate 	default:
9637c478bd9Sstevel@tonic-gate 		adp->arg_value = 0;
9647c478bd9Sstevel@tonic-gate 		adp->arg_object = 0;
9657c478bd9Sstevel@tonic-gate 		adp->arg_type = AT_BYVAL;
9667c478bd9Sstevel@tonic-gate 		adp->arg_inout = AI_INPUT;
9677c478bd9Sstevel@tonic-gate 		adp->arg_size = 0;
9687c478bd9Sstevel@tonic-gate 		adp++;
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate 		adp->arg_value = 0;
9717c478bd9Sstevel@tonic-gate 		adp->arg_object = 0;
9727c478bd9Sstevel@tonic-gate 		adp->arg_type = AT_BYVAL;
9737c478bd9Sstevel@tonic-gate 		adp->arg_inout = AI_INPUT;
9747c478bd9Sstevel@tonic-gate 		adp->arg_size = 0;
9757c478bd9Sstevel@tonic-gate 		adp++;
9767c478bd9Sstevel@tonic-gate 
9777c478bd9Sstevel@tonic-gate 		adp->arg_value = 0;
9787c478bd9Sstevel@tonic-gate 		adp->arg_object = 0;
9797c478bd9Sstevel@tonic-gate 		adp->arg_type = AT_BYVAL;
9807c478bd9Sstevel@tonic-gate 		adp->arg_inout = AI_INPUT;
9817c478bd9Sstevel@tonic-gate 		adp->arg_size = 0;
9827c478bd9Sstevel@tonic-gate 
9837c478bd9Sstevel@tonic-gate 		break;
9847c478bd9Sstevel@tonic-gate 	}
9857c478bd9Sstevel@tonic-gate 
9867c478bd9Sstevel@tonic-gate 	error = Psyscall(pctx->Pr, &rval, SYS_cpc, 5, &argd[0]);
9877c478bd9Sstevel@tonic-gate 
9887c478bd9Sstevel@tonic-gate 	if (error) {
9897c478bd9Sstevel@tonic-gate 		errno = error > 0 ? error : ENOSYS;
9907c478bd9Sstevel@tonic-gate 		return (-1);
9917c478bd9Sstevel@tonic-gate 	}
9927c478bd9Sstevel@tonic-gate 	return (rval.sys_rval1);
9937c478bd9Sstevel@tonic-gate }
9947c478bd9Sstevel@tonic-gate 
9957c478bd9Sstevel@tonic-gate /*
9967c478bd9Sstevel@tonic-gate  * libcpc-private hook used to register a callback. The callback is used to
9977c478bd9Sstevel@tonic-gate  * notify libcpc when a pctx handle is invalidated.
9987c478bd9Sstevel@tonic-gate  */
9997c478bd9Sstevel@tonic-gate void
10007c478bd9Sstevel@tonic-gate __pctx_cpc_register_callback(void (*arg)(struct __cpc *, struct __pctx *))
10017c478bd9Sstevel@tonic-gate {
10027c478bd9Sstevel@tonic-gate 	pctx_cpc_callback = arg;
10037c478bd9Sstevel@tonic-gate }
1004*b885580bSAlexander Kolbasov 
1005*b885580bSAlexander Kolbasov /*
1006*b885580bSAlexander Kolbasov  * Tell pctx_run to bail out immediately
1007*b885580bSAlexander Kolbasov  */
1008*b885580bSAlexander Kolbasov void
1009*b885580bSAlexander Kolbasov pctx_terminate(struct __pctx *pctx)
1010*b885580bSAlexander Kolbasov {
1011*b885580bSAlexander Kolbasov 	pctx->terminate = 1;
1012*b885580bSAlexander Kolbasov }
1013