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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/contract/process.h>
30 #include <sys/wait.h>
31 #include <sys/ctfs.h>
32 #include <unistd.h>
33 #include <fcntl.h>
34 #include <errno.h>
35 #include <string.h>
36 #include <limits.h>
37 #include <stdio.h>
38 #include <assert.h>
39 #include <signal.h>
40 #include <libuutil.h>
41 #include <libintl.h>
42 #include <libcontract.h>
43 #include <libcontract_priv.h>
44 #include "libcontract_impl.h"
45 
46 void
event_process(FILE * file,ct_evthdl_t ev,int verbose)47 event_process(FILE *file, ct_evthdl_t ev, int verbose)
48 {
49 	uint_t type;
50 	pid_t pid;
51 	char *s;
52 
53 	type = ct_event_get_type(ev);
54 	if (ct_pr_event_get_pid(ev, &pid) != 0) {
55 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "[bad event]\n"));
56 		return;
57 	}
58 
59 	switch (type) {
60 	case CT_PR_EV_EMPTY:
61 		s = dgettext(TEXT_DOMAIN, "contract empty\n");
62 		break;
63 	case CT_PR_EV_FORK:
64 		s = dgettext(TEXT_DOMAIN, "process %d was created\n");
65 		break;
66 	case CT_PR_EV_EXIT:
67 		s = dgettext(TEXT_DOMAIN, "process %d exited\n");
68 		break;
69 	case CT_PR_EV_CORE:
70 		s = dgettext(TEXT_DOMAIN, "process %d dumped core\n");
71 		break;
72 	case CT_PR_EV_SIGNAL:
73 		s = dgettext(TEXT_DOMAIN,
74 		    "process %d received a fatal signal\n");
75 		break;
76 	case CT_PR_EV_HWERR:
77 		s = dgettext(TEXT_DOMAIN,
78 		    "process %d was killed by a hardware error\n");
79 		break;
80 	default:
81 		s = dgettext(TEXT_DOMAIN, "process %d sent an unknown event\n");
82 		break;
83 	}
84 
85 	/*LINTED*/
86 	(void) fprintf(file, s, pid);
87 	if (!verbose)
88 		return;
89 
90 	switch (type) {
91 		int i;
92 		const char *c;
93 		char buf[SIG2STR_MAX];
94 		ctid_t ctid;
95 	case CT_PR_EV_FORK:
96 		if (ct_pr_event_get_ppid(ev, &pid) == 0)
97 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
98 			    "\tparent pid: %d\n"), pid);
99 		break;
100 	case CT_PR_EV_EXIT:
101 		if (ct_pr_event_get_exitstatus(ev, &i) != 0)
102 			break;
103 		(void) fprintf(file,
104 		    dgettext(TEXT_DOMAIN, "\twait status: 0x%x"), i);
105 		if (WIFEXITED(i)) {
106 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
107 			    " (exited, code %d)\n"), WEXITSTATUS(i));
108 		} else if (WIFSIGNALED(i)) {
109 			int sig = WTERMSIG(i);
110 			(void) fprintf(file,
111 			    dgettext(TEXT_DOMAIN, " (signal %d"), sig);
112 			if (sig2str(sig, buf) == 0)
113 				(void) fprintf(file,
114 				    dgettext(TEXT_DOMAIN, " (SIG%s)"), buf);
115 			if (WCOREDUMP(i))
116 				(void) fprintf(file,
117 				    dgettext(TEXT_DOMAIN, ", core dumped)\n"));
118 			else
119 				(void) fprintf(file,
120 				    dgettext(TEXT_DOMAIN, ")\n"));
121 		} else {
122 			/*
123 			 * We really shouldn't get here.
124 			 */
125 			(void) fprintf(file, dgettext(TEXT_DOMAIN, "\n"));
126 		}
127 		break;
128 	case CT_PR_EV_CORE:
129 		if (ct_pr_event_get_pcorefile(ev, &c) == 0)
130 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
131 			    "\tprocess core: %s\n"), c);
132 		if (ct_pr_event_get_gcorefile(ev, &c) == 0)
133 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
134 			    "\tglobal core: %s\n"), c);
135 		if (ct_pr_event_get_zcorefile(ev, &c) == 0)
136 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
137 			    "\tglobal zone core: %s\n"), c);
138 		break;
139 	case CT_PR_EV_SIGNAL:
140 		if (ct_pr_event_get_signal(ev, &i) == 0) {
141 			if (sig2str(i, buf) == -1)
142 				(void) fprintf(file, dgettext(TEXT_DOMAIN,
143 				    "\tsignal: %d\n"), i);
144 			else
145 				(void) fprintf(file, dgettext(TEXT_DOMAIN,
146 				    "\tsignal: %d (SIG%s)\n"), i, buf);
147 		}
148 		if (ct_pr_event_get_sender(ev, &pid) == 0)
149 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
150 			    "\tsender pid: %d\n"), pid);
151 		if (ct_pr_event_get_senderct(ev, &ctid) == 0)
152 			(void) fprintf(file, dgettext(TEXT_DOMAIN,
153 			    "\tsender ctid: %d\n"), ctid);
154 		break;
155 	}
156 }
157