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