xref: /illumos-gate/usr/src/cmd/cmd-inet/usr.lib/inetd/contracts.c (revision 2a8bcb4efb45d99ac41c94a75c396b362c414f7f)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <assert.h>
27 #include <errno.h>
28 #include <libintl.h>
29 #include <sys/wait.h>
30 #include <sys/ctfs.h>
31 #include <sys/contract/process.h>
32 #include <libcontract.h>
33 #include <libcontract_priv.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
37 #include "inetd_impl.h"
38 
39 
40 /* paths/filenames of contract related files */
41 #define	CONTRACT_ROOT_PATH	CTFS_ROOT "/process/"
42 #define	CONTRACT_TEMPLATE_PATH  CONTRACT_ROOT_PATH "template"
43 
44 static int active_tmpl_fd = -1;
45 
46 /*
47  * Creates and configures the the contract template used for all inetd's
48  * methods.
49  * Returns -1 on error, else the fd of the created template.
50  */
51 static int
create_contract_template(void)52 create_contract_template(void)
53 {
54 	int		fd;
55 	int		err;
56 
57 	if ((fd = open(CONTRACT_TEMPLATE_PATH, O_RDWR)) == -1) {
58 		error_msg(gettext("Failed to open contract file %s: %s"),
59 		    CONTRACT_TEMPLATE_PATH, strerror(errno));
60 		return (-1);
61 	}
62 
63 	/*
64 	 * Make contract inheritable and make hardware errors fatal.
65 	 * We also limit the scope of fatal events to the process
66 	 * group.  In order of preference we would have contract-aware
67 	 * login services or a property indicating which services need
68 	 * such scoping, but for the time being we'll assume that most
69 	 * non login-style services run in a single process group.
70 	 */
71 	if (((err = ct_pr_tmpl_set_param(fd,
72 	    CT_PR_INHERIT|CT_PR_PGRPONLY)) != 0) ||
73 	    ((err = ct_pr_tmpl_set_fatal(fd, CT_PR_EV_HWERR)) != 0) ||
74 	    ((err = ct_tmpl_set_critical(fd, 0)) != 0) ||
75 	    ((err = ct_tmpl_set_informative(fd, 0)) != 0)) {
76 		error_msg(gettext(
77 		    "Failed to set parameter for contract template: %s"),
78 		    strerror(err));
79 		(void) close(fd);
80 		return (-1);
81 	}
82 
83 	return (fd);
84 }
85 
86 /* Returns -1 on error, else 0. */
87 int
contract_init(void)88 contract_init(void)
89 {
90 	if ((active_tmpl_fd = create_contract_template()) == -1) {
91 		error_msg(gettext("Failed to create contract template"));
92 		return (-1);
93 	}
94 	return (0);
95 }
96 
97 void
contract_fini(void)98 contract_fini(void)
99 {
100 	if (active_tmpl_fd != -1) {
101 		(void) close(active_tmpl_fd);
102 		active_tmpl_fd = -1;
103 	}
104 }
105 
106 /*
107  * To be called directly before a service method is forked, this function
108  * results in the method process being in a new contract based on the active
109  * contract template.
110  */
111 int
contract_prefork(const char * fmri,int method)112 contract_prefork(const char *fmri, int method)
113 {
114 	int err;
115 
116 	if ((err = ct_pr_tmpl_set_svc_fmri(active_tmpl_fd, fmri)) != 0) {
117 		error_msg(gettext("Failed to set svc_fmri term: %s"),
118 		    strerror(err));
119 		return (-1);
120 	}
121 	if ((err = ct_pr_tmpl_set_svc_aux(active_tmpl_fd,
122 	    methods[method].name)) != 0) {
123 		error_msg(gettext("Failed to set svc_aux term: %s"),
124 		    strerror(err));
125 		return (-1);
126 	}
127 
128 	if ((err = ct_tmpl_activate(active_tmpl_fd)) != 0) {
129 		error_msg(gettext("Failed to activate contract template: %s"),
130 		    strerror(err));
131 		return (-1);
132 	}
133 	return (0);
134 }
135 
136 /*
137  * To be called in both processes directly after a service method is forked,
138  * this function results in switching off contract creation for any
139  * forks done by either process, unless contract_prefork() is called beforehand.
140  */
141 void
contract_postfork(void)142 contract_postfork(void)
143 {
144 	int err;
145 
146 	if ((err = ct_tmpl_clear(active_tmpl_fd)) != 0)
147 		error_msg("Failed to clear active contract template: %s",
148 		    strerror(err));
149 }
150 
151 /*
152  * Fetch the latest created contract id into the space referenced by 'cid'.
153  * Returns -1 on error, else 0.
154  */
155 int
get_latest_contract(ctid_t * cid)156 get_latest_contract(ctid_t *cid)
157 {
158 	if ((errno = contract_latest(cid)) != 0) {
159 		error_msg(gettext("Failed to get new contract's id: %s"),
160 		    strerror(errno));
161 		return (-1);
162 	}
163 
164 	return (0);
165 }
166 
167 /* Returns -1 on error (with errno set), else fd. */
168 static int
open_contract_ctl_file(ctid_t cid)169 open_contract_ctl_file(ctid_t cid)
170 {
171 	return (contract_open(cid, "process", "ctl", O_WRONLY));
172 }
173 
174 /*
175  * Adopt a contract.  Emits an error message and returns -1 on failure, else
176  * 0.
177  */
178 int
adopt_contract(ctid_t ctid,const char * fmri)179 adopt_contract(ctid_t ctid, const char *fmri)
180 {
181 	int fd;
182 	int err;
183 	int ret = 0;
184 
185 	if ((fd = open_contract_ctl_file(ctid)) == -1) {
186 		if (errno == EACCES || errno == ENOENT) {
187 			/*
188 			 * We must not have inherited this contract.  That can
189 			 * happen if we were disabled and restarted.
190 			 */
191 			debug_msg("Could not adopt contract %ld for %s "
192 			    "(could not open ctl file: permission denied).\n",
193 			    ctid, fmri);
194 			return (-1);
195 		}
196 
197 		error_msg(gettext("Could not adopt contract id %ld registered "
198 		    "with %s (could not open ctl file: %s).  Events will be "
199 		    "ignored."), ctid, fmri, strerror(errno));
200 		return (-1);
201 	}
202 
203 	if ((err = ct_ctl_adopt(fd)) != 0) {
204 		error_msg(gettext("Could not adopt contract id %ld registered "
205 		    "with %s (%s).  Events will be ignored."), ctid, fmri,
206 		    strerror(err));
207 		ret = -1;
208 	}
209 
210 	err = close(fd);
211 	if (err != 0)
212 		error_msg(gettext("Could not close file descriptor %d."), fd);
213 
214 	return (ret);
215 }
216 
217 /* Returns -1 on error, else 0. */
218 int
abandon_contract(ctid_t ctid)219 abandon_contract(ctid_t ctid)
220 {
221 	int fd;
222 	int err;
223 
224 	assert(ctid != -1);
225 
226 	if ((fd = open_contract_ctl_file(ctid)) == -1) {
227 		error_msg(gettext("Failed to abandon contract %d: %s"), ctid,
228 		    strerror(errno));
229 		return (-1);
230 	}
231 
232 	if ((err = ct_ctl_abandon(fd)) != 0) {
233 		(void) close(fd);
234 		error_msg(gettext("Failed to abandon contract %d: %s"), ctid,
235 		    strerror(err));
236 		return (-1);
237 	}
238 
239 	(void) close(fd);
240 
241 	return (0);
242 }
243