xref: /titanic_53/usr/src/cmd/ptools/psig/psig.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <stdio.h>
30*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
31*7c478bd9Sstevel@tonic-gate #include <unistd.h>
32*7c478bd9Sstevel@tonic-gate #include <ctype.h>
33*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
34*7c478bd9Sstevel@tonic-gate #include <string.h>
35*7c478bd9Sstevel@tonic-gate #include <signal.h>
36*7c478bd9Sstevel@tonic-gate #include <stddef.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
39*7c478bd9Sstevel@tonic-gate #include <libproc.h>
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate /* evil knowledge of libc internals */
42*7c478bd9Sstevel@tonic-gate #include "../../../lib/libc/inc/thr_uberdata.h"
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate #define	MAX_SYMNAMLEN	1024	/* Recommended max symbol name length */
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate static	char	*sigflags(int, int);
47*7c478bd9Sstevel@tonic-gate static	int	look(char *);
48*7c478bd9Sstevel@tonic-gate static	void	perr(char *);
49*7c478bd9Sstevel@tonic-gate static	int	usage(void);
50*7c478bd9Sstevel@tonic-gate static	uintptr_t deinterpose(int, void *, psinfo_t *, struct sigaction *);
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate static	char	*command;
53*7c478bd9Sstevel@tonic-gate static	char	*procname;
54*7c478bd9Sstevel@tonic-gate static	int	all_flag = 0;
55*7c478bd9Sstevel@tonic-gate static	int	lookuphandlers_flag = 1;
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate int
58*7c478bd9Sstevel@tonic-gate main(int argc, char **argv)
59*7c478bd9Sstevel@tonic-gate {
60*7c478bd9Sstevel@tonic-gate 	int rc = 0;
61*7c478bd9Sstevel@tonic-gate 	int c;
62*7c478bd9Sstevel@tonic-gate 	struct rlimit rlim;
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate 	if ((command = strrchr(argv[0], '/')) != NULL)
65*7c478bd9Sstevel@tonic-gate 		command++;
66*7c478bd9Sstevel@tonic-gate 	else
67*7c478bd9Sstevel@tonic-gate 		command = argv[0];
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "an")) != EOF) {
70*7c478bd9Sstevel@tonic-gate 		switch (c) {
71*7c478bd9Sstevel@tonic-gate 		case 'a':
72*7c478bd9Sstevel@tonic-gate 			all_flag = 1;
73*7c478bd9Sstevel@tonic-gate 			break;
74*7c478bd9Sstevel@tonic-gate 		case 'n':
75*7c478bd9Sstevel@tonic-gate 			lookuphandlers_flag = 0;
76*7c478bd9Sstevel@tonic-gate 			break;
77*7c478bd9Sstevel@tonic-gate 		default:
78*7c478bd9Sstevel@tonic-gate 			return (usage());
79*7c478bd9Sstevel@tonic-gate 		}
80*7c478bd9Sstevel@tonic-gate 	}
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate 	if (argc - optind < 1) {
83*7c478bd9Sstevel@tonic-gate 		return (usage());
84*7c478bd9Sstevel@tonic-gate 	}
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate 	/*
87*7c478bd9Sstevel@tonic-gate 	 * Make sure we'll have enough file descriptors to handle a target
88*7c478bd9Sstevel@tonic-gate 	 * that has many many mappings.
89*7c478bd9Sstevel@tonic-gate 	 */
90*7c478bd9Sstevel@tonic-gate 	if (getrlimit(RLIMIT_NOFILE, &rlim) == 0) {
91*7c478bd9Sstevel@tonic-gate 		rlim.rlim_cur = rlim.rlim_max;
92*7c478bd9Sstevel@tonic-gate 		(void) setrlimit(RLIMIT_NOFILE, &rlim);
93*7c478bd9Sstevel@tonic-gate 	}
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate 	for (; optind != argc; optind++) {
96*7c478bd9Sstevel@tonic-gate 		rc += look(argv[optind]);
97*7c478bd9Sstevel@tonic-gate 	}
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate 	return (rc);
100*7c478bd9Sstevel@tonic-gate }
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate static int
103*7c478bd9Sstevel@tonic-gate usage(void)
104*7c478bd9Sstevel@tonic-gate {
105*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "usage:\t%s [-n] pid ...\n", command);
106*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "  (report process signal actions)\n");
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate 	return (2);
109*7c478bd9Sstevel@tonic-gate }
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate static uintptr_t
112*7c478bd9Sstevel@tonic-gate uberdata_addr(struct ps_prochandle *Pr, char dmodel)
113*7c478bd9Sstevel@tonic-gate {
114*7c478bd9Sstevel@tonic-gate 	GElf_Sym sym;
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate 	if (Plookup_by_name(Pr, "libc.so", "_tdb_bootstrap", &sym) < 0)
117*7c478bd9Sstevel@tonic-gate 		return (NULL);
118*7c478bd9Sstevel@tonic-gate #ifdef _LP64
119*7c478bd9Sstevel@tonic-gate 	if (dmodel != PR_MODEL_NATIVE) {
120*7c478bd9Sstevel@tonic-gate 		caddr32_t uaddr;
121*7c478bd9Sstevel@tonic-gate 		caddr32_t addr;
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate 		if (Pread(Pr, &addr, sizeof (addr), sym.st_value)
124*7c478bd9Sstevel@tonic-gate 		    == sizeof (addr) &&
125*7c478bd9Sstevel@tonic-gate 		    addr != 0 &&
126*7c478bd9Sstevel@tonic-gate 		    Pread(Pr, &uaddr, sizeof (uaddr), (uintptr_t)addr)
127*7c478bd9Sstevel@tonic-gate 		    == sizeof (uaddr) &&
128*7c478bd9Sstevel@tonic-gate 		    uaddr != 0)
129*7c478bd9Sstevel@tonic-gate 			return ((uintptr_t)uaddr);
130*7c478bd9Sstevel@tonic-gate 	}
131*7c478bd9Sstevel@tonic-gate #endif
132*7c478bd9Sstevel@tonic-gate 	if (dmodel == PR_MODEL_NATIVE) {
133*7c478bd9Sstevel@tonic-gate 		uintptr_t uaddr;
134*7c478bd9Sstevel@tonic-gate 		uintptr_t addr;
135*7c478bd9Sstevel@tonic-gate 
136*7c478bd9Sstevel@tonic-gate 		if (Pread(Pr, &addr, sizeof (addr), sym.st_value)
137*7c478bd9Sstevel@tonic-gate 		    == sizeof (addr) &&
138*7c478bd9Sstevel@tonic-gate 		    addr != 0 &&
139*7c478bd9Sstevel@tonic-gate 		    Pread(Pr, &uaddr, sizeof (uaddr), addr)
140*7c478bd9Sstevel@tonic-gate 		    == sizeof (uaddr) &&
141*7c478bd9Sstevel@tonic-gate 		    uaddr != 0)
142*7c478bd9Sstevel@tonic-gate 			return (uaddr);
143*7c478bd9Sstevel@tonic-gate 	}
144*7c478bd9Sstevel@tonic-gate 	if (Plookup_by_name(Pr, "libc.so", "_uberdata", &sym) < 0)
145*7c478bd9Sstevel@tonic-gate 		return (0);
146*7c478bd9Sstevel@tonic-gate 	return (sym.st_value);
147*7c478bd9Sstevel@tonic-gate }
148*7c478bd9Sstevel@tonic-gate 
149*7c478bd9Sstevel@tonic-gate /*
150*7c478bd9Sstevel@tonic-gate  * Iterator function used to generate the process sigmask
151*7c478bd9Sstevel@tonic-gate  * from the individual lwp sigmasks.
152*7c478bd9Sstevel@tonic-gate  */
153*7c478bd9Sstevel@tonic-gate static int
154*7c478bd9Sstevel@tonic-gate lwp_iter(void *cd, const lwpstatus_t *lwpstatus)
155*7c478bd9Sstevel@tonic-gate {
156*7c478bd9Sstevel@tonic-gate 	sigset_t *ssp = cd;
157*7c478bd9Sstevel@tonic-gate 
158*7c478bd9Sstevel@tonic-gate 	ssp->__sigbits[0] &= lwpstatus->pr_lwphold.__sigbits[0];
159*7c478bd9Sstevel@tonic-gate 	ssp->__sigbits[1] &= lwpstatus->pr_lwphold.__sigbits[1];
160*7c478bd9Sstevel@tonic-gate 	ssp->__sigbits[2] &= lwpstatus->pr_lwphold.__sigbits[2];
161*7c478bd9Sstevel@tonic-gate 	ssp->__sigbits[3] &= lwpstatus->pr_lwphold.__sigbits[3];
162*7c478bd9Sstevel@tonic-gate 
163*7c478bd9Sstevel@tonic-gate 	/*
164*7c478bd9Sstevel@tonic-gate 	 * Return non-zero to terminate the iteration
165*7c478bd9Sstevel@tonic-gate 	 * if the sigmask has become all zeros.
166*7c478bd9Sstevel@tonic-gate 	 */
167*7c478bd9Sstevel@tonic-gate 	return ((ssp->__sigbits[0] | ssp->__sigbits[1] |
168*7c478bd9Sstevel@tonic-gate 	    ssp->__sigbits[2] | ssp->__sigbits[3]) == 0);
169*7c478bd9Sstevel@tonic-gate }
170*7c478bd9Sstevel@tonic-gate 
171*7c478bd9Sstevel@tonic-gate static int
172*7c478bd9Sstevel@tonic-gate look(char *arg)
173*7c478bd9Sstevel@tonic-gate {
174*7c478bd9Sstevel@tonic-gate 	char pathname[100];
175*7c478bd9Sstevel@tonic-gate 	struct stat statb;
176*7c478bd9Sstevel@tonic-gate 	int fd = -1;
177*7c478bd9Sstevel@tonic-gate 	int sig, gcode;
178*7c478bd9Sstevel@tonic-gate 	sigset_t holdmask;
179*7c478bd9Sstevel@tonic-gate 	int maxsig;
180*7c478bd9Sstevel@tonic-gate 	struct sigaction *action = NULL;
181*7c478bd9Sstevel@tonic-gate 	psinfo_t psinfo;
182*7c478bd9Sstevel@tonic-gate 	const psinfo_t *psinfop;
183*7c478bd9Sstevel@tonic-gate 	struct ps_prochandle *Pr = NULL;
184*7c478bd9Sstevel@tonic-gate 	uintptr_t uberaddr;
185*7c478bd9Sstevel@tonic-gate 	uintptr_t aharraddr;
186*7c478bd9Sstevel@tonic-gate 	uintptr_t intfnaddr;
187*7c478bd9Sstevel@tonic-gate 	size_t aharrlen;
188*7c478bd9Sstevel@tonic-gate 	void *aharr = NULL;
189*7c478bd9Sstevel@tonic-gate 	int error = 1;
190*7c478bd9Sstevel@tonic-gate 
191*7c478bd9Sstevel@tonic-gate 	procname = arg;		/* for perr() */
192*7c478bd9Sstevel@tonic-gate 	if ((Pr = proc_arg_grab(arg, PR_ARG_PIDS, PGRAB_RDONLY|PGRAB_FORCE,
193*7c478bd9Sstevel@tonic-gate 	    &gcode)) == NULL || (psinfop = Ppsinfo(Pr)) == NULL) {
194*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: cannot examine %s: %s\n",
195*7c478bd9Sstevel@tonic-gate 		    command, arg, Pgrab_error(gcode));
196*7c478bd9Sstevel@tonic-gate 		goto look_error;
197*7c478bd9Sstevel@tonic-gate 	}
198*7c478bd9Sstevel@tonic-gate 	(void) memcpy(&psinfo, psinfop, sizeof (psinfo_t));
199*7c478bd9Sstevel@tonic-gate 	proc_unctrl_psinfo(&psinfo);
200*7c478bd9Sstevel@tonic-gate 
201*7c478bd9Sstevel@tonic-gate 	(void) sprintf(pathname, "/proc/%d/sigact", (int)psinfo.pr_pid);
202*7c478bd9Sstevel@tonic-gate 	if ((fd = open(pathname, O_RDONLY)) < 0) {
203*7c478bd9Sstevel@tonic-gate 		perr("open sigact");
204*7c478bd9Sstevel@tonic-gate 		goto look_error;
205*7c478bd9Sstevel@tonic-gate 	}
206*7c478bd9Sstevel@tonic-gate 
207*7c478bd9Sstevel@tonic-gate 	if (fstat(fd, &statb) != 0) {
208*7c478bd9Sstevel@tonic-gate 		perr("fstat sigact");
209*7c478bd9Sstevel@tonic-gate 		goto look_error;
210*7c478bd9Sstevel@tonic-gate 	}
211*7c478bd9Sstevel@tonic-gate 	maxsig = statb.st_size / sizeof (struct sigaction);
212*7c478bd9Sstevel@tonic-gate 	action = malloc(maxsig * sizeof (struct sigaction));
213*7c478bd9Sstevel@tonic-gate 	if (action == NULL) {
214*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
215*7c478bd9Sstevel@tonic-gate 		"%s: cannot malloc() space for %d sigaction structures\n",
216*7c478bd9Sstevel@tonic-gate 			command, maxsig);
217*7c478bd9Sstevel@tonic-gate 		goto look_error;
218*7c478bd9Sstevel@tonic-gate 	}
219*7c478bd9Sstevel@tonic-gate 	if (read(fd, (char *)action, maxsig * sizeof (struct sigaction)) !=
220*7c478bd9Sstevel@tonic-gate 	    maxsig * sizeof (struct sigaction)) {
221*7c478bd9Sstevel@tonic-gate 		perr("read sigact");
222*7c478bd9Sstevel@tonic-gate 		goto look_error;
223*7c478bd9Sstevel@tonic-gate 	}
224*7c478bd9Sstevel@tonic-gate 	(void) close(fd);
225*7c478bd9Sstevel@tonic-gate 	fd = -1;
226*7c478bd9Sstevel@tonic-gate 
227*7c478bd9Sstevel@tonic-gate 	(void) printf("%d:\t%.70s\n", (int)psinfo.pr_pid, psinfo.pr_psargs);
228*7c478bd9Sstevel@tonic-gate 
229*7c478bd9Sstevel@tonic-gate 	(void) sigfillset(&holdmask);
230*7c478bd9Sstevel@tonic-gate 	(void) Plwp_iter(Pr, lwp_iter, &holdmask);
231*7c478bd9Sstevel@tonic-gate 
232*7c478bd9Sstevel@tonic-gate 	if ((uberaddr = uberdata_addr(Pr, psinfo.pr_dmodel)) == 0) {
233*7c478bd9Sstevel@tonic-gate 		aharraddr = 0;
234*7c478bd9Sstevel@tonic-gate 		aharrlen = 0;
235*7c478bd9Sstevel@tonic-gate 		intfnaddr = 0;
236*7c478bd9Sstevel@tonic-gate 	} else {
237*7c478bd9Sstevel@tonic-gate #ifdef _LP64
238*7c478bd9Sstevel@tonic-gate 		if (psinfo.pr_dmodel != PR_MODEL_NATIVE) {
239*7c478bd9Sstevel@tonic-gate 			caddr32_t addr;
240*7c478bd9Sstevel@tonic-gate 			aharraddr = uberaddr +
241*7c478bd9Sstevel@tonic-gate 				offsetof(uberdata32_t, siguaction);
242*7c478bd9Sstevel@tonic-gate 			aharrlen = sizeof (siguaction32_t) * NSIG;
243*7c478bd9Sstevel@tonic-gate 			(void) Pread(Pr, &addr, sizeof (addr),
244*7c478bd9Sstevel@tonic-gate 			    uberaddr + offsetof(uberdata32_t, sigacthandler));
245*7c478bd9Sstevel@tonic-gate 			intfnaddr = (uintptr_t)addr;
246*7c478bd9Sstevel@tonic-gate 		} else
247*7c478bd9Sstevel@tonic-gate #endif
248*7c478bd9Sstevel@tonic-gate 		{
249*7c478bd9Sstevel@tonic-gate 			aharraddr = uberaddr +
250*7c478bd9Sstevel@tonic-gate 				offsetof(uberdata_t, siguaction);
251*7c478bd9Sstevel@tonic-gate 			aharrlen = sizeof (siguaction_t) * NSIG;
252*7c478bd9Sstevel@tonic-gate 			(void) Pread(Pr, &intfnaddr, sizeof (intfnaddr),
253*7c478bd9Sstevel@tonic-gate 			    uberaddr + offsetof(uberdata_t, sigacthandler));
254*7c478bd9Sstevel@tonic-gate 		}
255*7c478bd9Sstevel@tonic-gate 	}
256*7c478bd9Sstevel@tonic-gate 
257*7c478bd9Sstevel@tonic-gate 	if (aharraddr) {
258*7c478bd9Sstevel@tonic-gate 		aharr = malloc(aharrlen);
259*7c478bd9Sstevel@tonic-gate 		if (aharr == NULL) {
260*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
261*7c478bd9Sstevel@tonic-gate 			"%s: cannot malloc() space for actual handler array\n",
262*7c478bd9Sstevel@tonic-gate 			    command);
263*7c478bd9Sstevel@tonic-gate 			goto look_error;
264*7c478bd9Sstevel@tonic-gate 		}
265*7c478bd9Sstevel@tonic-gate 
266*7c478bd9Sstevel@tonic-gate 		if (Pread(Pr, aharr, aharrlen, aharraddr) != aharrlen) {
267*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
268*7c478bd9Sstevel@tonic-gate 			    "%s: signal handler data at %p cannot be read.\n",
269*7c478bd9Sstevel@tonic-gate 			    command, (void *)aharraddr);
270*7c478bd9Sstevel@tonic-gate 			free(aharr);
271*7c478bd9Sstevel@tonic-gate 			aharr = NULL;
272*7c478bd9Sstevel@tonic-gate 		}
273*7c478bd9Sstevel@tonic-gate 	}
274*7c478bd9Sstevel@tonic-gate 
275*7c478bd9Sstevel@tonic-gate 	for (sig = 1; sig <= maxsig; sig++) {
276*7c478bd9Sstevel@tonic-gate 		struct sigaction *sp = &action[sig - 1];
277*7c478bd9Sstevel@tonic-gate 		int caught = 0;
278*7c478bd9Sstevel@tonic-gate 		char buf[SIG2STR_MAX];
279*7c478bd9Sstevel@tonic-gate 		char *s;
280*7c478bd9Sstevel@tonic-gate 
281*7c478bd9Sstevel@tonic-gate 		/* proc_signame() returns "SIG..."; skip the "SIG" part */
282*7c478bd9Sstevel@tonic-gate 		(void) printf("%s\t", proc_signame(sig, buf, sizeof (buf)) + 3);
283*7c478bd9Sstevel@tonic-gate 
284*7c478bd9Sstevel@tonic-gate 		if (prismember(&holdmask, sig))
285*7c478bd9Sstevel@tonic-gate 			(void) printf("blocked,");
286*7c478bd9Sstevel@tonic-gate 
287*7c478bd9Sstevel@tonic-gate 		if (sp->sa_handler == SIG_DFL)
288*7c478bd9Sstevel@tonic-gate 			(void) printf("default");
289*7c478bd9Sstevel@tonic-gate 		else if (sp->sa_handler == SIG_IGN)
290*7c478bd9Sstevel@tonic-gate 			(void) printf("ignored");
291*7c478bd9Sstevel@tonic-gate 		else
292*7c478bd9Sstevel@tonic-gate 			caught = 1;
293*7c478bd9Sstevel@tonic-gate 
294*7c478bd9Sstevel@tonic-gate 		if (caught || all_flag) {
295*7c478bd9Sstevel@tonic-gate 			uintptr_t haddr;
296*7c478bd9Sstevel@tonic-gate 			GElf_Sym hsym;
297*7c478bd9Sstevel@tonic-gate 			char hname[MAX_SYMNAMLEN];
298*7c478bd9Sstevel@tonic-gate 			char buf[PRSIGBUFSZ];
299*7c478bd9Sstevel@tonic-gate 
300*7c478bd9Sstevel@tonic-gate 			haddr = (uintptr_t)sp->sa_handler;
301*7c478bd9Sstevel@tonic-gate 
302*7c478bd9Sstevel@tonic-gate 			if (aharr && intfnaddr && haddr == intfnaddr)
303*7c478bd9Sstevel@tonic-gate 				haddr = deinterpose(sig, aharr, &psinfo, sp);
304*7c478bd9Sstevel@tonic-gate 
305*7c478bd9Sstevel@tonic-gate 			if (haddr == (uintptr_t)SIG_DFL) {
306*7c478bd9Sstevel@tonic-gate 				if (caught)
307*7c478bd9Sstevel@tonic-gate 					(void) printf("default");
308*7c478bd9Sstevel@tonic-gate 				caught = 0;
309*7c478bd9Sstevel@tonic-gate 			} else if (haddr == (uintptr_t)SIG_IGN) {
310*7c478bd9Sstevel@tonic-gate 				if (caught)
311*7c478bd9Sstevel@tonic-gate 					(void) printf("ignored");
312*7c478bd9Sstevel@tonic-gate 				caught = 0;
313*7c478bd9Sstevel@tonic-gate 			} else {
314*7c478bd9Sstevel@tonic-gate 				if (caught)
315*7c478bd9Sstevel@tonic-gate 					(void) printf("caught");
316*7c478bd9Sstevel@tonic-gate 			}
317*7c478bd9Sstevel@tonic-gate 
318*7c478bd9Sstevel@tonic-gate 			if (caught || all_flag) {
319*7c478bd9Sstevel@tonic-gate 				if (lookuphandlers_flag && haddr > 1 &&
320*7c478bd9Sstevel@tonic-gate 				    Plookup_by_addr(Pr, haddr, hname,
321*7c478bd9Sstevel@tonic-gate 				    sizeof (hname), &hsym) == 0)
322*7c478bd9Sstevel@tonic-gate 					(void) printf("\t%-8s", hname);
323*7c478bd9Sstevel@tonic-gate 				else
324*7c478bd9Sstevel@tonic-gate 					(void) printf("\t0x%-8lx",
325*7c478bd9Sstevel@tonic-gate 						(ulong_t)haddr);
326*7c478bd9Sstevel@tonic-gate 
327*7c478bd9Sstevel@tonic-gate 				s = sigflags(sig, sp->sa_flags);
328*7c478bd9Sstevel@tonic-gate 				(void) printf("%s", (*s != '\0')? s : "\t0");
329*7c478bd9Sstevel@tonic-gate 				(void) proc_sigset2str(&sp->sa_mask, ",", 1,
330*7c478bd9Sstevel@tonic-gate 				    buf, sizeof (buf));
331*7c478bd9Sstevel@tonic-gate 				if (buf[0] != '\0')
332*7c478bd9Sstevel@tonic-gate 					(void) printf("\t%s", buf);
333*7c478bd9Sstevel@tonic-gate 			}
334*7c478bd9Sstevel@tonic-gate 		} else if (sig == SIGCLD) {
335*7c478bd9Sstevel@tonic-gate 			s = sigflags(sig,
336*7c478bd9Sstevel@tonic-gate 				sp->sa_flags & (SA_NOCLDWAIT|SA_NOCLDSTOP));
337*7c478bd9Sstevel@tonic-gate 			if (*s != '\0')
338*7c478bd9Sstevel@tonic-gate 				(void) printf("\t\t%s", s);
339*7c478bd9Sstevel@tonic-gate 		}
340*7c478bd9Sstevel@tonic-gate 		(void) printf("\n");
341*7c478bd9Sstevel@tonic-gate 	}
342*7c478bd9Sstevel@tonic-gate 
343*7c478bd9Sstevel@tonic-gate 	error = 0;
344*7c478bd9Sstevel@tonic-gate 
345*7c478bd9Sstevel@tonic-gate look_error:
346*7c478bd9Sstevel@tonic-gate 	if (fd >= 0)
347*7c478bd9Sstevel@tonic-gate 		(void) close(fd);
348*7c478bd9Sstevel@tonic-gate 	if (aharr)
349*7c478bd9Sstevel@tonic-gate 		free(aharr);
350*7c478bd9Sstevel@tonic-gate 	if (action)
351*7c478bd9Sstevel@tonic-gate 		free(action);
352*7c478bd9Sstevel@tonic-gate 	if (Pr)
353*7c478bd9Sstevel@tonic-gate 		Prelease(Pr, 0);
354*7c478bd9Sstevel@tonic-gate 	return (error);
355*7c478bd9Sstevel@tonic-gate }
356*7c478bd9Sstevel@tonic-gate 
357*7c478bd9Sstevel@tonic-gate static void
358*7c478bd9Sstevel@tonic-gate perr(char *s)
359*7c478bd9Sstevel@tonic-gate {
360*7c478bd9Sstevel@tonic-gate 	if (s)
361*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: ", procname);
362*7c478bd9Sstevel@tonic-gate 	else
363*7c478bd9Sstevel@tonic-gate 		s = procname;
364*7c478bd9Sstevel@tonic-gate 	perror(s);
365*7c478bd9Sstevel@tonic-gate }
366*7c478bd9Sstevel@tonic-gate 
367*7c478bd9Sstevel@tonic-gate static char *
368*7c478bd9Sstevel@tonic-gate sigflags(int sig, int flags)
369*7c478bd9Sstevel@tonic-gate {
370*7c478bd9Sstevel@tonic-gate 	static char code_buf[100];
371*7c478bd9Sstevel@tonic-gate 	char *str = code_buf;
372*7c478bd9Sstevel@tonic-gate 	int flagmask =
373*7c478bd9Sstevel@tonic-gate 		(SA_ONSTACK|SA_RESETHAND|SA_RESTART|SA_SIGINFO|SA_NODEFER);
374*7c478bd9Sstevel@tonic-gate 
375*7c478bd9Sstevel@tonic-gate 	if (sig == SIGCLD)
376*7c478bd9Sstevel@tonic-gate 		flagmask |= (SA_NOCLDSTOP|SA_NOCLDWAIT);
377*7c478bd9Sstevel@tonic-gate 
378*7c478bd9Sstevel@tonic-gate 	*str = '\0';
379*7c478bd9Sstevel@tonic-gate 	if (flags & ~flagmask)
380*7c478bd9Sstevel@tonic-gate 		(void) sprintf(str, ",0x%x,", flags & ~flagmask);
381*7c478bd9Sstevel@tonic-gate 	else if (flags == 0)
382*7c478bd9Sstevel@tonic-gate 		return (str);
383*7c478bd9Sstevel@tonic-gate 
384*7c478bd9Sstevel@tonic-gate 	if (flags & SA_RESTART)
385*7c478bd9Sstevel@tonic-gate 		(void) strcat(str, ",RESTART");
386*7c478bd9Sstevel@tonic-gate 	if (flags & SA_RESETHAND)
387*7c478bd9Sstevel@tonic-gate 		(void) strcat(str, ",RESETHAND");
388*7c478bd9Sstevel@tonic-gate 	if (flags & SA_ONSTACK)
389*7c478bd9Sstevel@tonic-gate 		(void) strcat(str, ",ONSTACK");
390*7c478bd9Sstevel@tonic-gate 	if (flags & SA_SIGINFO)
391*7c478bd9Sstevel@tonic-gate 		(void) strcat(str, ",SIGINFO");
392*7c478bd9Sstevel@tonic-gate 	if (flags & SA_NODEFER)
393*7c478bd9Sstevel@tonic-gate 		(void) strcat(str, ",NODEFER");
394*7c478bd9Sstevel@tonic-gate 
395*7c478bd9Sstevel@tonic-gate 	if (sig == SIGCLD) {
396*7c478bd9Sstevel@tonic-gate 		if (flags & SA_NOCLDWAIT)
397*7c478bd9Sstevel@tonic-gate 			(void) strcat(str, ",NOCLDWAIT");
398*7c478bd9Sstevel@tonic-gate 		if (flags & SA_NOCLDSTOP)
399*7c478bd9Sstevel@tonic-gate 			(void) strcat(str, ",NOCLDSTOP");
400*7c478bd9Sstevel@tonic-gate 	}
401*7c478bd9Sstevel@tonic-gate 
402*7c478bd9Sstevel@tonic-gate 	*str = '\t';
403*7c478bd9Sstevel@tonic-gate 
404*7c478bd9Sstevel@tonic-gate 	return (str);
405*7c478bd9Sstevel@tonic-gate }
406*7c478bd9Sstevel@tonic-gate 
407*7c478bd9Sstevel@tonic-gate /*ARGSUSED2*/
408*7c478bd9Sstevel@tonic-gate static uintptr_t
409*7c478bd9Sstevel@tonic-gate deinterpose(int sig, void *aharr, psinfo_t *psinfo, struct sigaction *sp)
410*7c478bd9Sstevel@tonic-gate {
411*7c478bd9Sstevel@tonic-gate 	if (sp->sa_handler == SIG_DFL || sp->sa_handler == SIG_IGN)
412*7c478bd9Sstevel@tonic-gate 		return ((uintptr_t)sp->sa_handler);
413*7c478bd9Sstevel@tonic-gate #ifdef _LP64
414*7c478bd9Sstevel@tonic-gate 	if (psinfo->pr_dmodel != PR_MODEL_NATIVE) {
415*7c478bd9Sstevel@tonic-gate 		struct sigaction32 *sa32 = (struct sigaction32 *)
416*7c478bd9Sstevel@tonic-gate 			((uintptr_t)aharr + sig * sizeof (siguaction32_t) +
417*7c478bd9Sstevel@tonic-gate 			offsetof(siguaction32_t, sig_uaction));
418*7c478bd9Sstevel@tonic-gate 
419*7c478bd9Sstevel@tonic-gate 		sp->sa_flags = sa32->sa_flags;
420*7c478bd9Sstevel@tonic-gate 		sp->sa_handler = (void (*)())(uintptr_t)sa32->sa_handler;
421*7c478bd9Sstevel@tonic-gate 		(void) memcpy(&sp->sa_mask, &sa32->sa_mask,
422*7c478bd9Sstevel@tonic-gate 			sizeof (sp->sa_mask));
423*7c478bd9Sstevel@tonic-gate 	} else
424*7c478bd9Sstevel@tonic-gate #endif
425*7c478bd9Sstevel@tonic-gate 	{
426*7c478bd9Sstevel@tonic-gate 		struct sigaction *sa = (struct sigaction *)
427*7c478bd9Sstevel@tonic-gate 			((uintptr_t)aharr + sig * sizeof (siguaction_t) +
428*7c478bd9Sstevel@tonic-gate 			offsetof(siguaction_t, sig_uaction));
429*7c478bd9Sstevel@tonic-gate 
430*7c478bd9Sstevel@tonic-gate 		sp->sa_flags = sa->sa_flags;
431*7c478bd9Sstevel@tonic-gate 		sp->sa_handler = sa->sa_handler;
432*7c478bd9Sstevel@tonic-gate 		sp->sa_mask = sa->sa_mask;
433*7c478bd9Sstevel@tonic-gate 	}
434*7c478bd9Sstevel@tonic-gate 	return ((uintptr_t)sp->sa_handler);
435*7c478bd9Sstevel@tonic-gate }
436