xref: /onnv-gate/usr/src/cmd/cmd-inet/usr.lib/inetd/util.c (revision 6435:e526311db503)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*6435Sgm209912  * Common Development and Distribution License (the "License").
6*6435Sgm209912  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*6435Sgm209912  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate  * General utility routines.
300Sstevel@tonic-gate  */
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include <syslog.h>
330Sstevel@tonic-gate #include <stdlib.h>
340Sstevel@tonic-gate #include <stdio.h>
350Sstevel@tonic-gate #include <stdarg.h>
360Sstevel@tonic-gate #include <strings.h>
370Sstevel@tonic-gate #include <time.h>
380Sstevel@tonic-gate #include <errno.h>
390Sstevel@tonic-gate #include <libintl.h>
400Sstevel@tonic-gate #include <unistd.h>
410Sstevel@tonic-gate #include "inetd_impl.h"
420Sstevel@tonic-gate 
430Sstevel@tonic-gate 
440Sstevel@tonic-gate /* size of buffer used in msg() to expand printf() like messages into */
450Sstevel@tonic-gate #define	MSG_BUF_SIZE		1024
460Sstevel@tonic-gate 
470Sstevel@tonic-gate /* number of pollfd we grow the pollfd array by at a time in set_pollfd() */
480Sstevel@tonic-gate #define	POLLFDS_GROWTH_SIZE	16
490Sstevel@tonic-gate 
500Sstevel@tonic-gate /* enumeration of message types supported by msg() */
510Sstevel@tonic-gate typedef enum {
520Sstevel@tonic-gate 	MT_ERROR,
530Sstevel@tonic-gate 	MT_DEBUG,
540Sstevel@tonic-gate 	MT_WARN
550Sstevel@tonic-gate } si_msg_type_t;
560Sstevel@tonic-gate 
570Sstevel@tonic-gate /*
580Sstevel@tonic-gate  * Collection of information for each method type.
590Sstevel@tonic-gate  * NOTE:  This table is indexed into using the instance_method_t
600Sstevel@tonic-gate  * enumeration, so the ordering needs to be kept in synch.
610Sstevel@tonic-gate  */
620Sstevel@tonic-gate method_type_info_t methods[] = {
630Sstevel@tonic-gate 	{IM_START, START_METHOD_NAME, IIS_NONE},
640Sstevel@tonic-gate 	{IM_ONLINE, ONLINE_METHOD_NAME, IIS_ONLINE},
650Sstevel@tonic-gate 	{IM_OFFLINE, OFFLINE_METHOD_NAME, IIS_OFFLINE},
660Sstevel@tonic-gate 	{IM_DISABLE, DISABLE_METHOD_NAME, IIS_DISABLED},
670Sstevel@tonic-gate 	{IM_REFRESH, REFRESH_METHOD_NAME, IIS_ONLINE},
680Sstevel@tonic-gate 	{IM_NONE, "none", IIS_NONE}
690Sstevel@tonic-gate };
700Sstevel@tonic-gate 
710Sstevel@tonic-gate struct pollfd	*poll_fds = NULL;
720Sstevel@tonic-gate nfds_t		num_pollfds;
730Sstevel@tonic-gate 
74*6435Sgm209912 boolean_t	syslog_open = B_FALSE;
75*6435Sgm209912 boolean_t	debug_enabled = B_FALSE;
760Sstevel@tonic-gate 
770Sstevel@tonic-gate void
msg_init(void)780Sstevel@tonic-gate msg_init(void)
790Sstevel@tonic-gate {
800Sstevel@tonic-gate 	openlog(SYSLOG_IDENT, LOG_PID|LOG_CONS, LOG_DAEMON);
81*6435Sgm209912 	syslog_open = B_TRUE;
820Sstevel@tonic-gate }
830Sstevel@tonic-gate 
840Sstevel@tonic-gate void
msg_fini(void)850Sstevel@tonic-gate msg_fini(void)
860Sstevel@tonic-gate {
87*6435Sgm209912 	syslog_open = B_FALSE;
880Sstevel@tonic-gate 	closelog();
890Sstevel@tonic-gate }
900Sstevel@tonic-gate 
910Sstevel@tonic-gate /*
920Sstevel@tonic-gate  * Outputs a msg. If 'type' is set tp MT_ERROR or MT_WARN the message goes
930Sstevel@tonic-gate  * to syslog with severitys LOG_ERROR and LOG_WARN respectively. For all
940Sstevel@tonic-gate  * values of 'type' the message is written to the debug log file, if it
950Sstevel@tonic-gate  * was openable when inetd started.
960Sstevel@tonic-gate  */
970Sstevel@tonic-gate static void
msg(si_msg_type_t type,const char * format,va_list ap)980Sstevel@tonic-gate msg(si_msg_type_t type, const char *format, va_list ap)
990Sstevel@tonic-gate {
1000Sstevel@tonic-gate 	/*
1010Sstevel@tonic-gate 	 * Use a stack buffer so we stand more chance of reporting a
1020Sstevel@tonic-gate 	 * memory shortage failure.
1030Sstevel@tonic-gate 	 */
1040Sstevel@tonic-gate 	char		buf[MSG_BUF_SIZE];
1050Sstevel@tonic-gate 
106*6435Sgm209912 	if (!syslog_open)
1070Sstevel@tonic-gate 		return;
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 	(void) vsnprintf(buf, sizeof (buf), format, ap);
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 	/*
1120Sstevel@tonic-gate 	 * Log error and warning messages to syslog with appropriate severity.
1130Sstevel@tonic-gate 	 */
1140Sstevel@tonic-gate 	if (type == MT_ERROR) {
1150Sstevel@tonic-gate 		syslog(LOG_ERR, "%s", buf);
1160Sstevel@tonic-gate 	} else if (type == MT_WARN) {
1170Sstevel@tonic-gate 		syslog(LOG_WARNING, "%s", buf);
118*6435Sgm209912 	} else if (debug_enabled && type == MT_DEBUG) {
119*6435Sgm209912 		syslog(LOG_DEBUG, "%s", buf);
1200Sstevel@tonic-gate 	}
1210Sstevel@tonic-gate }
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate /*
1240Sstevel@tonic-gate  * Output a warning message. Unlike error_msg(), syslog doesn't get told
1250Sstevel@tonic-gate  * to log to the console if syslogd isn't around.
1260Sstevel@tonic-gate  */
1270Sstevel@tonic-gate void
warn_msg(const char * format,...)1280Sstevel@tonic-gate warn_msg(const char *format, ...)
1290Sstevel@tonic-gate {
1300Sstevel@tonic-gate 	va_list ap;
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 	closelog();
1330Sstevel@tonic-gate 	openlog(SYSLOG_IDENT, LOG_PID, LOG_DAEMON);
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 	va_start(ap, format);
1360Sstevel@tonic-gate 	msg(MT_WARN, format, ap);
1370Sstevel@tonic-gate 	va_end(ap);
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	closelog();
1400Sstevel@tonic-gate 	openlog(SYSLOG_IDENT, LOG_PID|LOG_CONS, LOG_DAEMON);
1410Sstevel@tonic-gate }
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate void
debug_msg(const char * format,...)1440Sstevel@tonic-gate debug_msg(const char *format, ...)
1450Sstevel@tonic-gate {
1460Sstevel@tonic-gate 	va_list ap;
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	va_start(ap, format);
1490Sstevel@tonic-gate 	msg(MT_DEBUG, format, ap);
1500Sstevel@tonic-gate 	va_end(ap);
1510Sstevel@tonic-gate }
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate void
error_msg(const char * format,...)1540Sstevel@tonic-gate error_msg(const char *format, ...)
1550Sstevel@tonic-gate {
1560Sstevel@tonic-gate 	va_list ap;
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	va_start(ap, format);
1590Sstevel@tonic-gate 	msg(MT_ERROR, format, ap);
1600Sstevel@tonic-gate 	va_end(ap);
1610Sstevel@tonic-gate }
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate void
poll_fini(void)1640Sstevel@tonic-gate poll_fini(void)
1650Sstevel@tonic-gate {
1660Sstevel@tonic-gate 	if (poll_fds != NULL) {
1670Sstevel@tonic-gate 		free(poll_fds);
1680Sstevel@tonic-gate 		poll_fds = NULL;
1690Sstevel@tonic-gate 	}
1700Sstevel@tonic-gate }
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate struct pollfd *
find_pollfd(int fd)1730Sstevel@tonic-gate find_pollfd(int fd)
1740Sstevel@tonic-gate {
1750Sstevel@tonic-gate 	nfds_t n;
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	for (n = 0; n < num_pollfds; n++) {
1780Sstevel@tonic-gate 		if (poll_fds[n].fd == fd)
1790Sstevel@tonic-gate 			return (&(poll_fds[n]));
1800Sstevel@tonic-gate 	}
1810Sstevel@tonic-gate 	return (NULL);
1820Sstevel@tonic-gate }
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate int
set_pollfd(int fd,uint16_t events)1850Sstevel@tonic-gate set_pollfd(int fd, uint16_t events)
1860Sstevel@tonic-gate {
1870Sstevel@tonic-gate 	struct pollfd	*p;
1880Sstevel@tonic-gate 	int		i;
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 	p = find_pollfd(fd);
1910Sstevel@tonic-gate 	if ((p == NULL) && ((p = find_pollfd(-1)) == NULL)) {
1920Sstevel@tonic-gate 		if ((p = realloc(poll_fds,
1930Sstevel@tonic-gate 		    ((num_pollfds + POLLFDS_GROWTH_SIZE) *
1940Sstevel@tonic-gate 		    sizeof (struct pollfd)))) == NULL) {
1950Sstevel@tonic-gate 			return (-1);
1960Sstevel@tonic-gate 		}
1970Sstevel@tonic-gate 		poll_fds = p;
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 		for (i = 1; i < POLLFDS_GROWTH_SIZE; i++)
2000Sstevel@tonic-gate 			poll_fds[num_pollfds + i].fd = -1;
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 		p = &poll_fds[num_pollfds];
2030Sstevel@tonic-gate 		num_pollfds += POLLFDS_GROWTH_SIZE;
2040Sstevel@tonic-gate 	}
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	p->fd = fd;
2070Sstevel@tonic-gate 	p->events = events;
2080Sstevel@tonic-gate 	p->revents = 0;
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	return (0);
2110Sstevel@tonic-gate }
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate void
clear_pollfd(int fd)2140Sstevel@tonic-gate clear_pollfd(int fd)
2150Sstevel@tonic-gate {
2160Sstevel@tonic-gate 	struct pollfd *p;
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	if ((p = find_pollfd(fd)) != NULL) {
2190Sstevel@tonic-gate 		p->fd = -1;
2200Sstevel@tonic-gate 		p->events = 0;
2210Sstevel@tonic-gate 		p->revents = 0;
2220Sstevel@tonic-gate 	}
2230Sstevel@tonic-gate }
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate boolean_t
isset_pollfd(int fd)2260Sstevel@tonic-gate isset_pollfd(int fd)
2270Sstevel@tonic-gate {
2280Sstevel@tonic-gate 	struct pollfd *p = find_pollfd(fd);
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	return ((p != NULL) && (p->revents & POLLIN));
2310Sstevel@tonic-gate }
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate /*
2340Sstevel@tonic-gate  * An extension of read() that keeps retrying until either the full request has
2350Sstevel@tonic-gate  * completed, the other end of the connection/pipe is closed, no data is
2360Sstevel@tonic-gate  * readable for a non-blocking socket/pipe, or an unexpected error occurs.
2370Sstevel@tonic-gate  * Returns 0 if the data is successfully read, 1 if the other end of the pipe/
2380Sstevel@tonic-gate  * socket is closed or there's nothing to read from a non-blocking socket/pipe,
2390Sstevel@tonic-gate  * else -1 if an unexpected error occurs.
2400Sstevel@tonic-gate  */
2410Sstevel@tonic-gate int
safe_read(int fd,void * buf,size_t sz)2420Sstevel@tonic-gate safe_read(int fd, void *buf, size_t sz)
2430Sstevel@tonic-gate {
2440Sstevel@tonic-gate 	int	ret;
2450Sstevel@tonic-gate 	size_t  cnt = 0;
2460Sstevel@tonic-gate 	char    *cp = (char *)buf;
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 	if (sz == 0)
2490Sstevel@tonic-gate 		return (0);
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 	do {
2520Sstevel@tonic-gate 		switch (ret = read(fd, cp + cnt, sz - cnt)) {
2530Sstevel@tonic-gate 		case 0:			/* other end of pipe/socket closed */
2540Sstevel@tonic-gate 			return (1);
2550Sstevel@tonic-gate 		case -1:
2560Sstevel@tonic-gate 			if (errno == EAGAIN) {		/* nothing to read */
2570Sstevel@tonic-gate 				return (1);
2580Sstevel@tonic-gate 			} else if (errno != EINTR) {
2590Sstevel@tonic-gate 				error_msg(gettext("Unexpected read error: %s"),
2600Sstevel@tonic-gate 				    strerror(errno));
2610Sstevel@tonic-gate 				return (-1);
2620Sstevel@tonic-gate 			}
2630Sstevel@tonic-gate 			break;
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate 		default:
2660Sstevel@tonic-gate 			cnt += ret;
2670Sstevel@tonic-gate 		}
2680Sstevel@tonic-gate 	} while (cnt != sz);
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 	return (0);
2710Sstevel@tonic-gate }
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate /*
2740Sstevel@tonic-gate  * Return B_TRUE if instance 'inst' has exceeded its configured maximum
2750Sstevel@tonic-gate  * concurrent copies limit, else B_FALSE.
2760Sstevel@tonic-gate  */
2770Sstevel@tonic-gate boolean_t
copies_limit_exceeded(instance_t * inst)2780Sstevel@tonic-gate copies_limit_exceeded(instance_t *inst)
2790Sstevel@tonic-gate {
2800Sstevel@tonic-gate 	/* any value <=0 means that copies limits are disabled */
2810Sstevel@tonic-gate 	return ((inst->config->basic->max_copies > 0) &&
2820Sstevel@tonic-gate 	    (inst->copies >= inst->config->basic->max_copies));
2830Sstevel@tonic-gate }
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate /*
2860Sstevel@tonic-gate  * Cancel the method/con-rate offline timer associated with the instance.
2870Sstevel@tonic-gate  */
2880Sstevel@tonic-gate void
cancel_inst_timer(instance_t * inst)2890Sstevel@tonic-gate cancel_inst_timer(instance_t *inst)
2900Sstevel@tonic-gate {
2910Sstevel@tonic-gate 	(void) iu_cancel_timer(timer_queue, inst->timer_id, NULL);
2920Sstevel@tonic-gate 	inst->timer_id = -1;
2930Sstevel@tonic-gate }
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate /*
2960Sstevel@tonic-gate  * Cancel the bind retry timer associated with the instance.
2970Sstevel@tonic-gate  */
2980Sstevel@tonic-gate void
cancel_bind_timer(instance_t * inst)2990Sstevel@tonic-gate cancel_bind_timer(instance_t *inst)
3000Sstevel@tonic-gate {
3010Sstevel@tonic-gate 	(void) iu_cancel_timer(timer_queue, inst->bind_timer_id, NULL);
3020Sstevel@tonic-gate 	inst->bind_timer_id = -1;
3030Sstevel@tonic-gate }
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate void
enable_blocking(int fd)3060Sstevel@tonic-gate enable_blocking(int fd)
3070Sstevel@tonic-gate {
3080Sstevel@tonic-gate 	int flags = fcntl(fd, F_GETFL, 0);
3090Sstevel@tonic-gate 	(void) fcntl(fd, F_SETFL, (flags & ~O_NONBLOCK));
3100Sstevel@tonic-gate }
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate void
disable_blocking(int fd)3130Sstevel@tonic-gate disable_blocking(int fd)
3140Sstevel@tonic-gate {
3150Sstevel@tonic-gate 	int flags = fcntl(fd, F_GETFL, 0);
3160Sstevel@tonic-gate 	(void) fcntl(fd, F_SETFL, (flags | O_NONBLOCK));
3170Sstevel@tonic-gate }
318