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 /* 22*3c96341aSnarayan * Copyright 2007 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 * Each group has a listen thread. It is created at the time 291ae08745Sheppo * of a group creation and destroyed when a group does not have 301ae08745Sheppo * any console associated with it. 311ae08745Sheppo */ 321ae08745Sheppo 331ae08745Sheppo #include <stdio.h> 341ae08745Sheppo #include <stdlib.h> 351ae08745Sheppo #include <string.h> 361ae08745Sheppo #include <unistd.h> 371ae08745Sheppo #include <sys/types.h> 381ae08745Sheppo #include <sys/socket.h> 391ae08745Sheppo #include <netinet/in.h> 401ae08745Sheppo #include <thread.h> 411ae08745Sheppo #include <assert.h> 421ae08745Sheppo #include <signal.h> 431ae08745Sheppo #include <ctype.h> 441ae08745Sheppo #include <syslog.h> 451ae08745Sheppo #include "vntsd.h" 461ae08745Sheppo 474d39be2bSsg70180 #define MAX_BIND_RETRIES 6 481ae08745Sheppo /* 491ae08745Sheppo * check the state of listen thread. exit if there is an fatal error 504d39be2bSsg70180 * or the group is removed. Main thread will call free_group 514d39be2bSsg70180 * to close group socket and free group structure. 521ae08745Sheppo */ 531ae08745Sheppo static void 541ae08745Sheppo listen_chk_status(vntsd_group_t *groupp, int status) 551ae08745Sheppo { 561ae08745Sheppo char err_msg[VNTSD_LINE_LEN]; 571ae08745Sheppo 581ae08745Sheppo 591ae08745Sheppo D1(stderr, "t@%d listen_chk_status() status=%d group=%s " 601ae08745Sheppo "tcp=%lld group status = %x\n", thr_self(), status, 611ae08745Sheppo groupp->group_name, groupp->tcp_port, groupp->status); 621ae08745Sheppo 631ae08745Sheppo (void) snprintf(err_msg, sizeof (err_msg), 641ae08745Sheppo "Group:%s TCP port %lld status %x", 651ae08745Sheppo groupp->group_name, groupp->tcp_port, groupp->status); 661ae08745Sheppo 671ae08745Sheppo 681ae08745Sheppo switch (status) { 691ae08745Sheppo 701ae08745Sheppo case VNTSD_SUCCESS: 711ae08745Sheppo return; 721ae08745Sheppo 731ae08745Sheppo case VNTSD_STATUS_INTR: 744d39be2bSsg70180 /* signal for deleting group */ 751ae08745Sheppo assert(groupp->status & VNTSD_GROUP_SIG_WAIT); 761ae08745Sheppo 774d39be2bSsg70180 /* let main thread know */ 784d39be2bSsg70180 (void) mutex_lock(&groupp->lock); 791ae08745Sheppo groupp->status &= ~VNTSD_GROUP_SIG_WAIT; 801ae08745Sheppo (void) cond_signal(&groupp->cvp); 811ae08745Sheppo (void) mutex_unlock(&groupp->lock); 824d39be2bSsg70180 831ae08745Sheppo thr_exit(0); 841ae08745Sheppo break; 851ae08745Sheppo 861ae08745Sheppo case VNTSD_STATUS_ACCEPT_ERR: 871ae08745Sheppo return; 881ae08745Sheppo 891ae08745Sheppo case VNTSD_STATUS_NO_CONS: 901ae08745Sheppo default: 914d39be2bSsg70180 /* fatal error or no console in the group, remove the group. */ 921ae08745Sheppo 931ae08745Sheppo (void) mutex_lock(&groupp->lock); 941ae08745Sheppo 954d39be2bSsg70180 if (groupp->status & VNTSD_GROUP_SIG_WAIT) { 964d39be2bSsg70180 /* group is already in deletion */ 974d39be2bSsg70180 (void) mutex_unlock(&groupp->lock); 984d39be2bSsg70180 return; 994d39be2bSsg70180 } 1004d39be2bSsg70180 1014d39be2bSsg70180 /* 1024d39be2bSsg70180 * if there still is console(s) in the group, 1034d39be2bSsg70180 * the console(s) could not be connected any more because of 1044d39be2bSsg70180 * a fatal error. Therefore, mark the console and notify 1054d39be2bSsg70180 * main thread to delete console and group. 1064d39be2bSsg70180 */ 1074d39be2bSsg70180 (void) vntsd_que_walk(groupp->conspq, 1084d39be2bSsg70180 (el_func_t)vntsd_mark_deleted_cons); 1094d39be2bSsg70180 groupp->status |= VNTSD_GROUP_CLEAN_CONS; 1104d39be2bSsg70180 1114d39be2bSsg70180 /* signal main thread to delete the group */ 1124d39be2bSsg70180 (void) thr_kill(groupp->vntsd->tid, SIGUSR1); 1134d39be2bSsg70180 (void) mutex_unlock(&groupp->lock); 1144d39be2bSsg70180 1154d39be2bSsg70180 /* log error */ 1164d39be2bSsg70180 if (status != VNTSD_STATUS_NO_CONS) 1174d39be2bSsg70180 vntsd_log(status, err_msg); 1181ae08745Sheppo break; 1191ae08745Sheppo } 1201ae08745Sheppo } 1211ae08745Sheppo 1221ae08745Sheppo /* allocate and initialize listening socket. */ 1231ae08745Sheppo static int 1241ae08745Sheppo open_socket(int port_no, int *sockfd) 1251ae08745Sheppo { 1261ae08745Sheppo 1271ae08745Sheppo struct sockaddr_in addr; 1281ae08745Sheppo int on; 1294d39be2bSsg70180 int retries = 0; 1301ae08745Sheppo 1311ae08745Sheppo 1321ae08745Sheppo /* allocate a socket */ 1331ae08745Sheppo *sockfd = socket(AF_INET, SOCK_STREAM, 0); 1341ae08745Sheppo if (*sockfd < 0) { 1351ae08745Sheppo if (errno == EINTR) { 1361ae08745Sheppo return (VNTSD_STATUS_INTR); 1371ae08745Sheppo } 1381ae08745Sheppo return (VNTSD_ERR_LISTEN_SOCKET); 1391ae08745Sheppo } 1401ae08745Sheppo 1411ae08745Sheppo #ifdef DEBUG 1421ae08745Sheppo /* set reuse local socket address */ 1431ae08745Sheppo on = 1; 1441ae08745Sheppo if (setsockopt(*sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on))) { 1451ae08745Sheppo return (VNTSD_ERR_LISTEN_OPTS); 1461ae08745Sheppo } 1471ae08745Sheppo #endif 1481ae08745Sheppo 1491ae08745Sheppo addr.sin_family = AF_INET; 1501ae08745Sheppo addr.sin_addr.s_addr = (vntsd_ip_addr()).s_addr; 1511ae08745Sheppo addr.sin_port = htons(port_no); 1521ae08745Sheppo 1531ae08745Sheppo /* bind socket */ 1544d39be2bSsg70180 1554d39be2bSsg70180 for (; ; ) { 1564d39be2bSsg70180 1574d39be2bSsg70180 /* 1584d39be2bSsg70180 * After a socket is closed, the port 1594d39be2bSsg70180 * is transitioned to TIME_WAIT state. 1604d39be2bSsg70180 * It may take a few retries to bind 1614d39be2bSsg70180 * a just released port. 1624d39be2bSsg70180 */ 1634d39be2bSsg70180 if (bind(*sockfd, (struct sockaddr *)&addr, 1644d39be2bSsg70180 sizeof (addr)) < 0) { 1654d39be2bSsg70180 1661ae08745Sheppo if (errno == EINTR) { 1671ae08745Sheppo return (VNTSD_STATUS_INTR); 1681ae08745Sheppo } 1694d39be2bSsg70180 1704d39be2bSsg70180 if (errno == EADDRINUSE && retries < MAX_BIND_RETRIES) { 1714d39be2bSsg70180 /* port may be in TIME_WAIT state, retry */ 1724d39be2bSsg70180 (void) sleep(5); 173*3c96341aSnarayan 174*3c96341aSnarayan /* woke up by signal? */ 175*3c96341aSnarayan if (errno == EINTR) { 176*3c96341aSnarayan return (VNTSD_STATUS_INTR); 177*3c96341aSnarayan } 178*3c96341aSnarayan 1794d39be2bSsg70180 retries++; 1804d39be2bSsg70180 continue; 1814d39be2bSsg70180 } 1824d39be2bSsg70180 1831ae08745Sheppo return (VNTSD_ERR_LISTEN_BIND); 1841ae08745Sheppo 1851ae08745Sheppo } 1861ae08745Sheppo 1874d39be2bSsg70180 break; 1884d39be2bSsg70180 1894d39be2bSsg70180 } 1904d39be2bSsg70180 1911ae08745Sheppo if (listen(*sockfd, VNTSD_MAX_SOCKETS) == -1) { 1921ae08745Sheppo if (errno == EINTR) { 1931ae08745Sheppo return (VNTSD_STATUS_INTR); 1941ae08745Sheppo } 1951ae08745Sheppo return (VNTSD_ERR_LISTEN_BIND); 1961ae08745Sheppo } 1971ae08745Sheppo 1981ae08745Sheppo D1(stderr, "t@%d open_socket() sockfd=%d\n", thr_self(), *sockfd); 1991ae08745Sheppo return (VNTSD_SUCCESS); 2001ae08745Sheppo } 2011ae08745Sheppo 2021ae08745Sheppo /* ceate console selection thread */ 2031ae08745Sheppo static int 2041ae08745Sheppo create_console_thread(vntsd_group_t *groupp, int sockfd) 2051ae08745Sheppo { 2061ae08745Sheppo vntsd_client_t *clientp; 207*3c96341aSnarayan vntsd_thr_arg_t *thr_arg; 2081ae08745Sheppo int rv; 2091ae08745Sheppo 2101ae08745Sheppo 2111ae08745Sheppo assert(groupp); 2121ae08745Sheppo D1(stderr, "t@%d create_console_thread@%lld:client@%d\n", thr_self(), 2131ae08745Sheppo groupp->tcp_port, sockfd); 2141ae08745Sheppo 2151ae08745Sheppo /* allocate a new client */ 2161ae08745Sheppo clientp = (vntsd_client_t *)malloc(sizeof (vntsd_client_t)); 2171ae08745Sheppo if (clientp == NULL) { 2181ae08745Sheppo return (VNTSD_ERR_NO_MEM); 2191ae08745Sheppo } 2201ae08745Sheppo 2211ae08745Sheppo /* initialize the client */ 2221ae08745Sheppo bzero(clientp, sizeof (vntsd_client_t)); 2231ae08745Sheppo 2241ae08745Sheppo clientp->sockfd = sockfd; 2251ae08745Sheppo clientp->cons_tid = (thread_t)-1; 2261ae08745Sheppo 2271ae08745Sheppo (void) mutex_init(&clientp->lock, USYNC_THREAD|LOCK_ERRORCHECK, NULL); 2281ae08745Sheppo 2291ae08745Sheppo /* append client to group */ 2301ae08745Sheppo (void) mutex_lock(&groupp->lock); 2311ae08745Sheppo 232*3c96341aSnarayan /* check if the group is [being] removed */ 233*3c96341aSnarayan if (groupp->status & VNTSD_GROUP_IN_CLEANUP) { 234*3c96341aSnarayan (void) mutex_unlock(&groupp->lock); 235*3c96341aSnarayan vntsd_free_client(clientp); 236*3c96341aSnarayan return (VNTSD_STATUS_NO_CONS); 237*3c96341aSnarayan } 238*3c96341aSnarayan 239*3c96341aSnarayan 2401ae08745Sheppo if ((rv = vntsd_que_append(&groupp->no_cons_clientpq, clientp)) 2411ae08745Sheppo != VNTSD_SUCCESS) { 2421ae08745Sheppo (void) mutex_unlock(&groupp->lock); 2431ae08745Sheppo vntsd_free_client(clientp); 2441ae08745Sheppo return (rv); 2451ae08745Sheppo } 2461ae08745Sheppo 2471ae08745Sheppo (void) mutex_unlock(&groupp->lock); 2481ae08745Sheppo 249*3c96341aSnarayan /* 250*3c96341aSnarayan * allocate thr_arg from heap for console thread so 251*3c96341aSnarayan * that thr_arg is still valid after this function exits. 252*3c96341aSnarayan * console thread will free thr_arg. 253*3c96341aSnarayan */ 254*3c96341aSnarayan 255*3c96341aSnarayan thr_arg = (vntsd_thr_arg_t *)malloc(sizeof (vntsd_thr_arg_t)); 256*3c96341aSnarayan if (thr_arg == NULL) { 257*3c96341aSnarayan vntsd_free_client(clientp); 258*3c96341aSnarayan return (VNTSD_ERR_NO_MEM); 259*3c96341aSnarayan } 260*3c96341aSnarayan thr_arg->handle = groupp; 261*3c96341aSnarayan thr_arg->arg = clientp; 262*3c96341aSnarayan 2631ae08745Sheppo (void) mutex_lock(&clientp->lock); 2641ae08745Sheppo 2651ae08745Sheppo 2661ae08745Sheppo /* create console selection thread */ 2671ae08745Sheppo if (thr_create(NULL, 0, (thr_func_t)vntsd_console_thread, 268*3c96341aSnarayan thr_arg, THR_DETACHED, &clientp->cons_tid)) { 2691ae08745Sheppo 270*3c96341aSnarayan free(thr_arg); 2711ae08745Sheppo (void) mutex_unlock(&clientp->lock); 2721ae08745Sheppo (void) mutex_lock(&groupp->lock); 2731ae08745Sheppo (void) vntsd_que_rm(&groupp->no_cons_clientpq, clientp); 2741ae08745Sheppo (void) mutex_unlock(&groupp->lock); 2751ae08745Sheppo vntsd_free_client(clientp); 2761ae08745Sheppo 2771ae08745Sheppo return (VNTSD_ERR_CREATE_CONS_THR); 2781ae08745Sheppo } 2791ae08745Sheppo 2801ae08745Sheppo (void) mutex_unlock(&clientp->lock); 2811ae08745Sheppo 2821ae08745Sheppo return (VNTSD_SUCCESS); 2831ae08745Sheppo } 2841ae08745Sheppo 2851ae08745Sheppo /* listen thread */ 2861ae08745Sheppo void * 2871ae08745Sheppo vntsd_listen_thread(vntsd_group_t *groupp) 2881ae08745Sheppo { 2891ae08745Sheppo 2901ae08745Sheppo int newsockfd; 2911ae08745Sheppo size_t clilen; 2921ae08745Sheppo struct sockaddr_in cli_addr; 2931ae08745Sheppo int rv; 2941ae08745Sheppo int num_cons; 2951ae08745Sheppo 2961ae08745Sheppo assert(groupp); 2971ae08745Sheppo 2981ae08745Sheppo D1(stderr, "t@%d listen@%lld\n", thr_self(), groupp->tcp_port); 2991ae08745Sheppo 3001ae08745Sheppo 3011ae08745Sheppo /* initialize listen socket */ 3021ae08745Sheppo (void) mutex_lock(&groupp->lock); 3031ae08745Sheppo rv = open_socket(groupp->tcp_port, &groupp->sockfd); 3041ae08745Sheppo (void) mutex_unlock(&groupp->lock); 3051ae08745Sheppo listen_chk_status(groupp, rv); 3061ae08745Sheppo 3071ae08745Sheppo for (; ; ) { 3081ae08745Sheppo 3091ae08745Sheppo clilen = sizeof (cli_addr); 3101ae08745Sheppo 3111ae08745Sheppo /* listen to the socket */ 3121ae08745Sheppo newsockfd = accept(groupp->sockfd, (struct sockaddr *)&cli_addr, 3131ae08745Sheppo &clilen); 3141ae08745Sheppo 3151ae08745Sheppo D1(stderr, "t@%d listen_thread() connected sockfd=%d\n", 3161ae08745Sheppo thr_self(), newsockfd); 3171ae08745Sheppo 3181ae08745Sheppo if (newsockfd <= 0) { 3191ae08745Sheppo 3201ae08745Sheppo if (errno == EINTR) { 3211ae08745Sheppo listen_chk_status(groupp, VNTSD_STATUS_INTR); 3221ae08745Sheppo } else { 3231ae08745Sheppo listen_chk_status(groupp, 3241ae08745Sheppo VNTSD_STATUS_ACCEPT_ERR); 3251ae08745Sheppo } 3261ae08745Sheppo continue; 3271ae08745Sheppo } 3281ae08745Sheppo num_cons = vntsd_chk_group_total_cons(groupp); 3291ae08745Sheppo if (num_cons == 0) { 3301ae08745Sheppo (void) close(newsockfd); 3311ae08745Sheppo listen_chk_status(groupp, VNTSD_STATUS_NO_CONS); 3324d39be2bSsg70180 continue; 3331ae08745Sheppo } 3341ae08745Sheppo 3351ae08745Sheppo /* a connection is established */ 3361ae08745Sheppo rv = vntsd_set_telnet_options(newsockfd); 3371ae08745Sheppo if (rv != VNTSD_SUCCESS) { 3381ae08745Sheppo (void) close(newsockfd); 3391ae08745Sheppo listen_chk_status(groupp, rv); 3401ae08745Sheppo } 3411ae08745Sheppo rv = create_console_thread(groupp, newsockfd); 3421ae08745Sheppo if (rv != VNTSD_SUCCESS) { 3431ae08745Sheppo (void) close(newsockfd); 3441ae08745Sheppo listen_chk_status(groupp, rv); 3451ae08745Sheppo } 3461ae08745Sheppo } 3471ae08745Sheppo 3481ae08745Sheppo /*NOTREACHED*/ 3491ae08745Sheppo return (NULL); 3501ae08745Sheppo } 351