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 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 debug_msg("Entering create_contract_template"); 600Sstevel@tonic-gate 610Sstevel@tonic-gate if ((fd = open(CONTRACT_TEMPLATE_PATH, O_RDWR)) == -1) { 620Sstevel@tonic-gate error_msg(gettext("Failed to open contract file %s: %s"), 630Sstevel@tonic-gate CONTRACT_TEMPLATE_PATH, strerror(errno)); 640Sstevel@tonic-gate return (-1); 650Sstevel@tonic-gate } 660Sstevel@tonic-gate 670Sstevel@tonic-gate /* 680Sstevel@tonic-gate * Make contract inheritable and make hardware errors fatal. 690Sstevel@tonic-gate * We also limit the scope of fatal events to the process 700Sstevel@tonic-gate * group. In order of preference we would have contract-aware 710Sstevel@tonic-gate * login services or a property indicating which services need 720Sstevel@tonic-gate * such scoping, but for the time being we'll assume that most 730Sstevel@tonic-gate * non login-style services run in a single process group. 740Sstevel@tonic-gate */ 750Sstevel@tonic-gate if (((err = ct_pr_tmpl_set_param(fd, 760Sstevel@tonic-gate CT_PR_INHERIT|CT_PR_PGRPONLY)) != 0) || 770Sstevel@tonic-gate ((err = ct_pr_tmpl_set_fatal(fd, CT_PR_EV_HWERR)) != 0) || 780Sstevel@tonic-gate ((err = ct_tmpl_set_critical(fd, 0)) != 0) || 790Sstevel@tonic-gate ((err = ct_tmpl_set_informative(fd, 0)) != 0)) { 800Sstevel@tonic-gate error_msg(gettext( 810Sstevel@tonic-gate "Failed to set parameter for contract template: %s"), 820Sstevel@tonic-gate strerror(err)); 830Sstevel@tonic-gate (void) close(fd); 840Sstevel@tonic-gate return (-1); 850Sstevel@tonic-gate } 860Sstevel@tonic-gate 870Sstevel@tonic-gate return (fd); 880Sstevel@tonic-gate } 890Sstevel@tonic-gate 900Sstevel@tonic-gate /* Returns -1 on error, else 0. */ 910Sstevel@tonic-gate int 920Sstevel@tonic-gate contract_init(void) 930Sstevel@tonic-gate { 940Sstevel@tonic-gate debug_msg("Entering contract_init"); 950Sstevel@tonic-gate 960Sstevel@tonic-gate if ((active_tmpl_fd = create_contract_template()) == -1) { 970Sstevel@tonic-gate error_msg(gettext("Failed to create contract template")); 980Sstevel@tonic-gate return (-1); 990Sstevel@tonic-gate } 1000Sstevel@tonic-gate return (0); 1010Sstevel@tonic-gate } 1020Sstevel@tonic-gate 1030Sstevel@tonic-gate void 1040Sstevel@tonic-gate contract_fini(void) 1050Sstevel@tonic-gate { 1060Sstevel@tonic-gate debug_msg("Entering contract_fini"); 1070Sstevel@tonic-gate 1080Sstevel@tonic-gate if (active_tmpl_fd != -1) { 1090Sstevel@tonic-gate (void) close(active_tmpl_fd); 1100Sstevel@tonic-gate active_tmpl_fd = -1; 1110Sstevel@tonic-gate } 1120Sstevel@tonic-gate } 1130Sstevel@tonic-gate 1140Sstevel@tonic-gate /* 1150Sstevel@tonic-gate * To be called directly before a service method is forked, this function 1160Sstevel@tonic-gate * results in the method process being in a new contract based on the active 1170Sstevel@tonic-gate * contract template. 1180Sstevel@tonic-gate */ 1190Sstevel@tonic-gate int 120*6073Sacruz contract_prefork(const char *fmri, int method) 1210Sstevel@tonic-gate { 1220Sstevel@tonic-gate int err; 1230Sstevel@tonic-gate 124*6073Sacruz if ((err = ct_pr_tmpl_set_svc_fmri(active_tmpl_fd, fmri)) != 0) { 125*6073Sacruz error_msg(gettext("Failed to set svc_fmri term: %s"), 126*6073Sacruz strerror(err)); 127*6073Sacruz return (-1); 128*6073Sacruz } 129*6073Sacruz if ((err = ct_pr_tmpl_set_svc_aux(active_tmpl_fd, 130*6073Sacruz methods[method].name)) != 0) { 131*6073Sacruz error_msg(gettext("Failed to set svc_aux term: %s"), 132*6073Sacruz strerror(err)); 133*6073Sacruz return (-1); 134*6073Sacruz } 135*6073Sacruz 1360Sstevel@tonic-gate debug_msg("Entering contract_prefork"); 1370Sstevel@tonic-gate 1380Sstevel@tonic-gate if ((err = ct_tmpl_activate(active_tmpl_fd)) != 0) { 1390Sstevel@tonic-gate error_msg(gettext("Failed to activate contract template: %s"), 1400Sstevel@tonic-gate strerror(err)); 1410Sstevel@tonic-gate return (-1); 1420Sstevel@tonic-gate } 1430Sstevel@tonic-gate return (0); 1440Sstevel@tonic-gate } 1450Sstevel@tonic-gate 1460Sstevel@tonic-gate /* 1470Sstevel@tonic-gate * To be called in both processes directly after a service method is forked, 1480Sstevel@tonic-gate * this function results in switching off contract creation for any 1490Sstevel@tonic-gate * forks done by either process, unless contract_prefork() is called beforehand. 1500Sstevel@tonic-gate */ 1510Sstevel@tonic-gate void 1520Sstevel@tonic-gate contract_postfork(void) 1530Sstevel@tonic-gate { 1540Sstevel@tonic-gate int err; 1550Sstevel@tonic-gate 1560Sstevel@tonic-gate debug_msg("Entering contract_postfork"); 1570Sstevel@tonic-gate 1580Sstevel@tonic-gate if ((err = ct_tmpl_clear(active_tmpl_fd)) != 0) 1590Sstevel@tonic-gate error_msg("Failed to clear active contract template: %s", 1600Sstevel@tonic-gate strerror(err)); 1610Sstevel@tonic-gate } 1620Sstevel@tonic-gate 1630Sstevel@tonic-gate /* 1640Sstevel@tonic-gate * Fetch the latest created contract id into the space referenced by 'cid'. 1650Sstevel@tonic-gate * Returns -1 on error, else 0. 1660Sstevel@tonic-gate */ 1670Sstevel@tonic-gate int 1680Sstevel@tonic-gate get_latest_contract(ctid_t *cid) 1690Sstevel@tonic-gate { 1700Sstevel@tonic-gate debug_msg("Entering get_latest_contract"); 1710Sstevel@tonic-gate 1720Sstevel@tonic-gate if ((errno = contract_latest(cid)) != 0) { 1730Sstevel@tonic-gate error_msg(gettext("Failed to get new contract's id: %s"), 1740Sstevel@tonic-gate strerror(errno)); 1750Sstevel@tonic-gate return (-1); 1760Sstevel@tonic-gate } 1770Sstevel@tonic-gate 1780Sstevel@tonic-gate return (0); 1790Sstevel@tonic-gate } 1800Sstevel@tonic-gate 1810Sstevel@tonic-gate /* Returns -1 on error (with errno set), else fd. */ 1820Sstevel@tonic-gate static int 1830Sstevel@tonic-gate open_contract_ctl_file(ctid_t cid) 1840Sstevel@tonic-gate { 1850Sstevel@tonic-gate debug_msg("Entering open_contract_ctl_file"); 1860Sstevel@tonic-gate 1870Sstevel@tonic-gate return (contract_open(cid, "process", "ctl", O_WRONLY)); 1880Sstevel@tonic-gate } 1890Sstevel@tonic-gate 1900Sstevel@tonic-gate /* 1910Sstevel@tonic-gate * Adopt a contract. Emits an error message and returns -1 on failure, else 1920Sstevel@tonic-gate * 0. 1930Sstevel@tonic-gate */ 1940Sstevel@tonic-gate int 1950Sstevel@tonic-gate adopt_contract(ctid_t ctid, const char *fmri) 1960Sstevel@tonic-gate { 1970Sstevel@tonic-gate int fd; 1980Sstevel@tonic-gate int err; 1990Sstevel@tonic-gate int ret = 0; 2000Sstevel@tonic-gate 2010Sstevel@tonic-gate debug_msg("Entering adopt_contract, id: %d", ctid); 2020Sstevel@tonic-gate 2030Sstevel@tonic-gate if ((fd = open_contract_ctl_file(ctid)) == -1) { 2040Sstevel@tonic-gate if (errno == EACCES || errno == ENOENT) { 2050Sstevel@tonic-gate /* 2060Sstevel@tonic-gate * We must not have inherited this contract. That can 2070Sstevel@tonic-gate * happen if we were disabled and restarted. 2080Sstevel@tonic-gate */ 2090Sstevel@tonic-gate debug_msg("Could not adopt contract %ld for %s " 2100Sstevel@tonic-gate "(could not open ctl file: permission denied).\n", 2110Sstevel@tonic-gate ctid, fmri); 2120Sstevel@tonic-gate return (-1); 2130Sstevel@tonic-gate } 2140Sstevel@tonic-gate 2150Sstevel@tonic-gate error_msg(gettext("Could not adopt contract id %ld registered " 2160Sstevel@tonic-gate "with %s (could not open ctl file: %s). Events will be " 2170Sstevel@tonic-gate "ignored."), ctid, fmri, strerror(errno)); 2180Sstevel@tonic-gate return (-1); 2190Sstevel@tonic-gate } 2200Sstevel@tonic-gate 2210Sstevel@tonic-gate if ((err = ct_ctl_adopt(fd)) != 0) { 2220Sstevel@tonic-gate error_msg(gettext("Could not adopt contract id %ld registered " 2230Sstevel@tonic-gate "with %s (%s). Events will be ignored."), ctid, fmri, 2240Sstevel@tonic-gate strerror(err)); 2250Sstevel@tonic-gate ret = -1; 2260Sstevel@tonic-gate } 2270Sstevel@tonic-gate 2280Sstevel@tonic-gate err = close(fd); 2290Sstevel@tonic-gate if (err != 0) 2300Sstevel@tonic-gate error_msg(gettext("Could not close file descriptor %d."), fd); 2310Sstevel@tonic-gate 2320Sstevel@tonic-gate return (ret); 2330Sstevel@tonic-gate } 2340Sstevel@tonic-gate 2350Sstevel@tonic-gate /* Returns -1 on error, else 0. */ 2360Sstevel@tonic-gate int 2370Sstevel@tonic-gate abandon_contract(ctid_t ctid) 2380Sstevel@tonic-gate { 2390Sstevel@tonic-gate int fd; 2400Sstevel@tonic-gate int err; 2410Sstevel@tonic-gate 2420Sstevel@tonic-gate debug_msg("Entering abandon_contract, id: %d", ctid); 2430Sstevel@tonic-gate 2440Sstevel@tonic-gate assert(ctid != -1); 2450Sstevel@tonic-gate 2460Sstevel@tonic-gate if ((fd = open_contract_ctl_file(ctid)) == -1) { 2470Sstevel@tonic-gate error_msg(gettext("Failed to abandon contract %d: %s"), ctid, 2480Sstevel@tonic-gate strerror(errno)); 2490Sstevel@tonic-gate return (-1); 2500Sstevel@tonic-gate } 2510Sstevel@tonic-gate 2520Sstevel@tonic-gate if ((err = ct_ctl_abandon(fd)) != 0) { 2530Sstevel@tonic-gate (void) close(fd); 2540Sstevel@tonic-gate error_msg(gettext("Failed to abandon contract %d: %s"), ctid, 2550Sstevel@tonic-gate strerror(err)); 2560Sstevel@tonic-gate return (-1); 2570Sstevel@tonic-gate } 2580Sstevel@tonic-gate 2590Sstevel@tonic-gate (void) close(fd); 2600Sstevel@tonic-gate 2610Sstevel@tonic-gate return (0); 2620Sstevel@tonic-gate } 263