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