xref: /illumos-gate/usr/src/cmd/vntsd/vntsd.c (revision f8871fbc)
11ae08745Sheppo /*
21ae08745Sheppo  * CDDL HEADER START
31ae08745Sheppo  *
41ae08745Sheppo  * The contents of this file are subject to the terms of the
51ae08745Sheppo  * Common Development and Distribution License (the "License").
61ae08745Sheppo  * You may not use this file except in compliance with the License.
71ae08745Sheppo  *
81ae08745Sheppo  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
91ae08745Sheppo  * or http://www.opensolaris.org/os/licensing.
101ae08745Sheppo  * See the License for the specific language governing permissions
111ae08745Sheppo  * and limitations under the License.
121ae08745Sheppo  *
131ae08745Sheppo  * When distributing Covered Code, include this CDDL HEADER in each
141ae08745Sheppo  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
151ae08745Sheppo  * If applicable, add the following below this CDDL HEADER, with the
161ae08745Sheppo  * fields enclosed by brackets "[]" replaced with your own identifying
171ae08745Sheppo  * information: Portions Copyright [yyyy] [name of copyright owner]
181ae08745Sheppo  *
191ae08745Sheppo  * CDDL HEADER END
201ae08745Sheppo  */
211ae08745Sheppo 
221ae08745Sheppo /*
2328b1e50eSSriharsha Basavapatna  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
241ae08745Sheppo  * Use is subject to license terms.
251ae08745Sheppo  */
261ae08745Sheppo 
271ae08745Sheppo /*
281ae08745Sheppo  * VNTSD main
291ae08745Sheppo  *
301ae08745Sheppo  * VNTSD takes the following options:
311ae08745Sheppo  * -i	<device instance>
321ae08745Sheppo  *	VCC device instance to use, e.g. virtual-console-concentrator@0.
331ae08745Sheppo  *	Required option.
341ae08745Sheppo  * -p	<ip address>
351ae08745Sheppo  *	IP address VNTSD listens to.
361ae08745Sheppo  * -d
371ae08745Sheppo  *	Do not daemonize. This is only available in a DEBUG build.
381ae08745Sheppo  * -t	timeout for inactivity 0 = indefinite
3928b1e50eSSriharsha Basavapatna  * -A	enable Authorization checking. Mutually exclusive with -p.
401ae08745Sheppo  */
411ae08745Sheppo 
421ae08745Sheppo #include <stdio.h>
438100efacSdtse #include <stdio_ext.h>
441ae08745Sheppo #include <sys/types.h>
451ae08745Sheppo #include <stdlib.h>
461ae08745Sheppo #include <string.h>
471ae08745Sheppo #include <unistd.h>
481ae08745Sheppo #include <sys/socket.h>
491ae08745Sheppo #include <arpa/inet.h>
501ae08745Sheppo #include <time.h>
511ae08745Sheppo #include <netinet/in.h>
521ae08745Sheppo #include <thread.h>
531ae08745Sheppo #include <signal.h>
541ae08745Sheppo #include <fcntl.h>
551ae08745Sheppo #include <ctype.h>
561ae08745Sheppo #include <libintl.h>
571ae08745Sheppo #include <locale.h>
581ae08745Sheppo #include <syslog.h>
59bd8f0338Snarayan #include <sys/socket.h>
60bd8f0338Snarayan #include <netdb.h>
611ae08745Sheppo #include "vntsd.h"
621ae08745Sheppo #include "chars.h"
631ae08745Sheppo 
641ae08745Sheppo #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
651ae08745Sheppo #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't. */
661ae08745Sheppo #endif
671ae08745Sheppo 
681ae08745Sheppo /* global variables */
691ae08745Sheppo 
701ae08745Sheppo #ifdef DEBUG
711ae08745Sheppo int vntsddbg = 0x8;
721ae08745Sheppo #endif
731ae08745Sheppo 
741ae08745Sheppo #define	MINUTE		60
751ae08745Sheppo 
76bd8f0338Snarayan #define	VNTSD_INVALID_LISTEN_ADDR	    ((in_addr_t)-1)
77bd8f0338Snarayan 
78205eeb1aSlm #define	LOCALHOST_IPv4	"127.0.0.1"
79205eeb1aSlm #define	LOCALHOST_IPv6	"::1"
80205eeb1aSlm 
811ae08745Sheppo static vntsd_t *vntsdp;
821ae08745Sheppo 
831ae08745Sheppo 
841ae08745Sheppo static void vntsd_exit(void);
851ae08745Sheppo /* Signal handler for SIGINT, SIGKILL and SIGHUP */
861ae08745Sheppo static void
exit_sig_handler(int sig)871ae08745Sheppo exit_sig_handler(int sig)
881ae08745Sheppo {
891ae08745Sheppo 
901ae08745Sheppo 	D1(stderr, "t@%d exit_sig_handler%d \n", thr_self(), sig);
911ae08745Sheppo 
924d39be2bSsg 	if (thr_self() != vntsdp->tid) {
934d39be2bSsg 		/* not main thread, pass to main thread */
944d39be2bSsg 		(void) thr_kill(vntsdp->tid, sig);
954d39be2bSsg 	} else {
964d39be2bSsg 		exit(0);
974d39be2bSsg 	}
981ae08745Sheppo }
991ae08745Sheppo 
1001ae08745Sheppo /*
1011ae08745Sheppo  * Before a thread reads in client's input, it attaches to vntsd timer so that
1021ae08745Sheppo  * it can be waken up if a client does not access the connection for
1031ae08745Sheppo  * VNTSD_INPUT_TIMEOUT(10) minutes.
1041ae08745Sheppo  */
1051ae08745Sheppo 
1061ae08745Sheppo /* attach a thread to timer */
1071ae08745Sheppo int
vntsd_attach_timer(vntsd_timeout_t * tmop)1081ae08745Sheppo vntsd_attach_timer(vntsd_timeout_t *tmop)
1091ae08745Sheppo {
1101ae08745Sheppo 	int	rv;
1111ae08745Sheppo 
1121ae08745Sheppo 	if (vntsdp->timeout == 0) {
1131ae08745Sheppo 		return (VNTSD_SUCCESS);
1141ae08745Sheppo 	}
1151ae08745Sheppo 
1161ae08745Sheppo 	(void) mutex_lock(&vntsdp->tmo_lock);
1171ae08745Sheppo 	rv = vntsd_que_append(&vntsdp->tmoq, (void *)tmop);
1181ae08745Sheppo 	(void) mutex_unlock(&vntsdp->tmo_lock);
1191ae08745Sheppo 	return (rv);
1201ae08745Sheppo }
1211ae08745Sheppo 
1221ae08745Sheppo /* detach a thread from timer */
1231ae08745Sheppo int
vntsd_detach_timer(vntsd_timeout_t * tmop)1241ae08745Sheppo vntsd_detach_timer(vntsd_timeout_t *tmop)
1251ae08745Sheppo {
1261ae08745Sheppo 	int	rv;
1271ae08745Sheppo 
1281ae08745Sheppo 	if (vntsdp->timeout == 0) {
1291ae08745Sheppo 		return (VNTSD_SUCCESS);
1301ae08745Sheppo 	}
1311ae08745Sheppo 
1321ae08745Sheppo 	(void) mutex_lock(&vntsdp->tmo_lock);
1331ae08745Sheppo 	rv = vntsd_que_rm(&vntsdp->tmoq, (void *)tmop);
1341ae08745Sheppo 	(void) mutex_unlock(&vntsdp->tmo_lock);
1351ae08745Sheppo 
1361ae08745Sheppo 	return (rv);
1371ae08745Sheppo }
1381ae08745Sheppo 
1391ae08745Sheppo /* check threadd's timeout */
1401ae08745Sheppo static boolean_t
chk_timeout(vntsd_timeout_t * tmop)1411ae08745Sheppo chk_timeout(vntsd_timeout_t *tmop)
1421ae08745Sheppo {
1431ae08745Sheppo 	tmop->minutes++;
1441ae08745Sheppo 
1451ae08745Sheppo 	if (tmop->minutes == vntsdp->timeout) {
1461ae08745Sheppo 		/* wake up the thread */
1471ae08745Sheppo 		tmop->clientp->status |= VNTSD_CLIENT_TIMEOUT;
1481ae08745Sheppo 		(void) thr_kill(tmop->tid, SIGALRM);
1491ae08745Sheppo 	}
1501ae08745Sheppo 
1511ae08745Sheppo 	/* return false to walk the queue */
1521ae08745Sheppo 	return (B_FALSE);
1531ae08745Sheppo }
1541ae08745Sheppo 
1551ae08745Sheppo /* reset timer */
156*f8871fbcSToomas Soome static int
reset_timeout(void * arg1,void * arg2)157*f8871fbcSToomas Soome reset_timeout(void *arg1, void *arg2)
1581ae08745Sheppo {
159*f8871fbcSToomas Soome 	vntsd_timeout_t *tmop = arg1;
160*f8871fbcSToomas Soome 	thread_t tid = (uintptr_t)arg2;
161*f8871fbcSToomas Soome 
1621ae08745Sheppo 	if (tmop->tid == tid) {
1631ae08745Sheppo 		tmop->minutes = 0;
1641ae08745Sheppo 	}
1651ae08745Sheppo 	/* return false to walk the queue */
1661ae08745Sheppo 	return (B_FALSE);
1671ae08745Sheppo }
1681ae08745Sheppo 
1691ae08745Sheppo void
vntsd_reset_timer(thread_t tid)1701ae08745Sheppo vntsd_reset_timer(thread_t tid)
1711ae08745Sheppo {
1721ae08745Sheppo 	if (vntsdp->timeout == 0) {
1731ae08745Sheppo 		return;
1741ae08745Sheppo 	}
1751ae08745Sheppo 
1761ae08745Sheppo 	(void) mutex_lock(&vntsdp->tmo_lock);
177*f8871fbcSToomas Soome 	(void) vntsd_que_find(vntsdp->tmoq, reset_timeout,
178*f8871fbcSToomas Soome 	    (void *)(uintptr_t)tid);
1791ae08745Sheppo 	(void) mutex_unlock(&vntsdp->tmo_lock);
1801ae08745Sheppo }
1811ae08745Sheppo 
1821ae08745Sheppo /*
1831ae08745Sheppo  * When alarm goes off, wake up timeout threads. Alarm is set off every
1841ae08745Sheppo  * minutes.
1851ae08745Sheppo  */
1861ae08745Sheppo static void
vntsd_alarm_sig_handler(int sig)1871ae08745Sheppo vntsd_alarm_sig_handler(int sig)
1881ae08745Sheppo {
1891ae08745Sheppo 	static thread_t main_thread = 0;
1901ae08745Sheppo 
1911ae08745Sheppo 	D1(stderr, "t@%d alarm signal %d\n", thr_self(), sig);
1921ae08745Sheppo 	if (vntsdp->timeout == 0) {
1931ae08745Sheppo 		DERR(stderr, "t@%d alarm signal should not recv %d\n",
1941ae08745Sheppo 		    thr_self(), sig);
1951ae08745Sheppo 		return;
1961ae08745Sheppo 	}
1971ae08745Sheppo 
1981ae08745Sheppo 
1991ae08745Sheppo 	if (main_thread == 0) {
2001ae08745Sheppo 		/* initialize thread id  */
2011ae08745Sheppo 		main_thread = thr_self();
2021ae08745Sheppo 	} else if (main_thread != thr_self()) {
2031ae08745Sheppo 		/* get signal because thread is timeout */
2041ae08745Sheppo 		return;
2051ae08745Sheppo 	}
2061ae08745Sheppo 
2071ae08745Sheppo 	/* in main thread */
2081ae08745Sheppo 	(void) mutex_lock(&vntsdp->tmo_lock);
2091ae08745Sheppo 
2101ae08745Sheppo 	/* wake up timeout threads */
2111ae08745Sheppo 	(void) vntsd_que_walk(vntsdp->tmoq, (el_func_t)chk_timeout);
2121ae08745Sheppo 	(void) mutex_unlock(&vntsdp->tmo_lock);
2131ae08745Sheppo 
2141ae08745Sheppo 	/* reset alarm */
2151ae08745Sheppo 	(void) alarm(MINUTE);
2161ae08745Sheppo }
2171ae08745Sheppo 
2181ae08745Sheppo /* got a  SIGUSER1 siginal */
2191ae08745Sheppo static void
vntsd_sig_handler(int sig)2201ae08745Sheppo vntsd_sig_handler(int sig)
2211ae08745Sheppo {
2221ae08745Sheppo 	char err_msg[VNTSD_LINE_LEN];
2231ae08745Sheppo 
2241ae08745Sheppo 	(void) snprintf(err_msg, sizeof (err_msg), "sig_handler() sig=%d",
2251ae08745Sheppo 	    sig);
2261ae08745Sheppo 
2271ae08745Sheppo 	if (sig != SIGUSR1) {
2281ae08745Sheppo 		vntsd_log(VNTSD_STATUS_SIG, err_msg);
2291ae08745Sheppo 	}
2301ae08745Sheppo }
2311ae08745Sheppo 
2321ae08745Sheppo /* vntsd exits */
2331ae08745Sheppo static void
vntsd_exit(void)2341ae08745Sheppo vntsd_exit(void)
2351ae08745Sheppo {
2361ae08745Sheppo 	D1(stderr, "t@%d vntsd_exit\n", thr_self());
2371ae08745Sheppo 
2381ae08745Sheppo 	(void) mutex_lock(&vntsdp->lock);
2391ae08745Sheppo 
2401ae08745Sheppo 	if (vntsdp->timeout > 0) {
2411ae08745Sheppo 		/* cancel the timer */
2421ae08745Sheppo 		(void) alarm(0);
2431ae08745Sheppo 	}
2441ae08745Sheppo 	/* delete all  groups */
2451ae08745Sheppo 	vntsd_free_que(&vntsdp->grouppq, (clean_func_t)vntsd_clean_group);
2461ae08745Sheppo 
2471ae08745Sheppo 	/* close control port */
2481ae08745Sheppo 	(void) close(vntsdp->ctrl_fd);
2491ae08745Sheppo 
2501ae08745Sheppo 	assert(vntsdp->tmoq == NULL);
2511ae08745Sheppo 	(void) mutex_unlock(&vntsdp->lock);
2521ae08745Sheppo 
2531ae08745Sheppo 	/* clean up vntsdp */
2541ae08745Sheppo 	(void) mutex_destroy(&vntsdp->tmo_lock);
2551ae08745Sheppo 	(void) mutex_destroy(&vntsdp->lock);
2561ae08745Sheppo 	free(vntsdp);
2571ae08745Sheppo 	closelog();
2581ae08745Sheppo }
2591ae08745Sheppo 
2601ae08745Sheppo /*
2611ae08745Sheppo  * vntsd_help()
2621ae08745Sheppo  * print out valid command line options
2631ae08745Sheppo  */
2641ae08745Sheppo static void
vntsd_help(void)2651ae08745Sheppo vntsd_help(void)
2661ae08745Sheppo {
2671ae08745Sheppo 	(void) fprintf(stderr, gettext("Usage: vntsd -i <VCC device instance> "
26828b1e50eSSriharsha Basavapatna 	    "[-p <listen address>] [-t <timeout in minutes>] [-A]\n"));
2691ae08745Sheppo }
2701ae08745Sheppo 
271bd8f0338Snarayan /*
272bd8f0338Snarayan  * get_listen_ip_addr()
273bd8f0338Snarayan  * check for a valid control domain ip address in format of xxx.xxx.xxx.xxx.
274bd8f0338Snarayan  * if ip address is valid and is assigned to this host, return ip address
275bd8f0338Snarayan  * or else return VNTSD_INVALID_LISTEN_ADDR.
276bd8f0338Snarayan  */
277bd8f0338Snarayan static in_addr_t
get_listen_ip_addr(char * listen_addr)278bd8f0338Snarayan get_listen_ip_addr(char *listen_addr)
279bd8f0338Snarayan {
280bd8f0338Snarayan 	char host_name[MAXPATHLEN];
281bd8f0338Snarayan 	in_addr_t addr;
282bd8f0338Snarayan 	struct addrinfo hints;
283bd8f0338Snarayan 	struct addrinfo *res, *infop;
284bd8f0338Snarayan 	int err;
285bd8f0338Snarayan 	struct sockaddr_in *sa;
286bd8f0338Snarayan 
287bd8f0338Snarayan 	if (gethostname(host_name, MAXPATHLEN) != 0) {
288bd8f0338Snarayan 		syslog(LOG_ERR, "Can not get host name!");
289bd8f0338Snarayan 		return (VNTSD_INVALID_LISTEN_ADDR);
290bd8f0338Snarayan 	}
291bd8f0338Snarayan 
292bd8f0338Snarayan 	if ((int)(addr = inet_addr(listen_addr)) == -1)
293bd8f0338Snarayan 		/* bad IP address format */
294bd8f0338Snarayan 		return (VNTSD_INVALID_LISTEN_ADDR);
295bd8f0338Snarayan 
296bd8f0338Snarayan 	bzero(&hints, sizeof (hints));
297bd8f0338Snarayan 	hints.ai_family = PF_INET;
298bd8f0338Snarayan 	hints.ai_socktype = SOCK_STREAM;
299bd8f0338Snarayan 
300bd8f0338Snarayan 	err = getaddrinfo(host_name, NULL, &hints, &res);
301bd8f0338Snarayan 	if (err != 0) {
302bd8f0338Snarayan 		syslog(LOG_ERR, "getaddrinfo failed: %s", gai_strerror(err));
303bd8f0338Snarayan 		return (VNTSD_INVALID_LISTEN_ADDR);
304bd8f0338Snarayan 	}
305bd8f0338Snarayan 
306bd8f0338Snarayan 	infop = res;
307bd8f0338Snarayan 	while (infop != NULL) {
308bd8f0338Snarayan 		/* LINTED E_BAD_PTR_CAST_ALIGN */
309bd8f0338Snarayan 		sa = (struct sockaddr_in *)infop->ai_addr;
310bd8f0338Snarayan 		if (sa->sin_addr.s_addr == addr) {
311bd8f0338Snarayan 			/* ip address found */
312bd8f0338Snarayan 			freeaddrinfo(res);
313bd8f0338Snarayan 			return (addr);
314bd8f0338Snarayan 		}
315bd8f0338Snarayan 		infop = infop->ai_next;
316bd8f0338Snarayan 	}
317bd8f0338Snarayan 
318bd8f0338Snarayan 	/* ip address not found */
319bd8f0338Snarayan 	freeaddrinfo(res);
320bd8f0338Snarayan 	return (VNTSD_INVALID_LISTEN_ADDR);
321bd8f0338Snarayan }
3221ae08745Sheppo 
3231ae08745Sheppo #ifdef DEBUG
3241ae08745Sheppo #define	DEBUG_OPTIONS	"d"
3251ae08745Sheppo #else
3261ae08745Sheppo #define	DEBUG_OPTIONS	""
3271ae08745Sheppo #endif
3281ae08745Sheppo 
3291ae08745Sheppo int
main(int argc,char ** argv)3301ae08745Sheppo main(int argc, char ** argv)
3311ae08745Sheppo {
3321ae08745Sheppo 	char	    *path;
3331ae08745Sheppo 	struct	    pollfd poll_drv[1];
3341ae08745Sheppo 	struct	    sigaction act;
3358100efacSdtse 	struct	    rlimit rlim;
3361ae08745Sheppo 	char	    *listen_addr = NULL;
3371ae08745Sheppo 	pid_t	    pid;
3381ae08745Sheppo 	int	    i;
3391ae08745Sheppo 	int	    option;
3401ae08745Sheppo 	int	    sz;
3411ae08745Sheppo 	int	    fd;
3421ae08745Sheppo 	int	    n;
3431ae08745Sheppo 
3441ae08745Sheppo 	/* internationalization */
3451ae08745Sheppo 	(void) setlocale(LC_MESSAGES, "");
3461ae08745Sheppo 	(void) textdomain(TEXT_DOMAIN);
3471ae08745Sheppo 	vntsd_init_esctable_msgs();
3481ae08745Sheppo 
3491ae08745Sheppo 	/* initialization */
3501ae08745Sheppo 	bzero(&act, sizeof (act));
3511ae08745Sheppo 
3528100efacSdtse 	/*
3538100efacSdtse 	 * ensure that we can obtain sufficient file descriptors for all
3548100efacSdtse 	 * the accept() calls when a machine contains many domains.
3558100efacSdtse 	 */
3568100efacSdtse 	(void) getrlimit(RLIMIT_NOFILE, &rlim);
3578100efacSdtse 	if (rlim.rlim_cur < rlim.rlim_max)
3588100efacSdtse 		rlim.rlim_cur = rlim.rlim_max;
3598100efacSdtse 	if (setrlimit(RLIMIT_NOFILE, &rlim) < 0)
3608100efacSdtse 		vntsd_log(VNTSD_STATUS_CONTINUE, "Unable to increase file "
3618100efacSdtse 		    "descriptor limit.");
3628100efacSdtse 	(void) enable_extended_FILE_stdio(-1, -1);
3638100efacSdtse 
364476d5ff7SToomas Soome 	vntsdp = calloc(1, sizeof (vntsd_t));
3651ae08745Sheppo 	if (vntsdp == NULL) {
3661ae08745Sheppo 		vntsd_log(VNTSD_ERR_NO_MEM, "main:vntsdp");
3671ae08745Sheppo 		exit(1);
3681ae08745Sheppo 	}
3691ae08745Sheppo 
3701ae08745Sheppo 	vntsdp->ctrl_fd = -1;
3711ae08745Sheppo 	vntsdp->devinst = NULL;
3721ae08745Sheppo 
3731ae08745Sheppo 	(void) mutex_init(&vntsdp->lock, USYNC_THREAD|LOCK_ERRORCHECK, NULL);
3741ae08745Sheppo 	(void) mutex_init(&vntsdp->tmo_lock, USYNC_THREAD|LOCK_ERRORCHECK,
3751ae08745Sheppo 	    NULL);
3761ae08745Sheppo 
3771ae08745Sheppo 	/* get CLI options */
37828b1e50eSSriharsha Basavapatna 	while ((option = getopt(argc, argv, "i:t:p:A"DEBUG_OPTIONS)) != EOF) {
3791ae08745Sheppo 		switch (option) {
3801ae08745Sheppo #ifdef DEBUG
3811ae08745Sheppo 		case 'd':
3821ae08745Sheppo 			vntsdp->options |= VNTSD_OPT_DAEMON_OFF;
3831ae08745Sheppo 			break;
3841ae08745Sheppo #endif
3851ae08745Sheppo 		case 'i':
386