17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*8fd04b83SRoger A. Faulkner * Common Development and Distribution License (the "License").
6*8fd04b83SRoger A. Faulkner * 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 */
21*8fd04b83SRoger A. Faulkner
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 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
287c478bd9Sstevel@tonic-gate /* All Rights Reserved */
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate #include <stdio.h>
317c478bd9Sstevel@tonic-gate #include <stdlib.h>
327c478bd9Sstevel@tonic-gate #include <unistd.h>
337c478bd9Sstevel@tonic-gate #include <ctype.h>
347c478bd9Sstevel@tonic-gate #include <string.h>
357c478bd9Sstevel@tonic-gate #include <signal.h>
367c478bd9Sstevel@tonic-gate #include <stropts.h>
377c478bd9Sstevel@tonic-gate #include <errno.h>
387c478bd9Sstevel@tonic-gate #include <sys/types.h>
397c478bd9Sstevel@tonic-gate #include <sys/termio.h>
407c478bd9Sstevel@tonic-gate #include <libproc.h>
417c478bd9Sstevel@tonic-gate #include "ramdata.h"
427c478bd9Sstevel@tonic-gate #include "proto.h"
437c478bd9Sstevel@tonic-gate
447c478bd9Sstevel@tonic-gate /*
457c478bd9Sstevel@tonic-gate * Routines related to interprocess communication
467c478bd9Sstevel@tonic-gate * among the truss processes which are controlling
477c478bd9Sstevel@tonic-gate * multiple traced processes.
487c478bd9Sstevel@tonic-gate */
497c478bd9Sstevel@tonic-gate
507c478bd9Sstevel@tonic-gate /*
517c478bd9Sstevel@tonic-gate * Function prototypes for static routines in this module.
527c478bd9Sstevel@tonic-gate */
537c478bd9Sstevel@tonic-gate void Ecritical(int);
547c478bd9Sstevel@tonic-gate void Xcritical(int);
557c478bd9Sstevel@tonic-gate
567c478bd9Sstevel@tonic-gate /*
577c478bd9Sstevel@tonic-gate * Ensure everyone keeps out of each other's way
587c478bd9Sstevel@tonic-gate * while writing lines of trace output.
597c478bd9Sstevel@tonic-gate */
607c478bd9Sstevel@tonic-gate void
Flush()617c478bd9Sstevel@tonic-gate Flush()
627c478bd9Sstevel@tonic-gate {
637c478bd9Sstevel@tonic-gate /*
647c478bd9Sstevel@tonic-gate * Except for regions bounded by Eserialize()/Xserialize(),
657c478bd9Sstevel@tonic-gate * this is the only place anywhere in the program where a
667c478bd9Sstevel@tonic-gate * write() to the trace output file takes place, so here
677c478bd9Sstevel@tonic-gate * is where we detect errors writing to the output.
687c478bd9Sstevel@tonic-gate */
697c478bd9Sstevel@tonic-gate
707c478bd9Sstevel@tonic-gate errno = 0;
717c478bd9Sstevel@tonic-gate
727c478bd9Sstevel@tonic-gate Ecritical(0);
737c478bd9Sstevel@tonic-gate (void) fflush(stdout);
747c478bd9Sstevel@tonic-gate Xcritical(0);
757c478bd9Sstevel@tonic-gate
767c478bd9Sstevel@tonic-gate if (ferror(stdout) && errno) /* error on write(), probably EPIPE */
777c478bd9Sstevel@tonic-gate interrupt = SIGTERM; /* post an interrupt */
787c478bd9Sstevel@tonic-gate }
797c478bd9Sstevel@tonic-gate
807c478bd9Sstevel@tonic-gate /*
817c478bd9Sstevel@tonic-gate * Eserialize() and Xserialize() are used to bracket
827c478bd9Sstevel@tonic-gate * a region which may produce large amounts of output,
837c478bd9Sstevel@tonic-gate * such as showargs()/dumpargs().
847c478bd9Sstevel@tonic-gate */
857c478bd9Sstevel@tonic-gate
867c478bd9Sstevel@tonic-gate void
Eserialize()877c478bd9Sstevel@tonic-gate Eserialize()
887c478bd9Sstevel@tonic-gate {
897c478bd9Sstevel@tonic-gate /* serialize output */
907c478bd9Sstevel@tonic-gate Ecritical(0);
917c478bd9Sstevel@tonic-gate }
927c478bd9Sstevel@tonic-gate
937c478bd9Sstevel@tonic-gate void
Xserialize()947c478bd9Sstevel@tonic-gate Xserialize()
957c478bd9Sstevel@tonic-gate {
967c478bd9Sstevel@tonic-gate (void) fflush(stdout);
977c478bd9Sstevel@tonic-gate Xcritical(0);
987c478bd9Sstevel@tonic-gate }
997c478bd9Sstevel@tonic-gate
1007c478bd9Sstevel@tonic-gate /*
1017c478bd9Sstevel@tonic-gate * Enter critical region --- Wait on mutex, lock out other processes.
1027c478bd9Sstevel@tonic-gate * Lock zero is used to serialize output in situations where multiple processes
1037c478bd9Sstevel@tonic-gate * may be writing to stdout/stderr and order must be preserved. Most of these
1047c478bd9Sstevel@tonic-gate * are in expound.c
1057c478bd9Sstevel@tonic-gate * Lock one is used to protect the table of processes currently being traced
1067c478bd9Sstevel@tonic-gate * every time a pid is added or removed from the table Ecritical(1)/Xcritical(1)
1077c478bd9Sstevel@tonic-gate * get called.
1087c478bd9Sstevel@tonic-gate */
1097c478bd9Sstevel@tonic-gate void
Ecritical(int num)1107c478bd9Sstevel@tonic-gate Ecritical(int num)
1117c478bd9Sstevel@tonic-gate {
1127c478bd9Sstevel@tonic-gate int rv;
1137c478bd9Sstevel@tonic-gate
1147c478bd9Sstevel@tonic-gate if (num == 0)
1157c478bd9Sstevel@tonic-gate rv = mutex_lock(&gps->ps_mutex0);
1167c478bd9Sstevel@tonic-gate else if (num == 1)
1177c478bd9Sstevel@tonic-gate rv = mutex_lock(&gps->ps_mutex1);
1187c478bd9Sstevel@tonic-gate else
1197c478bd9Sstevel@tonic-gate abend("Invalid mutex specified", NULL);
1207c478bd9Sstevel@tonic-gate
1217c478bd9Sstevel@tonic-gate if (rv != 0) {
1227c478bd9Sstevel@tonic-gate char mnum[2];
1237c478bd9Sstevel@tonic-gate mnum[0] = '0' + num;
1247c478bd9Sstevel@tonic-gate mnum[1] = '\0';
1257c478bd9Sstevel@tonic-gate errno = rv;
1267c478bd9Sstevel@tonic-gate perror(command);
1277c478bd9Sstevel@tonic-gate errmsg("cannot grab mutex #", mnum);
1287c478bd9Sstevel@tonic-gate }
1297c478bd9Sstevel@tonic-gate }
1307c478bd9Sstevel@tonic-gate
1317c478bd9Sstevel@tonic-gate /*
1327c478bd9Sstevel@tonic-gate * Exit critical region ---
1337c478bd9Sstevel@tonic-gate * Release other processes waiting on mutex.
1347c478bd9Sstevel@tonic-gate */
1357c478bd9Sstevel@tonic-gate void
Xcritical(int num)1367c478bd9Sstevel@tonic-gate Xcritical(int num)
1377c478bd9Sstevel@tonic-gate {
1387c478bd9Sstevel@tonic-gate int rv;
1397c478bd9Sstevel@tonic-gate
1407c478bd9Sstevel@tonic-gate if (num == 0)
1417c478bd9Sstevel@tonic-gate rv = mutex_unlock(&gps->ps_mutex0);
1427c478bd9Sstevel@tonic-gate else if (num == 1)
1437c478bd9Sstevel@tonic-gate rv = mutex_unlock(&gps->ps_mutex1);
1447c478bd9Sstevel@tonic-gate else
1457c478bd9Sstevel@tonic-gate abend("Invalid mutex specified", NULL);
1467c478bd9Sstevel@tonic-gate
1477c478bd9Sstevel@tonic-gate
1487c478bd9Sstevel@tonic-gate if (rv != 0) {
1497c478bd9Sstevel@tonic-gate char mnum[2];
1507c478bd9Sstevel@tonic-gate mnum[0] = '0' + num;
1517c478bd9Sstevel@tonic-gate mnum[1] = '\0';
1527c478bd9Sstevel@tonic-gate errno = rv;
1537c478bd9Sstevel@tonic-gate perror(command);
1547c478bd9Sstevel@tonic-gate errmsg("cannot release mutex #", mnum);
1557c478bd9Sstevel@tonic-gate }
1567c478bd9Sstevel@tonic-gate }
1577c478bd9Sstevel@tonic-gate
1587c478bd9Sstevel@tonic-gate /*
1597c478bd9Sstevel@tonic-gate * Add process to set of those being traced.
1607c478bd9Sstevel@tonic-gate */
1617c478bd9Sstevel@tonic-gate void
procadd(pid_t spid,const char * lwplist)1627c478bd9Sstevel@tonic-gate procadd(pid_t spid, const char *lwplist)
1637c478bd9Sstevel@tonic-gate {
1647c478bd9Sstevel@tonic-gate int i;
1657c478bd9Sstevel@tonic-gate int j = -1;
1667c478bd9Sstevel@tonic-gate
1677c478bd9Sstevel@tonic-gate if (gps == NULL)
1687c478bd9Sstevel@tonic-gate return;
1697c478bd9Sstevel@tonic-gate
1707c478bd9Sstevel@tonic-gate Ecritical(1);
1717c478bd9Sstevel@tonic-gate for (i = 0; i < sizeof (gps->tpid) / sizeof (gps->tpid[0]); i++) {
1727c478bd9Sstevel@tonic-gate if (gps->tpid[i] == 0) {
1737c478bd9Sstevel@tonic-gate if (j == -1) /* remember first vacant slot */
1747c478bd9Sstevel@tonic-gate j = i;
1757c478bd9Sstevel@tonic-gate if (gps->spid[i] == 0) /* this slot is better */
1767c478bd9Sstevel@tonic-gate break;
1777c478bd9Sstevel@tonic-gate }
1787c478bd9Sstevel@tonic-gate }
1797c478bd9Sstevel@tonic-gate if (i < sizeof (gps->tpid) / sizeof (gps->tpid[0]))
1807c478bd9Sstevel@tonic-gate j = i;
1817c478bd9Sstevel@tonic-gate if (j >= 0) {
1827c478bd9Sstevel@tonic-gate gps->tpid[j] = getpid();
1837c478bd9Sstevel@tonic-gate gps->spid[j] = spid;
1847c478bd9Sstevel@tonic-gate gps->lwps[j] = lwplist;
1857c478bd9Sstevel@tonic-gate }
1867c478bd9Sstevel@tonic-gate Xcritical(1);
1877c478bd9Sstevel@tonic-gate }
1887c478bd9Sstevel@tonic-gate
1897c478bd9Sstevel@tonic-gate /*
1907c478bd9Sstevel@tonic-gate * Delete process from set of those being traced.
1917c478bd9Sstevel@tonic-gate */
1927c478bd9Sstevel@tonic-gate void
procdel()1937c478bd9Sstevel@tonic-gate procdel()
1947c478bd9Sstevel@tonic-gate {
1957c478bd9Sstevel@tonic-gate int i;
1967c478bd9Sstevel@tonic-gate pid_t tpid;
1977c478bd9Sstevel@tonic-gate
1987c478bd9Sstevel@tonic-gate if (gps == NULL)
1997c478bd9Sstevel@tonic-gate return;
2007c478bd9Sstevel@tonic-gate
2017c478bd9Sstevel@tonic-gate tpid = getpid();
2027c478bd9Sstevel@tonic-gate
2037c478bd9Sstevel@tonic-gate Ecritical(1);
2047c478bd9Sstevel@tonic-gate for (i = 0; i < sizeof (gps->tpid) / sizeof (gps->tpid[0]); i++) {
2057c478bd9Sstevel@tonic-gate if (gps->tpid[i] == tpid) {
2067c478bd9Sstevel@tonic-gate gps->tpid[i] = 0;
2077c478bd9Sstevel@tonic-gate break;
2087c478bd9Sstevel@tonic-gate }
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate Xcritical(1);
2117c478bd9Sstevel@tonic-gate }
2127c478bd9Sstevel@tonic-gate
2137c478bd9Sstevel@tonic-gate /*
2147c478bd9Sstevel@tonic-gate * Determine if the lwp for this process should be traced.
2157c478bd9Sstevel@tonic-gate */
2167c478bd9Sstevel@tonic-gate int
lwptrace(pid_t spid,lwpid_t lwpid)2177c478bd9Sstevel@tonic-gate lwptrace(pid_t spid, lwpid_t lwpid)
2187c478bd9Sstevel@tonic-gate {
2197c478bd9Sstevel@tonic-gate int i;
2207c478bd9Sstevel@tonic-gate pid_t tpid;
2217c478bd9Sstevel@tonic-gate const char *lwps;
2227c478bd9Sstevel@tonic-gate
2237c478bd9Sstevel@tonic-gate if (gps == NULL)
2247c478bd9Sstevel@tonic-gate return (0);
2257c478bd9Sstevel@tonic-gate
2267c478bd9Sstevel@tonic-gate tpid = getpid();
2277c478bd9Sstevel@tonic-gate
2287c478bd9Sstevel@tonic-gate Ecritical(1);
2297c478bd9Sstevel@tonic-gate for (i = 0; i < sizeof (gps->tpid) / sizeof (gps->tpid[0]); i++) {
2307c478bd9Sstevel@tonic-gate if (gps->tpid[i] == tpid &&
2317c478bd9Sstevel@tonic-gate gps->spid[i] == spid)
2327c478bd9Sstevel@tonic-gate break;
2337c478bd9Sstevel@tonic-gate }
2347c478bd9Sstevel@tonic-gate lwps = gps->lwps[i];
2357c478bd9Sstevel@tonic-gate Xcritical(1);
2367c478bd9Sstevel@tonic-gate
2377c478bd9Sstevel@tonic-gate return (proc_lwp_in_set(lwps, lwpid));
2387c478bd9Sstevel@tonic-gate }
2397c478bd9Sstevel@tonic-gate
2407c478bd9Sstevel@tonic-gate /*
2417c478bd9Sstevel@tonic-gate * Check for open of a /proc/nnnnn file.
2427c478bd9Sstevel@tonic-gate * Return 0 if this is not an open of a /proc file.
2437c478bd9Sstevel@tonic-gate * Return 1 if the process opened itself.
2447c478bd9Sstevel@tonic-gate * Return 2 if the process failed to open another process
2457c478bd9Sstevel@tonic-gate * in truss's set of controlled processes.
2467c478bd9Sstevel@tonic-gate * Return 3 if the process successfully opened another process
2477c478bd9Sstevel@tonic-gate * in truss's set of controlled processes.
2487c478bd9Sstevel@tonic-gate * We notify and wait for the other controlling truss process
2497c478bd9Sstevel@tonic-gate * to terminate before returning in cases 2 and 3.
2507c478bd9Sstevel@tonic-gate */
2517c478bd9Sstevel@tonic-gate /* ARGSUSED */
2527c478bd9Sstevel@tonic-gate int
checkproc(private_t * pri)2537c478bd9Sstevel@tonic-gate checkproc(private_t *pri)
2547c478bd9Sstevel@tonic-gate {
2557c478bd9Sstevel@tonic-gate char *path = pri->sys_path;
2567c478bd9Sstevel@tonic-gate const pstatus_t *Psp = Pstatus(Proc);
2577c478bd9Sstevel@tonic-gate struct ps_lwphandle *Lwp = pri->Lwp;
2587c478bd9Sstevel@tonic-gate const lwpstatus_t *Lsp = pri->lwpstat;
259*8fd04b83SRoger A. Faulkner int what = Lsp->pr_what; /* one of the SYS_open* syscalls */
2607c478bd9Sstevel@tonic-gate int err = Lsp->pr_errno;
2617c478bd9Sstevel@tonic-gate int pid;
2627c478bd9Sstevel@tonic-gate int i;
2637c478bd9Sstevel@tonic-gate const char *dirname;
2647c478bd9Sstevel@tonic-gate char *next;
2657c478bd9Sstevel@tonic-gate char *sp1;
2667c478bd9Sstevel@tonic-gate char *sp2;
2677c478bd9Sstevel@tonic-gate prgreg_t pc;
2687c478bd9Sstevel@tonic-gate
2697c478bd9Sstevel@tonic-gate /*
2707c478bd9Sstevel@tonic-gate * A bit heuristic ...
2717c478bd9Sstevel@tonic-gate * Test for the cases:
2727c478bd9Sstevel@tonic-gate * 1234
2737c478bd9Sstevel@tonic-gate * 1234/as
2747c478bd9Sstevel@tonic-gate * 1234/ctl
2757c478bd9Sstevel@tonic-gate * 1234/lwp/24/lwpctl
2767c478bd9Sstevel@tonic-gate * .../1234
2777c478bd9Sstevel@tonic-gate * .../1234/as
2787c478bd9Sstevel@tonic-gate * .../1234/ctl
2797c478bd9Sstevel@tonic-gate * .../1234/lwp/24/lwpctl
2807c478bd9Sstevel@tonic-gate * Insert a '\0', if necessary, so the path becomes ".../1234".
2817c478bd9Sstevel@tonic-gate *
2827c478bd9Sstevel@tonic-gate * Along the way, watch out for /proc/self and /proc/1234/lwp/agent
2837c478bd9Sstevel@tonic-gate */
2847c478bd9Sstevel@tonic-gate if ((sp1 = strrchr(path, '/')) == NULL) /* last component */
2857c478bd9Sstevel@tonic-gate /* EMPTY */;
2867c478bd9Sstevel@tonic-gate else if (isdigit(*(sp1+1))) {
2877c478bd9Sstevel@tonic-gate sp1 += strlen(sp1);
2887c478bd9Sstevel@tonic-gate while (--sp1 > path && isdigit(*sp1))
2897c478bd9Sstevel@tonic-gate ;
2907c478bd9Sstevel@tonic-gate if (*sp1 != '/')
2917c478bd9Sstevel@tonic-gate return (0);
2927c478bd9Sstevel@tonic-gate } else if (strcmp(sp1+1, "as") == 0 ||
2937c478bd9Sstevel@tonic-gate strcmp(sp1+1, "ctl") == 0) {
2947c478bd9Sstevel@tonic-gate *sp1 = '\0';
2957c478bd9Sstevel@tonic-gate } else if (strcmp(sp1+1, "lwpctl") == 0) {
2967c478bd9Sstevel@tonic-gate /*
2977c478bd9Sstevel@tonic-gate * .../1234/lwp/24/lwpctl
2987c478bd9Sstevel@tonic-gate * ............ ^-- sp1
2997c478bd9Sstevel@tonic-gate */
3007c478bd9Sstevel@tonic-gate if (sp1-6 >= path && strncmp(sp1-6, "/agent", 6) == 0)
3017c478bd9Sstevel@tonic-gate sp1 -= 6;
3027c478bd9Sstevel@tonic-gate else {
3037c478bd9Sstevel@tonic-gate while (--sp1 > path && isdigit(*sp1))
3047c478bd9Sstevel@tonic-gate ;
3057c478bd9Sstevel@tonic-gate }
3067c478bd9Sstevel@tonic-gate if (*sp1 != '/' ||
3077c478bd9Sstevel@tonic-gate (sp1 -= 4) <= path ||
3087c478bd9Sstevel@tonic-gate strncmp(sp1, "/lwp", 4) != 0)
3097c478bd9Sstevel@tonic-gate return (0);
3107c478bd9Sstevel@tonic-gate *sp1 = '\0';
3117c478bd9Sstevel@tonic-gate } else if (strcmp(sp1+1, "self") != 0) {
3127c478bd9Sstevel@tonic-gate return (0);
3137c478bd9Sstevel@tonic-gate }
3147c478bd9Sstevel@tonic-gate
3157c478bd9Sstevel@tonic-gate if ((sp2 = strrchr(path, '/')) == NULL)
3167c478bd9Sstevel@tonic-gate dirname = path;
3177c478bd9Sstevel@tonic-gate else
3187c478bd9Sstevel@tonic-gate dirname = sp2 + 1;
3197c478bd9Sstevel@tonic-gate
3207c478bd9Sstevel@tonic-gate if (strcmp(dirname, "self") == 0) {
3217c478bd9Sstevel@tonic-gate pid = Psp->pr_pid;
3227c478bd9Sstevel@tonic-gate } else if ((pid = strtol(dirname, &next, 10)) < 0 ||
3237c478bd9Sstevel@tonic-gate *next != '\0') { /* dirname not a number */
3247c478bd9Sstevel@tonic-gate if (sp1 != NULL)
3257c478bd9Sstevel@tonic-gate *sp1 = '/';
3267c478bd9Sstevel@tonic-gate return (0);
3277c478bd9Sstevel@tonic-gate }
3287c478bd9Sstevel@tonic-gate if (sp2 == NULL)
3297c478bd9Sstevel@tonic-gate dirname = ".";
3307c478bd9Sstevel@tonic-gate else {
3317c478bd9Sstevel@tonic-gate *sp2 = '\0';
3327c478bd9Sstevel@tonic-gate dirname = path;
3337c478bd9Sstevel@tonic-gate }
3347c478bd9Sstevel@tonic-gate
3357c478bd9Sstevel@tonic-gate if (!Pisprocdir(Proc, dirname) || /* file not in a /proc directory */
3367c478bd9Sstevel@tonic-gate pid == getpid() || /* process opened truss's /proc file */
3377c478bd9Sstevel@tonic-gate pid == 0) { /* process opened process 0 */
3387c478bd9Sstevel@tonic-gate if (sp1 != NULL)
3397c478bd9Sstevel@tonic-gate *sp1 = '/';
3407c478bd9Sstevel@tonic-gate if (sp2 != NULL)
3417c478bd9Sstevel@tonic-gate *sp2 = '/';
3427c478bd9Sstevel@tonic-gate return (0);
3437c478bd9Sstevel@tonic-gate }
3447c478bd9Sstevel@tonic-gate if (sp1 != NULL)
3457c478bd9Sstevel@tonic-gate *sp1 = '/';
3467c478bd9Sstevel@tonic-gate if (sp2 != NULL)
3477c478bd9Sstevel@tonic-gate *sp2 = '/';
3487c478bd9Sstevel@tonic-gate
3497c478bd9Sstevel@tonic-gate /*
3507c478bd9Sstevel@tonic-gate * Process did open a /proc file ---
3517c478bd9Sstevel@tonic-gate */
3527c478bd9Sstevel@tonic-gate if (pid == Psp->pr_pid) { /* process opened its own /proc file */
3537c478bd9Sstevel@tonic-gate /*
3547c478bd9Sstevel@tonic-gate * In SunOS 5.6 and beyond, self-opens always succeed.
3557c478bd9Sstevel@tonic-gate */
3567c478bd9Sstevel@tonic-gate return (1);
3577c478bd9Sstevel@tonic-gate }
3587c478bd9Sstevel@tonic-gate
3597c478bd9Sstevel@tonic-gate /*
3607c478bd9Sstevel@tonic-gate * Search for a matching pid in our set of controlled processes.
3617c478bd9Sstevel@tonic-gate */
3627c478bd9Sstevel@tonic-gate for (i = 0; i < sizeof (gps->tpid)/sizeof (gps->tpid[0]); i++) {
3637c478bd9Sstevel@tonic-gate if (gps->spid[i] == pid) {
3647c478bd9Sstevel@tonic-gate pid = gps->tpid[i];
3657c478bd9Sstevel@tonic-gate break;
3667c478bd9Sstevel@tonic-gate }
3677c478bd9Sstevel@tonic-gate }
3687c478bd9Sstevel@tonic-gate if (i >= sizeof (gps->tpid) / sizeof (gps->tpid[0])) {
3697c478bd9Sstevel@tonic-gate /*
3707c478bd9Sstevel@tonic-gate * The process opened a /proc file, but not one we care about.
3717c478bd9Sstevel@tonic-gate */
3727c478bd9Sstevel@tonic-gate return (0);
3737c478bd9Sstevel@tonic-gate }
3747c478bd9Sstevel@tonic-gate
3757c478bd9Sstevel@tonic-gate /*
3767c478bd9Sstevel@tonic-gate * Notify and wait for the controlling process to terminate.
3777c478bd9Sstevel@tonic-gate */
3787c478bd9Sstevel@tonic-gate while (pid && gps->tpid[i] == pid) {
3797c478bd9Sstevel@tonic-gate if (kill(pid, SIGUSR1) == -1)
3807c478bd9Sstevel@tonic-gate break;
3817c478bd9Sstevel@tonic-gate (void) usleep(1000000);
3827c478bd9Sstevel@tonic-gate }
3837c478bd9Sstevel@tonic-gate Ecritical(1);
3847c478bd9Sstevel@tonic-gate if (gps->tpid[i] == 0)
3857c478bd9Sstevel@tonic-gate gps->spid[i] = 0;
3867c478bd9Sstevel@tonic-gate Xcritical(1);
3877c478bd9Sstevel@tonic-gate
3887c478bd9Sstevel@tonic-gate if (err) { /* prepare to reissue the failed open() system call */
3897c478bd9Sstevel@tonic-gate #if defined(__sparc)
3907c478bd9Sstevel@tonic-gate (void) Lgetareg(Lwp, R_PC, &pc);
3917c478bd9Sstevel@tonic-gate if (pri->sys_indirect) {
3927c478bd9Sstevel@tonic-gate (void) Lputareg(Lwp, R_G1, (prgreg_t)SYS_syscall);
3937c478bd9Sstevel@tonic-gate (void) Lputareg(Lwp, R_O0, (prgreg_t)what);
3947c478bd9Sstevel@tonic-gate for (i = 0; i < 5; i++)
3957c478bd9Sstevel@tonic-gate (void) Lputareg(Lwp, R_O1+i, pri->sys_args[i]);
3967c478bd9Sstevel@tonic-gate } else {
3977c478bd9Sstevel@tonic-gate (void) Lputareg(Lwp, R_G1, (prgreg_t)what);
3987c478bd9Sstevel@tonic-gate for (i = 0; i < 6; i++)
3997c478bd9Sstevel@tonic-gate (void) Lputareg(Lwp, R_O0+i, pri->sys_args[i]);
4007c478bd9Sstevel@tonic-gate }
4017c478bd9Sstevel@tonic-gate (void) Lputareg(Lwp, R_nPC, pc);
4027c478bd9Sstevel@tonic-gate #elif defined(__amd64)
4037c478bd9Sstevel@tonic-gate (void) Lgetareg(Lwp, R_PC, &pc);
4047c478bd9Sstevel@tonic-gate (void) Lputareg(Lwp, REG_RAX, (prgreg_t)what);
4057c478bd9Sstevel@tonic-gate #elif defined(__i386)
4067c478bd9Sstevel@tonic-gate (void) Lgetareg(Lwp, R_PC, &pc);
4077c478bd9Sstevel@tonic-gate (void) Lputareg(Lwp, EAX, (prgreg_t)what);
4087c478bd9Sstevel@tonic-gate #else
4097c478bd9Sstevel@tonic-gate #error "unrecognized architecture"
4107c478bd9Sstevel@tonic-gate #endif
4117c478bd9Sstevel@tonic-gate (void) Pissyscall_prev(Proc, pc, (uintptr_t *)&pc);
4127c478bd9Sstevel@tonic-gate (void) Lputareg(Lwp, R_PC, pc);
4137c478bd9Sstevel@tonic-gate return (2);
4147c478bd9Sstevel@tonic-gate }
4157c478bd9Sstevel@tonic-gate
4167c478bd9Sstevel@tonic-gate return (3);
4177c478bd9Sstevel@tonic-gate }
418