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*28b1e50eSSriharsha Basavapatna * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
231ae08745Sheppo * Use is subject to license terms.
241ae08745Sheppo */
251ae08745Sheppo
261ae08745Sheppo /*
271ae08745Sheppo * Each group has a listen thread. It is created at the time
281ae08745Sheppo * of a group creation and destroyed when a group does not have
291ae08745Sheppo * any console associated with it.
301ae08745Sheppo */
311ae08745Sheppo
321ae08745Sheppo #include <stdio.h>
331ae08745Sheppo #include <stdlib.h>
341ae08745Sheppo #include <string.h>
351ae08745Sheppo #include <unistd.h>
361ae08745Sheppo #include <sys/types.h>
371ae08745Sheppo #include <sys/socket.h>
381ae08745Sheppo #include <netinet/in.h>
391ae08745Sheppo #include <thread.h>
401ae08745Sheppo #include <assert.h>
411ae08745Sheppo #include <signal.h>
421ae08745Sheppo #include <ctype.h>
431ae08745Sheppo #include <syslog.h>
441ae08745Sheppo #include "vntsd.h"
451ae08745Sheppo
464d39be2bSsg #define MAX_BIND_RETRIES 6
47bd8f0338Snarayan
481ae08745Sheppo /*
491ae08745Sheppo * check the state of listen thread. exit if there is an fatal error
504d39be2bSsg * or the group is removed. Main thread will call free_group
514d39be2bSsg * to close group socket and free group structure.
521ae08745Sheppo */
531ae08745Sheppo static void
listen_chk_status(vntsd_group_t * groupp,int status)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
741ae08745Sheppo case VNTSD_STATUS_ACCEPT_ERR:
751ae08745Sheppo return;
761ae08745Sheppo
77bd8f0338Snarayan case VNTSD_STATUS_INTR:
78bd8f0338Snarayan assert(groupp->status & VNTSD_GROUP_SIG_WAIT);
79bd8f0338Snarayan /*FALLTHRU*/
801ae08745Sheppo case VNTSD_STATUS_NO_CONS:
811ae08745Sheppo default:
824d39be2bSsg /* fatal error or no console in the group, remove the group. */
831ae08745Sheppo
841ae08745Sheppo (void) mutex_lock(&groupp->lock);
854d39be2bSsg
864d39be2bSsg if (groupp->status & VNTSD_GROUP_SIG_WAIT) {
87bd8f0338Snarayan /*
88bd8f0338Snarayan * group is already being deleted, notify main
89bd8f0338Snarayan * thread and exit.
90bd8f0338Snarayan */
91bd8f0338Snarayan groupp->status &= ~VNTSD_GROUP_SIG_WAIT;
92bd8f0338Snarayan (void) cond_signal(&groupp->cvp);
934d39be2bSsg (void) mutex_unlock(&groupp->lock);
94bd8f0338Snarayan thr_exit(0);
954d39be2bSsg }
964d39be2bSsg
974d39be2bSsg /*
984d39be2bSsg * if there still is console(s) in the group,
994d39be2bSsg * the console(s) could not be connected any more because of
1004d39be2bSsg * a fatal error. Therefore, mark the console and notify
1014d39be2bSsg * main thread to delete console and group.
1024d39be2bSsg */
1034d39be2bSsg (void) vntsd_que_walk(groupp->conspq,
1044d39be2bSsg (el_func_t)vntsd_mark_deleted_cons);
1054d39be2bSsg groupp->status |= VNTSD_GROUP_CLEAN_CONS;
1064d39be2bSsg
1074d39be2bSsg /* signal main thread to delete the group */
1084d39be2bSsg (void) thr_kill(groupp->vntsd->tid, SIGUSR1);
1091ae08745Sheppo (void) mutex_unlock(&groupp->lock);
1101ae08745Sheppo
1114d39be2bSsg /* log error */
1124d39be2bSsg if (status != VNTSD_STATUS_NO_CONS)
1134d39be2bSsg vntsd_log(status, err_msg);
114bd8f0338Snarayan thr_exit(0);
1151ae08745Sheppo }
1161ae08745Sheppo }
1171ae08745Sheppo
1181ae08745Sheppo /* allocate and initialize listening socket. */
1191ae08745Sheppo static int
open_socket(int port_no,int * sockfd)1201ae08745Sheppo open_socket(int port_no, int *sockfd)
1211ae08745Sheppo {
1221ae08745Sheppo
1231ae08745Sheppo struct sockaddr_in addr;
1241ae08745Sheppo int on;
1254d39be2bSsg int retries = 0;
1261ae08745Sheppo
1271ae08745Sheppo
1281ae08745Sheppo /* allocate a socket */
1291ae08745Sheppo *sockfd = socket(AF_INET, SOCK_STREAM, 0);
1301ae08745Sheppo if (*sockfd < 0) {
1311ae08745Sheppo if (errno == EINTR) {
1321ae08745Sheppo return (VNTSD_STATUS_INTR);
1331ae08745Sheppo }
1341ae08745Sheppo return (VNTSD_ERR_LISTEN_SOCKET);
1351ae08745Sheppo }
1361ae08745Sheppo
1371ae08745Sheppo #ifdef DEBUG
1381ae08745Sheppo /* set reuse local socket address */
1391ae08745Sheppo on = 1;
1401ae08745Sheppo if (setsockopt(*sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on))) {
1411ae08745Sheppo return (VNTSD_ERR_LISTEN_OPTS);
1421ae08745Sheppo }
1431ae08745Sheppo #endif
1441ae08745Sheppo
1451ae08745Sheppo addr.sin_family = AF_INET;
1461ae08745Sheppo addr.sin_addr.s_addr = (vntsd_ip_addr()).s_addr;
1471ae08745Sheppo addr.sin_port = htons(port_no);
1481ae08745Sheppo
1491ae08745Sheppo /* bind socket */
1504d39be2bSsg
1514d39be2bSsg for (; ; ) {
1524d39be2bSsg
1534d39be2bSsg /*
1544d39be2bSsg * After a socket is closed, the port
1554d39be2bSsg * is transitioned to TIME_WAIT state.
1564d39be2bSsg * It may take a few retries to bind
1574d39be2bSsg * a just released port.
1584d39be2bSsg */
1594d39be2bSsg if (bind(*sockfd, (struct sockaddr *)&addr,
160*28b1e50eSSriharsha Basavapatna sizeof (addr)) < 0) {
1614d39be2bSsg
1624d39be2bSsg if (errno == EINTR) {
1634d39be2bSsg return (VNTSD_STATUS_INTR);
1644d39be2bSsg }
1654d39be2bSsg
1664d39be2bSsg if (errno == EADDRINUSE && retries < MAX_BIND_RETRIES) {
1674d39be2bSsg /* port may be in TIME_WAIT state, retry */
1684d39be2bSsg (void) sleep(5);
1693c96341aSnarayan
1703c96341aSnarayan /* woke up by signal? */
1713c96341aSnarayan if (errno == EINTR) {
1723c96341aSnarayan return (VNTSD_STATUS_INTR);
1733c96341aSnarayan }
1743c96341aSnarayan
1754d39be2bSsg retries++;
1764d39be2bSsg continue;
1774d39be2bSsg }
1784d39be2bSsg
1794d39be2bSsg return (VNTSD_ERR_LISTEN_BIND);
1804d39be2bSsg
1811ae08745Sheppo }
1824d39be2bSsg
1834d39be2bSsg break;
1841ae08745Sheppo
1851ae08745Sheppo }
1861ae08745Sheppo
1871ae08745Sheppo if (listen(*sockfd, VNTSD_MAX_SOCKETS) == -1) {
1881ae08745Sheppo if (errno == EINTR) {
1891ae08745Sheppo return (VNTSD_STATUS_INTR);
1901ae08745Sheppo }
1911ae08745Sheppo return (VNTSD_ERR_LISTEN_BIND);
1921ae08745Sheppo }
1931ae08745Sheppo
1941ae08745Sheppo D1(stderr, "t@%d open_socket() sockfd=%d\n", thr_self(), *sockfd);
1951ae08745Sheppo return (VNTSD_SUCCESS);
1961ae08745Sheppo }
1971ae08745Sheppo
1981ae08745Sheppo /* ceate console selection thread */
1991ae08745Sheppo static int
create_console_thread(vntsd_group_t * groupp,int sockfd)2001ae08745Sheppo create_console_thread(vntsd_group_t *groupp, int sockfd)
2011ae08745Sheppo {
2021ae08745Sheppo vntsd_client_t *clientp;
2033c96341aSnarayan vntsd_thr_arg_t *thr_arg;
2041ae08745Sheppo int rv;
2051ae08745Sheppo
2061ae08745Sheppo
2071ae08745Sheppo assert(groupp);
2081ae08745Sheppo D1(stderr, "t@%d create_console_thread@%lld:client@%d\n", thr_self(),
2091ae08745Sheppo groupp->tcp_port, sockfd);
2101ae08745Sheppo
2111ae08745Sheppo /* allocate a new client */
2121ae08745Sheppo clientp = (vntsd_client_t *)malloc(sizeof (vntsd_client_t));
2131ae08745Sheppo if (clientp == NULL) {
2141ae08745Sheppo return (VNTSD_ERR_NO_MEM);
2151ae08745Sheppo }
2161ae08745Sheppo
2171ae08745Sheppo /* initialize the client */
2181ae08745Sheppo bzero(clientp, sizeof (vntsd_client_t));
2191ae08745Sheppo
2201ae08745Sheppo clientp->sockfd = sockfd;
2211ae08745Sheppo clientp->cons_tid = (thread_t)-1;
2221ae08745Sheppo
2231ae08745Sheppo (void) mutex_init(&clientp->lock, USYNC_THREAD|LOCK_ERRORCHECK, NULL);
2241ae08745Sheppo
2251ae08745Sheppo /* append client to group */
2261ae08745Sheppo (void) mutex_lock(&groupp->lock);
2271ae08745Sheppo
2283c96341aSnarayan /* check if the group is [being] removed */
2293c96341aSnarayan if (groupp->status & VNTSD_GROUP_IN_CLEANUP) {
2303c96341aSnarayan (void) mutex_unlock(&groupp->lock);
2313c96341aSnarayan vntsd_free_client(clientp);
2323c96341aSnarayan return (VNTSD_STATUS_NO_CONS);
2333c96341aSnarayan }
2343c96341aSnarayan
2353c96341aSnarayan
2361ae08745Sheppo if ((rv = vntsd_que_append(&groupp->no_cons_clientpq, clientp))
2371ae08745Sheppo != VNTSD_SUCCESS) {
2381ae08745Sheppo (void) mutex_unlock(&groupp->lock);
2391ae08745Sheppo vntsd_free_client(clientp);
2401ae08745Sheppo return (rv);
2411ae08745Sheppo }
2421ae08745Sheppo
2431ae08745Sheppo (void) mutex_unlock(&groupp->lock);
2441ae08745Sheppo
2453c96341aSnarayan /*
2463c96341aSnarayan * allocate thr_arg from heap for console thread so
2473c96341aSnarayan * that thr_arg is still valid after this function exits.
2483c96341aSnarayan * console thread will free thr_arg.
2493c96341aSnarayan */
2503c96341aSnarayan
2513c96341aSnarayan thr_arg = (vntsd_thr_arg_t *)malloc(sizeof (vntsd_thr_arg_t));
2523c96341aSnarayan if (thr_arg == NULL) {
2533c96341aSnarayan vntsd_free_client(clientp);
2543c96341aSnarayan return (VNTSD_ERR_NO_MEM);
2553c96341aSnarayan }
2563c96341aSnarayan thr_arg->handle = groupp;
2573c96341aSnarayan thr_arg->arg = clientp;
2581ae08745Sheppo
2593c96341aSnarayan (void) mutex_lock(&clientp->lock);
2601ae08745Sheppo
2611ae08745Sheppo
2621ae08745Sheppo /* create console selection thread */
2631ae08745Sheppo if (thr_create(NULL, 0, (thr_func_t)vntsd_console_thread,
264*28b1e50eSSriharsha Basavapatna thr_arg, THR_DETACHED, &clientp->cons_tid)) {
2651ae08745Sheppo
2663c96341aSnarayan free(thr_arg);
2671ae08745Sheppo (void) mutex_unlock(&clientp->lock);
2681ae08745Sheppo (void) mutex_lock(&groupp->lock);
2691ae08745Sheppo (void) vntsd_que_rm(&groupp->no_cons_clientpq, clientp);
2701ae08745Sheppo (void) mutex_unlock(&groupp->lock);
2711ae08745Sheppo vntsd_free_client(clientp);
2721ae08745Sheppo
2731ae08745Sheppo return (VNTSD_ERR_CREATE_CONS_THR);
2741ae08745Sheppo }
2751ae08745Sheppo
2761ae08745Sheppo (void) mutex_unlock(&clientp->lock);
2771ae08745Sheppo
2781ae08745Sheppo return (VNTSD_SUCCESS);
2791ae08745Sheppo }
2801ae08745Sheppo
2811ae08745Sheppo /* listen thread */
2821ae08745Sheppo void *
vntsd_listen_thread(vntsd_group_t * groupp)2831ae08745Sheppo vntsd_listen_thread(vntsd_group_t *groupp)
2841ae08745Sheppo {
2851ae08745Sheppo
2861ae08745Sheppo int newsockfd;
2871ae08745Sheppo size_t clilen;
2881ae08745Sheppo struct sockaddr_in cli_addr;
2891ae08745Sheppo int rv;
2901ae08745Sheppo int num_cons;
291*28b1e50eSSriharsha Basavapatna vntsd_t *vntsdp;
2921ae08745Sheppo
2931ae08745Sheppo assert(groupp);
2941ae08745Sheppo
2951ae08745Sheppo D1(stderr, "t@%d listen@%lld\n", thr_self(), groupp->tcp_port);
2961ae08745Sheppo
2971ae08745Sheppo
298*28b1e50eSSriharsha Basavapatna vntsdp = groupp->vntsd;
299*28b1e50eSSriharsha Basavapatna
3001ae08745Sheppo /* initialize listen socket */
3011ae08745Sheppo (void) mutex_lock(&groupp->lock);
3021ae08745Sheppo rv = open_socket(groupp->tcp_port, &groupp->sockfd);
3031ae08745Sheppo (void) mutex_unlock(&groupp->lock);
3041ae08745Sheppo listen_chk_status(groupp, rv);
3051ae08745Sheppo
3061ae08745Sheppo for (; ; ) {
3071ae08745Sheppo
3081ae08745Sheppo clilen = sizeof (cli_addr);
3091ae08745Sheppo
3101ae08745Sheppo /* listen to the socket */
3111ae08745Sheppo newsockfd = accept(groupp->sockfd, (struct sockaddr *)&cli_addr,
312*28b1e50eSSriharsha Basavapatna &clilen);
3131ae08745Sheppo
3141ae08745Sheppo D1(stderr, "t@%d listen_thread() connected sockfd=%d\n",
3151ae08745Sheppo thr_self(), newsockfd);
3161ae08745Sheppo
3171ae08745Sheppo if (newsockfd <= 0) {
3181ae08745Sheppo
3191ae08745Sheppo if (errno == EINTR) {
3201ae08745Sheppo listen_chk_status(groupp, VNTSD_STATUS_INTR);
3211ae08745Sheppo } else {
3221ae08745Sheppo listen_chk_status(groupp,
3231ae08745Sheppo VNTSD_STATUS_ACCEPT_ERR);
3241ae08745Sheppo }
3251ae08745Sheppo continue;
3261ae08745Sheppo }
327*28b1e50eSSriharsha Basavapatna
328*28b1e50eSSriharsha Basavapatna /* Check authorization if enabled */
329*28b1e50eSSriharsha Basavapatna if ((vntsdp->options & VNTSD_OPT_AUTH_CHECK) != 0) {
330*28b1e50eSSriharsha Basavapatna rv = auth_check_fd(newsockfd, groupp->group_name);
331*28b1e50eSSriharsha Basavapatna if (rv != B_TRUE) {
332*28b1e50eSSriharsha Basavapatna D3(stderr, "t@%d listen@%lld group@%s: "
333*28b1e50eSSriharsha Basavapatna "authorization failure\n", thr_self(),
334*28b1e50eSSriharsha Basavapatna groupp->tcp_port, groupp->group_name);
335*28b1e50eSSriharsha Basavapatna (void) close(newsockfd);
336*28b1e50eSSriharsha Basavapatna continue;
337*28b1e50eSSriharsha Basavapatna }
338*28b1e50eSSriharsha Basavapatna }
339*28b1e50eSSriharsha Basavapatna
3401ae08745Sheppo num_cons = vntsd_chk_group_total_cons(groupp);
3411ae08745Sheppo if (num_cons == 0) {
3421ae08745Sheppo (void) close(newsockfd);
3431ae08745Sheppo listen_chk_status(groupp, VNTSD_STATUS_NO_CONS);
3444d39be2bSsg continue;
3451ae08745Sheppo }
3461ae08745Sheppo
3471ae08745Sheppo /* a connection is established */
3481ae08745Sheppo rv = vntsd_set_telnet_options(newsockfd);
3491ae08745Sheppo if (rv != VNTSD_SUCCESS) {
3501ae08745Sheppo (void) close(newsockfd);
3511ae08745Sheppo listen_chk_status(groupp, rv);
3521ae08745Sheppo }
3531ae08745Sheppo rv = create_console_thread(groupp, newsockfd);
3541ae08745Sheppo if (rv != VNTSD_SUCCESS) {
3551ae08745Sheppo (void) close(newsockfd);
3561ae08745Sheppo listen_chk_status(groupp, rv);
3571ae08745Sheppo }
3581ae08745Sheppo }
3591ae08745Sheppo
3601ae08745Sheppo /*NOTREACHED*/
3611ae08745Sheppo return (NULL);
3621ae08745Sheppo }
363