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 * 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 47*4d39be2bSsg #define MAX_BIND_RETRIES 6 481ae08745Sheppo /* 491ae08745Sheppo * check the state of listen thread. exit if there is an fatal error 50*4d39be2bSsg * or the group is removed. Main thread will call free_group 51*4d39be2bSsg * 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: 74*4d39be2bSsg /* signal for deleting group */ 751ae08745Sheppo assert(groupp->status & VNTSD_GROUP_SIG_WAIT); 761ae08745Sheppo 77*4d39be2bSsg /* let main thread know */ 78*4d39be2bSsg (void) mutex_lock(&groupp->lock); 791ae08745Sheppo groupp->status &= ~VNTSD_GROUP_SIG_WAIT; 801ae08745Sheppo (void) cond_signal(&groupp->cvp); 811ae08745Sheppo (void) mutex_unlock(&groupp->lock); 82*4d39be2bSsg 831ae08745Sheppo thr_exit(0); 841ae08745Sheppo break; 851ae08745Sheppo 861ae08745Sheppo case VNTSD_STATUS_ACCEPT_ERR: 871ae08745Sheppo return; 881ae08745Sheppo 891ae08745Sheppo case VNTSD_STATUS_NO_CONS: 901ae08745Sheppo default: 91*4d39be2bSsg /* fatal error or no console in the group, remove the group. */ 921ae08745Sheppo 931ae08745Sheppo (void) mutex_lock(&groupp->lock); 94*4d39be2bSsg 95*4d39be2bSsg if (groupp->status & VNTSD_GROUP_SIG_WAIT) { 96*4d39be2bSsg /* group is already in deletion */ 97*4d39be2bSsg (void) mutex_unlock(&groupp->lock); 98*4d39be2bSsg return; 99*4d39be2bSsg } 100*4d39be2bSsg 101*4d39be2bSsg /* 102*4d39be2bSsg * if there still is console(s) in the group, 103*4d39be2bSsg * the console(s) could not be connected any more because of 104*4d39be2bSsg * a fatal error. Therefore, mark the console and notify 105*4d39be2bSsg * main thread to delete console and group. 106*4d39be2bSsg */ 107*4d39be2bSsg (void) vntsd_que_walk(groupp->conspq, 108*4d39be2bSsg (el_func_t)vntsd_mark_deleted_cons); 109*4d39be2bSsg groupp->status |= VNTSD_GROUP_CLEAN_CONS; 110*4d39be2bSsg 111*4d39be2bSsg /* signal main thread to delete the group */ 112*4d39be2bSsg (void) thr_kill(groupp->vntsd->tid, SIGUSR1); 1131ae08745Sheppo (void) mutex_unlock(&groupp->lock); 1141ae08745Sheppo 115*4d39be2bSsg /* log error */ 116*4d39be2bSsg if (status != VNTSD_STATUS_NO_CONS) 117*4d39be2bSsg 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; 129*4d39be2bSsg 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 */ 154*4d39be2bSsg 155*4d39be2bSsg for (; ; ) { 156*4d39be2bSsg 157*4d39be2bSsg /* 158*4d39be2bSsg * After a socket is closed, the port 159*4d39be2bSsg * is transitioned to TIME_WAIT state. 160*4d39be2bSsg * It may take a few retries to bind 161*4d39be2bSsg * a just released port. 162*4d39be2bSsg */ 163*4d39be2bSsg if (bind(*sockfd, (struct sockaddr *)&addr, 164*4d39be2bSsg sizeof (addr)) < 0) { 165*4d39be2bSsg 166*4d39be2bSsg if (errno == EINTR) { 167*4d39be2bSsg return (VNTSD_STATUS_INTR); 168*4d39be2bSsg } 169*4d39be2bSsg 170*4d39be2bSsg if (errno == EADDRINUSE && retries < MAX_BIND_RETRIES) { 171*4d39be2bSsg /* port may be in TIME_WAIT state, retry */ 172*4d39be2bSsg (void) sleep(5); 173*4d39be2bSsg retries++; 174*4d39be2bSsg continue; 175*4d39be2bSsg } 176*4d39be2bSsg 177*4d39be2bSsg return (VNTSD_ERR_LISTEN_BIND); 178*4d39be2bSsg 1791ae08745Sheppo } 180*4d39be2bSsg 181*4d39be2bSsg break; 1821ae08745Sheppo 1831ae08745Sheppo } 1841ae08745Sheppo 1851ae08745Sheppo if (listen(*sockfd, VNTSD_MAX_SOCKETS) == -1) { 1861ae08745Sheppo if (errno == EINTR) { 1871ae08745Sheppo return (VNTSD_STATUS_INTR); 1881ae08745Sheppo } 1891ae08745Sheppo return (VNTSD_ERR_LISTEN_BIND); 1901ae08745Sheppo } 1911ae08745Sheppo 1921ae08745Sheppo D1(stderr, "t@%d open_socket() sockfd=%d\n", thr_self(), *sockfd); 1931ae08745Sheppo return (VNTSD_SUCCESS); 1941ae08745Sheppo } 1951ae08745Sheppo 1961ae08745Sheppo /* ceate console selection thread */ 1971ae08745Sheppo static int 1981ae08745Sheppo create_console_thread(vntsd_group_t *groupp, int sockfd) 1991ae08745Sheppo { 2001ae08745Sheppo vntsd_client_t *clientp; 2011ae08745Sheppo vntsd_thr_arg_t arg; 2021ae08745Sheppo int rv; 2031ae08745Sheppo 2041ae08745Sheppo 2051ae08745Sheppo assert(groupp); 2061ae08745Sheppo D1(stderr, "t@%d create_console_thread@%lld:client@%d\n", thr_self(), 2071ae08745Sheppo groupp->tcp_port, sockfd); 2081ae08745Sheppo 2091ae08745Sheppo /* allocate a new client */ 2101ae08745Sheppo clientp = (vntsd_client_t *)malloc(sizeof (vntsd_client_t)); 2111ae08745Sheppo if (clientp == NULL) { 2121ae08745Sheppo return (VNTSD_ERR_NO_MEM); 2131ae08745Sheppo } 2141ae08745Sheppo 2151ae08745Sheppo /* initialize the client */ 2161ae08745Sheppo bzero(clientp, sizeof (vntsd_client_t)); 2171ae08745Sheppo 2181ae08745Sheppo clientp->sockfd = sockfd; 2191ae08745Sheppo clientp->cons_tid = (thread_t)-1; 2201ae08745Sheppo 2211ae08745Sheppo (void) mutex_init(&clientp->lock, USYNC_THREAD|LOCK_ERRORCHECK, NULL); 2221ae08745Sheppo 2231ae08745Sheppo /* append client to group */ 2241ae08745Sheppo (void) mutex_lock(&groupp->lock); 2251ae08745Sheppo 2261ae08745Sheppo if ((rv = vntsd_que_append(&groupp->no_cons_clientpq, clientp)) 2271ae08745Sheppo != VNTSD_SUCCESS) { 2281ae08745Sheppo (void) mutex_unlock(&groupp->lock); 2291ae08745Sheppo vntsd_free_client(clientp); 2301ae08745Sheppo return (rv); 2311ae08745Sheppo } 2321ae08745Sheppo 2331ae08745Sheppo (void) mutex_unlock(&groupp->lock); 2341ae08745Sheppo 2351ae08745Sheppo (void) mutex_lock(&clientp->lock); 2361ae08745Sheppo 2371ae08745Sheppo /* parameters for console thread */ 2381ae08745Sheppo bzero(&arg, sizeof (arg)); 2391ae08745Sheppo 2401ae08745Sheppo arg.handle = groupp; 2411ae08745Sheppo arg.arg = clientp; 2421ae08745Sheppo 2431ae08745Sheppo /* create console selection thread */ 2441ae08745Sheppo if (thr_create(NULL, 0, (thr_func_t)vntsd_console_thread, 2451ae08745Sheppo &arg, THR_DETACHED, &clientp->cons_tid)) { 2461ae08745Sheppo 2471ae08745Sheppo (void) mutex_unlock(&clientp->lock); 2481ae08745Sheppo (void) mutex_lock(&groupp->lock); 2491ae08745Sheppo (void) vntsd_que_rm(&groupp->no_cons_clientpq, clientp); 2501ae08745Sheppo (void) mutex_unlock(&groupp->lock); 2511ae08745Sheppo vntsd_free_client(clientp); 2521ae08745Sheppo 2531ae08745Sheppo return (VNTSD_ERR_CREATE_CONS_THR); 2541ae08745Sheppo } 2551ae08745Sheppo 2561ae08745Sheppo (void) mutex_unlock(&clientp->lock); 2571ae08745Sheppo 2581ae08745Sheppo return (VNTSD_SUCCESS); 2591ae08745Sheppo } 2601ae08745Sheppo 2611ae08745Sheppo /* listen thread */ 2621ae08745Sheppo void * 2631ae08745Sheppo vntsd_listen_thread(vntsd_group_t *groupp) 2641ae08745Sheppo { 2651ae08745Sheppo 2661ae08745Sheppo int newsockfd; 2671ae08745Sheppo size_t clilen; 2681ae08745Sheppo struct sockaddr_in cli_addr; 2691ae08745Sheppo int rv; 2701ae08745Sheppo int num_cons; 2711ae08745Sheppo 2721ae08745Sheppo assert(groupp); 2731ae08745Sheppo 2741ae08745Sheppo D1(stderr, "t@%d listen@%lld\n", thr_self(), groupp->tcp_port); 2751ae08745Sheppo 2761ae08745Sheppo 2771ae08745Sheppo /* initialize listen socket */ 2781ae08745Sheppo (void) mutex_lock(&groupp->lock); 2791ae08745Sheppo rv = open_socket(groupp->tcp_port, &groupp->sockfd); 2801ae08745Sheppo (void) mutex_unlock(&groupp->lock); 2811ae08745Sheppo listen_chk_status(groupp, rv); 2821ae08745Sheppo 2831ae08745Sheppo for (; ; ) { 2841ae08745Sheppo 2851ae08745Sheppo clilen = sizeof (cli_addr); 2861ae08745Sheppo 2871ae08745Sheppo /* listen to the socket */ 2881ae08745Sheppo newsockfd = accept(groupp->sockfd, (struct sockaddr *)&cli_addr, 2891ae08745Sheppo &clilen); 2901ae08745Sheppo 2911ae08745Sheppo D1(stderr, "t@%d listen_thread() connected sockfd=%d\n", 2921ae08745Sheppo thr_self(), newsockfd); 2931ae08745Sheppo 2941ae08745Sheppo if (newsockfd <= 0) { 2951ae08745Sheppo 2961ae08745Sheppo if (errno == EINTR) { 2971ae08745Sheppo listen_chk_status(groupp, VNTSD_STATUS_INTR); 2981ae08745Sheppo } else { 2991ae08745Sheppo listen_chk_status(groupp, 3001ae08745Sheppo VNTSD_STATUS_ACCEPT_ERR); 3011ae08745Sheppo } 3021ae08745Sheppo continue; 3031ae08745Sheppo } 3041ae08745Sheppo num_cons = vntsd_chk_group_total_cons(groupp); 3051ae08745Sheppo if (num_cons == 0) { 3061ae08745Sheppo (void) close(newsockfd); 3071ae08745Sheppo listen_chk_status(groupp, VNTSD_STATUS_NO_CONS); 308*4d39be2bSsg continue; 3091ae08745Sheppo } 3101ae08745Sheppo 3111ae08745Sheppo /* a connection is established */ 3121ae08745Sheppo rv = vntsd_set_telnet_options(newsockfd); 3131ae08745Sheppo if (rv != VNTSD_SUCCESS) { 3141ae08745Sheppo (void) close(newsockfd); 3151ae08745Sheppo listen_chk_status(groupp, rv); 3161ae08745Sheppo } 3171ae08745Sheppo rv = create_console_thread(groupp, newsockfd); 3181ae08745Sheppo if (rv != VNTSD_SUCCESS) { 3191ae08745Sheppo (void) close(newsockfd); 3201ae08745Sheppo listen_chk_status(groupp, rv); 3211ae08745Sheppo } 3221ae08745Sheppo } 3231ae08745Sheppo 3241ae08745Sheppo /*NOTREACHED*/ 3251ae08745Sheppo return (NULL); 3261ae08745Sheppo } 327