wait.c revision 5e2844d4e1d6321400668cbd30b2ccd36887492f
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* This file contains a set of routines used to perform wait based method
* reaping.
*/
#include <wait.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"
/* inetd's open file limit, set in method_init() */
#define INETD_NOFILE_LIMIT RLIM_INFINITY
/* structure used to represent an active method process */
typedef struct {
int fd; /* fd of process's /proc psinfo file */
/* associated contract id if known, else -1 */
/* associated endpoint protocol name if known, else NULL */
char *proto_name;
} method_el_t;
static void unregister_method(method_el_t *);
/* list of currently executing method processes */
/*
* File limit saved during initialization before modification, so that it can
* be reverted back to for inetd's exec'd methods.
*/
static struct rlimit saved_file_limit;
/*
* Setup structures used for method termination monitoring.
* Returns -1 if an allocation failure occurred, else 0.
*/
int
method_init(void)
{
/*
* Save aside the old file limit and impose one large enough to support
* all the /proc file handles we could have open.
*/
return (-1);
}
UU_LIST_POOL_DEBUG)) == NULL) {
uu_strerror(uu_error()));
return (-1);
}
error_msg("%s: %s",
gettext("Failed to create method list"),
uu_strerror(uu_error()));
/* let method_fini() clean-up */
return (-1);
}
return (0);
}
/*
* Tear-down structures created in method_init().
*/
void
method_fini(void)
{
if (method_list != NULL) {
(void) uu_list_destroy(method_list);
method_list = NULL;
}
if (method_pool != NULL) {
(void) uu_list_pool_destroy(method_pool);
method_pool = NULL;
}
/* revert file limit */
}
/*
* Revert file limit back to pre-initialization one. This shouldn't fail as
* long as its called *after* descriptor cleanup.
*/
void
method_preexec(void)
{
}
/*
* Callback function that handles the timeout of an instance's method.
* 'arg' points at the method_el_t representing the method.
*/
/* ARGSUSED0 */
static void
{
} else {
}
}
/*
* Registers the attributes of a running method passed as arguments so that
* the method's termination is noticed and any further processing of the
* associated instance is carried out. The function also sets up any
* necessary timers so we can detect hung methods.
* Returns -1 if either it failed to open the /proc psinfo file which is used
* to monitor the method process, it failed to setup a required timer or
* memory allocation failed; else 0.
*/
int
char *proto_name)
{
char path[MAXPATHLEN];
int fd;
/* open /proc psinfo file of process to listen for POLLHUP events on */
for (;;) {
break;
/*
* Don't output an error for ENOENT; we get this
* if a method has gone away whilst we were stopped,
* and we're now trying to re-listen for it.
*/
}
return (-1);
}
}
/* add method record to in-memory list */
return (-1);
}
if (proto_name != NULL) {
return (-1);
}
} else
/* register a timeout for the method, if required */
"Failed to schedule method timeout"));
return (-1);
}
}
}
/*
* Add fd of psinfo file to poll set, but pass 0 for events to
* poll for, so we should only get a POLLHUP event on the fd.
*/
return (-1);
}
return (0);
}
/*
* A counterpart to register_method(), this function stops the monitoring of a
* method process for its termination.
*/
static void
{
/* cancel any timer associated with the method */
/* stop polling on the psinfo file fd */
/* remove method record from list */
}
/*
* Unregister all methods associated with instance 'inst'.
*/
void
{
} else {
}
}
}
/*
* Process any terminated methods. For each method determined to have
* terminated, the function determines its return value and calls the
* appropriate handling function, depending on the type of the method.
*/
void
{
int status;
int ret;
/*
* We expect to get a POLLHUP back on the fd of the process's
* open psinfo file from /proc when the method terminates.
* A POLLERR could(?) mask a POLLHUP, so handle this
* also.
*/
continue;
}
/* get the method's exit code (no need to loop for EINTR) */
switch (pid) {
case 0: /* child still around */
/*
* Either poll() is sending us invalid POLLHUP events
* or is flagging a POLLERR on the fd. Neither should
* happen, but in the event they do, ignore this fd
* this time around and wait out the termination
* of its associated method. This may result in
* inetd swiftly looping in event_loop(), but means
* we don't miss the termination of a method.
*/
continue;
case -1: /* non-existent child */
/*
* the method must not be owned by inetd due to it
* persisting over an inetd restart. Let's assume the
* best, that it was successful.
*/
ret = IMRET_SUCCESS;
break;
default: /* child terminated */
debug_msg("process %ld of instance %s returned "
} else if (WIFSIGNALED(status)) {
/*
* Terminated by signal. This may be due
* to a kill that we sent from a disable or
* offline event. We flag it as a failure, but
* this flagged failure will only be processed
* in the case of non-start methods, or when
* the instance is still enabled.
*/
debug_msg("process %ld of instance %s exited "
ret = IMRET_FAILURE;
} else {
/*
* Can we actually get here? Don't think so.
* Treat it as a failure, anyway.
*/
debug_msg("waitpid() for %s method of "
"instance %s returned %d",
status);
ret = IMRET_FAILURE;
}
}
/* continue state transition processing of the instance */
} else {
}
}
}