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 #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 474d39be2bSsg #define MAX_BIND_RETRIES 6 48*bd8f0338Snarayan 491ae08745Sheppo /* 501ae08745Sheppo * check the state of listen thread. exit if there is an fatal error 514d39be2bSsg * or the group is removed. Main thread will call free_group 524d39be2bSsg * to close group socket and free group structure. 531ae08745Sheppo */ 541ae08745Sheppo static void 551ae08745Sheppo listen_chk_status(vntsd_group_t *groupp, int status) 561ae08745Sheppo { 571ae08745Sheppo char err_msg[VNTSD_LINE_LEN]; 581ae08745Sheppo 591ae08745Sheppo 601ae08745Sheppo D1(stderr, "t@%d listen_chk_status() status=%d group=%s " 611ae08745Sheppo "tcp=%lld group status = %x\n", thr_self(), status, 621ae08745Sheppo groupp->group_name, groupp->tcp_port, groupp->status); 631ae08745Sheppo 641ae08745Sheppo (void) snprintf(err_msg, sizeof (err_msg), 651ae08745Sheppo "Group:%s TCP port %lld status %x", 661ae08745Sheppo groupp->group_name, groupp->tcp_port, groupp->status); 671ae08745Sheppo 681ae08745Sheppo 691ae08745Sheppo switch (status) { 701ae08745Sheppo 711ae08745Sheppo case VNTSD_SUCCESS: 721ae08745Sheppo return; 731ae08745Sheppo 741ae08745Sheppo 751ae08745Sheppo case VNTSD_STATUS_ACCEPT_ERR: 761ae08745Sheppo return; 771ae08745Sheppo 78*bd8f0338Snarayan case VNTSD_STATUS_INTR: 79*bd8f0338Snarayan assert(groupp->status & VNTSD_GROUP_SIG_WAIT); 80*bd8f0338Snarayan /*FALLTHRU*/ 811ae08745Sheppo case VNTSD_STATUS_NO_CONS: 821ae08745Sheppo default: 834d39be2bSsg /* fatal error or no console in the group, remove the group. */ 841ae08745Sheppo 851ae08745Sheppo (void) mutex_lock(&groupp->lock); 864d39be2bSsg 874d39be2bSsg if (groupp->status & VNTSD_GROUP_SIG_WAIT) { 88*bd8f0338Snarayan /* 89*bd8f0338Snarayan * group is already being deleted, notify main 90*bd8f0338Snarayan * thread and exit. 91*bd8f0338Snarayan */ 92*bd8f0338Snarayan groupp->status &= ~VNTSD_GROUP_SIG_WAIT; 93*bd8f0338Snarayan (void) cond_signal(&groupp->cvp); 944d39be2bSsg (void) mutex_unlock(&groupp->lock); 95*bd8f0338Snarayan thr_exit(0); 964d39be2bSsg } 974d39be2bSsg 984d39be2bSsg /* 994d39be2bSsg * if there still is console(s) in the group, 1004d39be2bSsg * the console(s) could not be connected any more because of 1014d39be2bSsg * a fatal error. Therefore, mark the console and notify 1024d39be2bSsg * main thread to delete console and group. 1034d39be2bSsg */ 1044d39be2bSsg (void) vntsd_que_walk(groupp->conspq, 1054d39be2bSsg (el_func_t)vntsd_mark_deleted_cons); 1064d39be2bSsg groupp->status |= VNTSD_GROUP_CLEAN_CONS; 1074d39be2bSsg 1084d39be2bSsg /* signal main thread to delete the group */ 1094d39be2bSsg (void) thr_kill(groupp->vntsd->tid, SIGUSR1); 1101ae08745Sheppo (void) mutex_unlock(&groupp->lock); 1111ae08745Sheppo 1124d39be2bSsg /* log error */ 1134d39be2bSsg if (status != VNTSD_STATUS_NO_CONS) 1144d39be2bSsg vntsd_log(status, err_msg); 115*bd8f0338Snarayan thr_exit(0); 1161ae08745Sheppo } 1171ae08745Sheppo } 1181ae08745Sheppo 1191ae08745Sheppo /* allocate and initialize listening socket. */ 1201ae08745Sheppo static int 1211ae08745Sheppo open_socket(int port_no, int *sockfd) 1221ae08745Sheppo { 1231ae08745Sheppo 1241ae08745Sheppo struct sockaddr_in addr; 1251ae08745Sheppo int on; 1264d39be2bSsg int retries = 0; 1271ae08745Sheppo 1281ae08745Sheppo 1291ae08745Sheppo /* allocate a socket */ 1301ae08745Sheppo *sockfd = socket(AF_INET, SOCK_STREAM, 0); 1311ae08745Sheppo if (*sockfd < 0) { 1321ae08745Sheppo if (errno == EINTR) { 1331ae08745Sheppo return (VNTSD_STATUS_INTR); 1341ae08745Sheppo } 1351ae08745Sheppo return (VNTSD_ERR_LISTEN_SOCKET); 1361ae08745Sheppo } 1371ae08745Sheppo 1381ae08745Sheppo #ifdef DEBUG 1391ae08745Sheppo /* set reuse local socket address */ 1401ae08745Sheppo on = 1; 1411ae08745Sheppo if (setsockopt(*sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on))) { 1421ae08745Sheppo return (VNTSD_ERR_LISTEN_OPTS); 1431ae08745Sheppo } 1441ae08745Sheppo #endif 1451ae08745Sheppo 1461ae08745Sheppo addr.sin_family = AF_INET; 1471ae08745Sheppo addr.sin_addr.s_addr = (vntsd_ip_addr()).s_addr; 1481ae08745Sheppo addr.sin_port = htons(port_no); 1491ae08745Sheppo 1501ae08745Sheppo /* bind socket */ 1514d39be2bSsg 1524d39be2bSsg for (; ; ) { 1534d39be2bSsg 1544d39be2bSsg /* 1554d39be2bSsg * After a socket is closed, the port 1564d39be2bSsg * is transitioned to TIME_WAIT state. 1574d39be2bSsg * It may take a few retries to bind 1584d39be2bSsg * a just released port. 1594d39be2bSsg */ 1604d39be2bSsg if (bind(*sockfd, (struct sockaddr *)&addr, 1614d39be2bSsg sizeof (addr)) < 0) { 1624d39be2bSsg 1634d39be2bSsg if (errno == EINTR) { 1644d39be2bSsg return (VNTSD_STATUS_INTR); 1654d39be2bSsg } 1664d39be2bSsg 1674d39be2bSsg if (errno == EADDRINUSE && retries < MAX_BIND_RETRIES) { 1684d39be2bSsg /* port may be in TIME_WAIT state, retry */ 1694d39be2bSsg (void) sleep(5); 1703c96341aSnarayan 1713c96341aSnarayan /* woke up by signal? */ 1723c96341aSnarayan if (errno == EINTR) { 1733c96341aSnarayan return (VNTSD_STATUS_INTR); 1743c96341aSnarayan } 1753c96341aSnarayan 1764d39be2bSsg retries++; 1774d39be2bSsg continue; 1784d39be2bSsg } 1794d39be2bSsg 1804d39be2bSsg return (VNTSD_ERR_LISTEN_BIND); 1814d39be2bSsg 1821ae08745Sheppo } 1834d39be2bSsg 1844d39be2bSsg break; 1851ae08745Sheppo 1861ae08745Sheppo } 1871ae08745Sheppo 1881ae08745Sheppo if (listen(*sockfd, VNTSD_MAX_SOCKETS) == -1) { 1891ae08745Sheppo if (errno == EINTR) { 1901ae08745Sheppo return (VNTSD_STATUS_INTR); 1911ae08745Sheppo } 1921ae08745Sheppo return (VNTSD_ERR_LISTEN_BIND); 1931ae08745Sheppo } 1941ae08745Sheppo 1951ae08745Sheppo D1(stderr, "t@%d open_socket() sockfd=%d\n", thr_self(), *sockfd); 1961ae08745Sheppo return (VNTSD_SUCCESS); 1971ae08745Sheppo } 1981ae08745Sheppo 1991ae08745Sheppo /* ceate console selection thread */ 2001ae08745Sheppo static int 2011ae08745Sheppo create_console_thread(vntsd_group_t *groupp, int sockfd) 2021ae08745Sheppo { 2031ae08745Sheppo vntsd_client_t *clientp; 2043c96341aSnarayan vntsd_thr_arg_t *thr_arg; 2051ae08745Sheppo int rv; 2061ae08745Sheppo 2071ae08745Sheppo 2081ae08745Sheppo assert(groupp); 2091ae08745Sheppo D1(stderr, "t@%d create_console_thread@%lld:client@%d\n", thr_self(), 2101ae08745Sheppo groupp->tcp_port, sockfd); 2111ae08745Sheppo 2121ae08745Sheppo /* allocate a new client */ 2131ae08745Sheppo clientp = (vntsd_client_t *)malloc(sizeof (vntsd_client_t)); 2141ae08745Sheppo if (clientp == NULL) { 2151ae08745Sheppo return (VNTSD_ERR_NO_MEM); 2161ae08745Sheppo } 2171ae08745Sheppo 2181ae08745Sheppo /* initialize the client */ 2191ae08745Sheppo bzero(clientp, sizeof (vntsd_client_t)); 2201ae08745Sheppo 2211ae08745Sheppo clientp->sockfd = sockfd; 2221ae08745Sheppo clientp->cons_tid = (thread_t)-1; 2231ae08745Sheppo 2241ae08745Sheppo (void) mutex_init(&clientp->lock, USYNC_THREAD|LOCK_ERRORCHECK, NULL); 2251ae08745Sheppo 2261ae08745Sheppo /* append client to group */ 2271ae08745Sheppo (void) mutex_lock(&groupp->lock); 2281ae08745Sheppo 2293c96341aSnarayan /* check if the group is [being] removed */ 2303c96341aSnarayan if (groupp->status & VNTSD_GROUP_IN_CLEANUP) { 2313c96341aSnarayan (void) mutex_unlock(&groupp->lock); 2323c96341aSnarayan vntsd_free_client(clientp); 2333c96341aSnarayan return (VNTSD_STATUS_NO_CONS); 2343c96341aSnarayan } 2353c96341aSnarayan 2363c96341aSnarayan 2371ae08745Sheppo if ((rv = vntsd_que_append(&groupp->no_cons_clientpq, clientp)) 2381ae08745Sheppo != VNTSD_SUCCESS) { 2391ae08745Sheppo (void) mutex_unlock(&groupp->lock); 2401ae08745Sheppo vntsd_free_client(clientp); 2411ae08745Sheppo return (rv); 2421ae08745Sheppo } 2431ae08745Sheppo 2441ae08745Sheppo (void) mutex_unlock(&groupp->lock); 2451ae08745Sheppo 2463c96341aSnarayan /* 2473c96341aSnarayan * allocate thr_arg from heap for console thread so 2483c96341aSnarayan * that thr_arg is still valid after this function exits. 2493c96341aSnarayan * console thread will free thr_arg. 2503c96341aSnarayan */ 2513c96341aSnarayan 2523c96341aSnarayan thr_arg = (vntsd_thr_arg_t *)malloc(sizeof (vntsd_thr_arg_t)); 2533c96341aSnarayan if (thr_arg == NULL) { 2543c96341aSnarayan vntsd_free_client(clientp); 2553c96341aSnarayan return (VNTSD_ERR_NO_MEM); 2563c96341aSnarayan } 2573c96341aSnarayan thr_arg->handle = groupp; 2583c96341aSnarayan thr_arg->arg = clientp; 2591ae08745Sheppo 2603c96341aSnarayan (void) mutex_lock(&clientp->lock); 2611ae08745Sheppo 2621ae08745Sheppo 2631ae08745Sheppo /* create console selection thread */ 2641ae08745Sheppo if (thr_create(NULL, 0, (thr_func_t)vntsd_console_thread, 2653c96341aSnarayan thr_arg, THR_DETACHED, &clientp->cons_tid)) { 2661ae08745Sheppo 2673c96341aSnarayan free(thr_arg); 2681ae08745Sheppo (void) mutex_unlock(&clientp->lock); 2691ae08745Sheppo (void) mutex_lock(&groupp->lock); 2701ae08745Sheppo (void) vntsd_que_rm(&groupp->no_cons_clientpq, clientp); 2711ae08745Sheppo (void) mutex_unlock(&groupp->lock); 2721ae08745Sheppo vntsd_free_client(clientp); 2731ae08745Sheppo 2741ae08745Sheppo return (VNTSD_ERR_CREATE_CONS_THR); 2751ae08745Sheppo } 2761ae08745Sheppo 2771ae08745Sheppo (void) mutex_unlock(&clientp->lock); 2781ae08745Sheppo 2791ae08745Sheppo return (VNTSD_SUCCESS); 2801ae08745Sheppo } 2811ae08745Sheppo 2821ae08745Sheppo /* listen thread */ 2831ae08745Sheppo void * 2841ae08745Sheppo vntsd_listen_thread(vntsd_group_t *groupp) 2851ae08745Sheppo { 2861ae08745Sheppo 2871ae08745Sheppo int newsockfd; 2881ae08745Sheppo size_t clilen; 2891ae08745Sheppo struct sockaddr_in cli_addr; 2901ae08745Sheppo int rv; 2911ae08745Sheppo int num_cons; 2921ae08745Sheppo 2931ae08745Sheppo assert(groupp); 2941ae08745Sheppo 2951ae08745Sheppo D1(stderr, "t@%d listen@%lld\n", thr_self(), groupp->tcp_port); 2961ae08745Sheppo 2971ae08745Sheppo 2981ae08745Sheppo /* initialize listen socket */ 2991ae08745Sheppo (void) mutex_lock(&groupp->lock); 3001ae08745Sheppo rv = open_socket(groupp->tcp_port, &groupp->sockfd); 3011ae08745Sheppo (void) mutex_unlock(&groupp->lock); 3021ae08745Sheppo listen_chk_status(groupp, rv); 3031ae08745Sheppo 3041ae08745Sheppo for (; ; ) { 3051ae08745Sheppo 3061ae08745Sheppo clilen = sizeof (cli_addr); 3071ae08745Sheppo 3081ae08745Sheppo /* listen to the socket */ 3091ae08745Sheppo newsockfd = accept(groupp->sockfd, (struct sockaddr *)&cli_addr, 3101ae08745Sheppo &clilen); 3111ae08745Sheppo 3121ae08745Sheppo D1(stderr, "t@%d listen_thread() connected sockfd=%d\n", 3131ae08745Sheppo thr_self(), newsockfd); 3141ae08745Sheppo 3151ae08745Sheppo if (newsockfd <= 0) { 3161ae08745Sheppo 3171ae08745Sheppo if (errno == EINTR) { 3181ae08745Sheppo listen_chk_status(groupp, VNTSD_STATUS_INTR); 3191ae08745Sheppo } else { 3201ae08745Sheppo listen_chk_status(groupp, 3211ae08745Sheppo VNTSD_STATUS_ACCEPT_ERR); 3221ae08745Sheppo } 3231ae08745Sheppo continue; 3241ae08745Sheppo } 3251ae08745Sheppo num_cons = vntsd_chk_group_total_cons(groupp); 3261ae08745Sheppo if (num_cons == 0) { 3271ae08745Sheppo (void) close(newsockfd); 3281ae08745Sheppo listen_chk_status(groupp, VNTSD_STATUS_NO_CONS); 3294d39be2bSsg continue; 3301ae08745Sheppo } 3311ae08745Sheppo 3321ae08745Sheppo /* a connection is established */ 3331ae08745Sheppo rv = vntsd_set_telnet_options(newsockfd); 3341ae08745Sheppo if (rv != VNTSD_SUCCESS) { 3351ae08745Sheppo (void) close(newsockfd); 3361ae08745Sheppo listen_chk_status(groupp, rv); 3371ae08745Sheppo } 3381ae08745Sheppo rv = create_console_thread(groupp, newsockfd); 3391ae08745Sheppo if (rv != VNTSD_SUCCESS) { 3401ae08745Sheppo (void) close(newsockfd); 3411ae08745Sheppo listen_chk_status(groupp, rv); 3421ae08745Sheppo } 3431ae08745Sheppo } 3441ae08745Sheppo 3451ae08745Sheppo /*NOTREACHED*/ 3461ae08745Sheppo return (NULL); 3471ae08745Sheppo } 348