xref: /titanic_41/usr/src/lib/libcontract/common/process.c (revision 45916cd2fec6e79bca5dee0421bd39e3c2910d1e)
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/ctfs.h>
30 #include <sys/contract.h>
31 #include <sys/contract/process.h>
32 #include <errno.h>
33 #include <unistd.h>
34 #include <libnvpair.h>
35 #include <libcontract.h>
36 #include "libcontract_impl.h"
37 
38 /*
39  * Process contract template routines
40  */
41 
42 int
43 ct_pr_tmpl_set_transfer(int fd, ctid_t ctid)
44 {
45 	return (ct_tmpl_set_internal(fd, CTPP_SUBSUME, ctid));
46 }
47 
48 int
49 ct_pr_tmpl_set_fatal(int fd, uint_t events)
50 {
51 	return (ct_tmpl_set_internal(fd, CTPP_EV_FATAL, events));
52 }
53 
54 int
55 ct_pr_tmpl_set_param(int fd, uint_t param)
56 {
57 	return (ct_tmpl_set_internal(fd, CTPP_PARAMS, param));
58 }
59 
60 int
61 ct_pr_tmpl_get_transfer(int fd, ctid_t *ctid)
62 {
63 	return (ct_tmpl_get_internal(fd, CTPP_SUBSUME, (uint_t *)ctid));
64 }
65 
66 int
67 ct_pr_tmpl_get_fatal(int fd, uint_t *events)
68 {
69 	return (ct_tmpl_get_internal(fd, CTPP_EV_FATAL, events));
70 }
71 
72 int
73 ct_pr_tmpl_get_param(int fd, uint_t *param)
74 {
75 	return (ct_tmpl_get_internal(fd, CTPP_PARAMS, param));
76 }
77 
78 /*
79  * Process contract event routines
80  */
81 
82 int
83 ct_pr_event_get_pid(ct_evthdl_t evthdl, pid_t *pid)
84 {
85 	struct ctlib_event_info *info = evthdl;
86 	if (info->event.ctev_cttype != CTT_PROCESS)
87 		return (EINVAL);
88 	if (info->nvl == NULL)
89 		return (ENOENT);
90 	return (nvlist_lookup_uint32(info->nvl, CTPE_PID, (uint_t *)pid));
91 }
92 
93 int
94 ct_pr_event_get_ppid(ct_evthdl_t evthdl, pid_t *ppid)
95 {
96 	struct ctlib_event_info *info = evthdl;
97 	if (info->event.ctev_cttype != CTT_PROCESS)
98 		return (EINVAL);
99 	if (info->event.ctev_type != CT_PR_EV_FORK)
100 		return (EINVAL);
101 	if (info->nvl == NULL)
102 		return (ENOENT);
103 	return (nvlist_lookup_uint32(info->nvl, CTPE_PPID, (uint_t *)ppid));
104 }
105 
106 int
107 ct_pr_event_get_signal(ct_evthdl_t evthdl, int *signal)
108 {
109 	struct ctlib_event_info *info = evthdl;
110 	if (info->event.ctev_cttype != CTT_PROCESS)
111 		return (EINVAL);
112 	if (info->event.ctev_type != CT_PR_EV_SIGNAL)
113 		return (EINVAL);
114 	if (info->nvl == NULL)
115 		return (ENOENT);
116 	return (nvlist_lookup_uint32(info->nvl, CTPE_SIGNAL, (uint_t *)signal));
117 }
118 
119 int
120 ct_pr_event_get_sender(ct_evthdl_t evthdl, pid_t *sender)
121 {
122 	struct ctlib_event_info *info = evthdl;
123 	if (info->event.ctev_cttype != CTT_PROCESS)
124 		return (EINVAL);
125 	if (info->event.ctev_type != CT_PR_EV_SIGNAL)
126 		return (EINVAL);
127 	if (info->nvl == NULL)
128 		return (ENOENT);
129 	return (nvlist_lookup_uint32(info->nvl, CTPE_SENDER, (uint_t *)sender));
130 }
131 
132 int
133 ct_pr_event_get_senderct(ct_evthdl_t evthdl, ctid_t *sendct)
134 {
135 	struct ctlib_event_info *info = evthdl;
136 	if (info->event.ctev_cttype != CTT_PROCESS)
137 		return (EINVAL);
138 	if (info->event.ctev_type != CT_PR_EV_SIGNAL)
139 		return (EINVAL);
140 	if (info->nvl == NULL)
141 		return (ENOENT);
142 	return (nvlist_lookup_uint32(info->nvl, CTPE_SENDCT, (uint_t *)sendct));
143 }
144 
145 int
146 ct_pr_event_get_exitstatus(ct_evthdl_t evthdl, int *exitstatus)
147 {
148 	struct ctlib_event_info *info = evthdl;
149 	if (info->event.ctev_cttype != CTT_PROCESS)
150 		return (EINVAL);
151 	if (info->event.ctev_type != CT_PR_EV_EXIT)
152 		return (EINVAL);
153 	if (info->nvl == NULL)
154 		return (ENOENT);
155 	return (nvlist_lookup_int32(info->nvl, CTPE_EXITSTATUS, exitstatus));
156 }
157 
158 int
159 ct_pr_event_get_pcorefile(ct_evthdl_t evthdl, const char **pcorefile)
160 {
161 	struct ctlib_event_info *info = evthdl;
162 	if (info->event.ctev_cttype != CTT_PROCESS)
163 		return (EINVAL);
164 	if (info->event.ctev_type != CT_PR_EV_CORE)
165 		return (EINVAL);
166 	if (info->nvl == NULL)
167 		return (ENOENT);
168 	return (nvlist_lookup_string(info->nvl, CTPE_PCOREFILE,
169 	    (char **)pcorefile));
170 }
171 
172 int
173 ct_pr_event_get_gcorefile(ct_evthdl_t evthdl, const char **gcorefile)
174 {
175 	struct ctlib_event_info *info = evthdl;
176 	if (info->event.ctev_cttype != CTT_PROCESS)
177 		return (EINVAL);
178 	if (info->event.ctev_type != CT_PR_EV_CORE)
179 		return (EINVAL);
180 	if (info->nvl == NULL)
181 		return (ENOENT);
182 	return (nvlist_lookup_string(info->nvl, CTPE_GCOREFILE,
183 	    (char **)gcorefile));
184 }
185 
186 int
187 ct_pr_event_get_zcorefile(ct_evthdl_t evthdl, const char **zcorefile)
188 {
189 	struct ctlib_event_info *info = evthdl;
190 	if (info->event.ctev_cttype != CTT_PROCESS)
191 		return (EINVAL);
192 	if (info->event.ctev_type != CT_PR_EV_CORE)
193 		return (EINVAL);
194 	if (info->nvl == NULL)
195 		return (ENOENT);
196 	return (nvlist_lookup_string(info->nvl, CTPE_ZCOREFILE,
197 	    (char **)zcorefile));
198 }
199 
200 /*
201  * Process contract status routines
202  */
203 
204 int
205 ct_pr_status_get_param(ct_stathdl_t stathdl, uint_t *param)
206 {
207 	struct ctlib_status_info *info = stathdl;
208 	if (info->status.ctst_type != CTT_PROCESS)
209 		return (EINVAL);
210 	if (info->nvl == NULL)
211 		return (ENOENT);
212 	return (nvlist_lookup_uint32(info->nvl, CTPS_PARAMS, param));
213 }
214 
215 int
216 ct_pr_status_get_fatal(ct_stathdl_t stathdl, uint_t *fatal)
217 {
218 	struct ctlib_status_info *info = stathdl;
219 	if (info->status.ctst_type != CTT_PROCESS)
220 		return (EINVAL);
221 	if (info->nvl == NULL)
222 		return (ENOENT);
223 	return (nvlist_lookup_uint32(info->nvl, CTPS_EV_FATAL, fatal));
224 }
225 
226 int
227 ct_pr_status_get_members(ct_stathdl_t stathdl, pid_t **members, uint_t *n)
228 {
229 	struct ctlib_status_info *info = stathdl;
230 	if (info->status.ctst_type != CTT_PROCESS)
231 		return (EINVAL);
232 	if (info->nvl == NULL)
233 		return (ENOENT);
234 	return (nvlist_lookup_uint32_array(info->nvl, CTPS_MEMBERS,
235 	    (uint_t **)members, n));
236 }
237 
238 int
239 ct_pr_status_get_contracts(ct_stathdl_t stathdl, ctid_t **contracts,
240     uint_t *n)
241 {
242 	struct ctlib_status_info *info = stathdl;
243 	if (info->status.ctst_type != CTT_PROCESS)
244 		return (EINVAL);
245 	if (info->nvl == NULL)
246 		return (ENOENT);
247 	return (nvlist_lookup_uint32_array(info->nvl, CTPS_CONTRACTS,
248 	    (uint_t **)contracts, n));
249 }
250