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