xref: /illumos-gate/usr/src/lib/libcontract/common/process_dump.c (revision 20a7641f9918de8574b8b3b47dbe35c4bfc78df1)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <sys/contract/process.h>
28 #include <sys/wait.h>
29 #include <sys/ctfs.h>
30 #include <unistd.h>
31 #include <fcntl.h>
32 #include <errno.h>
33 #include <string.h>
34 #include <limits.h>
35 #include <stdio.h>
36 #include <assert.h>
37 #include <signal.h>
38 #include <libuutil.h>
39 #include <libintl.h>
40 #include <libcontract.h>
41 #include <libcontract_priv.h>
42 #include "libcontract_impl.h"
43 
44 void
45 event_process(FILE *file, ct_evthdl_t ev, int verbose)
46 {
47 	uint_t type;
48 	pid_t pid;
49 	char *s;
50 
51 	type = ct_event_get_type(ev);
52 	if (ct_pr_event_get_pid(ev, &pid) != 0) {
53 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "[bad event]\n"));
54 		return;
55 	}
56 
57 	switch (type) {
58 	case CT_PR_EV_EMPTY:
59 		s = dgettext(TEXT_DOMAIN, "contract empty\n");
60 		break;
61 	case CT_PR_EV_FORK:
62 		s = dgettext(TEXT_DOMAIN, "process %d was created\n");
63 		break;
64 	case CT_PR_EV_EXIT:
65 		s = dgettext(TEXT_DOMAIN, "process %d exited\n");
66 		break;
67 	case CT_PR_EV_CORE:
68 		s = dgettext(TEXT_DOMAIN, "process %d dumped core\n");
69 		break;
70 	case CT_PR_EV_SIGNAL:
71 		s = dgettext(TEXT_DOMAIN,
72 		    "process %d received a fatal signal\n");
73 		break;
74 	case CT_PR_EV_HWERR:
75 		s = dgettext(TEXT_DOMAIN,
76 		    "process %d was killed by a hardware error\n");
77 		break;
78 	default:
79 		s = dgettext(TEXT_DOMAIN, "process %d sent an unknown event\n");
80 		break;
81 	}
82 
83 	/*LINTED*/
84 	(void) fprintf(file, s, pid);
85 	if (!verbose)
86 		return;
87 
88 	switch (type) {
89 		int i;
90 		const char *c;
91 		char buf[SIG2STR_MAX];
92 		ctid_t ctid;
93 	case CT_PR_EV_FORK:
94 		if (ct_pr_event_get_ppid(ev, &pid) == 0)
95 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
96 			    "\tparent pid: %d\n"), pid);
97 		break;
98 	case CT_PR_EV_EXIT:
99 		if (ct_pr_event_get_exitstatus(ev, &i) != 0)
100 			break;
101 		(void) fprintf(file,
102 		    dgettext(TEXT_DOMAIN, "\twait status: 0x%x"), i);
103 		if (WIFEXITED(i)) {
104 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
105 			    " (exited, code %d)\n"), WEXITSTATUS(i));
106 		} else if (WIFSIGNALED(i)) {
107 			int sig = WTERMSIG(i);
108 			(void) fprintf(file,
109 			    dgettext(TEXT_DOMAIN, " (signal %d"), sig);
110 			if (sig2str(sig, buf) == 0)
111 				(void) fprintf(file,
112 				    dgettext(TEXT_DOMAIN, " (SIG%s)"), buf);
113 			if (WCOREDUMP(i))
114 				(void) fprintf(file,
115 				    dgettext(TEXT_DOMAIN, ", core dumped)\n"));
116 			else
117 				(void) fprintf(file,
118 				    dgettext(TEXT_DOMAIN, ")\n"));
119 		} else {
120 			/*
121 			 * We really shouldn't get here.
122 			 */
123 			(void) fprintf(file, dgettext(TEXT_DOMAIN, "\n"));
124 		}
125 		break;
126 	case CT_PR_EV_CORE:
127 		if (ct_pr_event_get_pcorefile(ev, &c) == 0)
128 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
129 			    "\tprocess core: %s\n"), c);
130 		if (ct_pr_event_get_gcorefile(ev, &c) == 0)
131 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
132 			    "\tglobal core: %s\n"), c);
133 		if (ct_pr_event_get_zcorefile(ev, &c) == 0)
134 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
135 			    "\tglobal zone core: %s\n"), c);
136 		break;
137 	case CT_PR_EV_SIGNAL:
138 		if (ct_pr_event_get_signal(ev, &i) == 0) {
139 			if (sig2str(i, buf) == -1)
140 				(void) fprintf(file, dgettext(TEXT_DOMAIN,
141 				    "\tsignal: %d\n"), i);
142 			else
143 				(void) fprintf(file, dgettext(TEXT_DOMAIN,
144 				    "\tsignal: %d (SIG%s)\n"), i, buf);
145 		}
146 		if (ct_pr_event_get_sender(ev, &pid) == 0)
147 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
148 			    "\tsender pid: %d\n"), pid);
149 		if (ct_pr_event_get_senderct(ev, &ctid) == 0)
150 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
151 			    "\tsender ctid: %d\n"), ctid);
152 		break;
153 	}
154 }
155