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