xref: /illumos-gate/usr/src/cmd/vntsd/vntsdvcc.c (revision 476d5ff7)
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 /*
223c96341aSnarayan  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
231ae08745Sheppo  * Use is subject to license terms.
241ae08745Sheppo  */
251ae08745Sheppo 
261ae08745Sheppo /*
271ae08745Sheppo  * Configuration and setup interface to vcc driver.
281ae08745Sheppo  * At intialization time, vntsd opens vcc ctrl port and read initial
291ae08745Sheppo  * configuratioa. It manages console groups, creates the listen thread,
301ae08745Sheppo  * dynamically adds and removes virtual console within a group.
311ae08745Sheppo  */
321ae08745Sheppo 
331ae08745Sheppo 
341ae08745Sheppo #include <syslog.h>
351ae08745Sheppo #include <stdio.h>
361ae08745Sheppo #include <sys/types.h>
371ae08745Sheppo #include <sys/ipc.h>
381ae08745Sheppo #include <stdlib.h>
391ae08745Sheppo #include <string.h>
401ae08745Sheppo #include <unistd.h>
411ae08745Sheppo #include <sys/socket.h>
421ae08745Sheppo #include <sys/ipc.h>
431ae08745Sheppo #include <sys/shm.h>
441ae08745Sheppo #include <sys/sem.h>
451ae08745Sheppo #include <wait.h>
461ae08745Sheppo #include <time.h>
471ae08745Sheppo #include <synch.h>
481ae08745Sheppo #include <netinet/in.h>
491ae08745Sheppo #include <thread.h>
501ae08745Sheppo #include <signal.h>
511ae08745Sheppo #include "vntsd.h"
521ae08745Sheppo 
531ae08745Sheppo /* signal all clients that console has been deleted */
541ae08745Sheppo boolean_t
vntsd_notify_client_cons_del(vntsd_client_t * clientp)551ae08745Sheppo vntsd_notify_client_cons_del(vntsd_client_t *clientp)
561ae08745Sheppo {
571ae08745Sheppo 	(void) mutex_lock(&clientp->lock);
581ae08745Sheppo 	clientp->status |= VNTSD_CLIENT_CONS_DELETED;
591ae08745Sheppo 	(void) thr_kill(clientp->cons_tid, SIGUSR1);
601ae08745Sheppo 	(void) mutex_unlock(&clientp->lock);
611ae08745Sheppo 	return (B_FALSE);
621ae08745Sheppo }
631ae08745Sheppo 
641ae08745Sheppo /* free console  structure */
651ae08745Sheppo static void
free_cons(vntsd_cons_t * consp)661ae08745Sheppo free_cons(vntsd_cons_t *consp)
671ae08745Sheppo {
681ae08745Sheppo 	assert(consp);
691ae08745Sheppo 	(void) mutex_destroy(&consp->lock);
701ae08745Sheppo 	(void) cond_destroy(&consp->cvp);
714d39be2bSsg 	if (consp->vcc_fd != -1)
724d39be2bSsg 		(void) close(consp->vcc_fd);
731ae08745Sheppo 	free(consp);
741ae08745Sheppo }
751ae08745Sheppo 
764d39be2bSsg /* free group structure */
774d39be2bSsg static void
free_group(vntsd_group_t * groupp)784d39be2bSsg free_group(vntsd_group_t *groupp)
794d39be2bSsg {
804d39be2bSsg 	assert(groupp);
814d39be2bSsg 	(void) mutex_destroy(&groupp->lock);
824d39be2bSsg 	(void) cond_destroy(&groupp->cvp);
834d39be2bSsg 	if (groupp->sockfd != -1)
844d39be2bSsg 		(void) close(groupp->sockfd);
854d39be2bSsg 	free(groupp);
864d39be2bSsg }
874d39be2bSsg 
881ae08745Sheppo /*
891ae08745Sheppo  *  all clients connected to a console must disconnect before
901ae08745Sheppo  *  removing a console.
911ae08745Sheppo  */
921ae08745Sheppo static void
cleanup_cons(vntsd_cons_t * consp)931ae08745Sheppo cleanup_cons(vntsd_cons_t *consp)
941ae08745Sheppo {
951ae08745Sheppo 	vntsd_group_t	*groupp;
961ae08745Sheppo 	timestruc_t	to;
971ae08745Sheppo 
981ae08745Sheppo 	assert(consp);
991ae08745Sheppo 	D1(stderr, "t@%d vntsd_disconn_clients@%d\n", thr_self(),
1001ae08745Sheppo 	    consp->cons_no);
1011ae08745Sheppo 
1021ae08745Sheppo 	groupp = consp->group;
1031ae08745Sheppo 	assert(groupp);
1041ae08745Sheppo 
1051ae08745Sheppo 
1061ae08745Sheppo 	(void) mutex_lock(&consp->lock);
1071ae08745Sheppo 
1081ae08745Sheppo 	/* wait for all clients disconnect from the console */
1091ae08745Sheppo 	while (consp->clientpq != NULL) {
1101ae08745Sheppo 		consp->status |= VNTSD_CONS_SIG_WAIT;
1111ae08745Sheppo 
1121ae08745Sheppo 		/* signal client to disconnect the console */
1131ae08745Sheppo 		(void) vntsd_que_walk(consp->clientpq,
1141ae08745Sheppo 		    (el_func_t)vntsd_notify_client_cons_del);
1151ae08745Sheppo 
1161ae08745Sheppo 		(void) thr_kill(consp->wr_tid, SIGUSR1);
1171ae08745Sheppo 		to.tv_sec = VNTSD_CV_WAIT_DELTIME;
1181ae08745Sheppo 		to.tv_nsec = 0;
1191ae08745Sheppo 
1201ae08745Sheppo 		/* wait for clients to disconnect  */
1211ae08745Sheppo 		(void) cond_reltimedwait(&consp->cvp, &consp->lock, &to);
1221ae08745Sheppo 	}
1231ae08745Sheppo 
1243af08d82Slm 	/* reduce console count in the group */
1253af08d82Slm 	(void) mutex_lock(&groupp->lock);
1263af08d82Slm 	assert(groupp->num_cons > 0);
1273af08d82Slm 	groupp->num_cons--;
1283af08d82Slm 	(void) mutex_unlock(&groupp->lock);
1293af08d82Slm 
1301ae08745Sheppo 	(void) mutex_unlock(&consp->lock);
1311ae08745Sheppo 
1321ae08745Sheppo 	free_cons(consp);
1331ae08745Sheppo }
1341ae08745Sheppo 
1351ae08745Sheppo /* search for a group whose console is being deleted */
1361ae08745Sheppo static boolean_t
find_clean_cons_group(vntsd_group_t * groupp)1371ae08745Sheppo find_clean_cons_group(vntsd_group_t *groupp)
1381ae08745Sheppo {
1391ae08745Sheppo 	if (groupp->status & VNTSD_GROUP_CLEAN_CONS) {
1401ae08745Sheppo 		return (B_TRUE);
1411ae08745Sheppo 	} else {
1421ae08745Sheppo 		return (B_FALSE);
1431ae08745Sheppo 	}
1441ae08745Sheppo }
1451ae08745Sheppo 
1461ae08745Sheppo /* search for a console that is being deleted */
1471ae08745Sheppo static boolean_t
find_clean_cons(vntsd_cons_t * consp)1481ae08745Sheppo find_clean_cons(vntsd_cons_t *consp)
1491ae08745Sheppo {
1501ae08745Sheppo 	if (consp->status & VNTSD_CONS_DELETED) {
1511ae08745Sheppo 		return (B_TRUE);
1521ae08745Sheppo 	} else {
1531ae08745Sheppo 		return (B_FALSE);
1541ae08745Sheppo 	}
1551ae08745Sheppo }
1561ae08745Sheppo 
1571ae08745Sheppo /* delete a console */
1581ae08745Sheppo void
vntsd_delete_cons(vntsd_t * vntsdp)1591ae08745Sheppo vntsd_delete_cons(vntsd_t *vntsdp)
1601ae08745Sheppo {
1611ae08745Sheppo 	vntsd_group_t *groupp;
1621ae08745Sheppo 	vntsd_cons_t *consp;
1631ae08745Sheppo 
1641ae08745Sheppo 	for (; ; ) {
1651ae08745Sheppo 		/* get the group contains deleted console */
1661ae08745Sheppo 		(void) mutex_lock(&vntsdp->lock);
1671ae08745Sheppo 		groupp = vntsd_que_walk(vntsdp->grouppq,
1681ae08745Sheppo 		    (el_func_t)find_clean_cons_group);
1691ae08745Sheppo 		if (groupp == NULL) {
1701ae08745Sheppo 			/* no more group has console deleted */
1711ae08745Sheppo 			(void) mutex_unlock(&vntsdp->lock);
1721ae08745Sheppo 			return;
1731ae08745Sheppo 		}
1747636cb21Slm 		(void) mutex_lock(&groupp->lock);
1751ae08745Sheppo 		groupp->status &= ~VNTSD_GROUP_CLEAN_CONS;
1767636cb21Slm 		(void) mutex_unlock(&groupp->lock);
1771ae08745Sheppo 		(void) mutex_unlock(&vntsdp->lock);
1781ae08745Sheppo 
1791ae08745Sheppo 		for (; ; ) {
1801ae08745Sheppo 			/* get the console to be deleted */
1811ae08745Sheppo 			(void) mutex_lock(&groupp->lock);
1821ae08745Sheppo 
1834d39be2bSsg 			/* clean up any deleted console in the group */
1844d39be2bSsg 			if (groupp->conspq != NULL) {
1854d39be2bSsg 				consp = vntsd_que_walk(groupp->conspq,
1864d39be2bSsg 				    (el_func_t)find_clean_cons);
1874d39be2bSsg 				if (consp == NULL) {
1884d39be2bSsg 					/* no more cons to delete */
1894d39be2bSsg 					(void) mutex_unlock(&groupp->lock);
1904d39be2bSsg 					break;
1914d39be2bSsg 				}
1924d39be2bSsg 
1934d39be2bSsg 				/* remove console from the group */
1944d39be2bSsg 				(void) vntsd_que_rm(&groupp->conspq, consp);
1954d39be2bSsg 				(void) mutex_unlock(&groupp->lock);
1961ae08745Sheppo 
1974d39be2bSsg 				/* clean up the console */
1984d39be2bSsg 				cleanup_cons(consp);
1994d39be2bSsg 			}
2001ae08745Sheppo 
2011ae08745Sheppo 			/* delete group? */
2024d39be2bSsg 			if (groupp->conspq == NULL) {
2034d39be2bSsg 				/* no more console in the group delete group */
2041ae08745Sheppo 				assert(groupp->vntsd);
2051ae08745Sheppo 
2061ae08745Sheppo 				(void) mutex_lock(&groupp->vntsd->lock);
2071ae08745Sheppo 				(void) vntsd_que_rm(&groupp->vntsd->grouppq,
208*476d5ff7SToomas Soome 				    groupp);
2091ae08745Sheppo 				(void) mutex_unlock(&groupp->vntsd->lock);
2101ae08745Sheppo 
2111ae08745Sheppo 				/* clean up the group */
2121ae08745Sheppo 				vntsd_clean_group(groupp);
2131ae08745Sheppo 				break;
2141ae08745Sheppo 			}
2151ae08745Sheppo 		}
2161ae08745Sheppo 	}
2171ae08745Sheppo }
2181ae08745Sheppo 
2191ae08745Sheppo /* clean up a group */
2201ae08745Sheppo void
vntsd_clean_group(vntsd_group_t * groupp)2211ae08745Sheppo vntsd_clean_group(vntsd_group_t *groupp)
2221ae08745Sheppo {
2231ae08745Sheppo 
2241ae08745Sheppo 	timestruc_t	to;
2251ae08745Sheppo 
2261ae08745Sheppo 	D1(stderr, "t@%d clean_group() group=%s tcp=%lld\n", thr_self(),
2271ae08745Sheppo 	    groupp->group_name, groupp->tcp_port);
2281ae08745Sheppo 
2291ae08745Sheppo 	(void) mutex_lock(&groupp->lock);
2301ae08745Sheppo 
2311ae08745Sheppo 	/* prevent from reentry */
2324d39be2bSsg 	if (groupp->status & VNTSD_GROUP_IN_CLEANUP) {
2331ae08745Sheppo 		(void) mutex_unlock(&groupp->lock);
2341ae08745Sheppo 		return;
2351ae08745Sheppo 	}
2364d39be2bSsg 	groupp->status |= VNTSD_GROUP_IN_CLEANUP;
2374d39be2bSsg 
2384d39be2bSsg 	/* mark group waiting for listen thread to exits */
2394d39be2bSsg 	groupp->status |= VNTSD_GROUP_SIG_WAIT;
2401ae08745Sheppo 	(void) mutex_unlock(&groupp->lock);
2411ae08745Sheppo 
2423af08d82Slm 	vntsd_free_que(&groupp->conspq, (clean_func_t)cleanup_cons);
2433af08d82Slm 
2444d39be2bSsg 	(void) mutex_lock(&groupp->lock);
2451ae08745Sheppo 	/* walk through no cons client queue */
2461ae08745Sheppo 	while (groupp->no_cons_clientpq != NULL) {
2471ae08745Sheppo 		(void) vntsd_que_walk(groupp->no_cons_clientpq,
2481ae08745Sheppo 		    (el_func_t)vntsd_notify_client_cons_del);
2491ae08745Sheppo 		to.tv_sec = VNTSD_CV_WAIT_DELTIME;
2501ae08745Sheppo 		to.tv_nsec = 0;
2511ae08745Sheppo 		(void) cond_reltimedwait(&groupp->cvp, &groupp->lock, &to);
2521ae08745Sheppo 	}
2531ae08745Sheppo 
2544d39be2bSsg 	/* waiting for listen thread to exit */
2551ae08745Sheppo 	while (groupp->status & VNTSD_GROUP_SIG_WAIT) {
2564d39be2bSsg 		/* signal listen thread to exit  */
2571ae08745Sheppo 		(void) thr_kill(groupp->listen_tid, SIGUSR1);
2581ae08745Sheppo 		to.tv_sec = VNTSD_CV_WAIT_DELTIME;
2591ae08745Sheppo 		to.tv_nsec = 0;
2601ae08745Sheppo 		/* wait listen thread to exit  */
2611ae08745Sheppo 		(void) cond_reltimedwait(&groupp->cvp, &groupp->lock, &to);
2621ae08745Sheppo 	}
2631ae08745Sheppo 
2641ae08745Sheppo 	(void) mutex_unlock(&groupp->lock);
2651ae08745Sheppo 	(void) thr_join(groupp->listen_tid, NULL, NULL);
2661ae08745Sheppo 	/* free group */
2674d39be2bSsg 	free_group(groupp);
2681ae08745Sheppo }
2691ae08745Sheppo 
2701ae08745Sheppo /* allocate and initialize console structure */
2711ae08745Sheppo static vntsd_cons_t *
alloc_cons(vntsd_group_t * groupp,vcc_console_t * consolep)2721ae08745Sheppo alloc_cons(vntsd_group_t *groupp, vcc_console_t *consolep)
2731ae08745Sheppo {
2741ae08745Sheppo 	vntsd_cons_t *consp;
2751ae08745Sheppo 	int	rv;
2761ae08745Sheppo 
2771ae08745Sheppo 	/* allocate console */
2781ae08745Sheppo 	consp = (vntsd_cons_t *)malloc(sizeof (vntsd_cons_t));
2791ae08745Sheppo 	if (consp == NULL) {
2801ae08745Sheppo 		vntsd_log(VNTSD_ERR_NO_MEM, "alloc_cons");
2811ae08745Sheppo 		return (NULL);
2821ae08745Sheppo 	}
2831ae08745Sheppo 
2841ae08745Sheppo 	/* intialize console */
2851ae08745Sheppo 	bzero(consp, sizeof (vntsd_cons_t));
2861ae08745Sheppo 
2871ae08745Sheppo 	(void) mutex_init(&consp->lock, USYNC_THREAD|LOCK_ERRORCHECK, NULL);
2881ae08745Sheppo 	(void) cond_init(&consp->cvp, USYNC_THREAD, NULL);
2891ae08745Sheppo 
2901ae08745Sheppo 	consp->cons_no = consolep->cons_no;
2911ae08745Sheppo 	(void) strlcpy(consp->domain_name, consolep->domain_name, MAXPATHLEN);
2921ae08745Sheppo 	(void) strlcpy(consp->dev_name, consolep->dev_name, MAXPATHLEN);
2931ae08745Sheppo 	consp->wr_tid = (thread_t)-1;
2944d39be2bSsg 	consp->vcc_fd = -1;
2951ae08745Sheppo 
2961ae08745Sheppo 	/* join the group */
2971ae08745Sheppo 	(void) mutex_lock(&groupp->lock);
2981ae08745Sheppo 
2991ae08745Sheppo 	if ((rv = vntsd_que_append(&groupp->conspq, consp)) !=
3001ae08745Sheppo 	    VNTSD_SUCCESS) {
3011ae08745Sheppo 		(void) mutex_unlock(&groupp->lock);
3021ae08745Sheppo 		vntsd_log(rv, "alloc_cons");
3031ae08745Sheppo 		free_cons(consp);
3041ae08745Sheppo 		return (NULL);
3051ae08745Sheppo 	}
3061ae08745Sheppo 	groupp->num_cons++;
3071ae08745Sheppo 	consp->group = groupp;
3081ae08745Sheppo 
3091ae08745Sheppo 	(void) mutex_unlock(&groupp->lock);
3101ae08745Sheppo 
3111ae08745Sheppo 	D1(stderr, "t@%d alloc_cons@%d %s %s\n", thr_self(),
3121ae08745Sheppo 	    consp->cons_no, consp->domain_name, consp->dev_name);
3131ae08745Sheppo 
3141ae08745Sheppo 	return (consp);
3151ae08745Sheppo }
3161ae08745Sheppo 
3171ae08745Sheppo /* compare tcp with group->tcp */
3181ae08745Sheppo static boolean_t
grp_by_tcp(vntsd_group_t * groupp,uint64_t * tcp_port)3191ae08745Sheppo grp_by_tcp(vntsd_group_t *groupp, uint64_t *tcp_port)
3201ae08745Sheppo {
3211ae08745Sheppo 	assert(groupp);
3221ae08745Sheppo 	assert(tcp_port);
3231ae08745Sheppo 	return (groupp->tcp_port == *tcp_port);
3241ae08745Sheppo }
3251ae08745Sheppo 
3261ae08745Sheppo /* allocate and initialize group */
3271ae08745Sheppo static vntsd_group_t *
alloc_group(vntsd_t * vntsdp,char * group_name,uint64_t tcp_port)3281ae08745Sheppo alloc_group(vntsd_t *vntsdp, char *group_name, uint64_t tcp_port)
3291ae08745Sheppo {
3301ae08745Sheppo 	vntsd_group_t *groupp;
3311ae08745Sheppo 
3321ae08745Sheppo 	/* allocate group */
3331ae08745Sheppo 	groupp = (vntsd_group_t *)malloc(sizeof (vntsd_group_t));
3341ae08745Sheppo 	if (groupp == NULL) {
3351ae08745Sheppo 		vntsd_log(VNTSD_ERR_NO_MEM, "alloc_group");
3361ae08745Sheppo 		return (NULL);
3371ae08745Sheppo 	}
3381ae08745Sheppo 
3391ae08745Sheppo 	/* initialize group */
3401ae08745Sheppo 	bzero(groupp, sizeof (vntsd_group_t));
3411ae08745Sheppo 
3421ae08745Sheppo 	(void) mutex_init(&groupp->lock, USYNC_THREAD|LOCK_ERRORCHECK, NULL);
3431ae08745Sheppo 	(void) cond_init(&groupp->cvp, USYNC_THREAD, NULL);
3441ae08745Sheppo 
3451ae08745Sheppo 	if (group_name != NULL) {
3461ae08745Sheppo 		(void) memcpy(groupp->group_name, group_name, MAXPATHLEN);
3471ae08745Sheppo 	}
3481ae08745Sheppo 
3491ae08745Sheppo 	groupp->tcp_port = tcp_port;
3501ae08745Sheppo 	groupp->listen_tid = (thread_t)-1;
3514d39be2bSsg 	groupp->sockfd = -1;
3521ae08745Sheppo 	groupp->vntsd = vntsdp;
3531ae08745Sheppo 
3541ae08745Sheppo 	D1(stderr, "t@%d alloc_group@%lld:%s\n", thr_self(), groupp->tcp_port,
3551ae08745Sheppo 	    groupp->group_name);
3561ae08745Sheppo 
3571ae08745Sheppo 	return (groupp);
3581ae08745Sheppo }
3591ae08745Sheppo 
3604d39be2bSsg /* mark a deleted console */
3614d39be2bSsg boolean_t
vntsd_mark_deleted_cons(vntsd_cons_t * consp)3624d39be2bSsg vntsd_mark_deleted_cons(vntsd_cons_t *consp)
3634d39be2bSsg {
3644d39be2bSsg 	(void) mutex_lock(&consp->lock);
3654d39be2bSsg 	consp->status |= VNTSD_CONS_DELETED;
3664d39be2bSsg 	(void) mutex_unlock(&consp->lock);
3674d39be2bSsg 	return (B_FALSE);
3684d39be2bSsg }
3694d39be2bSsg 
3701ae08745Sheppo /*
3711ae08745Sheppo  * Initialize a console, if console is associated with with a
3721ae08745Sheppo  * new group, intialize the group.
3731ae08745Sheppo  */
3741ae08745Sheppo static int
alloc_cons_with_group(vntsd_t * vntsdp,vcc_console_t * consp,vntsd_group_t ** new_groupp)3751ae08745Sheppo alloc_cons_with_group(vntsd_t *vntsdp, vcc_console_t *consp,
3761ae08745Sheppo     vntsd_group_t **new_groupp)
3771ae08745Sheppo {
3781ae08745Sheppo 	vntsd_group_t	*groupp = NULL;
3791ae08745Sheppo 	int		rv;
3801ae08745Sheppo 
3811ae08745Sheppo 	*new_groupp = NULL;
3821ae08745Sheppo 
3831ae08745Sheppo 	/* match group by tcp port */
3841ae08745Sheppo 
3851ae08745Sheppo 
3861ae08745Sheppo 	(void) mutex_lock(&vntsdp->lock);
3871ae08745Sheppo 	groupp = vntsd_que_find(vntsdp->grouppq,
3881ae08745Sheppo 	    (compare_func_t)grp_by_tcp, (void *)&(consp->tcp_port));
3894d39be2bSsg 	if (groupp != NULL)
3904d39be2bSsg 		(void) mutex_lock(&groupp->lock);
3914d39be2bSsg 
3921ae08745Sheppo 	(void) mutex_unlock(&vntsdp->lock);
3931ae08745Sheppo 
3941ae08745Sheppo 	if (groupp != NULL) {
3954d39be2bSsg 		/*
3964d39be2bSsg 		 *  group with same tcp port found.
3974d39be2bSsg 		 *  if there is no console in the group, the
3984d39be2bSsg 		 *  group should be removed and the tcp port can
3994d39be2bSsg 		 *  be used for tne new group.
4004d39be2bSsg 		 *  This is possible, when there is tight loop of
4014d39be2bSsg 		 *  creating/deleting domains. When a vcc port is
4024d39be2bSsg 		 *  removed, a read thread will have an I/O error because
4034d39be2bSsg 		 *  vcc has closed the port. The read thread then marks
4044d39be2bSsg 		 *  the console is removed and notify main thread to
4054d39be2bSsg 		 *  remove the console.
4064d39be2bSsg 		 *  Meanwhile, the same port and its group (with same
4074d39be2bSsg 		 *  tcp port and group name) is created. Vcc notify
4084d39be2bSsg 		 *  vntsd that new console is added.
4094d39be2bSsg 		 *  Main thread now have two events. If main thread polls
4104d39be2bSsg 		 *  out vcc notification first, it will find that there is
4114d39be2bSsg 		 *  a group has no console.
4124d39be2bSsg 		 */
4134d39be2bSsg 
4144d39be2bSsg 		if (vntsd_chk_group_total_cons(groupp) == 0) {
4154d39be2bSsg 
4164d39be2bSsg 			/* all consoles in the group have been removed */
4174d39be2bSsg 			(void) vntsd_que_walk(groupp->conspq,
4184d39be2bSsg 			    (el_func_t)vntsd_mark_deleted_cons);
4194d39be2bSsg 			groupp->status |= VNTSD_GROUP_CLEAN_CONS;
4204d39be2bSsg 			(void) mutex_unlock(&groupp->lock);
4214d39be2bSsg 			groupp = NULL;
4221ae08745Sheppo 
4234d39be2bSsg 		} else if (strcmp(groupp->group_name, consp->group_name)) {
4241ae08745Sheppo 			/* conflict group name */
4251ae08745Sheppo 			vntsd_log(VNTSD_ERR_VCC_GRP_NAME,
4261ae08745Sheppo 			    "group name is different from existing group");
4274d39be2bSsg 			(void) mutex_unlock(&groupp->lock);
4281ae08745Sheppo 			return (VNTSD_ERR_VCC_CTRL_DATA);
4294d39be2bSsg 
4304d39be2bSsg 		} else {
4314d39be2bSsg 			/* group already existed */
4324d39be2bSsg 			(void) mutex_unlock(&groupp->lock);
4331ae08745Sheppo 		}
4341ae08745Sheppo 
4354d39be2bSsg 	}
4364d39be2bSsg 
4374d39be2bSsg 	if (groupp == NULL) {
4381ae08745Sheppo 		/* new group */
4391ae08745Sheppo 		groupp = alloc_group(vntsdp, consp->group_name,
4401ae08745Sheppo 		    consp->tcp_port);
4411ae08745Sheppo 		if (groupp == NULL) {
4421ae08745Sheppo 			return (VNTSD_ERR_NO_MEM);
4431ae08745Sheppo 		}
4441ae08745Sheppo 
4451ae08745Sheppo 		assert(groupp->conspq == NULL);
4461ae08745Sheppo 		/* queue group to vntsdp */
4471ae08745Sheppo 		(void) mutex_lock(&vntsdp->lock);
4481ae08745Sheppo 		rv = vntsd_que_append(&vntsdp->grouppq, groupp);
4491ae08745Sheppo 		(void) mutex_unlock(&vntsdp->lock);
4501ae08745Sheppo 
4511ae08745Sheppo 		if (rv != VNTSD_SUCCESS) {
4521ae08745Sheppo 			return (rv);
4531ae08745Sheppo 		}
4541ae08745Sheppo 
4551ae08745Sheppo 		*new_groupp = groupp;
4561ae08745Sheppo 	}
4571ae08745Sheppo 
4581ae08745Sheppo 	/* intialize console */
4591ae08745Sheppo 	if (alloc_cons(groupp, consp) == NULL) {
4601ae08745Sheppo 		/* no memory */
461*476d5ff7SToomas Soome 		if (*new_groupp != NULL) {
4621ae08745Sheppo 			/* clean up new group */
4634d39be2bSsg 			free_group(groupp);
4641ae08745Sheppo 		}
4651ae08745Sheppo 
4661ae08745Sheppo 		return (VNTSD_ERR_NO_MEM);
4671ae08745Sheppo 	}
4681ae08745Sheppo 
4691ae08745Sheppo 	return (VNTSD_SUCCESS);
4701ae08745Sheppo 
4711ae08745Sheppo }
4721ae08745Sheppo 
4731ae08745Sheppo 
4741ae08745Sheppo /* create listen thread */
4751ae08745Sheppo static boolean_t
create_listen_thread(vntsd_group_t * groupp)4761ae08745Sheppo create_listen_thread(vntsd_group_t *groupp)
4771ae08745Sheppo {
4781ae08745Sheppo 
4791ae08745Sheppo 	char err_msg[VNTSD_LINE_LEN];
4801ae08745Sheppo 	int rv;
4811ae08745Sheppo 
4821ae08745Sheppo 	assert(groupp);
4831ae08745Sheppo 
4841ae08745Sheppo 	(void) mutex_lock(&groupp->lock);
4851ae08745Sheppo 	assert(groupp->num_cons);
4861ae08745Sheppo 
4871ae08745Sheppo 	D1(stderr, "t@%d create_listen:%lld\n", thr_self(), groupp->tcp_port);
4881ae08745Sheppo 
4891ae08745Sheppo 	if ((rv = thr_create(NULL, 0, (thr_func_t)vntsd_listen_thread,
490*476d5ff7SToomas Soome 	    (void *)groupp, THR_DETACHED, &groupp->listen_tid)) != 0) {
4911ae08745Sheppo 		(void) (void) snprintf(err_msg, sizeof (err_msg),
4921ae08745Sheppo 		    "Can not create listen thread for"
4931ae08745Sheppo 		    "group %s tcp %llx\n", groupp->group_name,
4941ae08745Sheppo 		    groupp->tcp_port);
4951ae08745Sheppo 		vntsd_log(VNTSD_ERR_CREATE_LISTEN_THR, err_msg);
4961ae08745Sheppo 
4971ae08745Sheppo 		/* clean up group queue */
4981ae08745Sheppo 		vntsd_free_que(&groupp->conspq, (clean_func_t)free_cons);
4991ae08745Sheppo 		groupp->listen_tid = (thread_t)-1;
5001ae08745Sheppo 	}
5011ae08745Sheppo 
5021ae08745Sheppo 	(void) mutex_unlock(&groupp->lock);
5031ae08745Sheppo 
5041ae08745Sheppo 	return (rv != 0);
5051ae08745Sheppo }
5061ae08745Sheppo 
5074d39be2bSsg /* find deleted console by console no */
5084d39be2bSsg static boolean_t
deleted_cons_by_consno(vntsd_cons_t * consp,int * cons_no)5094d39be2bSsg deleted_cons_by_consno(vntsd_cons_t *consp, int *cons_no)
5104d39be2bSsg {
5114d39be2bSsg 	vntsd_client_t *clientp;
5124d39be2bSsg 
5134d39be2bSsg 	assert(consp);
5144d39be2bSsg 
5154d39be2bSsg 	if (consp->cons_no != *cons_no)
5164d39be2bSsg 		return (B_FALSE);
5174d39be2bSsg 
5184d39be2bSsg 	/* has console marked as deleted? */
5194d39be2bSsg 	if ((consp->status & VNTSD_CONS_DELETED) == 0)
5204d39be2bSsg 		return (B_TRUE);
5214d39be2bSsg 
5223c96341aSnarayan 	if (consp->clientpq == NULL)
5233c96341aSnarayan 		/* there is no client for this console */
5244d39be2bSsg 		return (B_TRUE);
5254d39be2bSsg 
5263c96341aSnarayan 	/* need to notify clients of console ? */
5273c96341aSnarayan 	clientp = (vntsd_client_t *)consp->clientpq->handle;
5283c96341aSnarayan 
5294d39be2bSsg 	if (clientp->status & VNTSD_CLIENT_CONS_DELETED)
5304d39be2bSsg 		/* clients of console have notified */
5314d39be2bSsg 		return (B_FALSE);
5324d39be2bSsg 
5334d39be2bSsg 	return (B_TRUE);
5344d39be2bSsg }
5354d39be2bSsg 
5364d39be2bSsg /* find group structure from console no */
5374d39be2bSsg static boolean_t
find_cons_group_by_cons_no(vntsd_group_t * groupp,uint_t * cons_no)5384d39be2bSsg find_cons_group_by_cons_no(vntsd_group_t *groupp, uint_t *cons_no)
5394d39be2bSsg {
5404d39be2bSsg 	vntsd_cons_t *consp;
5414d39be2bSsg 
5424d39be2bSsg 	consp = vntsd_que_find(groupp->conspq,
5434d39be2bSsg 	    (compare_func_t)deleted_cons_by_consno, cons_no);
5444d39be2bSsg 	return (consp != NULL);
5454d39be2bSsg 
5464d39be2bSsg }
5474d39be2bSsg 
5481ae08745Sheppo /* delete a console if the console exists in the vntsd */
5491ae08745Sheppo static void
delete_cons_before_add(vntsd_t * vntsdp,uint_t cons_no)5504d39be2bSsg delete_cons_before_add(vntsd_t *vntsdp, uint_t cons_no)
5511ae08745Sheppo {
5521ae08745Sheppo 	vntsd_group_t	    *groupp;
5531ae08745Sheppo 	vntsd_cons_t	    *consp;
5541ae08745Sheppo 
5551ae08745Sheppo 	/* group exists? */
5561ae08745Sheppo 	(void) mutex_lock(&vntsdp->lock);
5574d39be2bSsg 	groupp = vntsd_que_find(vntsdp->grouppq,
5584d39be2bSsg 	    (compare_func_t)find_cons_group_by_cons_no,
5594d39be2bSsg 	    &cons_no);
5601ae08745Sheppo 	(void) mutex_unlock(&vntsdp->lock);
5611ae08745Sheppo 
5621ae08745Sheppo 	if (groupp == NULL) {
5631ae08745Sheppo 		/* no such group */
5641ae08745Sheppo 		return;
5651ae08745Sheppo 	}
5661ae08745Sheppo 
5671ae08745Sheppo 	/* group exists, if console exists? */
5681ae08745Sheppo 	(void) mutex_lock(&groupp->lock);
5691ae08745Sheppo 	consp = vntsd_que_find(groupp->conspq,
5704d39be2bSsg 	    (compare_func_t)deleted_cons_by_consno, &cons_no);
5711ae08745Sheppo 
5721ae08745Sheppo 	if (consp == NULL) {
5731ae08745Sheppo 		/* no such console */
5741ae08745Sheppo 		(void) mutex_unlock(&groupp->lock);
5751ae08745Sheppo 		return;
5761ae08745Sheppo 	}
5771ae08745Sheppo 
5783c96341aSnarayan 	/* console exists - mark console for main thread to delete it */
5791ae08745Sheppo 	(void) mutex_lock(&consp->lock);
5801ae08745Sheppo 
5813c96341aSnarayan 	if (consp->status & VNTSD_CONS_DELETED) {
5823c96341aSnarayan 		/* already marked */
5833c96341aSnarayan 		(void) mutex_unlock(&consp->lock);
5843c96341aSnarayan 		(void) mutex_unlock(&groupp->lock);
5853c96341aSnarayan 		return;
5863c96341aSnarayan 	}
5873c96341aSnarayan 
5881ae08745Sheppo 	consp->status |= VNTSD_CONS_DELETED;
5891ae08745Sheppo 	groupp->status |= VNTSD_GROUP_CLEAN_CONS;
5901ae08745Sheppo 
5911ae08745Sheppo 	(void) mutex_unlock(&consp->lock);
5921ae08745Sheppo 	(void) mutex_unlock(&groupp->lock);
5931ae08745Sheppo 
5941ae08745Sheppo }
5951ae08745Sheppo 
5961ae08745Sheppo /* add a console */
5971ae08745Sheppo static void
do_add_cons(vntsd_t * vntsdp,int cons_no)5981ae08745Sheppo do_add_cons(vntsd_t *vntsdp, int cons_no)
5991ae08745Sheppo {
6001ae08745Sheppo 	vcc_console_t	console;
6011ae08745Sheppo 	vntsd_group_t	*groupp;
6021ae08745Sheppo 	int		rv;
6031ae08745Sheppo 	char		err_msg[VNTSD_LINE_LEN];
6041ae08745Sheppo 
6051ae08745Sheppo 
6061ae08745Sheppo 	(void) snprintf(err_msg, sizeof (err_msg),
6071ae08745Sheppo 	    "do_add_cons():Can not add console=%d", cons_no);
6081ae08745Sheppo 
6091ae08745Sheppo 	/* get console configuration from vcc */
6101ae08745Sheppo 
6111ae08745Sheppo 	if ((rv = vntsd_vcc_ioctl(VCC_CONS_INFO, cons_no, (void *)&console))
6121ae08745Sheppo 	    != VNTSD_SUCCESS) {
6131ae08745Sheppo 		vntsd_log(rv, err_msg);
6141ae08745Sheppo 		return;
6151ae08745Sheppo 	}
6161ae08745Sheppo 
6171ae08745Sheppo 	/* clean up the console if console was deleted and added again */
6184d39be2bSsg 	delete_cons_before_add(vntsdp, console.cons_no);
6191ae08745Sheppo 
6201ae08745Sheppo 	/* initialize console */
6211ae08745Sheppo 
6221ae08745Sheppo 	if ((rv = alloc_cons_with_group(vntsdp, &console, &groupp)) !=
6231ae08745Sheppo 	    VNTSD_SUCCESS) {
6241ae08745Sheppo 		/* no memory to add this new console */
6251ae08745Sheppo 		vntsd_log(rv, err_msg);
6261ae08745Sheppo 		return;
6271ae08745Sheppo 	}
6281ae08745Sheppo 
6291ae08745Sheppo 	if (groupp != NULL) {
6301ae08745Sheppo 		/* new group */
6311ae08745Sheppo 		/* create listen thread for this console */
6321ae08745Sheppo 		if (create_listen_thread(groupp)) {
6331ae08745Sheppo 			vntsd_log(VNTSD_ERR_CREATE_LISTEN_THR, err_msg);
6344d39be2bSsg 			free_group(groupp);
6351ae08745Sheppo 		}
6361ae08745Sheppo 
6371ae08745Sheppo 	}
6381ae08745Sheppo }
6391ae08745Sheppo 
6401ae08745Sheppo /* daemon wake up */
6411ae08745Sheppo void
vntsd_daemon_wakeup(vntsd_t * vntsdp)6421ae08745Sheppo vntsd_daemon_wakeup(vntsd_t *vntsdp)
6431ae08745Sheppo {
6441ae08745Sheppo 
6451ae08745Sheppo 	vcc_response_t	inq_data;
6461ae08745Sheppo 
6471ae08745Sheppo 	/* reason to wake up  */
6481ae08745Sheppo 	if (vntsd_vcc_ioctl(VCC_INQUIRY, 0, (void *)&inq_data) !=
6491ae08745Sheppo 	    VNTSD_SUCCESS) {
6501ae08745Sheppo 		vntsd_log(VNTSD_ERR_VCC_IOCTL, "vntsd_daemon_wakeup()");
6511ae08745Sheppo 		return;
6521ae08745Sheppo 	}
6531ae08745Sheppo 
6541ae08745Sheppo 	D1(stderr, "t@%d vntsd_daemon_wakup:msg %d port %x\n", thr_self(),
6551ae08745Sheppo 	    inq_data.reason, inq_data.cons_no);
6561ae08745Sheppo 
6571ae08745Sheppo 	switch (inq_data.reason) {
6581ae08745Sheppo 
6591ae08745Sheppo 	case VCC_CONS_ADDED:
6601ae08745Sheppo 		do_add_cons(vntsdp, inq_data.cons_no);
6611ae08745Sheppo 		break;
6621ae08745Sheppo 
6637636cb21Slm 	case VCC_CONS_MISS_ADDED:
6647636cb21Slm 		/* an added port was deleted before vntsd can process it */
6657636cb21Slm 		return;
6667636cb21Slm 
6671ae08745Sheppo 	default:
6681ae08745Sheppo 		DERR(stderr, "t@%d daemon_wakeup:ioctl_unknown %d\n",
6691ae08745Sheppo 		    thr_self(), inq_data.reason);
6701ae08745Sheppo 		vntsd_log(VNTSD_ERR_UNKNOWN_CMD, "from vcc\n");
6711ae08745Sheppo 		break;
6721ae08745Sheppo 	}
6731ae08745Sheppo }
6741ae08745Sheppo 
6751ae08745Sheppo /* initial console configuration */
6761ae08745Sheppo void
vntsd_get_config(vntsd_t * vntsdp)6771ae08745Sheppo vntsd_get_config(vntsd_t *vntsdp)
6781ae08745Sheppo {
6791ae08745Sheppo 
6801ae08745Sheppo 	int		i;
6811ae08745Sheppo 	int		num_cons;
6821ae08745Sheppo 	vcc_console_t	*consp;
6831ae08745Sheppo 	vntsd_group_t	*groupp;
6841ae08745Sheppo 
6851ae08745Sheppo 	/* num of consoles */
6861ae08745Sheppo 	num_cons = 0;
6871ae08745Sheppo 
6881ae08745Sheppo 	if (vntsd_vcc_ioctl(VCC_NUM_CONSOLE, 0, (void *)&num_cons) !=
6891ae08745Sheppo 	    VNTSD_SUCCESS) {
6901ae08745Sheppo 		vntsd_log(VNTSD_ERR_VCC_IOCTL, "VCC_NUM_CONSOLE failed\n");
6911ae08745Sheppo 		return;
6921ae08745Sheppo 	}
6931ae08745Sheppo 
6941ae08745Sheppo 	D3(stderr, "get_config:num_cons=%d", num_cons);
6951ae08745Sheppo 
6961ae08745Sheppo 	if (num_cons == 0) {
6971ae08745Sheppo 		return;
6981ae08745Sheppo 	}
6991ae08745Sheppo 
7001ae08745Sheppo 	/* allocate memory for all consoles */
7011ae08745Sheppo 	consp = malloc(num_cons*sizeof (vcc_console_t));
7021ae08745Sheppo 
7031ae08745Sheppo 	if (consp == NULL) {
7041ae08745Sheppo 		vntsd_log(VNTSD_ERR_NO_MEM, "for console table.");
7051ae08745Sheppo 		return;
7061ae08745Sheppo 	}
7071ae08745Sheppo 
7081ae08745Sheppo 	/* get console table */
7091ae08745Sheppo 	if (vntsd_vcc_ioctl(VCC_CONS_TBL, 0, (void *)consp) != VNTSD_SUCCESS) {
7101ae08745Sheppo 		vntsd_log(VNTSD_ERR_VCC_IOCTL, " VCC_CONS_TBL "
7111ae08745Sheppo 		    "for console table\n");
7121ae08745Sheppo 		return;
7131ae08745Sheppo 	}
7141ae08745Sheppo 
7151ae08745Sheppo 	/* intialize groups and consoles  */
7161ae08745Sheppo 	for (i = 0; i < num_cons; i++) {
7171ae08745Sheppo 		if (alloc_cons_with_group(vntsdp, &consp[i], &groupp)
7181ae08745Sheppo 		    != VNTSD_SUCCESS) {
7191ae08745Sheppo 			vntsd_log(VNTSD_ERR_ADD_CONS_FAILED, "get_config");
7201ae08745Sheppo 		}
7211ae08745Sheppo 	}
7221ae08745Sheppo 
7231ae08745Sheppo 	/* create listen thread for each group */
7241ae08745Sheppo 	(void) mutex_lock(&vntsdp->lock);
7251ae08745Sheppo 
7261ae08745Sheppo 	for (; ; ) {
7271ae08745Sheppo 		groupp = vntsd_que_walk(vntsdp->grouppq,
7281ae08745Sheppo 		    (el_func_t)create_listen_thread);
7291ae08745Sheppo 		if (groupp == NULL) {
7301ae08745Sheppo 			break;
7311ae08745Sheppo 		}
7321ae08745Sheppo 		vntsd_log(VNTSD_ERR_CREATE_LISTEN_THR, "get config()");
7331ae08745Sheppo 	}
7341ae08745Sheppo 
7351ae08745Sheppo 	(void) mutex_unlock(&vntsdp->lock);
7361ae08745Sheppo }
737