4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
33 #include <sys/contract/process.h>
34 #include <libcontract.h>
35 #include <libcontract_priv.h>
39 #include "inetd_impl.h"
42 /* paths/filenames of contract related files */
43 #define CONTRACT_ROOT_PATH CTFS_ROOT "/process/"
44 #define CONTRACT_TEMPLATE_PATH CONTRACT_ROOT_PATH "template"
46 static int active_tmpl_fd
= -1;
49 * Creates and configures the the contract template used for all inetd's
51 * Returns -1 on error, else the fd of the created template.
54 create_contract_template(void)
59 if ((fd
= open(CONTRACT_TEMPLATE_PATH
, O_RDWR
)) == -1) {
60 error_msg(gettext("Failed to open contract file %s: %s"),
61 CONTRACT_TEMPLATE_PATH
, strerror(errno
));
66 * Make contract inheritable and make hardware errors fatal.
67 * We also limit the scope of fatal events to the process
68 * group. In order of preference we would have contract-aware
69 * login services or a property indicating which services need
70 * such scoping, but for the time being we'll assume that most
71 * non login-style services run in a single process group.
73 if (((err
= ct_pr_tmpl_set_param(fd
,
74 CT_PR_INHERIT
|CT_PR_PGRPONLY
)) != 0) ||
75 ((err
= ct_pr_tmpl_set_fatal(fd
, CT_PR_EV_HWERR
)) != 0) ||
76 ((err
= ct_tmpl_set_critical(fd
, 0)) != 0) ||
77 ((err
= ct_tmpl_set_informative(fd
, 0)) != 0)) {
79 "Failed to set parameter for contract template: %s"),
88 /* Returns -1 on error, else 0. */
92 if ((active_tmpl_fd
= create_contract_template()) == -1) {
93 error_msg(gettext("Failed to create contract template"));
102 if (active_tmpl_fd
!= -1) {
103 (void) close(active_tmpl_fd
);
109 * To be called directly before a service method is forked, this function
110 * results in the method process being in a new contract based on the active
114 contract_prefork(const char *fmri
, int method
)
118 if ((err
= ct_pr_tmpl_set_svc_fmri(active_tmpl_fd
, fmri
)) != 0) {
119 error_msg(gettext("Failed to set svc_fmri term: %s"),
123 if ((err
= ct_pr_tmpl_set_svc_aux(active_tmpl_fd
,
124 methods
[method
].name
)) != 0) {
125 error_msg(gettext("Failed to set svc_aux term: %s"),
130 if ((err
= ct_tmpl_activate(active_tmpl_fd
)) != 0) {
131 error_msg(gettext("Failed to activate contract template: %s"),
139 * To be called in both processes directly after a service method is forked,
140 * this function results in switching off contract creation for any
141 * forks done by either process, unless contract_prefork() is called beforehand.
144 contract_postfork(void)
148 if ((err
= ct_tmpl_clear(active_tmpl_fd
)) != 0)
149 error_msg("Failed to clear active contract template: %s",
154 * Fetch the latest created contract id into the space referenced by 'cid'.
155 * Returns -1 on error, else 0.
158 get_latest_contract(ctid_t
*cid
)
160 if ((errno
= contract_latest(cid
)) != 0) {
161 error_msg(gettext("Failed to get new contract's id: %s"),
169 /* Returns -1 on error (with errno set), else fd. */
171 open_contract_ctl_file(ctid_t cid
)
173 return (contract_open(cid
, "process", "ctl", O_WRONLY
));
177 * Adopt a contract. Emits an error message and returns -1 on failure, else
181 adopt_contract(ctid_t ctid
, const char *fmri
)
187 if ((fd
= open_contract_ctl_file(ctid
)) == -1) {
188 if (errno
== EACCES
|| errno
== ENOENT
) {
190 * We must not have inherited this contract. That can
191 * happen if we were disabled and restarted.
193 debug_msg("Could not adopt contract %ld for %s "
194 "(could not open ctl file: permission denied).\n",
199 error_msg(gettext("Could not adopt contract id %ld registered "
200 "with %s (could not open ctl file: %s). Events will be "
201 "ignored."), ctid
, fmri
, strerror(errno
));
205 if ((err
= ct_ctl_adopt(fd
)) != 0) {
206 error_msg(gettext("Could not adopt contract id %ld registered "
207 "with %s (%s). Events will be ignored."), ctid
, fmri
,
214 error_msg(gettext("Could not close file descriptor %d."), fd
);
219 /* Returns -1 on error, else 0. */
221 abandon_contract(ctid_t ctid
)
228 if ((fd
= open_contract_ctl_file(ctid
)) == -1) {
229 error_msg(gettext("Failed to abandon contract %d: %s"), ctid
,
234 if ((err
= ct_ctl_abandon(fd
)) != 0) {
236 error_msg(gettext("Failed to abandon contract %d: %s"), ctid
,