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