xref: /illumos-gate/usr/src/cmd/vntsd/vntsdvcc.c (revision 4d39be2b)
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  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
231ae08745Sheppo  * Use is subject to license terms.
241ae08745Sheppo  */
251ae08745Sheppo #pragma ident	"%Z%%M%	%I%	%E% SMI"
261ae08745Sheppo 
271ae08745Sheppo /*
281ae08745Sheppo  * Configuration and setup interface to vcc driver.
291ae08745Sheppo  * At intialization time, vntsd opens vcc ctrl port and read initial
301ae08745Sheppo  * configuratioa. It manages console groups, creates the listen thread,
311ae08745Sheppo  * dynamically adds and removes virtual console within a group.
321ae08745Sheppo  */
331ae08745Sheppo 
341ae08745Sheppo 
351ae08745Sheppo #include <syslog.h>
361ae08745Sheppo #include <stdio.h>
371ae08745Sheppo #include <sys/types.h>
381ae08745Sheppo #include <sys/ipc.h>
391ae08745Sheppo #include <stdlib.h>
401ae08745Sheppo #include <string.h>
411ae08745Sheppo #include <unistd.h>
421ae08745Sheppo #include <sys/socket.h>
431ae08745Sheppo #include <sys/ipc.h>
441ae08745Sheppo #include <sys/shm.h>
451ae08745Sheppo #include <sys/sem.h>
461ae08745Sheppo #include <wait.h>
471ae08745Sheppo #include <time.h>
481ae08745Sheppo #include <synch.h>
491ae08745Sheppo #include <netinet/in.h>
501ae08745Sheppo #include <thread.h>
511ae08745Sheppo #include <signal.h>
521ae08745Sheppo #include "vntsd.h"
531ae08745Sheppo 
541ae08745Sheppo /* signal all clients that console has been deleted */
551ae08745Sheppo boolean_t
561ae08745Sheppo vntsd_notify_client_cons_del(vntsd_client_t *clientp)
571ae08745Sheppo {
581ae08745Sheppo 	(void) mutex_lock(&clientp->lock);
591ae08745Sheppo 	clientp->status |= VNTSD_CLIENT_CONS_DELETED;
601ae08745Sheppo 	(void) thr_kill(clientp->cons_tid, SIGUSR1);
611ae08745Sheppo 	(void) mutex_unlock(&clientp->lock);
621ae08745Sheppo 	return (B_FALSE);
631ae08745Sheppo }
641ae08745Sheppo 
651ae08745Sheppo /* free console  structure */
661ae08745Sheppo static void
671ae08745Sheppo free_cons(vntsd_cons_t *consp)
681ae08745Sheppo {
691ae08745Sheppo 	assert(consp);
701ae08745Sheppo 	(void) mutex_destroy(&consp->lock);
711ae08745Sheppo 	(void) cond_destroy(&consp->cvp);
72*4d39be2bSsg 	if (consp->vcc_fd != -1)
73*4d39be2bSsg 		(void) close(consp->vcc_fd);
741ae08745Sheppo 	free(consp);
751ae08745Sheppo }
761ae08745Sheppo 
77*4d39be2bSsg /* free group structure */
78*4d39be2bSsg static void
79*4d39be2bSsg free_group(vntsd_group_t *groupp)
80*4d39be2bSsg {
81*4d39be2bSsg 	assert(groupp);
82*4d39be2bSsg 	(void) mutex_destroy(&groupp->lock);
83*4d39be2bSsg 	(void) cond_destroy(&groupp->cvp);
84*4d39be2bSsg 	if (groupp->sockfd != -1)
85*4d39be2bSsg 		(void) close(groupp->sockfd);
86*4d39be2bSsg 	free(groupp);
87*4d39be2bSsg }
88*4d39be2bSsg 
891ae08745Sheppo /*
901ae08745Sheppo  *  all clients connected to a console must disconnect before
911ae08745Sheppo  *  removing a console.
921ae08745Sheppo  */
931ae08745Sheppo static void
941ae08745Sheppo cleanup_cons(vntsd_cons_t *consp)
951ae08745Sheppo {
961ae08745Sheppo 	vntsd_group_t	*groupp;
971ae08745Sheppo 	timestruc_t	to;
981ae08745Sheppo 
991ae08745Sheppo 	assert(consp);
1001ae08745Sheppo 	D1(stderr, "t@%d vntsd_disconn_clients@%d\n", thr_self(),
1011ae08745Sheppo 	    consp->cons_no);
1021ae08745Sheppo 
1031ae08745Sheppo 	groupp = consp->group;
1041ae08745Sheppo 	assert(groupp);
1051ae08745Sheppo 
1061ae08745Sheppo 
1071ae08745Sheppo 	(void) mutex_lock(&consp->lock);
1081ae08745Sheppo 
1091ae08745Sheppo 	/* wait for all clients disconnect from the console */
1101ae08745Sheppo 	while (consp->clientpq != NULL) {
1111ae08745Sheppo 		consp->status |= VNTSD_CONS_SIG_WAIT;
1121ae08745Sheppo 
1131ae08745Sheppo 		/* signal client to disconnect the console */
1141ae08745Sheppo 		(void) vntsd_que_walk(consp->clientpq,
1151ae08745Sheppo 		    (el_func_t)vntsd_notify_client_cons_del);
1161ae08745Sheppo 
1171ae08745Sheppo 		(void) thr_kill(consp->wr_tid, SIGUSR1);
1181ae08745Sheppo 		to.tv_sec = VNTSD_CV_WAIT_DELTIME;
1191ae08745Sheppo 		to.tv_nsec = 0;
1201ae08745Sheppo 
1211ae08745Sheppo 		/* wait for clients to disconnect  */
1221ae08745Sheppo 		(void) cond_reltimedwait(&consp->cvp, &consp->lock, &to);
1231ae08745Sheppo 	}
1241ae08745Sheppo 
1253af08d82Slm 	/* reduce console count in the group */
1263af08d82Slm 	(void) mutex_lock(&groupp->lock);
1273af08d82Slm 	assert(groupp->num_cons > 0);
1283af08d82Slm 	groupp->num_cons--;
1293af08d82Slm 	(void) mutex_unlock(&groupp->lock);
1303af08d82Slm 
1311ae08745Sheppo 	(void) mutex_unlock(&consp->lock);
1321ae08745Sheppo 
1331ae08745Sheppo 	free_cons(consp);
1341ae08745Sheppo }
1351ae08745Sheppo 
1361ae08745Sheppo /* search for a group whose console is being deleted */
1371ae08745Sheppo static boolean_t
1381ae08745Sheppo find_clean_cons_group(vntsd_group_t *groupp)
1391ae08745Sheppo {
1401ae08745Sheppo 	if (groupp->status & VNTSD_GROUP_CLEAN_CONS) {
1411ae08745Sheppo 		return (B_TRUE);
1421ae08745Sheppo 	} else {
1431ae08745Sheppo 		return (B_FALSE);
1441ae08745Sheppo 	}
1451ae08745Sheppo }
1461ae08745Sheppo 
1471ae08745Sheppo /* search for a console that is being deleted */
1481ae08745Sheppo static boolean_t
1491ae08745Sheppo find_clean_cons(vntsd_cons_t *consp)
1501ae08745Sheppo {
1511ae08745Sheppo 	if (consp->status & VNTSD_CONS_DELETED) {
1521ae08745Sheppo 		return (B_TRUE);
1531ae08745Sheppo 	} else {
1541ae08745Sheppo 		return (B_FALSE);
1551ae08745Sheppo 	}
1561ae08745Sheppo }
1571ae08745Sheppo 
1581ae08745Sheppo /* delete a console */
1591ae08745Sheppo void
1601ae08745Sheppo vntsd_delete_cons(vntsd_t *vntsdp)
1611ae08745Sheppo {
1621ae08745Sheppo 	vntsd_group_t *groupp;
1631ae08745Sheppo 	vntsd_cons_t *consp;
1641ae08745Sheppo 
1651ae08745Sheppo 	for (; ; ) {
1661ae08745Sheppo 		/* get the group contains deleted console */
1671ae08745Sheppo 		(void) mutex_lock(&vntsdp->lock);
1681ae08745Sheppo 		groupp = vntsd_que_walk(vntsdp->grouppq,
1691ae08745Sheppo 		    (el_func_t)find_clean_cons_group);
1701ae08745Sheppo 		if (groupp == NULL) {
1711ae08745Sheppo 			/* no more group has console deleted */
1721ae08745Sheppo 			(void) mutex_unlock(&vntsdp->lock);
1731ae08745Sheppo 			return;
1741ae08745Sheppo 		}
1757636cb21Slm 		(void) mutex_lock(&groupp->lock);
1761ae08745Sheppo 		groupp->status &= ~VNTSD_GROUP_CLEAN_CONS;
1777636cb21Slm 		(void) mutex_unlock(&groupp->lock);
1781ae08745Sheppo 		(void) mutex_unlock(&vntsdp->lock);
1791ae08745Sheppo 
1801ae08745Sheppo 		for (; ; ) {
1811ae08745Sheppo 			/* get the console to be deleted */
1821ae08745Sheppo 			(void) mutex_lock(&groupp->lock);
1831ae08745Sheppo 
184*4d39be2bSsg 			/* clean up any deleted console in the group */
185*4d39be2bSsg 			if (groupp->conspq != NULL) {
186*4d39be2bSsg 				consp = vntsd_que_walk(groupp->conspq,
187*4d39be2bSsg 				    (el_func_t)find_clean_cons);
188*4d39be2bSsg 				if (consp == NULL) {
189*4d39be2bSsg 					/* no more cons to delete */
190*4d39be2bSsg 					(void) mutex_unlock(&groupp->lock);
191*4d39be2bSsg 					break;
192*4d39be2bSsg 				}
193*4d39be2bSsg 
194*4d39be2bSsg 				/* remove console from the group */
195*4d39be2bSsg 				(void) vntsd_que_rm(&groupp->conspq, consp);
196*4d39be2bSsg 				(void) mutex_unlock(&groupp->lock);
1971ae08745Sheppo 
198*4d39be2bSsg 				/* clean up the console */
199*4d39be2bSsg 				cleanup_cons(consp);
200*4d39be2bSsg 			}
2011ae08745Sheppo 
2021ae08745Sheppo 			/* delete group? */
203*4d39be2bSsg 			if (groupp->conspq == NULL) {
204*4d39be2bSsg 				/* no more console in the group delete group */
2051ae08745Sheppo 				assert(groupp->vntsd);
2061ae08745Sheppo 
2071ae08745Sheppo 				(void) mutex_lock(&groupp->vntsd->lock);
2081ae08745Sheppo 				(void) vntsd_que_rm(&groupp->vntsd->grouppq,
2091ae08745Sheppo 						    groupp);
2101ae08745Sheppo 				(void) mutex_unlock(&groupp->vntsd->lock);
2111ae08745Sheppo 
2121ae08745Sheppo 				/* clean up the group */
2131ae08745Sheppo 				vntsd_clean_group(groupp);
2141ae08745Sheppo 				break;
2151ae08745Sheppo 			}
2161ae08745Sheppo 		}
2171ae08745Sheppo 	}
2181ae08745Sheppo }
2191ae08745Sheppo 
2201ae08745Sheppo /* clean up a group */
2211ae08745Sheppo void
2221ae08745Sheppo vntsd_clean_group(vntsd_group_t *groupp)
2231ae08745Sheppo {
2241ae08745Sheppo 
2251ae08745Sheppo 	timestruc_t	to;
2261ae08745Sheppo 
2271ae08745Sheppo 	D1(stderr, "t@%d clean_group() group=%s tcp=%lld\n", thr_self(),
2281ae08745Sheppo 	    groupp->group_name, groupp->tcp_port);
2291ae08745Sheppo 
2301ae08745Sheppo 	(void) mutex_lock(&groupp->lock);
2311ae08745Sheppo 
2321ae08745Sheppo 	/* prevent from reentry */
233*4d39be2bSsg 	if (groupp->status & VNTSD_GROUP_IN_CLEANUP) {
2341ae08745Sheppo 		(void) mutex_unlock(&groupp->lock);
2351ae08745Sheppo 		return;
2361ae08745Sheppo 	}
237*4d39be2bSsg 	groupp->status |= VNTSD_GROUP_IN_CLEANUP;
238*4d39be2bSsg 
239*4d39be2bSsg 	/* mark group waiting for listen thread to exits */
240*4d39be2bSsg 	groupp->status |= VNTSD_GROUP_SIG_WAIT;
2411ae08745Sheppo 	(void) mutex_unlock(&groupp->lock);
2421ae08745Sheppo 
2433af08d82Slm 	vntsd_free_que(&groupp->conspq, (clean_func_t)cleanup_cons);
2443af08d82Slm 
245*4d39be2bSsg 	(void) mutex_lock(&groupp->lock);
2461ae08745Sheppo 	/* walk through no cons client queue */
2471ae08745Sheppo 	while (groupp->no_cons_clientpq != NULL) {
2481ae08745Sheppo 		(void) vntsd_que_walk(groupp->no_cons_clientpq,
2491ae08745Sheppo 		    (el_func_t)vntsd_notify_client_cons_del);
2501ae08745Sheppo 		to.tv_sec = VNTSD_CV_WAIT_DELTIME;
2511ae08745Sheppo 		to.tv_nsec = 0;
2521ae08745Sheppo 		(void) cond_reltimedwait(&groupp->cvp, &groupp->lock, &to);
2531ae08745Sheppo 	}
2541ae08745Sheppo 
255*4d39be2bSsg 	/* waiting for listen thread to exit */
2561ae08745Sheppo 	while (groupp->status & VNTSD_GROUP_SIG_WAIT) {
257*4d39be2bSsg 		/* signal listen thread to exit  */
2581ae08745Sheppo 		(void) thr_kill(groupp->listen_tid, SIGUSR1);
2591ae08745Sheppo 		to.tv_sec = VNTSD_CV_WAIT_DELTIME;
2601ae08745Sheppo 		to.tv_nsec = 0;
2611ae08745Sheppo 		/* wait listen thread to exit  */
2621ae08745Sheppo 		(void) cond_reltimedwait(&groupp->cvp, &groupp->lock, &to);
2631ae08745Sheppo 	}
2641ae08745Sheppo 
2651ae08745Sheppo 	(void) mutex_unlock(&groupp->lock);
2661ae08745Sheppo 	(void) thr_join(groupp->listen_tid, NULL, NULL);
2671ae08745Sheppo 	/* free group */
268*4d39be2bSsg 	free_group(groupp);
2691ae08745Sheppo }
2701ae08745Sheppo 
2711ae08745Sheppo /* allocate and initialize console structure */
2721ae08745Sheppo static vntsd_cons_t *
2731ae08745Sheppo alloc_cons(vntsd_group_t *groupp, vcc_console_t *consolep)
2741ae08745Sheppo {
2751ae08745Sheppo 	vntsd_cons_t *consp;
2761ae08745Sheppo 	int	rv;
2771ae08745Sheppo 
2781ae08745Sheppo 	/* allocate console */
2791ae08745Sheppo 	consp = (vntsd_cons_t *)malloc(sizeof (vntsd_cons_t));
2801ae08745Sheppo 	if (consp == NULL) {
2811ae08745Sheppo 		vntsd_log(VNTSD_ERR_NO_MEM, "alloc_cons");
2821ae08745Sheppo 		return (NULL);
2831ae08745Sheppo 	}
2841ae08745Sheppo 
2851ae08745Sheppo 	/* intialize console */
2861ae08745Sheppo 	bzero(consp, sizeof (vntsd_cons_t));
2871ae08745Sheppo 
2881ae08745Sheppo 	(void) mutex_init(&consp->lock, USYNC_THREAD|LOCK_ERRORCHECK, NULL);
2891ae08745Sheppo 	(void) cond_init(&consp->cvp, USYNC_THREAD, NULL);
2901ae08745Sheppo 
2911ae08745Sheppo 	consp->cons_no = consolep->cons_no;
2921ae08745Sheppo 	(void) strlcpy(consp->domain_name, consolep->domain_name, MAXPATHLEN);
2931ae08745Sheppo 	(void) strlcpy(consp->dev_name, consolep->dev_name, MAXPATHLEN);
2941ae08745Sheppo 	consp->wr_tid = (thread_t)-1;
295*4d39be2bSsg 	consp->vcc_fd = -1;
2961ae08745Sheppo 
2971ae08745Sheppo 	/* join the group */
2981ae08745Sheppo 	(void) mutex_lock(&groupp->lock);
2991ae08745Sheppo 
3001ae08745Sheppo 	if ((rv = vntsd_que_append(&groupp->conspq, consp)) !=
3011ae08745Sheppo 	    VNTSD_SUCCESS) {
3021ae08745Sheppo 		(void) mutex_unlock(&groupp->lock);
3031ae08745Sheppo 		vntsd_log(rv, "alloc_cons");
3041ae08745Sheppo 		free_cons(consp);
3051ae08745Sheppo 		return (NULL);
3061ae08745Sheppo 	}
3071ae08745Sheppo 	groupp->num_cons++;
3081ae08745Sheppo 	consp->group = groupp;
3091ae08745Sheppo 
3101ae08745Sheppo 	(void) mutex_unlock(&groupp->lock);
3111ae08745Sheppo 
3121ae08745Sheppo 	D1(stderr, "t@%d alloc_cons@%d %s %s\n", thr_self(),
3131ae08745Sheppo 	    consp->cons_no, consp->domain_name, consp->dev_name);
3141ae08745Sheppo 
3151ae08745Sheppo 	return (consp);
3161ae08745Sheppo }
3171ae08745Sheppo 
3181ae08745Sheppo /* compare tcp with group->tcp */
3191ae08745Sheppo static boolean_t
3201ae08745Sheppo grp_by_tcp(vntsd_group_t *groupp, uint64_t *tcp_port)
3211ae08745Sheppo {
3221ae08745Sheppo 	assert(groupp);
3231ae08745Sheppo 	assert(tcp_port);
3241ae08745Sheppo 	return (groupp->tcp_port == *tcp_port);
3251ae08745Sheppo }
3261ae08745Sheppo 
3271ae08745Sheppo /* allocate and initialize group */
3281ae08745Sheppo static vntsd_group_t *
3291ae08745Sheppo alloc_group(vntsd_t *vntsdp, char *group_name, uint64_t tcp_port)
3301ae08745Sheppo {
3311ae08745Sheppo 	vntsd_group_t *groupp;
3321ae08745Sheppo 
3331ae08745Sheppo 	/* allocate group */
3341ae08745Sheppo 	groupp = (vntsd_group_t *)malloc(sizeof (vntsd_group_t));
3351ae08745Sheppo 	if (groupp == NULL) {
3361ae08745Sheppo 		vntsd_log(VNTSD_ERR_NO_MEM, "alloc_group");
3371ae08745Sheppo 		return (NULL);
3381ae08745Sheppo 	}
3391ae08745Sheppo 
3401ae08745Sheppo 	/* initialize group */
3411ae08745Sheppo 	bzero(groupp, sizeof (vntsd_group_t));
3421ae08745Sheppo 
3431ae08745Sheppo 	(void) mutex_init(&groupp->lock, USYNC_THREAD|LOCK_ERRORCHECK, NULL);
3441ae08745Sheppo 	(void) cond_init(&groupp->cvp, USYNC_THREAD, NULL);
3451ae08745Sheppo 
3461ae08745Sheppo 	if (group_name != NULL) {
3471ae08745Sheppo 		(void) memcpy(groupp->group_name, group_name, MAXPATHLEN);
3481ae08745Sheppo 	}
3491ae08745Sheppo 
3501ae08745Sheppo 	groupp->tcp_port = tcp_port;
3511ae08745Sheppo 	groupp->listen_tid = (thread_t)-1;
352*4d39be2bSsg 	groupp->sockfd = -1;
3531ae08745Sheppo 	groupp->vntsd = vntsdp;
3541ae08745Sheppo 
3551ae08745Sheppo 	D1(stderr, "t@%d alloc_group@%lld:%s\n", thr_self(), groupp->tcp_port,
3561ae08745Sheppo 	    groupp->group_name);
3571ae08745Sheppo 
3581ae08745Sheppo 	return (groupp);
3591ae08745Sheppo }
3601ae08745Sheppo 
361*4d39be2bSsg /* mark a deleted console */
362*4d39be2bSsg boolean_t
363*4d39be2bSsg vntsd_mark_deleted_cons(vntsd_cons_t *consp)
364*4d39be2bSsg {
365*4d39be2bSsg 	(void) mutex_lock(&consp->lock);
366*4d39be2bSsg 	consp->status |= VNTSD_CONS_DELETED;
367*4d39be2bSsg 	(void) mutex_unlock(&consp->lock);
368*4d39be2bSsg 	return (B_FALSE);
369*4d39be2bSsg }
370*4d39be2bSsg 
3711ae08745Sheppo /*
3721ae08745Sheppo  * Initialize a console, if console is associated with with a
3731ae08745Sheppo  * new group, intialize the group.
3741ae08745Sheppo  */
3751ae08745Sheppo static int
3761ae08745Sheppo alloc_cons_with_group(vntsd_t *vntsdp, vcc_console_t *consp,
3771ae08745Sheppo     vntsd_group_t **new_groupp)
3781ae08745Sheppo {
3791ae08745Sheppo 	vntsd_group_t	*groupp = NULL;
3801ae08745Sheppo 	int		rv;
3811ae08745Sheppo 
3821ae08745Sheppo 	*new_groupp = NULL;
3831ae08745Sheppo 
3841ae08745Sheppo 	/* match group by tcp port */
3851ae08745Sheppo 
3861ae08745Sheppo 
3871ae08745Sheppo 	(void) mutex_lock(&vntsdp->lock);
3881ae08745Sheppo 	groupp = vntsd_que_find(vntsdp->grouppq,
3891ae08745Sheppo 	    (compare_func_t)grp_by_tcp, (void *)&(consp->tcp_port));
390*4d39be2bSsg 	if (groupp != NULL)
391*4d39be2bSsg 		(void) mutex_lock(&groupp->lock);
392*4d39be2bSsg 
3931ae08745Sheppo 	(void) mutex_unlock(&vntsdp->lock);
3941ae08745Sheppo 
3951ae08745Sheppo 	if (groupp != NULL) {
396*4d39be2bSsg 		/*
397*4d39be2bSsg 		 *  group with same tcp port found.
398*4d39be2bSsg 		 *  if there is no console in the group, the
399*4d39be2bSsg 		 *  group should be removed and the tcp port can
400*4d39be2bSsg 		 *  be used for tne new group.
401*4d39be2bSsg 		 *  This is possible, when there is tight loop of
402*4d39be2bSsg 		 *  creating/deleting domains. When a vcc port is
403*4d39be2bSsg 		 *  removed, a read thread will have an I/O error because
404*4d39be2bSsg 		 *  vcc has closed the port. The read thread then marks
405*4d39be2bSsg 		 *  the console is removed and notify main thread to
406*4d39be2bSsg 		 *  remove the console.
407*4d39be2bSsg 		 *  Meanwhile, the same port and its group (with same
408*4d39be2bSsg 		 *  tcp port and group name) is created. Vcc notify
409*4d39be2bSsg 		 *  vntsd that new console is added.
410*4d39be2bSsg 		 *  Main thread now have two events. If main thread polls
411*4d39be2bSsg 		 *  out vcc notification first, it will find that there is
412*4d39be2bSsg 		 *  a group has no console.
413*4d39be2bSsg 		 */
414*4d39be2bSsg 
415*4d39be2bSsg 		if (vntsd_chk_group_total_cons(groupp) == 0) {
416*4d39be2bSsg 
417*4d39be2bSsg 			/* all consoles in the group have been removed */
418*4d39be2bSsg 			(void) vntsd_que_walk(groupp->conspq,
419*4d39be2bSsg 			    (el_func_t)vntsd_mark_deleted_cons);
420*4d39be2bSsg 			groupp->status |= VNTSD_GROUP_CLEAN_CONS;
421*4d39be2bSsg 			(void) mutex_unlock(&groupp->lock);
422*4d39be2bSsg 			groupp = NULL;
4231ae08745Sheppo 
424*4d39be2bSsg 		} else if (strcmp(groupp->group_name, consp->group_name)) {
4251ae08745Sheppo 			/* conflict group name */
4261ae08745Sheppo 			vntsd_log(VNTSD_ERR_VCC_GRP_NAME,
4271ae08745Sheppo 			    "group name is different from existing group");
428*4d39be2bSsg 			(void) mutex_unlock(&groupp->lock);
4291ae08745Sheppo 			return (VNTSD_ERR_VCC_CTRL_DATA);
430*4d39be2bSsg 
431*4d39be2bSsg 		} else {
432*4d39be2bSsg 			/* group already existed */
433*4d39be2bSsg 			(void) mutex_unlock(&groupp->lock);
4341ae08745Sheppo 		}
4351ae08745Sheppo 
436*4d39be2bSsg 	}
437*4d39be2bSsg 
438*4d39be2bSsg 	if (groupp == NULL) {
4391ae08745Sheppo 		/* new group */
4401ae08745Sheppo 		groupp = alloc_group(vntsdp, consp->group_name,
4411ae08745Sheppo 		    consp->tcp_port);
4421ae08745Sheppo 		if (groupp == NULL) {
4431ae08745Sheppo 			return (VNTSD_ERR_NO_MEM);
4441ae08745Sheppo 		}
4451ae08745Sheppo 
4461ae08745Sheppo 		assert(groupp->conspq == NULL);
4471ae08745Sheppo 		/* queue group to vntsdp */
4481ae08745Sheppo 		(void) mutex_lock(&vntsdp->lock);
4491ae08745Sheppo 		rv = vntsd_que_append(&vntsdp->grouppq, groupp);
4501ae08745Sheppo 		(void) mutex_unlock(&vntsdp->lock);
4511ae08745Sheppo 
4521ae08745Sheppo 		if (rv != VNTSD_SUCCESS) {
4531ae08745Sheppo 			return (rv);
4541ae08745Sheppo 		}
4551ae08745Sheppo 
4561ae08745Sheppo 		*new_groupp = groupp;
4571ae08745Sheppo 	}
4581ae08745Sheppo 
4591ae08745Sheppo 	/* intialize console */
4601ae08745Sheppo 	if (alloc_cons(groupp, consp) == NULL) {
4611ae08745Sheppo 		/* no memory */
4621ae08745Sheppo 		if (new_groupp != NULL) {
4631ae08745Sheppo 			/* clean up new group */
464*4d39be2bSsg 			free_group(groupp);
4651ae08745Sheppo 		}
4661ae08745Sheppo 
4671ae08745Sheppo 		return (VNTSD_ERR_NO_MEM);
4681ae08745Sheppo 	}
4691ae08745Sheppo 
4701ae08745Sheppo 	return (VNTSD_SUCCESS);
4711ae08745Sheppo 
4721ae08745Sheppo }
4731ae08745Sheppo 
4741ae08745Sheppo 
4751ae08745Sheppo /* create listen thread */
4761ae08745Sheppo static boolean_t
4771ae08745Sheppo create_listen_thread(vntsd_group_t *groupp)
4781ae08745Sheppo {
4791ae08745Sheppo 
4801ae08745Sheppo 	char err_msg[VNTSD_LINE_LEN];
4811ae08745Sheppo 	int rv;
4821ae08745Sheppo 
4831ae08745Sheppo 	assert(groupp);
4841ae08745Sheppo 
4851ae08745Sheppo 	(void) mutex_lock(&groupp->lock);
4861ae08745Sheppo 	assert(groupp->num_cons);
4871ae08745Sheppo 
4881ae08745Sheppo 	D1(stderr, "t@%d create_listen:%lld\n", thr_self(), groupp->tcp_port);
4891ae08745Sheppo 
4901ae08745Sheppo 	if ((rv = thr_create(NULL, 0, (thr_func_t)vntsd_listen_thread,
4911ae08745Sheppo 			    (void *)groupp, THR_DETACHED, &groupp->listen_tid))
4921ae08745Sheppo 	    != 0) {
4931ae08745Sheppo 		(void) (void) snprintf(err_msg, sizeof (err_msg),
4941ae08745Sheppo 		    "Can not create listen thread for"
4951ae08745Sheppo 		    "group %s tcp %llx\n", groupp->group_name,
4961ae08745Sheppo 		    groupp->tcp_port);
4971ae08745Sheppo 		vntsd_log(VNTSD_ERR_CREATE_LISTEN_THR, err_msg);
4981ae08745Sheppo 
4991ae08745Sheppo 		/* clean up group queue */
5001ae08745Sheppo 		vntsd_free_que(&groupp->conspq, (clean_func_t)free_cons);
5011ae08745Sheppo 		groupp->listen_tid = (thread_t)-1;
5021ae08745Sheppo 	}
5031ae08745Sheppo 
5041ae08745Sheppo 	(void) mutex_unlock(&groupp->lock);
5051ae08745Sheppo 
5061ae08745Sheppo 	return (rv != 0);
5071ae08745Sheppo }
5081ae08745Sheppo 
509*4d39be2bSsg /* find deleted console by console no */
510*4d39be2bSsg static boolean_t
511*4d39be2bSsg deleted_cons_by_consno(vntsd_cons_t *consp, int *cons_no)
512*4d39be2bSsg {
513*4d39be2bSsg 	vntsd_client_t *clientp;
514*4d39be2bSsg 
515*4d39be2bSsg 	assert(consp);
516*4d39be2bSsg 
517*4d39be2bSsg 	if (consp->cons_no != *cons_no)
518*4d39be2bSsg 		return (B_FALSE);
519*4d39be2bSsg 
520*4d39be2bSsg 	/* has console marked as deleted? */
521*4d39be2bSsg 	if ((consp->status & VNTSD_CONS_DELETED) == 0)
522*4d39be2bSsg 		return (B_TRUE);
523*4d39be2bSsg 
524*4d39be2bSsg 	/* notify clients of console ? */
525*4d39be2bSsg 	clientp = (vntsd_client_t *)consp->clientpq->handle;
526*4d39be2bSsg 
527*4d39be2bSsg 	if (clientp == NULL)
528*4d39be2bSsg 		/* therre is no client for this console */
529*4d39be2bSsg 		return (B_TRUE);
530*4d39be2bSsg 
531*4d39be2bSsg 	if (clientp->status & VNTSD_CLIENT_CONS_DELETED)
532*4d39be2bSsg 		/* clients of console have notified */
533*4d39be2bSsg 		return (B_FALSE);
534*4d39be2bSsg 
535*4d39be2bSsg 	return (B_TRUE);
536*4d39be2bSsg }
537*4d39be2bSsg 
538*4d39be2bSsg /* find group structure from console no */
539*4d39be2bSsg static boolean_t
540*4d39be2bSsg find_cons_group_by_cons_no(vntsd_group_t *groupp, uint_t *cons_no)
541*4d39be2bSsg {
542*4d39be2bSsg 	vntsd_cons_t *consp;
543*4d39be2bSsg 
544*4d39be2bSsg 	consp = vntsd_que_find(groupp->conspq,
545*4d39be2bSsg 	    (compare_func_t)deleted_cons_by_consno, cons_no);
546*4d39be2bSsg 	return (consp != NULL);
547*4d39be2bSsg 
548*4d39be2bSsg }
549*4d39be2bSsg 
5501ae08745Sheppo /* delete a console if the console exists in the vntsd */
5511ae08745Sheppo static void
552*4d39be2bSsg delete_cons_before_add(vntsd_t *vntsdp, uint_t cons_no)
5531ae08745Sheppo {
5541ae08745Sheppo 	vntsd_group_t	    *groupp;
5551ae08745Sheppo 	vntsd_cons_t	    *consp;
5561ae08745Sheppo 
5571ae08745Sheppo 	/* group exists? */
5581ae08745Sheppo 	(void) mutex_lock(&vntsdp->lock);
559*4d39be2bSsg 	groupp = vntsd_que_find(vntsdp->grouppq,
560*4d39be2bSsg 	    (compare_func_t)find_cons_group_by_cons_no,
561*4d39be2bSsg 	    &cons_no);
5621ae08745Sheppo 	(void) mutex_unlock(&vntsdp->lock);
5631ae08745Sheppo 
5641ae08745Sheppo 	if (groupp == NULL) {
5651ae08745Sheppo 		/* no such group */
5661ae08745Sheppo 		return;
5671ae08745Sheppo 	}
5681ae08745Sheppo 
5691ae08745Sheppo 	/* group exists, if console exists? */
5701ae08745Sheppo 	(void) mutex_lock(&groupp->lock);
5711ae08745Sheppo 	consp = vntsd_que_find(groupp->conspq,
572*4d39be2bSsg 	    (compare_func_t)deleted_cons_by_consno, &cons_no);
5731ae08745Sheppo 
5741ae08745Sheppo 	if (consp == NULL) {
5751ae08745Sheppo 		/* no such console */
5761ae08745Sheppo 		(void) mutex_unlock(&groupp->lock);
5771ae08745Sheppo 		return;
5781ae08745Sheppo 	}
5791ae08745Sheppo 	/* console exists - delete console */
5801ae08745Sheppo 
5811ae08745Sheppo 	(void) mutex_lock(&consp->lock);
5821ae08745Sheppo 
5831ae08745Sheppo 	consp->status |= VNTSD_CONS_DELETED;
5841ae08745Sheppo 	groupp->status |= VNTSD_GROUP_CLEAN_CONS;
5851ae08745Sheppo 
5861ae08745Sheppo 	(void) mutex_unlock(&consp->lock);
5871ae08745Sheppo 
5881ae08745Sheppo 	(void) mutex_unlock(&groupp->lock);
5891ae08745Sheppo 
5901ae08745Sheppo 	vntsd_delete_cons(vntsdp);
5911ae08745Sheppo }
5921ae08745Sheppo 
5931ae08745Sheppo /* add a console */
5941ae08745Sheppo static void
5951ae08745Sheppo do_add_cons(vntsd_t *vntsdp, int cons_no)
5961ae08745Sheppo {
5971ae08745Sheppo 	vcc_console_t	console;
5981ae08745Sheppo 	vntsd_group_t	*groupp;
5991ae08745Sheppo 	int		rv;
6001ae08745Sheppo 	char		err_msg[VNTSD_LINE_LEN];
6011ae08745Sheppo 
6021ae08745Sheppo 
6031ae08745Sheppo 	(void) snprintf(err_msg, sizeof (err_msg),
6041ae08745Sheppo 	    "do_add_cons():Can not add console=%d", cons_no);
6051ae08745Sheppo 
6061ae08745Sheppo 	/* get console configuration from vcc */
6071ae08745Sheppo 
6081ae08745Sheppo 	if ((rv = vntsd_vcc_ioctl(VCC_CONS_INFO, cons_no, (void *)&console))
6091ae08745Sheppo 	    != VNTSD_SUCCESS) {
6101ae08745Sheppo 		vntsd_log(rv, err_msg);
6111ae08745Sheppo 		return;
6121ae08745Sheppo 	}
6131ae08745Sheppo 
6141ae08745Sheppo 	/* clean up the console if console was deleted and added again */
615*4d39be2bSsg 	delete_cons_before_add(vntsdp, console.cons_no);
6161ae08745Sheppo 
6171ae08745Sheppo 	/* initialize console */
6181ae08745Sheppo 
6191ae08745Sheppo 	if ((rv = alloc_cons_with_group(vntsdp, &console, &groupp)) !=
6201ae08745Sheppo 	    VNTSD_SUCCESS) {
6211ae08745Sheppo 		/* no memory to add this new console */
6221ae08745Sheppo 		vntsd_log(rv, err_msg);
6231ae08745Sheppo 		return;
6241ae08745Sheppo 	}
6251ae08745Sheppo 
6261ae08745Sheppo 	if (groupp != NULL) {
6271ae08745Sheppo 		/* new group */
6281ae08745Sheppo 		/* create listen thread for this console */
6291ae08745Sheppo 		if (create_listen_thread(groupp)) {
6301ae08745Sheppo 			vntsd_log(VNTSD_ERR_CREATE_LISTEN_THR, err_msg);
631*4d39be2bSsg 			free_group(groupp);
6321ae08745Sheppo 		}
6331ae08745Sheppo 
6341ae08745Sheppo 	}
6351ae08745Sheppo }
6361ae08745Sheppo 
6371ae08745Sheppo /* daemon wake up */
6381ae08745Sheppo void
6391ae08745Sheppo vntsd_daemon_wakeup(vntsd_t *vntsdp)
6401ae08745Sheppo {
6411ae08745Sheppo 
6421ae08745Sheppo 	vcc_response_t	inq_data;
6431ae08745Sheppo 
6441ae08745Sheppo 	/* reason to wake up  */
6451ae08745Sheppo 	if (vntsd_vcc_ioctl(VCC_INQUIRY, 0, (void *)&inq_data) !=
6461ae08745Sheppo 	    VNTSD_SUCCESS) {
6471ae08745Sheppo 		vntsd_log(VNTSD_ERR_VCC_IOCTL, "vntsd_daemon_wakeup()");
6481ae08745Sheppo 		return;
6491ae08745Sheppo 	}
6501ae08745Sheppo 
6511ae08745Sheppo 	D1(stderr, "t@%d vntsd_daemon_wakup:msg %d port %x\n", thr_self(),
6521ae08745Sheppo 	    inq_data.reason, inq_data.cons_no);
6531ae08745Sheppo 
6541ae08745Sheppo 	switch (inq_data.reason) {
6551ae08745Sheppo 
6561ae08745Sheppo 	case VCC_CONS_ADDED:
6571ae08745Sheppo 		do_add_cons(vntsdp, inq_data.cons_no);
6581ae08745Sheppo 		break;
6591ae08745Sheppo 
6607636cb21Slm 	case VCC_CONS_MISS_ADDED:
6617636cb21Slm 		/* an added port was deleted before vntsd can process it */
6627636cb21Slm 		return;
6637636cb21Slm 
6641ae08745Sheppo 	default:
6651ae08745Sheppo 		DERR(stderr, "t@%d daemon_wakeup:ioctl_unknown %d\n",
6661ae08745Sheppo 		    thr_self(), inq_data.reason);
6671ae08745Sheppo 		vntsd_log(VNTSD_ERR_UNKNOWN_CMD, "from vcc\n");
6681ae08745Sheppo 		break;
6691ae08745Sheppo 	}
6701ae08745Sheppo }
6711ae08745Sheppo 
6721ae08745Sheppo /* initial console configuration */
6731ae08745Sheppo void
6741ae08745Sheppo vntsd_get_config(vntsd_t *vntsdp)
6751ae08745Sheppo {
6761ae08745Sheppo 
6771ae08745Sheppo 	int		i;
6781ae08745Sheppo 	int		num_cons;
6791ae08745Sheppo 	vcc_console_t	*consp;
6801ae08745Sheppo 	vntsd_group_t	*groupp;
6811ae08745Sheppo 
6821ae08745Sheppo 	/* num of consoles */
6831ae08745Sheppo 	num_cons = 0;
6841ae08745Sheppo 
6851ae08745Sheppo 	if (vntsd_vcc_ioctl(VCC_NUM_CONSOLE, 0, (void *)&num_cons) !=
6861ae08745Sheppo 	    VNTSD_SUCCESS) {
6871ae08745Sheppo 		vntsd_log(VNTSD_ERR_VCC_IOCTL, "VCC_NUM_CONSOLE failed\n");
6881ae08745Sheppo 		return;
6891ae08745Sheppo 	}
6901ae08745Sheppo 
6911ae08745Sheppo 	D3(stderr, "get_config:num_cons=%d", num_cons);
6921ae08745Sheppo 
6931ae08745Sheppo 	if (num_cons == 0) {
6941ae08745Sheppo 		return;
6951ae08745Sheppo 	}
6961ae08745Sheppo 
6971ae08745Sheppo 	/* allocate memory for all consoles */
6981ae08745Sheppo 	consp = malloc(num_cons*sizeof (vcc_console_t));
6991ae08745Sheppo 
7001ae08745Sheppo 	if (consp == NULL) {
7011ae08745Sheppo 		vntsd_log(VNTSD_ERR_NO_MEM, "for console table.");
7021ae08745Sheppo 		return;
7031ae08745Sheppo 	}
7041ae08745Sheppo 
7051ae08745Sheppo 	/* get console table */
7061ae08745Sheppo 	if (vntsd_vcc_ioctl(VCC_CONS_TBL, 0, (void *)consp) != VNTSD_SUCCESS) {
7071ae08745Sheppo 		vntsd_log(VNTSD_ERR_VCC_IOCTL, " VCC_CONS_TBL "
7081ae08745Sheppo 		    "for console table\n");
7091ae08745Sheppo 		return;
7101ae08745Sheppo 	}
7111ae08745Sheppo 
7121ae08745Sheppo 	/* intialize groups and consoles  */
7131ae08745Sheppo 	for (i = 0; i < num_cons; i++) {
7141ae08745Sheppo 		if (alloc_cons_with_group(vntsdp, &consp[i], &groupp)
7151ae08745Sheppo 		    != VNTSD_SUCCESS) {
7161ae08745Sheppo 			vntsd_log(VNTSD_ERR_ADD_CONS_FAILED, "get_config");
7171ae08745Sheppo 		}
7181ae08745Sheppo 	}
7191ae08745Sheppo 
7201ae08745Sheppo 	/* create listen thread for each group */
7211ae08745Sheppo 	(void) mutex_lock(&vntsdp->lock);
7221ae08745Sheppo 
7231ae08745Sheppo 	for (; ; ) {
7241ae08745Sheppo 		groupp = vntsd_que_walk(vntsdp->grouppq,
7251ae08745Sheppo 		    (el_func_t)create_listen_thread);
7261ae08745Sheppo 		if (groupp == NULL) {
7271ae08745Sheppo 			break;
7281ae08745Sheppo 		}
7291ae08745Sheppo 		vntsd_log(VNTSD_ERR_CREATE_LISTEN_THR, "get config()");
7301ae08745Sheppo 	}
7311ae08745Sheppo 
7321ae08745Sheppo 	(void) mutex_unlock(&vntsdp->lock);
7331ae08745Sheppo }
734