#include <wait.h>
#include <sys/param.h>
#include <fcntl.h>
#include <libcontract.h>
#include <errno.h>
#include <libintl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/resource.h>
#include "inetd_impl.h"
#define INETD_NOFILE_LIMIT RLIM_INFINITY
typedef struct {
int fd;
ctid_t cid;
pid_t pid;
instance_t *inst;
instance_method_t method;
char *proto_name;
uu_list_node_t link;
} method_el_t;
static void unregister_method(method_el_t *);
static uu_list_pool_t *method_pool = NULL;
static uu_list_t *method_list = NULL;
static struct rlimit saved_file_limit;
int
method_init(void)
{
struct rlimit rl;
(void) getrlimit(RLIMIT_NOFILE, &saved_file_limit);
rl.rlim_cur = rl.rlim_max = INETD_NOFILE_LIMIT;
if (setrlimit(RLIMIT_NOFILE, &rl) == -1) {
error_msg("Failed to set file limit: %s", strerror(errno));
return (-1);
}
if ((method_pool = uu_list_pool_create("method_pool",
sizeof (method_el_t), offsetof(method_el_t, link), NULL,
UU_LIST_POOL_DEBUG)) == NULL) {
error_msg("%s: %s", gettext("Failed to create method pool"),
uu_strerror(uu_error()));
return (-1);
}
if ((method_list = uu_list_create(method_pool, NULL, 0)) == NULL) {
error_msg("%s: %s",
gettext("Failed to create method list"),
uu_strerror(uu_error()));
return (-1);
}
return (0);
}
void
method_fini(void)
{
if (method_list != NULL) {
method_el_t *me;
while ((me = uu_list_first(method_list)) != NULL)
unregister_method(me);
(void) uu_list_destroy(method_list);
method_list = NULL;
}
if (method_pool != NULL) {
(void) uu_list_pool_destroy(method_pool);
method_pool = NULL;
}
method_preexec();
}
void
method_preexec(void)
{
(void) setrlimit(RLIMIT_NOFILE, &saved_file_limit);
}
static void
method_timeout(iu_tq_t *tq, void *arg)
{
method_el_t *mp = arg;
error_msg(gettext("The %s method of instance %s timed-out"),
methods[mp->method].name, mp->inst->fmri);
mp->inst->timer_id = -1;
if (mp->method == IM_START) {
process_start_term(mp->inst, mp->proto_name);
} else {
process_non_start_term(mp->inst, IMRET_FAILURE);
}
unregister_method(mp);
}
int
register_method(instance_t *ins, pid_t pid, ctid_t cid, instance_method_t mthd,
char *proto_name)
{
char path[MAXPATHLEN];
int fd;
method_el_t *me;
(void) snprintf(path, sizeof (path), "/proc/%u/psinfo", pid);
for (;;) {
if ((fd = open(path, O_RDONLY)) >= 0) {
break;
} else if (errno != EINTR) {
if (errno != ENOENT) {
error_msg(gettext("Failed to open %s: %s"),
path, strerror(errno));
}
return (-1);
}
}
if ((me = calloc(1, sizeof (method_el_t))) == NULL) {
error_msg(strerror(errno));
(void) close(fd);
return (-1);
}
me->fd = fd;
me->inst = (instance_t *)ins;
me->method = mthd;
me->pid = pid;
me->cid = cid;
if (proto_name != NULL) {
if ((me->proto_name = strdup(proto_name)) == NULL) {
error_msg(strerror(errno));
free(me);
(void) close(fd);
return (-1);
}
} else
me->proto_name = NULL;
if (mthd != IM_START) {
method_info_t *mi = ins->config->methods[mthd];
if (mi->timeout > 0) {
assert(ins->timer_id == -1);
ins->timer_id = iu_schedule_timer(timer_queue,
mi->timeout, method_timeout, me);
if (ins->timer_id == -1) {
error_msg(gettext(
"Failed to schedule method timeout"));
if (me->proto_name != NULL)
free(me->proto_name);
free(me);
(void) close(fd);
return (-1);
}
}
}
if (set_pollfd(fd, 0) == -1) {
cancel_inst_timer(ins);
if (me->proto_name != NULL)
free(me->proto_name);
free(me);
(void) close(fd);
return (-1);
}
uu_list_node_init(me, &me->link, method_pool);
(void) uu_list_insert_after(method_list, NULL, me);
return (0);
}
static void
unregister_method(method_el_t *me)
{
if (me->inst->timer_id != -1)
cancel_inst_timer(me->inst);
clear_pollfd(me->fd);
(void) close(me->fd);
uu_list_remove(method_list, me);
if (me->proto_name != NULL)
free(me->proto_name);
free(me);
}
void
unregister_instance_methods(const instance_t *inst)
{
method_el_t *me = uu_list_first(method_list);
while (me != NULL) {
if (me->inst == inst) {
method_el_t *tmp = me;
me = uu_list_next(method_list, me);
unregister_method(tmp);
} else {
me = uu_list_next(method_list, me);
}
}
}
void
process_terminated_methods(void)
{
method_el_t *me = uu_list_first(method_list);
while (me != NULL) {
struct pollfd *pfd;
pid_t pid;
int status;
int ret;
method_el_t *tmp;
pfd = find_pollfd(me->fd);
if ((pfd->revents & (POLLHUP|POLLERR)) == 0) {
me = uu_list_next(method_list, me);
continue;
}
pid = waitpid(me->pid, &status, WNOHANG);
switch (pid) {
case 0:
me = uu_list_next(method_list, me);
continue;
case -1:
assert(errno == ECHILD);
ret = IMRET_SUCCESS;
break;
default:
if (WIFEXITED(status)) {
ret = WEXITSTATUS(status);
debug_msg("process %ld of instance %s returned "
"%d", pid, me->inst->fmri, ret);
} else if (WIFSIGNALED(status)) {
debug_msg("process %ld of instance %s exited "
"due to signal %d", pid, me->inst->fmri,
WTERMSIG(status));
ret = IMRET_FAILURE;
} else {
debug_msg("waitpid() for %s method of "
"instance %s returned %d",
methods[me->method].name, me->inst->fmri,
status);
ret = IMRET_FAILURE;
}
}
remove_method_ids(me->inst, me->pid, me->cid, me->method);
if (me->method != IM_START) {
process_non_start_term(me->inst, ret);
} else {
process_start_term(me->inst, me->proto_name);
}
if (me->cid != -1)
(void) abandon_contract(me->cid);
tmp = me;
me = uu_list_next(method_list, me);
unregister_method(tmp);
}
}