17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7b209c2cSacruz  * Common Development and Distribution License (the "License").
6*7b209c2cSacruz  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*7b209c2cSacruz  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <assert.h>
277c478bd9Sstevel@tonic-gate #include <errno.h>
287c478bd9Sstevel@tonic-gate #include <libintl.h>
297c478bd9Sstevel@tonic-gate #include <sys/wait.h>
307c478bd9Sstevel@tonic-gate #include <sys/ctfs.h>
317c478bd9Sstevel@tonic-gate #include <sys/contract/process.h>
327c478bd9Sstevel@tonic-gate #include <libcontract.h>
337c478bd9Sstevel@tonic-gate #include <libcontract_priv.h>
347c478bd9Sstevel@tonic-gate #include <stdlib.h>
357c478bd9Sstevel@tonic-gate #include <string.h>
367c478bd9Sstevel@tonic-gate #include <unistd.h>
377c478bd9Sstevel@tonic-gate #include "inetd_impl.h"
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate /* paths/filenames of contract related files */
417c478bd9Sstevel@tonic-gate #define	CONTRACT_ROOT_PATH	CTFS_ROOT "/process/"
427c478bd9Sstevel@tonic-gate #define	CONTRACT_TEMPLATE_PATH  CONTRACT_ROOT_PATH "template"
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate static int active_tmpl_fd = -1;
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate /*
477c478bd9Sstevel@tonic-gate  * Creates and configures the the contract template used for all inetd's
487c478bd9Sstevel@tonic-gate  * methods.
497c478bd9Sstevel@tonic-gate  * Returns -1 on error, else the fd of the created template.
507c478bd9Sstevel@tonic-gate  */
517c478bd9Sstevel@tonic-gate static int
create_contract_template(void)527c478bd9Sstevel@tonic-gate create_contract_template(void)
537c478bd9Sstevel@tonic-gate {
547c478bd9Sstevel@tonic-gate 	int		fd;
557c478bd9Sstevel@tonic-gate 	int		err;
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate 	if ((fd = open(CONTRACT_TEMPLATE_PATH, O_RDWR)) == -1) {
587c478bd9Sstevel@tonic-gate 		error_msg(gettext("Failed to open contract file %s: %s"),
597c478bd9Sstevel@tonic-gate 		    CONTRACT_TEMPLATE_PATH, strerror(errno));
607c478bd9Sstevel@tonic-gate 		return (-1);
617c478bd9Sstevel@tonic-gate 	}
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate 	/*
647c478bd9Sstevel@tonic-gate 	 * Make contract inheritable and make hardware errors fatal.
657c478bd9Sstevel@tonic-gate 	 * We also limit the scope of fatal events to the process
667c478bd9Sstevel@tonic-gate 	 * group.  In order of preference we would have contract-aware
677c478bd9Sstevel@tonic-gate 	 * login services or a property indicating which services need
687c478bd9Sstevel@tonic-gate 	 * such scoping, but for the time being we'll assume that most
697c478bd9Sstevel@tonic-gate 	 * non login-style services run in a single process group.
707c478bd9Sstevel@tonic-gate 	 */
717c478bd9Sstevel@tonic-gate 	if (((err = ct_pr_tmpl_set_param(fd,
727c478bd9Sstevel@tonic-gate 	    CT_PR_INHERIT|CT_PR_PGRPONLY)) != 0) ||
737c478bd9Sstevel@tonic-gate 	    ((err = ct_pr_tmpl_set_fatal(fd, CT_PR_EV_HWERR)) != 0) ||
747c478bd9Sstevel@tonic-gate 	    ((err = ct_tmpl_set_critical(fd, 0)) != 0) ||
757c478bd9Sstevel@tonic-gate 	    ((err = ct_tmpl_set_informative(fd, 0)) != 0)) {
767c478bd9Sstevel@tonic-gate 		error_msg(gettext(
777c478bd9Sstevel@tonic-gate 		    "Failed to set parameter for contract template: %s"),
787c478bd9Sstevel@tonic-gate 		    strerror(err));
797c478bd9Sstevel@tonic-gate 		(void) close(fd);
807c478bd9Sstevel@tonic-gate 		return (-1);
817c478bd9Sstevel@tonic-gate 	}
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 	return (fd);
847c478bd9Sstevel@tonic-gate }
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate /* Returns -1 on error, else 0. */
877c478bd9Sstevel@tonic-gate int
contract_init(void)887c478bd9Sstevel@tonic-gate contract_init(void)
897c478bd9Sstevel@tonic-gate {
907c478bd9Sstevel@tonic-gate 	if ((active_tmpl_fd = create_contract_template()) == -1) {
917c478bd9Sstevel@tonic-gate 		error_msg(gettext("Failed to create contract template"));
927c478bd9Sstevel@tonic-gate 		return (-1);
937c478bd9Sstevel@tonic-gate 	}
947c478bd9Sstevel@tonic-gate 	return (0);
957c478bd9Sstevel@tonic-gate }
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate void
contract_fini(void)987c478bd9Sstevel@tonic-gate contract_fini(void)
997c478bd9Sstevel@tonic-gate {
1007c478bd9Sstevel@tonic-gate 	if (active_tmpl_fd != -1) {
1017c478bd9Sstevel@tonic-gate 		(void) close(active_tmpl_fd);
1027c478bd9Sstevel@tonic-gate 		active_tmpl_fd = -1;
1037c478bd9Sstevel@tonic-gate 	}
1047c478bd9Sstevel@tonic-gate }
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate /*
1077c478bd9Sstevel@tonic-gate  * To be called directly before a service method is forked, this function
1087c478bd9Sstevel@tonic-gate  * results in the method process being in a new contract based on the active
1097c478bd9Sstevel@tonic-gate  * contract template.
1107c478bd9Sstevel@tonic-gate  */
1117c478bd9Sstevel@tonic-gate int
contract_prefork(const char * fmri,int method)112*7b209c2cSacruz contract_prefork(const char *fmri, int method)
1137c478bd9Sstevel@tonic-gate {
1147c478bd9Sstevel@tonic-gate 	int err;
1157c478bd9Sstevel@tonic-gate 
116*7b209c2cSacruz 	if ((err = ct_pr_tmpl_set_svc_fmri(active_tmpl_fd, fmri)) != 0) {
117*7b209c2cSacruz 		error_msg(gettext("Failed to set svc_fmri term: %s"),
118*7b209c2cSacruz 		    strerror(err));
119*7b209c2cSacruz 		return (-1);
120*7b209c2cSacruz 	}
121*7b209c2cSacruz 	if ((err = ct_pr_tmpl_set_svc_aux(active_tmpl_fd,
122*7b209c2cSacruz 	    methods[method].name)) != 0) {
123*7b209c2cSacruz 		error_msg(gettext("Failed to set svc_aux term: %s"),
124*7b209c2cSacruz 		    strerror(err));
125*7b209c2cSacruz 		return (-1);
126*7b209c2cSacruz 	}
127*7b209c2cSacruz 
1287c478bd9Sstevel@tonic-gate 	if ((err = ct_tmpl_activate(active_tmpl_fd)) != 0) {
1297c478bd9Sstevel@tonic-gate 		error_msg(gettext("Failed to activate contract template: %s"),
1307c478bd9Sstevel@tonic-gate 		    strerror(err));
1317c478bd9Sstevel@tonic-gate 		return (-1);
1327c478bd9Sstevel@tonic-gate 	}
1337c478bd9Sstevel@tonic-gate 	return (0);
1347c478bd9Sstevel@tonic-gate }
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate /*
1377c478bd9Sstevel@tonic-gate  * To be called in both processes directly after a service method is forked,
1387c478bd9Sstevel@tonic-gate  * this function results in switching off contract creation for any
1397c478bd9Sstevel@tonic-gate  * forks done by either process, unless contract_prefork() is called beforehand.
1407c478bd9Sstevel@tonic-gate  */
1417c478bd9Sstevel@tonic-gate void
contract_postfork(void)1427c478bd9Sstevel@tonic-gate contract_postfork(void)
1437c478bd9Sstevel@tonic-gate {
1447c478bd9Sstevel@tonic-gate 	int err;
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	if ((err = ct_tmpl_clear(active_tmpl_fd)) != 0)
1477c478bd9Sstevel@tonic-gate 		error_msg("Failed to clear active contract template: %s",
1487c478bd9Sstevel@tonic-gate 		    strerror(err));
1497c478bd9Sstevel@tonic-gate }
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate /*
1527c478bd9Sstevel@tonic-gate  * Fetch the latest created contract id into the space referenced by 'cid'.
1537c478bd9Sstevel@tonic-gate  * Returns -1 on error, else 0.
1547c478bd9Sstevel@tonic-gate  */
1557c478bd9Sstevel@tonic-gate int
get_latest_contract(ctid_t * cid)1567c478bd9Sstevel@tonic-gate get_latest_contract(ctid_t *cid)
1577c478bd9Sstevel@tonic-gate {
1587c478bd9Sstevel@tonic-gate 	if ((errno = contract_latest(cid)) != 0) {
1597c478bd9Sstevel@tonic-gate 		error_msg(gettext("Failed to get new contract's id: %s"),
1607c478bd9Sstevel@tonic-gate 		    strerror(errno));
1617c478bd9Sstevel@tonic-gate 		return (-1);
1627c478bd9Sstevel@tonic-gate 	}
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	return (0);
1657c478bd9Sstevel@tonic-gate }
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate /* Returns -1 on error (with errno set), else fd. */
1687c478bd9Sstevel@tonic-gate static int
open_contract_ctl_file(ctid_t cid)1697c478bd9Sstevel@tonic-gate open_contract_ctl_file(ctid_t cid)
1707c478bd9Sstevel@tonic-gate {
1717c478bd9Sstevel@tonic-gate 	return (contract_open(cid, "process", "ctl", O_WRONLY));
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate /*
1757c478bd9Sstevel@tonic-gate  * Adopt a contract.  Emits an error message and returns -1 on failure, else
1767c478bd9Sstevel@tonic-gate  * 0.
1777c478bd9Sstevel@tonic-gate  */
1787c478bd9Sstevel@tonic-gate int
adopt_contract(ctid_t ctid,const char * fmri)1797c478bd9Sstevel@tonic-gate adopt_contract(ctid_t ctid, const char *fmri)
1807c478bd9Sstevel@tonic-gate {
1817c478bd9Sstevel@tonic-gate 	int fd;
1827c478bd9Sstevel@tonic-gate 	int err;
1837c478bd9Sstevel@tonic-gate 	int ret = 0;
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	if ((fd = open_contract_ctl_file(ctid)) == -1) {
1867c478bd9Sstevel@tonic-gate 		if (errno == EACCES || errno == ENOENT) {
1877c478bd9Sstevel@tonic-gate 			/*
1887c478bd9Sstevel@tonic-gate 			 * We must not have inherited this contract.  That can
1897c478bd9Sstevel@tonic-gate 			 * happen if we were disabled and restarted.
1907c478bd9Sstevel@tonic-gate 			 */
1917c478bd9Sstevel@tonic-gate 			debug_msg("Could not adopt contract %ld for %s "
1927c478bd9Sstevel@tonic-gate 			    "(could not open ctl file: permission denied).\n",
1937c478bd9Sstevel@tonic-gate 			    ctid, fmri);
1947c478bd9Sstevel@tonic-gate 			return (-1);
1957c478bd9Sstevel@tonic-gate 		}
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 		error_msg(gettext("Could not adopt contract id %ld registered "
1987c478bd9Sstevel@tonic-gate 		    "with %s (could not open ctl file: %s).  Events will be "
1997c478bd9Sstevel@tonic-gate 		    "ignored."), ctid, fmri, strerror(errno));
2007c478bd9Sstevel@tonic-gate 		return (-1);
2017c478bd9Sstevel@tonic-gate 	}
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	if ((err = ct_ctl_adopt(fd)) != 0) {
2047c478bd9Sstevel@tonic-gate 		error_msg(gettext("Could not adopt contract id %ld registered "
2057c478bd9Sstevel@tonic-gate 		    "with %s (%s).  Events will be ignored."), ctid, fmri,
2067c478bd9Sstevel@tonic-gate 		    strerror(err));
2077c478bd9Sstevel@tonic-gate 		ret = -1;
2087c478bd9Sstevel@tonic-gate 	}
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	err = close(fd);
2117c478bd9Sstevel@tonic-gate 	if (err != 0)
2127c478bd9Sstevel@tonic-gate 		error_msg(gettext("Could not close file descriptor %d."), fd);
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	return (ret);
2157c478bd9Sstevel@tonic-gate }
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate /* Returns -1 on error, else 0. */
2187c478bd9Sstevel@tonic-gate int
abandon_contract(ctid_t ctid)2197c478bd9Sstevel@tonic-gate abandon_contract(ctid_t ctid)
2207c478bd9Sstevel@tonic-gate {
2217c478bd9Sstevel@tonic-gate 	int fd;
2227c478bd9Sstevel@tonic-gate 	int err;
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	assert(ctid != -1);
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	if ((fd = open_contract_ctl_file(ctid)) == -1) {
2277c478bd9Sstevel@tonic-gate 		error_msg(gettext("Failed to abandon contract %d: %s"), ctid,
2287c478bd9Sstevel@tonic-gate 		    strerror(errno));
2297c478bd9Sstevel@tonic-gate 		return (-1);
2307c478bd9Sstevel@tonic-gate 	}
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	if ((err = ct_ctl_abandon(fd)) != 0) {
2337c478bd9Sstevel@tonic-gate 		(void) close(fd);
2347c478bd9Sstevel@tonic-gate 		error_msg(gettext("Failed to abandon contract %d: %s"), ctid,
2357c478bd9Sstevel@tonic-gate 		    strerror(err));
2367c478bd9Sstevel@tonic-gate 		return (-1);
2377c478bd9Sstevel@tonic-gate 	}
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	(void) close(fd);
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	return (0);
2427c478bd9Sstevel@tonic-gate }
243