#include <assert.h>
#include <errno.h>
#include <libintl.h>
#include <sys/wait.h>
#include <sys/ctfs.h>
#include <sys/contract/process.h>
#include <libcontract.h>
#include <libcontract_priv.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "inetd_impl.h"
#define CONTRACT_ROOT_PATH CTFS_ROOT "/process/"
#define CONTRACT_TEMPLATE_PATH CONTRACT_ROOT_PATH "template"
static int active_tmpl_fd = -1;
static int
create_contract_template(void)
{
int fd;
int err;
if ((fd = open(CONTRACT_TEMPLATE_PATH, O_RDWR)) == -1) {
error_msg(gettext("Failed to open contract file %s: %s"),
CONTRACT_TEMPLATE_PATH, strerror(errno));
return (-1);
}
if (((err = ct_pr_tmpl_set_param(fd,
CT_PR_INHERIT|CT_PR_PGRPONLY)) != 0) ||
((err = ct_pr_tmpl_set_fatal(fd, CT_PR_EV_HWERR)) != 0) ||
((err = ct_tmpl_set_critical(fd, 0)) != 0) ||
((err = ct_tmpl_set_informative(fd, 0)) != 0)) {
error_msg(gettext(
"Failed to set parameter for contract template: %s"),
strerror(err));
(void) close(fd);
return (-1);
}
return (fd);
}
int
contract_init(void)
{
if ((active_tmpl_fd = create_contract_template()) == -1) {
error_msg(gettext("Failed to create contract template"));
return (-1);
}
return (0);
}
void
contract_fini(void)
{
if (active_tmpl_fd != -1) {
(void) close(active_tmpl_fd);
active_tmpl_fd = -1;
}
}
int
contract_prefork(const char *fmri, int method)
{
int err;
if ((err = ct_pr_tmpl_set_svc_fmri(active_tmpl_fd, fmri)) != 0) {
error_msg(gettext("Failed to set svc_fmri term: %s"),
strerror(err));
return (-1);
}
if ((err = ct_pr_tmpl_set_svc_aux(active_tmpl_fd,
methods[method].name)) != 0) {
error_msg(gettext("Failed to set svc_aux term: %s"),
strerror(err));
return (-1);
}
if ((err = ct_tmpl_activate(active_tmpl_fd)) != 0) {
error_msg(gettext("Failed to activate contract template: %s"),
strerror(err));
return (-1);
}
return (0);
}
void
contract_postfork(void)
{
int err;
if ((err = ct_tmpl_clear(active_tmpl_fd)) != 0)
error_msg("Failed to clear active contract template: %s",
strerror(err));
}
int
get_latest_contract(ctid_t *cid)
{
if ((errno = contract_latest(cid)) != 0) {
error_msg(gettext("Failed to get new contract's id: %s"),
strerror(errno));
return (-1);
}
return (0);
}
static int
open_contract_ctl_file(ctid_t cid)
{
return (contract_open(cid, "process", "ctl", O_WRONLY));
}
int
adopt_contract(ctid_t ctid, const char *fmri)
{
int fd;
int err;
int ret = 0;
if ((fd = open_contract_ctl_file(ctid)) == -1) {
if (errno == EACCES || errno == ENOENT) {
debug_msg("Could not adopt contract %ld for %s "
"(could not open ctl file: permission denied).\n",
ctid, fmri);
return (-1);
}
error_msg(gettext("Could not adopt contract id %ld registered "
"with %s (could not open ctl file: %s). Events will be "
"ignored."), ctid, fmri, strerror(errno));
return (-1);
}
if ((err = ct_ctl_adopt(fd)) != 0) {
error_msg(gettext("Could not adopt contract id %ld registered "
"with %s (%s). Events will be ignored."), ctid, fmri,
strerror(err));
ret = -1;
}
err = close(fd);
if (err != 0)
error_msg(gettext("Could not close file descriptor %d."), fd);
return (ret);
}
int
abandon_contract(ctid_t ctid)
{
int fd;
int err;
assert(ctid != -1);
if ((fd = open_contract_ctl_file(ctid)) == -1) {
error_msg(gettext("Failed to abandon contract %d: %s"), ctid,
strerror(errno));
return (-1);
}
if ((err = ct_ctl_abandon(fd)) != 0) {
(void) close(fd);
error_msg(gettext("Failed to abandon contract %d: %s"), ctid,
strerror(err));
return (-1);
}
(void) close(fd);
return (0);
}