1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <sys/ctfs.h>
30*0Sstevel@tonic-gate #include <sys/contract.h>
31*0Sstevel@tonic-gate #include <sys/contract/process.h>
32*0Sstevel@tonic-gate #include <errno.h>
33*0Sstevel@tonic-gate #include <unistd.h>
34*0Sstevel@tonic-gate #include <libnvpair.h>
35*0Sstevel@tonic-gate #include <libcontract.h>
36*0Sstevel@tonic-gate #include "libcontract_impl.h"
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate /*
39*0Sstevel@tonic-gate  * Process contract template routines
40*0Sstevel@tonic-gate  */
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate int
43*0Sstevel@tonic-gate ct_pr_tmpl_set_transfer(int fd, ctid_t ctid)
44*0Sstevel@tonic-gate {
45*0Sstevel@tonic-gate 	return (ct_tmpl_set_internal(fd, CTPP_SUBSUME, ctid));
46*0Sstevel@tonic-gate }
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate int
49*0Sstevel@tonic-gate ct_pr_tmpl_set_fatal(int fd, uint_t events)
50*0Sstevel@tonic-gate {
51*0Sstevel@tonic-gate 	return (ct_tmpl_set_internal(fd, CTPP_EV_FATAL, events));
52*0Sstevel@tonic-gate }
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate int
55*0Sstevel@tonic-gate ct_pr_tmpl_set_param(int fd, uint_t param)
56*0Sstevel@tonic-gate {
57*0Sstevel@tonic-gate 	return (ct_tmpl_set_internal(fd, CTPP_PARAMS, param));
58*0Sstevel@tonic-gate }
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate int
61*0Sstevel@tonic-gate ct_pr_tmpl_get_transfer(int fd, ctid_t *ctid)
62*0Sstevel@tonic-gate {
63*0Sstevel@tonic-gate 	return (ct_tmpl_get_internal(fd, CTPP_SUBSUME, (uint_t *)ctid));
64*0Sstevel@tonic-gate }
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate int
67*0Sstevel@tonic-gate ct_pr_tmpl_get_fatal(int fd, uint_t *events)
68*0Sstevel@tonic-gate {
69*0Sstevel@tonic-gate 	return (ct_tmpl_get_internal(fd, CTPP_EV_FATAL, events));
70*0Sstevel@tonic-gate }
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate int
73*0Sstevel@tonic-gate ct_pr_tmpl_get_param(int fd, uint_t *param)
74*0Sstevel@tonic-gate {
75*0Sstevel@tonic-gate 	return (ct_tmpl_get_internal(fd, CTPP_PARAMS, param));
76*0Sstevel@tonic-gate }
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate /*
79*0Sstevel@tonic-gate  * Process contract event routines
80*0Sstevel@tonic-gate  */
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate int
83*0Sstevel@tonic-gate ct_pr_event_get_pid(ct_evthdl_t evthdl, pid_t *pid)
84*0Sstevel@tonic-gate {
85*0Sstevel@tonic-gate 	struct ctlib_event_info *info = evthdl;
86*0Sstevel@tonic-gate 	if (info->event.ctev_cttype != CTT_PROCESS)
87*0Sstevel@tonic-gate 		return (EINVAL);
88*0Sstevel@tonic-gate 	if (info->nvl == NULL)
89*0Sstevel@tonic-gate 		return (ENOENT);
90*0Sstevel@tonic-gate 	return (nvlist_lookup_uint32(info->nvl, CTPE_PID, (uint_t *)pid));
91*0Sstevel@tonic-gate }
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate int
94*0Sstevel@tonic-gate ct_pr_event_get_ppid(ct_evthdl_t evthdl, pid_t *ppid)
95*0Sstevel@tonic-gate {
96*0Sstevel@tonic-gate 	struct ctlib_event_info *info = evthdl;
97*0Sstevel@tonic-gate 	if (info->event.ctev_cttype != CTT_PROCESS)
98*0Sstevel@tonic-gate 		return (EINVAL);
99*0Sstevel@tonic-gate 	if (info->event.ctev_type != CT_PR_EV_FORK)
100*0Sstevel@tonic-gate 		return (EINVAL);
101*0Sstevel@tonic-gate 	if (info->nvl == NULL)
102*0Sstevel@tonic-gate 		return (ENOENT);
103*0Sstevel@tonic-gate 	return (nvlist_lookup_uint32(info->nvl, CTPE_PPID, (uint_t *)ppid));
104*0Sstevel@tonic-gate }
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate int
107*0Sstevel@tonic-gate ct_pr_event_get_signal(ct_evthdl_t evthdl, int *signal)
108*0Sstevel@tonic-gate {
109*0Sstevel@tonic-gate 	struct ctlib_event_info *info = evthdl;
110*0Sstevel@tonic-gate 	if (info->event.ctev_cttype != CTT_PROCESS)
111*0Sstevel@tonic-gate 		return (EINVAL);
112*0Sstevel@tonic-gate 	if (info->event.ctev_type != CT_PR_EV_SIGNAL)
113*0Sstevel@tonic-gate 		return (EINVAL);
114*0Sstevel@tonic-gate 	if (info->nvl == NULL)
115*0Sstevel@tonic-gate 		return (ENOENT);
116*0Sstevel@tonic-gate 	return (nvlist_lookup_uint32(info->nvl, CTPE_SIGNAL, (uint_t *)signal));
117*0Sstevel@tonic-gate }
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate int
120*0Sstevel@tonic-gate ct_pr_event_get_sender(ct_evthdl_t evthdl, pid_t *sender)
121*0Sstevel@tonic-gate {
122*0Sstevel@tonic-gate 	struct ctlib_event_info *info = evthdl;
123*0Sstevel@tonic-gate 	if (info->event.ctev_cttype != CTT_PROCESS)
124*0Sstevel@tonic-gate 		return (EINVAL);
125*0Sstevel@tonic-gate 	if (info->event.ctev_type != CT_PR_EV_SIGNAL)
126*0Sstevel@tonic-gate 		return (EINVAL);
127*0Sstevel@tonic-gate 	if (info->nvl == NULL)
128*0Sstevel@tonic-gate 		return (ENOENT);
129*0Sstevel@tonic-gate 	return (nvlist_lookup_uint32(info->nvl, CTPE_SENDER, (uint_t *)sender));
130*0Sstevel@tonic-gate }
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate int
133*0Sstevel@tonic-gate ct_pr_event_get_senderct(ct_evthdl_t evthdl, ctid_t *sendct)
134*0Sstevel@tonic-gate {
135*0Sstevel@tonic-gate 	struct ctlib_event_info *info = evthdl;
136*0Sstevel@tonic-gate 	if (info->event.ctev_cttype != CTT_PROCESS)
137*0Sstevel@tonic-gate 		return (EINVAL);
138*0Sstevel@tonic-gate 	if (info->event.ctev_type != CT_PR_EV_SIGNAL)
139*0Sstevel@tonic-gate 		return (EINVAL);
140*0Sstevel@tonic-gate 	if (info->nvl == NULL)
141*0Sstevel@tonic-gate 		return (ENOENT);
142*0Sstevel@tonic-gate 	return (nvlist_lookup_uint32(info->nvl, CTPE_SENDCT, (uint_t *)sendct));
143*0Sstevel@tonic-gate }
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate int
146*0Sstevel@tonic-gate ct_pr_event_get_exitstatus(ct_evthdl_t evthdl, int *exitstatus)
147*0Sstevel@tonic-gate {
148*0Sstevel@tonic-gate 	struct ctlib_event_info *info = evthdl;
149*0Sstevel@tonic-gate 	if (info->event.ctev_cttype != CTT_PROCESS)
150*0Sstevel@tonic-gate 		return (EINVAL);
151*0Sstevel@tonic-gate 	if (info->event.ctev_type != CT_PR_EV_EXIT)
152*0Sstevel@tonic-gate 		return (EINVAL);
153*0Sstevel@tonic-gate 	if (info->nvl == NULL)
154*0Sstevel@tonic-gate 		return (ENOENT);
155*0Sstevel@tonic-gate 	return (nvlist_lookup_int32(info->nvl, CTPE_EXITSTATUS, exitstatus));
156*0Sstevel@tonic-gate }
157*0Sstevel@tonic-gate 
158*0Sstevel@tonic-gate int
159*0Sstevel@tonic-gate ct_pr_event_get_pcorefile(ct_evthdl_t evthdl, const char **pcorefile)
160*0Sstevel@tonic-gate {
161*0Sstevel@tonic-gate 	struct ctlib_event_info *info = evthdl;
162*0Sstevel@tonic-gate 	if (info->event.ctev_cttype != CTT_PROCESS)
163*0Sstevel@tonic-gate 		return (EINVAL);
164*0Sstevel@tonic-gate 	if (info->event.ctev_type != CT_PR_EV_CORE)
165*0Sstevel@tonic-gate 		return (EINVAL);
166*0Sstevel@tonic-gate 	if (info->nvl == NULL)
167*0Sstevel@tonic-gate 		return (ENOENT);
168*0Sstevel@tonic-gate 	return (nvlist_lookup_string(info->nvl, CTPE_PCOREFILE,
169*0Sstevel@tonic-gate 	    (char **)pcorefile));
170*0Sstevel@tonic-gate }
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate int
173*0Sstevel@tonic-gate ct_pr_event_get_gcorefile(ct_evthdl_t evthdl, const char **gcorefile)
174*0Sstevel@tonic-gate {
175*0Sstevel@tonic-gate 	struct ctlib_event_info *info = evthdl;
176*0Sstevel@tonic-gate 	if (info->event.ctev_cttype != CTT_PROCESS)
177*0Sstevel@tonic-gate 		return (EINVAL);
178*0Sstevel@tonic-gate 	if (info->event.ctev_type != CT_PR_EV_CORE)
179*0Sstevel@tonic-gate 		return (EINVAL);
180*0Sstevel@tonic-gate 	if (info->nvl == NULL)
181*0Sstevel@tonic-gate 		return (ENOENT);
182*0Sstevel@tonic-gate 	return (nvlist_lookup_string(info->nvl, CTPE_GCOREFILE,
183*0Sstevel@tonic-gate 	    (char **)gcorefile));
184*0Sstevel@tonic-gate }
185*0Sstevel@tonic-gate 
186*0Sstevel@tonic-gate int
187*0Sstevel@tonic-gate ct_pr_event_get_zcorefile(ct_evthdl_t evthdl, const char **zcorefile)
188*0Sstevel@tonic-gate {
189*0Sstevel@tonic-gate 	struct ctlib_event_info *info = evthdl;
190*0Sstevel@tonic-gate 	if (info->event.ctev_cttype != CTT_PROCESS)
191*0Sstevel@tonic-gate 		return (EINVAL);
192*0Sstevel@tonic-gate 	if (info->event.ctev_type != CT_PR_EV_CORE)
193*0Sstevel@tonic-gate 		return (EINVAL);
194*0Sstevel@tonic-gate 	if (info->nvl == NULL)
195*0Sstevel@tonic-gate 		return (ENOENT);
196*0Sstevel@tonic-gate 	return (nvlist_lookup_string(info->nvl, CTPE_ZCOREFILE,
197*0Sstevel@tonic-gate 	    (char **)zcorefile));
198*0Sstevel@tonic-gate }
199*0Sstevel@tonic-gate 
200*0Sstevel@tonic-gate /*
201*0Sstevel@tonic-gate  * Process contract status routines
202*0Sstevel@tonic-gate  */
203*0Sstevel@tonic-gate 
204*0Sstevel@tonic-gate int
205*0Sstevel@tonic-gate ct_pr_status_get_param(ct_stathdl_t stathdl, uint_t *param)
206*0Sstevel@tonic-gate {
207*0Sstevel@tonic-gate 	struct ctlib_status_info *info = stathdl;
208*0Sstevel@tonic-gate 	if (info->status.ctst_type != CTT_PROCESS)
209*0Sstevel@tonic-gate 		return (EINVAL);
210*0Sstevel@tonic-gate 	if (info->nvl == NULL)
211*0Sstevel@tonic-gate 		return (ENOENT);
212*0Sstevel@tonic-gate 	return (nvlist_lookup_uint32(info->nvl, CTPS_PARAMS, param));
213*0Sstevel@tonic-gate }
214*0Sstevel@tonic-gate 
215*0Sstevel@tonic-gate int
216*0Sstevel@tonic-gate ct_pr_status_get_fatal(ct_stathdl_t stathdl, uint_t *fatal)
217*0Sstevel@tonic-gate {
218*0Sstevel@tonic-gate 	struct ctlib_status_info *info = stathdl;
219*0Sstevel@tonic-gate 	if (info->status.ctst_type != CTT_PROCESS)
220*0Sstevel@tonic-gate 		return (EINVAL);
221*0Sstevel@tonic-gate 	if (info->nvl == NULL)
222*0Sstevel@tonic-gate 		return (ENOENT);
223*0Sstevel@tonic-gate 	return (nvlist_lookup_uint32(info->nvl, CTPS_EV_FATAL, fatal));
224*0Sstevel@tonic-gate }
225*0Sstevel@tonic-gate 
226*0Sstevel@tonic-gate int
227*0Sstevel@tonic-gate ct_pr_status_get_members(ct_stathdl_t stathdl, pid_t **members, uint_t *n)
228*0Sstevel@tonic-gate {
229*0Sstevel@tonic-gate 	struct ctlib_status_info *info = stathdl;
230*0Sstevel@tonic-gate 	if (info->status.ctst_type != CTT_PROCESS)
231*0Sstevel@tonic-gate 		return (EINVAL);
232*0Sstevel@tonic-gate 	if (info->nvl == NULL)
233*0Sstevel@tonic-gate 		return (ENOENT);
234*0Sstevel@tonic-gate 	return (nvlist_lookup_uint32_array(info->nvl, CTPS_MEMBERS,
235*0Sstevel@tonic-gate 	    (uint_t **)members, n));
236*0Sstevel@tonic-gate }
237*0Sstevel@tonic-gate 
238*0Sstevel@tonic-gate int
239*0Sstevel@tonic-gate ct_pr_status_get_contracts(ct_stathdl_t stathdl, ctid_t **contracts,
240*0Sstevel@tonic-gate     uint_t *n)
241*0Sstevel@tonic-gate {
242*0Sstevel@tonic-gate 	struct ctlib_status_info *info = stathdl;
243*0Sstevel@tonic-gate 	if (info->status.ctst_type != CTT_PROCESS)
244*0Sstevel@tonic-gate 		return (EINVAL);
245*0Sstevel@tonic-gate 	if (info->nvl == NULL)
246*0Sstevel@tonic-gate 		return (ENOENT);
247*0Sstevel@tonic-gate 	return (nvlist_lookup_uint32_array(info->nvl, CTPS_CONTRACTS,
248*0Sstevel@tonic-gate 	    (uint_t **)contracts, n));
249*0Sstevel@tonic-gate }
250