xref: /illumos-gate/usr/src/lib/libc/port/gen/psiginfo.c (revision 43449cdcd0600512dd862537f2cf014140dd0844)
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 2015 Circonus, Inc.  All rights reserved.
23  */
24 
25 /*
26  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
27  * Use is subject to license terms.
28  */
29 
30 /*	Copyright (c) 1988 AT&T	*/
31 /*	  All Rights Reserved  	*/
32 
33 /*
34  * Print the name of the siginfo indicated by "sig", along with the
35  * supplied message
36  */
37 
38 #include "lint.h"
39 #include "_libc_gettext.h"
40 #include <sys/types.h>
41 #include <stdio.h>
42 #include <unistd.h>
43 #include <string.h>
44 #include <signal.h>
45 #include <siginfo.h>
46 
47 #define	strsignal(i)	(_libc_gettext(_sys_siglistp[i]))
48 
49 void
50 psiginfo(const siginfo_t *sip, const char *s)
51 {
52 	char buf[256];
53 	char *c;
54 	size_t l = 0;
55 	const struct siginfolist *listp;
56 
57 	if (sip == NULL)
58 		return;
59 
60 	if (s != NULL && *s != '\0') {
61 		l = snprintf(buf, sizeof (buf), _libc_gettext("%s : "), s);
62 		if (l > sizeof (buf))
63 			l = sizeof (buf);
64 	}
65 
66 
67 	if (sip->si_code <= 0) {
68 		(void) snprintf(buf + l, sizeof (buf) - l,
69 		    _libc_gettext("%s ( from process  %d )\n"),
70 		    strsignal(sip->si_signo), sip->si_pid);
71 	} else if (((listp = &_sys_siginfolist[sip->si_signo-1]) != NULL) &&
72 	    sip->si_code <= listp->nsiginfo) {
73 		c = _libc_gettext(listp->vsiginfo[sip->si_code-1]);
74 		switch (sip->si_signo) {
75 		case SIGSEGV:
76 		case SIGBUS:
77 		case SIGILL:
78 		case SIGFPE:
79 			(void) snprintf(buf + l, sizeof (buf) - l,
80 			    _libc_gettext("%s ( [%p] %s)\n"),
81 			    strsignal(sip->si_signo),
82 			    sip->si_addr, c);
83 			break;
84 		default:
85 			(void) snprintf(buf + l, sizeof (buf) - l,
86 			    _libc_gettext("%s (%s)\n"),
87 			    strsignal(sip->si_signo), c);
88 			break;
89 		}
90 	} else {
91 		(void) snprintf(buf + l, sizeof (buf) - l,
92 		    _libc_gettext("%s\n"), strsignal(sip->si_signo));
93 	}
94 	(void) write(2, buf, strlen(buf));
95 }
96