17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*eed64e98Sgm  * Common Development and Distribution License (the "License").
6*eed64e98Sgm  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*eed64e98Sgm  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * General utility routines.
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <syslog.h>
317c478bd9Sstevel@tonic-gate #include <stdlib.h>
327c478bd9Sstevel@tonic-gate #include <stdio.h>
337c478bd9Sstevel@tonic-gate #include <stdarg.h>
347c478bd9Sstevel@tonic-gate #include <strings.h>
357c478bd9Sstevel@tonic-gate #include <time.h>
367c478bd9Sstevel@tonic-gate #include <errno.h>
377c478bd9Sstevel@tonic-gate #include <libintl.h>
387c478bd9Sstevel@tonic-gate #include <unistd.h>
397c478bd9Sstevel@tonic-gate #include "inetd_impl.h"
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate /* size of buffer used in msg() to expand printf() like messages into */
437c478bd9Sstevel@tonic-gate #define	MSG_BUF_SIZE		1024
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate /* number of pollfd we grow the pollfd array by at a time in set_pollfd() */
467c478bd9Sstevel@tonic-gate #define	POLLFDS_GROWTH_SIZE	16
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate /* enumeration of message types supported by msg() */
497c478bd9Sstevel@tonic-gate typedef enum {
507c478bd9Sstevel@tonic-gate 	MT_ERROR,
517c478bd9Sstevel@tonic-gate 	MT_DEBUG,
527c478bd9Sstevel@tonic-gate 	MT_WARN
537c478bd9Sstevel@tonic-gate } si_msg_type_t;
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate /*
567c478bd9Sstevel@tonic-gate  * Collection of information for each method type.
577c478bd9Sstevel@tonic-gate  * NOTE:  This table is indexed into using the instance_method_t
587c478bd9Sstevel@tonic-gate  * enumeration, so the ordering needs to be kept in synch.
597c478bd9Sstevel@tonic-gate  */
607c478bd9Sstevel@tonic-gate method_type_info_t methods[] = {
617c478bd9Sstevel@tonic-gate 	{IM_START, START_METHOD_NAME, IIS_NONE},
627c478bd9Sstevel@tonic-gate 	{IM_ONLINE, ONLINE_METHOD_NAME, IIS_ONLINE},
637c478bd9Sstevel@tonic-gate 	{IM_OFFLINE, OFFLINE_METHOD_NAME, IIS_OFFLINE},
647c478bd9Sstevel@tonic-gate 	{IM_DISABLE, DISABLE_METHOD_NAME, IIS_DISABLED},
657c478bd9Sstevel@tonic-gate 	{IM_REFRESH, REFRESH_METHOD_NAME, IIS_ONLINE},
667c478bd9Sstevel@tonic-gate 	{IM_NONE, "none", IIS_NONE}
677c478bd9Sstevel@tonic-gate };
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate struct pollfd	*poll_fds = NULL;
707c478bd9Sstevel@tonic-gate nfds_t		num_pollfds;
717c478bd9Sstevel@tonic-gate 
72*eed64e98Sgm boolean_t	syslog_open = B_FALSE;
73*eed64e98Sgm boolean_t	debug_enabled = B_FALSE;
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate void
msg_init(void)767c478bd9Sstevel@tonic-gate msg_init(void)
777c478bd9Sstevel@tonic-gate {
787c478bd9Sstevel@tonic-gate 	openlog(SYSLOG_IDENT, LOG_PID|LOG_CONS, LOG_DAEMON);
79*eed64e98Sgm 	syslog_open = B_TRUE;
807c478bd9Sstevel@tonic-gate }
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate void
msg_fini(void)837c478bd9Sstevel@tonic-gate msg_fini(void)
847c478bd9Sstevel@tonic-gate {
85*eed64e98Sgm 	syslog_open = B_FALSE;
867c478bd9Sstevel@tonic-gate 	closelog();
877c478bd9Sstevel@tonic-gate }
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate /*
907c478bd9Sstevel@tonic-gate  * Outputs a msg. If 'type' is set tp MT_ERROR or MT_WARN the message goes
917c478bd9Sstevel@tonic-gate  * to syslog with severitys LOG_ERROR and LOG_WARN respectively. For all
927c478bd9Sstevel@tonic-gate  * values of 'type' the message is written to the debug log file, if it
937c478bd9Sstevel@tonic-gate  * was openable when inetd started.
947c478bd9Sstevel@tonic-gate  */
957c478bd9Sstevel@tonic-gate static void
msg(si_msg_type_t type,const char * format,va_list ap)967c478bd9Sstevel@tonic-gate msg(si_msg_type_t type, const char *format, va_list ap)
977c478bd9Sstevel@tonic-gate {
987c478bd9Sstevel@tonic-gate 	/*
997c478bd9Sstevel@tonic-gate 	 * Use a stack buffer so we stand more chance of reporting a
1007c478bd9Sstevel@tonic-gate 	 * memory shortage failure.
1017c478bd9Sstevel@tonic-gate 	 */
1027c478bd9Sstevel@tonic-gate 	char		buf[MSG_BUF_SIZE];
1037c478bd9Sstevel@tonic-gate 
104*eed64e98Sgm 	if (!syslog_open)
1057c478bd9Sstevel@tonic-gate 		return;
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	(void) vsnprintf(buf, sizeof (buf), format, ap);
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	/*
1107c478bd9Sstevel@tonic-gate 	 * Log error and warning messages to syslog with appropriate severity.
1117c478bd9Sstevel@tonic-gate 	 */
1127c478bd9Sstevel@tonic-gate 	if (type == MT_ERROR) {
1137c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "%s", buf);
1147c478bd9Sstevel@tonic-gate 	} else if (type == MT_WARN) {
1157c478bd9Sstevel@tonic-gate 		syslog(LOG_WARNING, "%s", buf);
116*eed64e98Sgm 	} else if (debug_enabled && type == MT_DEBUG) {
117*eed64e98Sgm 		syslog(LOG_DEBUG, "%s", buf);
1187c478bd9Sstevel@tonic-gate 	}
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate /*
1227c478bd9Sstevel@tonic-gate  * Output a warning message. Unlike error_msg(), syslog doesn't get told
1237c478bd9Sstevel@tonic-gate  * to log to the console if syslogd isn't around.
1247c478bd9Sstevel@tonic-gate  */
1257c478bd9Sstevel@tonic-gate void
warn_msg(const char * format,...)1267c478bd9Sstevel@tonic-gate warn_msg(const char *format, ...)
1277c478bd9Sstevel@tonic-gate {
1287c478bd9Sstevel@tonic-gate 	va_list ap;
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	closelog();
1317c478bd9Sstevel@tonic-gate 	openlog(SYSLOG_IDENT, LOG_PID, LOG_DAEMON);
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	va_start(ap, format);
1347c478bd9Sstevel@tonic-gate 	msg(MT_WARN, format, ap);
1357c478bd9Sstevel@tonic-gate 	va_end(ap);
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	closelog();
1387c478bd9Sstevel@tonic-gate 	openlog(SYSLOG_IDENT, LOG_PID|LOG_CONS, LOG_DAEMON);
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate void
debug_msg(const char * format,...)1427c478bd9Sstevel@tonic-gate debug_msg(const char *format, ...)
1437c478bd9Sstevel@tonic-gate {
1447c478bd9Sstevel@tonic-gate 	va_list ap;
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	va_start(ap, format);
1477c478bd9Sstevel@tonic-gate 	msg(MT_DEBUG, format, ap);
1487c478bd9Sstevel@tonic-gate 	va_end(ap);
1497c478bd9Sstevel@tonic-gate }
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate void
error_msg(const char * format,...)1527c478bd9Sstevel@tonic-gate error_msg(const char *format, ...)
1537c478bd9Sstevel@tonic-gate {
1547c478bd9Sstevel@tonic-gate 	va_list ap;
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	va_start(ap, format);
1577c478bd9Sstevel@tonic-gate 	msg(MT_ERROR, format, ap);
1587c478bd9Sstevel@tonic-gate 	va_end(ap);
1597c478bd9Sstevel@tonic-gate }
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate void
poll_fini(void)1627c478bd9Sstevel@tonic-gate poll_fini(void)
1637c478bd9Sstevel@tonic-gate {
1647c478bd9Sstevel@tonic-gate 	if (poll_fds != NULL) {
1657c478bd9Sstevel@tonic-gate 		free(poll_fds);
1667c478bd9Sstevel@tonic-gate 		poll_fds = NULL;
1677c478bd9Sstevel@tonic-gate 	}
1687c478bd9Sstevel@tonic-gate }
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate struct pollfd *
find_pollfd(int fd)1717c478bd9Sstevel@tonic-gate find_pollfd(int fd)
1727c478bd9Sstevel@tonic-gate {
1737c478bd9Sstevel@tonic-gate 	nfds_t n;
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	for (n = 0; n < num_pollfds; n++) {
1767c478bd9Sstevel@tonic-gate 		if (poll_fds[n].fd == fd)
1777c478bd9Sstevel@tonic-gate 			return (&(poll_fds[n]));
1787c478bd9Sstevel@tonic-gate 	}
1797c478bd9Sstevel@tonic-gate 	return (NULL);
1807c478bd9Sstevel@tonic-gate }
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate int
set_pollfd(int fd,uint16_t events)1837c478bd9Sstevel@tonic-gate set_pollfd(int fd, uint16_t events)
1847c478bd9Sstevel@tonic-gate {
1857c478bd9Sstevel@tonic-gate 	struct pollfd	*p;
1867c478bd9Sstevel@tonic-gate 	int		i;
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	p = find_pollfd(fd);
1897c478bd9Sstevel@tonic-gate 	if ((p == NULL) && ((p = find_pollfd(-1)) == NULL)) {
1907c478bd9Sstevel@tonic-gate 		if ((p = realloc(poll_fds,
1917c478bd9Sstevel@tonic-gate 		    ((num_pollfds + POLLFDS_GROWTH_SIZE) *
1927c478bd9Sstevel@tonic-gate 		    sizeof (struct pollfd)))) == NULL) {
1937c478bd9Sstevel@tonic-gate 			return (-1);
1947c478bd9Sstevel@tonic-gate 		}
1957c478bd9Sstevel@tonic-gate 		poll_fds = p;
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 		for (i = 1; i < POLLFDS_GROWTH_SIZE; i++)
1987c478bd9Sstevel@tonic-gate 			poll_fds[num_pollfds + i].fd = -1;
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 		p = &poll_fds[num_pollfds];
2017c478bd9Sstevel@tonic-gate 		num_pollfds += POLLFDS_GROWTH_SIZE;
2027c478bd9Sstevel@tonic-gate 	}
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	p->fd = fd;
2057c478bd9Sstevel@tonic-gate 	p->events = events;
2067c478bd9Sstevel@tonic-gate 	p->revents = 0;
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	return (0);
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate void
clear_pollfd(int fd)2127c478bd9Sstevel@tonic-gate clear_pollfd(int fd)
2137c478bd9Sstevel@tonic-gate {
2147c478bd9Sstevel@tonic-gate 	struct pollfd *p;
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	if ((p = find_pollfd(fd)) != NULL) {
2177c478bd9Sstevel@tonic-gate 		p->fd = -1;
2187c478bd9Sstevel@tonic-gate 		p->events = 0;
2197c478bd9Sstevel@tonic-gate 		p->revents = 0;
2207c478bd9Sstevel@tonic-gate 	}
2217c478bd9Sstevel@tonic-gate }
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate boolean_t
isset_pollfd(int fd)2247c478bd9Sstevel@tonic-gate isset_pollfd(int fd)
2257c478bd9Sstevel@tonic-gate {
2267c478bd9Sstevel@tonic-gate 	struct pollfd *p = find_pollfd(fd);
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	return ((p != NULL) && (p->revents & POLLIN));
2297c478bd9Sstevel@tonic-gate }
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate /*
2327c478bd9Sstevel@tonic-gate  * An extension of read() that keeps retrying until either the full request has
2337c478bd9Sstevel@tonic-gate  * completed, the other end of the connection/pipe is closed, no data is
2347c478bd9Sstevel@tonic-gate  * readable for a non-blocking socket/pipe, or an unexpected error occurs.
2357c478bd9Sstevel@tonic-gate  * Returns 0 if the data is successfully read, 1 if the other end of the pipe/
2367c478bd9Sstevel@tonic-gate  * socket is closed or there's nothing to read from a non-blocking socket/pipe,
2377c478bd9Sstevel@tonic-gate  * else -1 if an unexpected error occurs.
2387c478bd9Sstevel@tonic-gate  */
2397c478bd9Sstevel@tonic-gate int
safe_read(int fd,void * buf,size_t sz)2407c478bd9Sstevel@tonic-gate safe_read(int fd, void *buf, size_t sz)
2417c478bd9Sstevel@tonic-gate {
2427c478bd9Sstevel@tonic-gate 	int	ret;
2437c478bd9Sstevel@tonic-gate 	size_t  cnt = 0;
2447c478bd9Sstevel@tonic-gate 	char    *cp = (char *)buf;
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	if (sz == 0)
2477c478bd9Sstevel@tonic-gate 		return (0);
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	do {
2507c478bd9Sstevel@tonic-gate 		switch (ret = read(fd, cp + cnt, sz - cnt)) {
2517c478bd9Sstevel@tonic-gate 		case 0:			/* other end of pipe/socket closed */
2527c478bd9Sstevel@tonic-gate 			return (1);
2537c478bd9Sstevel@tonic-gate 		case -1:
2547c478bd9Sstevel@tonic-gate 			if (errno == EAGAIN) {		/* nothing to read */
2557c478bd9Sstevel@tonic-gate 				return (1);
2567c478bd9Sstevel@tonic-gate 			} else if (errno != EINTR) {
2577c478bd9Sstevel@tonic-gate 				error_msg(gettext("Unexpected read error: %s"),
2587c478bd9Sstevel@tonic-gate 				    strerror(errno));
2597c478bd9Sstevel@tonic-gate 				return (-1);
2607c478bd9Sstevel@tonic-gate 			}
2617c478bd9Sstevel@tonic-gate 			break;
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 		default:
2647c478bd9Sstevel@tonic-gate 			cnt += ret;
2657c478bd9Sstevel@tonic-gate 		}
2667c478bd9Sstevel@tonic-gate 	} while (cnt != sz);
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	return (0);
2697c478bd9Sstevel@tonic-gate }
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate /*
2727c478bd9Sstevel@tonic-gate  * Return B_TRUE if instance 'inst' has exceeded its configured maximum
2737c478bd9Sstevel@tonic-gate  * concurrent copies limit, else B_FALSE.
2747c478bd9Sstevel@tonic-gate  */
2757c478bd9Sstevel@tonic-gate boolean_t
copies_limit_exceeded(instance_t * inst)2767c478bd9Sstevel@tonic-gate copies_limit_exceeded(instance_t *inst)
2777c478bd9Sstevel@tonic-gate {
2787c478bd9Sstevel@tonic-gate 	/* any value <=0 means that copies limits are disabled */
2797c478bd9Sstevel@tonic-gate 	return ((inst->config->basic->max_copies > 0) &&
2807c478bd9Sstevel@tonic-gate 	    (inst->copies >= inst->config->basic->max_copies));
2817c478bd9Sstevel@tonic-gate }
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate /*
2847c478bd9Sstevel@tonic-gate  * Cancel the method/con-rate offline timer associated with the instance.
2857c478bd9Sstevel@tonic-gate  */
2867c478bd9Sstevel@tonic-gate void
cancel_inst_timer(instance_t * inst)2877c478bd9Sstevel@tonic-gate cancel_inst_timer(instance_t *inst)
2887c478bd9Sstevel@tonic-gate {
2897c478bd9Sstevel@tonic-gate 	(void) iu_cancel_timer(timer_queue, inst->timer_id, NULL);
2907c478bd9Sstevel@tonic-gate 	inst->timer_id = -1;
2917c478bd9Sstevel@tonic-gate }
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate /*
2947c478bd9Sstevel@tonic-gate  * Cancel the bind retry timer associated with the instance.
2957c478bd9Sstevel@tonic-gate  */
2967c478bd9Sstevel@tonic-gate void
cancel_bind_timer(instance_t * inst)2977c478bd9Sstevel@tonic-gate cancel_bind_timer(instance_t *inst)
2987c478bd9Sstevel@tonic-gate {
2997c478bd9Sstevel@tonic-gate 	(void) iu_cancel_timer(timer_queue, inst->bind_timer_id, NULL);
3007c478bd9Sstevel@tonic-gate 	inst->bind_timer_id = -1;
3017c478bd9Sstevel@tonic-gate }
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate void
enable_blocking(int fd)3047c478bd9Sstevel@tonic-gate enable_blocking(int fd)
3057c478bd9Sstevel@tonic-gate {
3067c478bd9Sstevel@tonic-gate 	int flags = fcntl(fd, F_GETFL, 0);
3077c478bd9Sstevel@tonic-gate 	(void) fcntl(fd, F_SETFL, (flags & ~O_NONBLOCK));
3087c478bd9Sstevel@tonic-gate }
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate void
disable_blocking(int fd)3117c478bd9Sstevel@tonic-gate disable_blocking(int fd)
3127c478bd9Sstevel@tonic-gate {
3137c478bd9Sstevel@tonic-gate 	int flags = fcntl(fd, F_GETFL, 0);
3147c478bd9Sstevel@tonic-gate 	(void) fcntl(fd, F_SETFL, (flags | O_NONBLOCK));
3157c478bd9Sstevel@tonic-gate }
316