10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*6073Sacruz * Common Development and Distribution License (the "License").
6*6073Sacruz * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*6073Sacruz * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
270Sstevel@tonic-gate
280Sstevel@tonic-gate #include <assert.h>
290Sstevel@tonic-gate #include <errno.h>
300Sstevel@tonic-gate #include <libintl.h>
310Sstevel@tonic-gate #include <sys/wait.h>
320Sstevel@tonic-gate #include <sys/ctfs.h>
330Sstevel@tonic-gate #include <sys/contract/process.h>
340Sstevel@tonic-gate #include <libcontract.h>
350Sstevel@tonic-gate #include <libcontract_priv.h>
360Sstevel@tonic-gate #include <stdlib.h>
370Sstevel@tonic-gate #include <string.h>
380Sstevel@tonic-gate #include <unistd.h>
390Sstevel@tonic-gate #include "inetd_impl.h"
400Sstevel@tonic-gate
410Sstevel@tonic-gate
420Sstevel@tonic-gate /* paths/filenames of contract related files */
430Sstevel@tonic-gate #define CONTRACT_ROOT_PATH CTFS_ROOT "/process/"
440Sstevel@tonic-gate #define CONTRACT_TEMPLATE_PATH CONTRACT_ROOT_PATH "template"
450Sstevel@tonic-gate
460Sstevel@tonic-gate static int active_tmpl_fd = -1;
470Sstevel@tonic-gate
480Sstevel@tonic-gate /*
490Sstevel@tonic-gate * Creates and configures the the contract template used for all inetd's
500Sstevel@tonic-gate * methods.
510Sstevel@tonic-gate * Returns -1 on error, else the fd of the created template.
520Sstevel@tonic-gate */
530Sstevel@tonic-gate static int
create_contract_template(void)540Sstevel@tonic-gate create_contract_template(void)
550Sstevel@tonic-gate {
560Sstevel@tonic-gate int fd;
570Sstevel@tonic-gate int err;
580Sstevel@tonic-gate
590Sstevel@tonic-gate if ((fd = open(CONTRACT_TEMPLATE_PATH, O_RDWR)) == -1) {
600Sstevel@tonic-gate error_msg(gettext("Failed to open contract file %s: %s"),
610Sstevel@tonic-gate CONTRACT_TEMPLATE_PATH, strerror(errno));
620Sstevel@tonic-gate return (-1);
630Sstevel@tonic-gate }
640Sstevel@tonic-gate
650Sstevel@tonic-gate /*
660Sstevel@tonic-gate * Make contract inheritable and make hardware errors fatal.
670Sstevel@tonic-gate * We also limit the scope of fatal events to the process
680Sstevel@tonic-gate * group. In order of preference we would have contract-aware
690Sstevel@tonic-gate * login services or a property indicating which services need
700Sstevel@tonic-gate * such scoping, but for the time being we'll assume that most
710Sstevel@tonic-gate * non login-style services run in a single process group.
720Sstevel@tonic-gate */
730Sstevel@tonic-gate if (((err = ct_pr_tmpl_set_param(fd,
740Sstevel@tonic-gate CT_PR_INHERIT|CT_PR_PGRPONLY)) != 0) ||
750Sstevel@tonic-gate ((err = ct_pr_tmpl_set_fatal(fd, CT_PR_EV_HWERR)) != 0) ||
760Sstevel@tonic-gate ((err = ct_tmpl_set_critical(fd, 0)) != 0) ||
770Sstevel@tonic-gate ((err = ct_tmpl_set_informative(fd, 0)) != 0)) {
780Sstevel@tonic-gate error_msg(gettext(
790Sstevel@tonic-gate "Failed to set parameter for contract template: %s"),
800Sstevel@tonic-gate strerror(err));
810Sstevel@tonic-gate (void) close(fd);
820Sstevel@tonic-gate return (-1);
830Sstevel@tonic-gate }
840Sstevel@tonic-gate
850Sstevel@tonic-gate return (fd);
860Sstevel@tonic-gate }
870Sstevel@tonic-gate
880Sstevel@tonic-gate /* Returns -1 on error, else 0. */
890Sstevel@tonic-gate int
contract_init(void)900Sstevel@tonic-gate contract_init(void)
910Sstevel@tonic-gate {
920Sstevel@tonic-gate if ((active_tmpl_fd = create_contract_template()) == -1) {
930Sstevel@tonic-gate error_msg(gettext("Failed to create contract template"));
940Sstevel@tonic-gate return (-1);
950Sstevel@tonic-gate }
960Sstevel@tonic-gate return (0);
970Sstevel@tonic-gate }
980Sstevel@tonic-gate
990Sstevel@tonic-gate void
contract_fini(void)1000Sstevel@tonic-gate contract_fini(void)
1010Sstevel@tonic-gate {
1020Sstevel@tonic-gate if (active_tmpl_fd != -1) {
1030Sstevel@tonic-gate (void) close(active_tmpl_fd);
1040Sstevel@tonic-gate active_tmpl_fd = -1;
1050Sstevel@tonic-gate }
1060Sstevel@tonic-gate }
1070Sstevel@tonic-gate
1080Sstevel@tonic-gate /*
1090Sstevel@tonic-gate * To be called directly before a service method is forked, this function
1100Sstevel@tonic-gate * results in the method process being in a new contract based on the active
1110Sstevel@tonic-gate * contract template.
1120Sstevel@tonic-gate */
1130Sstevel@tonic-gate int
contract_prefork(const char * fmri,int method)114*6073Sacruz contract_prefork(const char *fmri, int method)
1150Sstevel@tonic-gate {
1160Sstevel@tonic-gate int err;
1170Sstevel@tonic-gate
118*6073Sacruz if ((err = ct_pr_tmpl_set_svc_fmri(active_tmpl_fd, fmri)) != 0) {
119*6073Sacruz error_msg(gettext("Failed to set svc_fmri term: %s"),
120*6073Sacruz strerror(err));
121*6073Sacruz return (-1);
122*6073Sacruz }
123*6073Sacruz if ((err = ct_pr_tmpl_set_svc_aux(active_tmpl_fd,
124*6073Sacruz methods[method].name)) != 0) {
125*6073Sacruz error_msg(gettext("Failed to set svc_aux term: %s"),
126*6073Sacruz strerror(err));
127*6073Sacruz return (-1);
128*6073Sacruz }
129*6073Sacruz
1300Sstevel@tonic-gate if ((err = ct_tmpl_activate(active_tmpl_fd)) != 0) {
1310Sstevel@tonic-gate error_msg(gettext("Failed to activate contract template: %s"),
1320Sstevel@tonic-gate strerror(err));
1330Sstevel@tonic-gate return (-1);
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate return (0);
1360Sstevel@tonic-gate }
1370Sstevel@tonic-gate
1380Sstevel@tonic-gate /*
1390Sstevel@tonic-gate * To be called in both processes directly after a service method is forked,
1400Sstevel@tonic-gate * this function results in switching off contract creation for any
1410Sstevel@tonic-gate * forks done by either process, unless contract_prefork() is called beforehand.
1420Sstevel@tonic-gate */
1430Sstevel@tonic-gate void
contract_postfork(void)1440Sstevel@tonic-gate contract_postfork(void)
1450Sstevel@tonic-gate {
1460Sstevel@tonic-gate int err;
1470Sstevel@tonic-gate
1480Sstevel@tonic-gate if ((err = ct_tmpl_clear(active_tmpl_fd)) != 0)
1490Sstevel@tonic-gate error_msg("Failed to clear active contract template: %s",
1500Sstevel@tonic-gate strerror(err));
1510Sstevel@tonic-gate }
1520Sstevel@tonic-gate
1530Sstevel@tonic-gate /*
1540Sstevel@tonic-gate * Fetch the latest created contract id into the space referenced by 'cid'.
1550Sstevel@tonic-gate * Returns -1 on error, else 0.
1560Sstevel@tonic-gate */
1570Sstevel@tonic-gate int
get_latest_contract(ctid_t * cid)1580Sstevel@tonic-gate get_latest_contract(ctid_t *cid)
1590Sstevel@tonic-gate {
1600Sstevel@tonic-gate if ((errno = contract_latest(cid)) != 0) {
1610Sstevel@tonic-gate error_msg(gettext("Failed to get new contract's id: %s"),
1620Sstevel@tonic-gate strerror(errno));
1630Sstevel@tonic-gate return (-1);
1640Sstevel@tonic-gate }
1650Sstevel@tonic-gate
1660Sstevel@tonic-gate return (0);
1670Sstevel@tonic-gate }
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate /* Returns -1 on error (with errno set), else fd. */
1700Sstevel@tonic-gate static int
open_contract_ctl_file(ctid_t cid)1710Sstevel@tonic-gate open_contract_ctl_file(ctid_t cid)
1720Sstevel@tonic-gate {
1730Sstevel@tonic-gate return (contract_open(cid, "process", "ctl", O_WRONLY));
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate
1760Sstevel@tonic-gate /*
1770Sstevel@tonic-gate * Adopt a contract. Emits an error message and returns -1 on failure, else
1780Sstevel@tonic-gate * 0.
1790Sstevel@tonic-gate */
1800Sstevel@tonic-gate int
adopt_contract(ctid_t ctid,const char * fmri)1810Sstevel@tonic-gate adopt_contract(ctid_t ctid, const char *fmri)
1820Sstevel@tonic-gate {
1830Sstevel@tonic-gate int fd;
1840Sstevel@tonic-gate int err;
1850Sstevel@tonic-gate int ret = 0;
1860Sstevel@tonic-gate
1870Sstevel@tonic-gate if ((fd = open_contract_ctl_file(ctid)) == -1) {
1880Sstevel@tonic-gate if (errno == EACCES || errno == ENOENT) {
1890Sstevel@tonic-gate /*
1900Sstevel@tonic-gate * We must not have inherited this contract. That can
1910Sstevel@tonic-gate * happen if we were disabled and restarted.
1920Sstevel@tonic-gate */
1930Sstevel@tonic-gate debug_msg("Could not adopt contract %ld for %s "
1940Sstevel@tonic-gate "(could not open ctl file: permission denied).\n",
1950Sstevel@tonic-gate ctid, fmri);
1960Sstevel@tonic-gate return (-1);
1970Sstevel@tonic-gate }
1980Sstevel@tonic-gate
1990Sstevel@tonic-gate error_msg(gettext("Could not adopt contract id %ld registered "
2000Sstevel@tonic-gate "with %s (could not open ctl file: %s). Events will be "
2010Sstevel@tonic-gate "ignored."), ctid, fmri, strerror(errno));
2020Sstevel@tonic-gate return (-1);
2030Sstevel@tonic-gate }
2040Sstevel@tonic-gate
2050Sstevel@tonic-gate if ((err = ct_ctl_adopt(fd)) != 0) {
2060Sstevel@tonic-gate error_msg(gettext("Could not adopt contract id %ld registered "
2070Sstevel@tonic-gate "with %s (%s). Events will be ignored."), ctid, fmri,
2080Sstevel@tonic-gate strerror(err));
2090Sstevel@tonic-gate ret = -1;
2100Sstevel@tonic-gate }
2110Sstevel@tonic-gate
2120Sstevel@tonic-gate err = close(fd);
2130Sstevel@tonic-gate if (err != 0)
2140Sstevel@tonic-gate error_msg(gettext("Could not close file descriptor %d."), fd);
2150Sstevel@tonic-gate
2160Sstevel@tonic-gate return (ret);
2170Sstevel@tonic-gate }
2180Sstevel@tonic-gate
2190Sstevel@tonic-gate /* Returns -1 on error, else 0. */
2200Sstevel@tonic-gate int
abandon_contract(ctid_t ctid)2210Sstevel@tonic-gate abandon_contract(ctid_t ctid)
2220Sstevel@tonic-gate {
2230Sstevel@tonic-gate int fd;
2240Sstevel@tonic-gate int err;
2250Sstevel@tonic-gate
2260Sstevel@tonic-gate assert(ctid != -1);
2270Sstevel@tonic-gate
2280Sstevel@tonic-gate if ((fd = open_contract_ctl_file(ctid)) == -1) {
2290Sstevel@tonic-gate error_msg(gettext("Failed to abandon contract %d: %s"), ctid,
2300Sstevel@tonic-gate strerror(errno));
2310Sstevel@tonic-gate return (-1);
2320Sstevel@tonic-gate }
2330Sstevel@tonic-gate
2340Sstevel@tonic-gate if ((err = ct_ctl_abandon(fd)) != 0) {
2350Sstevel@tonic-gate (void) close(fd);
2360Sstevel@tonic-gate error_msg(gettext("Failed to abandon contract %d: %s"), ctid,
2370Sstevel@tonic-gate strerror(err));
2380Sstevel@tonic-gate return (-1);
2390Sstevel@tonic-gate }
2400Sstevel@tonic-gate
2410Sstevel@tonic-gate (void) close(fd);
2420Sstevel@tonic-gate
2430Sstevel@tonic-gate return (0);
2440Sstevel@tonic-gate }
245