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 * Listen thread creates a console thread whenever there is a tcp client 291ae08745Sheppo * made a conection to its port. In the console thread, if there are 301ae08745Sheppo * multiple consoles in the group, client will be asked for a console selection. 311ae08745Sheppo * a write thread for a console is created when first client connects to a 321ae08745Sheppo * selected console and console thread becomes read thread for the client. 331ae08745Sheppo */ 341ae08745Sheppo 351ae08745Sheppo #include <stdio.h> 361ae08745Sheppo #include <stdlib.h> 371ae08745Sheppo #include <string.h> 381ae08745Sheppo #include <unistd.h> 391ae08745Sheppo #include <sys/types.h> 401ae08745Sheppo #include <sys/socket.h> 411ae08745Sheppo #include <netinet/in.h> 421ae08745Sheppo #include <thread.h> 431ae08745Sheppo #include <synch.h> 441ae08745Sheppo #include <signal.h> 451ae08745Sheppo #include <assert.h> 461ae08745Sheppo #include <ctype.h> 471ae08745Sheppo #include <syslog.h> 481ae08745Sheppo #include <libintl.h> 491ae08745Sheppo #include <netdb.h> 501ae08745Sheppo #include "vntsd.h" 511ae08745Sheppo #include "chars.h" 521ae08745Sheppo 531ae08745Sheppo /* display domain names in the group */ 541ae08745Sheppo static boolean_t 551ae08745Sheppo display_domain_name(vntsd_cons_t *consp, int *fd) 561ae08745Sheppo { 571ae08745Sheppo char buf[VNTSD_LINE_LEN]; 581ae08745Sheppo char *status; 591ae08745Sheppo 601ae08745Sheppo 611ae08745Sheppo if (consp->clientpq != NULL) { 621ae08745Sheppo status = gettext("connected"); 631ae08745Sheppo } else if (consp->status & VNTSD_CONS_DELETED) { 641ae08745Sheppo status = gettext("removing..."); 651ae08745Sheppo } else { 661ae08745Sheppo status = gettext("online"); 671ae08745Sheppo } 681ae08745Sheppo 691ae08745Sheppo (void) snprintf(buf, sizeof (buf), "%-20d%-30s%-25s%s", 701ae08745Sheppo consp->cons_no, consp->domain_name, status, vntsd_eol); 711ae08745Sheppo 721ae08745Sheppo return (vntsd_write_fd(*fd, buf, strlen(buf)) != VNTSD_SUCCESS); 731ae08745Sheppo } 741ae08745Sheppo 751ae08745Sheppo /* output connected message to tcp client */ 761ae08745Sheppo static int 771ae08745Sheppo write_connect_msg(vntsd_client_t *clientp, char *group_name, 781ae08745Sheppo char *domain_name) 791ae08745Sheppo { 801ae08745Sheppo 811ae08745Sheppo int rv = VNTSD_SUCCESS; 821ae08745Sheppo char buf[VNTSD_LINE_LEN]; 831ae08745Sheppo 841ae08745Sheppo if ((rv = vntsd_write_client(clientp, vntsd_eol, VNTSD_EOL_LEN)) != 851ae08745Sheppo VNTSD_SUCCESS) { 861ae08745Sheppo return (rv); 871ae08745Sheppo } 881ae08745Sheppo 891ae08745Sheppo (void) snprintf(buf, sizeof (buf), 901ae08745Sheppo gettext("Connecting to console \"%s\" in group \"%s\" ...."), 911ae08745Sheppo domain_name, group_name); 921ae08745Sheppo 931ae08745Sheppo if ((rv = vntsd_write_line(clientp, buf)) != VNTSD_SUCCESS) { 941ae08745Sheppo return (rv); 951ae08745Sheppo } 961ae08745Sheppo 971ae08745Sheppo if ((rv = vntsd_write_line(clientp, 981ae08745Sheppo gettext("Press ~? for control options .."))) != 991ae08745Sheppo VNTSD_SUCCESS) { 1001ae08745Sheppo return (rv); 1011ae08745Sheppo } 1021ae08745Sheppo 1031ae08745Sheppo return (VNTSD_SUCCESS); 1041ae08745Sheppo } 1051ae08745Sheppo 1061ae08745Sheppo static int 1071ae08745Sheppo create_write_thread(vntsd_cons_t *consp) 1081ae08745Sheppo { 1091ae08745Sheppo 1101ae08745Sheppo assert(consp); 1111ae08745Sheppo 1121ae08745Sheppo /* create write thread for the console */ 1131ae08745Sheppo (void) mutex_lock(&consp->lock); 1141ae08745Sheppo if (thr_create(NULL, 0, (thr_func_t)vntsd_write_thread, 1151ae08745Sheppo (void *)consp, NULL, &consp->wr_tid)) { 1161ae08745Sheppo 1171ae08745Sheppo DERR(stderr, "t@%d create_rd_wr_thread@%d: " 1181ae08745Sheppo "create write thread failed\n", 1191ae08745Sheppo thr_self(), consp->cons_no); 1201ae08745Sheppo (void) close(consp->vcc_fd); 1211ae08745Sheppo consp->vcc_fd = -1; 1221ae08745Sheppo (void) mutex_unlock(&consp->lock); 1231ae08745Sheppo 1241ae08745Sheppo return (VNTSD_ERR_CREATE_WR_THR); 1251ae08745Sheppo } 1261ae08745Sheppo (void) mutex_unlock(&consp->lock); 1271ae08745Sheppo return (VNTSD_SUCCESS); 1281ae08745Sheppo } 1291ae08745Sheppo 1301ae08745Sheppo /* Display all domain consoles in a group. */ 1311ae08745Sheppo static int 1321ae08745Sheppo list_all_domains(vntsd_group_t *groupp, vntsd_client_t *clientp) 1331ae08745Sheppo { 1341ae08745Sheppo char vntsd_line[VNTSD_LINE_LEN]; 1351ae08745Sheppo int rv = VNTSD_SUCCESS; 1361ae08745Sheppo 1371ae08745Sheppo if ((rv = vntsd_write_client(clientp, vntsd_eol, VNTSD_EOL_LEN)) 1381ae08745Sheppo != VNTSD_SUCCESS) { 1391ae08745Sheppo return (rv); 1401ae08745Sheppo } 1411ae08745Sheppo 1421ae08745Sheppo /* 1431ae08745Sheppo * TRANSLATION_NOTE 1441ae08745Sheppo * The following three strings of the form "DOMAIN .." are table 1451ae08745Sheppo * headers and should be all uppercase. 1461ae08745Sheppo */ 1471ae08745Sheppo (void) snprintf(vntsd_line, sizeof (vntsd_line), 1481ae08745Sheppo "%-20s%-30s%-25s", 1491ae08745Sheppo gettext("DOMAIN ID"), gettext("DOMAIN NAME"), 1501ae08745Sheppo gettext("DOMAIN STATE")); 1511ae08745Sheppo 1521ae08745Sheppo if ((rv = vntsd_write_line(clientp, vntsd_line)) != VNTSD_SUCCESS) { 1531ae08745Sheppo return (rv); 1541ae08745Sheppo } 1551ae08745Sheppo 1561ae08745Sheppo (void) mutex_lock(&groupp->lock); 1571ae08745Sheppo 1581ae08745Sheppo if (vntsd_que_find(groupp->conspq, (compare_func_t)display_domain_name, 1591ae08745Sheppo &(clientp->sockfd)) != NULL) { 1601ae08745Sheppo rv = VNTSD_ERR_WRITE_CLIENT; 1611ae08745Sheppo } 1621ae08745Sheppo 1631ae08745Sheppo (void) mutex_unlock(&groupp->lock); 1641ae08745Sheppo 1651ae08745Sheppo return (rv); 1661ae08745Sheppo } 1671ae08745Sheppo 1681ae08745Sheppo /* display help */ 1691ae08745Sheppo static int 1701ae08745Sheppo display_help(vntsd_client_t *clientp) 1711ae08745Sheppo { 1721ae08745Sheppo int rv = VNTSD_SUCCESS; 1731ae08745Sheppo char *bufp; 1741ae08745Sheppo 1751ae08745Sheppo if ((rv = vntsd_write_client(clientp, vntsd_eol, VNTSD_EOL_LEN)) 1761ae08745Sheppo != VNTSD_SUCCESS) { 1771ae08745Sheppo return (rv); 1781ae08745Sheppo } 1791ae08745Sheppo 1801ae08745Sheppo /* 1811ae08745Sheppo * TRANSLATION_NOTE 1821ae08745Sheppo * The following three strings of the form ". -- ..." are help 1831ae08745Sheppo * messages for single character commands. Do not translate the 1841ae08745Sheppo * character before the --. 1851ae08745Sheppo */ 186*8e6a2a04Slm66018 bufp = gettext("h -- this help"); 1871ae08745Sheppo 1881ae08745Sheppo if ((rv = vntsd_write_line(clientp, bufp)) != VNTSD_SUCCESS) { 1891ae08745Sheppo return (rv); 1901ae08745Sheppo } 1911ae08745Sheppo 1921ae08745Sheppo bufp = gettext("l -- list of consoles"); 1931ae08745Sheppo 1941ae08745Sheppo if ((rv = vntsd_write_line(clientp, bufp)) != VNTSD_SUCCESS) { 1951ae08745Sheppo return (rv); 1961ae08745Sheppo } 1971ae08745Sheppo 1981ae08745Sheppo bufp = gettext("q -- quit"); 1991ae08745Sheppo 2001ae08745Sheppo if ((rv = vntsd_write_line(clientp, bufp)) != VNTSD_SUCCESS) { 2011ae08745Sheppo return (rv); 2021ae08745Sheppo } 2031ae08745Sheppo 2041ae08745Sheppo /* 2051ae08745Sheppo * TRANSLATION_NOTE 2061ae08745Sheppo * In the following string, "id" is a short mnemonic for 2071ae08745Sheppo * "identifier" and both occurrences should be translated. 2081ae08745Sheppo */ 2091ae08745Sheppo 210*8e6a2a04Slm66018 bufp = gettext("c{id}, n{name} -- connect to a console of domain {id}" 211*8e6a2a04Slm66018 " or domain {name}"); 2121ae08745Sheppo 2131ae08745Sheppo if ((rv = vntsd_write_line(clientp, bufp)) != VNTSD_SUCCESS) { 2141ae08745Sheppo return (rv); 2151ae08745Sheppo } 2161ae08745Sheppo 2171ae08745Sheppo return (VNTSD_SUCCESS); 2181ae08745Sheppo } 2191ae08745Sheppo 220*8e6a2a04Slm66018 /* cons_by_name() - find a console structure according to a ldom's name */ 221*8e6a2a04Slm66018 static boolean_t 222*8e6a2a04Slm66018 cons_by_name(vntsd_cons_t *consp, char *name) 223*8e6a2a04Slm66018 { 224*8e6a2a04Slm66018 if (consp->status & VNTSD_CONS_DELETED) { 225*8e6a2a04Slm66018 return (B_FALSE); 226*8e6a2a04Slm66018 } 227*8e6a2a04Slm66018 return (strcmp(consp->domain_name, name) == 0); 228*8e6a2a04Slm66018 } 229*8e6a2a04Slm66018 230*8e6a2a04Slm66018 /* name_to_cons_no - convert a ldom's name to its consno */ 231*8e6a2a04Slm66018 static int 232*8e6a2a04Slm66018 name_to_cons_no(vntsd_group_t *groupp, char *name) 233*8e6a2a04Slm66018 { 234*8e6a2a04Slm66018 vntsd_cons_t *consp; 235*8e6a2a04Slm66018 236*8e6a2a04Slm66018 consp = (vntsd_cons_t *)vntsd_que_find(groupp->conspq, 237*8e6a2a04Slm66018 (compare_func_t)cons_by_name, name); 238*8e6a2a04Slm66018 239*8e6a2a04Slm66018 if (consp == NULL) { 240*8e6a2a04Slm66018 return (-1); 241*8e6a2a04Slm66018 } 242*8e6a2a04Slm66018 243*8e6a2a04Slm66018 return (consp->cons_no); 244*8e6a2a04Slm66018 } 245*8e6a2a04Slm66018 2461ae08745Sheppo /* select a console to connect */ 2471ae08745Sheppo static int 248*8e6a2a04Slm66018 select_cons(vntsd_group_t *groupp, vntsd_cons_t **consp, 2491ae08745Sheppo vntsd_client_t *clientp, char c) 2501ae08745Sheppo { 251*8e6a2a04Slm66018 int cons_no = -1; 2521ae08745Sheppo int n; 2531ae08745Sheppo int i; 2541ae08745Sheppo char buf[VNTSD_LINE_LEN]; 2551ae08745Sheppo int rv; 2561ae08745Sheppo 2571ae08745Sheppo 2581ae08745Sheppo 2591ae08745Sheppo (void) mutex_lock(&groupp->lock); 2601ae08745Sheppo if (groupp->num_cons == 0) { 2611ae08745Sheppo (void) mutex_unlock(&groupp->lock); 2621ae08745Sheppo /* no console in this group */ 2631ae08745Sheppo return (VNTSD_STATUS_NO_CONS); 2641ae08745Sheppo } 2651ae08745Sheppo (void) mutex_unlock(&groupp->lock); 2661ae08745Sheppo 2671ae08745Sheppo 268*8e6a2a04Slm66018 /* c{id} or n{name} */ 2691ae08745Sheppo 2701ae08745Sheppo n = VNTSD_LINE_LEN; 2711ae08745Sheppo 2721ae08745Sheppo if ((rv = vntsd_read_line(clientp, buf, &n)) != VNTSD_SUCCESS) { 2731ae08745Sheppo return (rv); 2741ae08745Sheppo } 2751ae08745Sheppo 2761ae08745Sheppo /* parse command */ 2771ae08745Sheppo for (i = 0; i < n; i++) { 278*8e6a2a04Slm66018 switch (c) { 2791ae08745Sheppo 280*8e6a2a04Slm66018 case 'c': 281*8e6a2a04Slm66018 /* c{id} or c {id} */ 282*8e6a2a04Slm66018 if (isspace(buf[i])) { 2831ae08745Sheppo continue; 2841ae08745Sheppo } 2851ae08745Sheppo 286*8e6a2a04Slm66018 if (!isdigit(buf[i])) { 2871ae08745Sheppo return (VNTSD_ERR_INVALID_INPUT); 2881ae08745Sheppo } 289*8e6a2a04Slm66018 290*8e6a2a04Slm66018 cons_no = atoi(buf + i); 291*8e6a2a04Slm66018 break; 292*8e6a2a04Slm66018 293*8e6a2a04Slm66018 case 'n': 294*8e6a2a04Slm66018 /* n{name) or n {name} */ 295*8e6a2a04Slm66018 if (isspace(buf[i])) { 296*8e6a2a04Slm66018 continue; 297*8e6a2a04Slm66018 } 298*8e6a2a04Slm66018 299*8e6a2a04Slm66018 buf[n-1] = 0; 300*8e6a2a04Slm66018 cons_no = name_to_cons_no(groupp, buf+i); 301*8e6a2a04Slm66018 break; 302*8e6a2a04Slm66018 303*8e6a2a04Slm66018 default: 304*8e6a2a04Slm66018 /* should never get here */ 305*8e6a2a04Slm66018 return (VNTSD_ERR_INVALID_INPUT); 306*8e6a2a04Slm66018 307*8e6a2a04Slm66018 } 308*8e6a2a04Slm66018 309*8e6a2a04Slm66018 /* got user selection */ 310*8e6a2a04Slm66018 break; 3111ae08745Sheppo } 3121ae08745Sheppo 3131ae08745Sheppo if (cons_no < 0) { 3141ae08745Sheppo return (VNTSD_ERR_INVALID_INPUT); 3151ae08745Sheppo } 3161ae08745Sheppo 3171ae08745Sheppo /* get selected console */ 3181ae08745Sheppo (void) mutex_lock(&groupp->lock); 3191ae08745Sheppo 3201ae08745Sheppo *consp = (vntsd_cons_t *)vntsd_que_find(groupp->conspq, 3211ae08745Sheppo (compare_func_t)vntsd_cons_by_consno, &cons_no); 3221ae08745Sheppo 3231ae08745Sheppo if (*consp == NULL) { 3241ae08745Sheppo /* during console selection, the console has been deleted */ 3251ae08745Sheppo (void) mutex_unlock(&groupp->lock); 3261ae08745Sheppo 3271ae08745Sheppo return (VNTSD_ERR_INVALID_INPUT); 3281ae08745Sheppo } 3291ae08745Sheppo if ((*consp)->status & VNTSD_CONS_DELETED) { 3301ae08745Sheppo return (VNTSD_ERR_INVALID_INPUT); 3311ae08745Sheppo } 3321ae08745Sheppo 3331ae08745Sheppo (void) mutex_unlock(&groupp->lock); 3341ae08745Sheppo 3351ae08745Sheppo return (VNTSD_SUCCESS); 3361ae08745Sheppo } 3371ae08745Sheppo 3381ae08745Sheppo /* compare if there is a match console in the gorup */ 3391ae08745Sheppo static boolean_t 3401ae08745Sheppo find_cons_in_group(vntsd_cons_t *consp_in_group, vntsd_cons_t *consp) 3411ae08745Sheppo { 3421ae08745Sheppo if (consp_in_group == consp) { 3431ae08745Sheppo return (B_TRUE); 3441ae08745Sheppo } else { 3451ae08745Sheppo return (B_FALSE); 3461ae08745Sheppo } 3471ae08745Sheppo } 3481ae08745Sheppo 3491ae08745Sheppo /* connect a client to a console */ 3501ae08745Sheppo static int 3511ae08745Sheppo connect_cons(vntsd_cons_t *consp, vntsd_client_t *clientp) 3521ae08745Sheppo { 3531ae08745Sheppo int rv, rv1; 3541ae08745Sheppo vntsd_group_t *groupp; 3551ae08745Sheppo 3561ae08745Sheppo assert(consp); 3571ae08745Sheppo groupp = consp->group; 3581ae08745Sheppo assert(groupp); 3591ae08745Sheppo assert(clientp); 3601ae08745Sheppo 3611ae08745Sheppo (void) mutex_lock(&groupp->lock); 3621ae08745Sheppo 3631ae08745Sheppo /* check if console is valid */ 3641ae08745Sheppo consp = vntsd_que_find(groupp->conspq, 3651ae08745Sheppo (compare_func_t)find_cons_in_group, consp); 3661ae08745Sheppo 3671ae08745Sheppo if (consp == NULL) { 3681ae08745Sheppo (void) mutex_unlock(&groupp->lock); 3691ae08745Sheppo return (VNTSD_STATUS_NO_CONS); 3701ae08745Sheppo } 3711ae08745Sheppo if (consp->status & VNTSD_CONS_DELETED) { 3721ae08745Sheppo (void) mutex_unlock(&groupp->lock); 3731ae08745Sheppo return (VNTSD_STATUS_NO_CONS); 3741ae08745Sheppo } 3751ae08745Sheppo 3761ae08745Sheppo (void) mutex_lock(&consp->lock); 3771ae08745Sheppo (void) mutex_lock(&clientp->lock); 3781ae08745Sheppo 3791ae08745Sheppo 3801ae08745Sheppo clientp->cons = consp; 3811ae08745Sheppo 3821ae08745Sheppo /* enable daemon cmd */ 3831ae08745Sheppo clientp->status &= ~VNTSD_CLIENT_DISABLE_DAEMON_CMD; 3841ae08745Sheppo 3851ae08745Sheppo if (consp->clientpq == NULL) { 3861ae08745Sheppo /* first connect to console - a writer */ 3871ae08745Sheppo assert(consp->vcc_fd == -1); 3881ae08745Sheppo /* open vcc */ 3891ae08745Sheppo consp->vcc_fd = vntsd_open_vcc(consp->dev_name, consp->cons_no); 3901ae08745Sheppo if (consp->vcc_fd < 0) { 3911ae08745Sheppo (void) mutex_unlock(&clientp->lock); 3921ae08745Sheppo (void) mutex_unlock(&consp->lock); 3931ae08745Sheppo (void) mutex_unlock(&groupp->lock); 3941ae08745Sheppo assert(consp->group); 3951ae08745Sheppo return (vntsd_vcc_err(consp)); 3961ae08745Sheppo } 3971ae08745Sheppo } 3981ae08745Sheppo 3991ae08745Sheppo (void) mutex_unlock(&clientp->lock); 4001ae08745Sheppo 4011ae08745Sheppo /* 4021ae08745Sheppo * move the client from group's no console selected queue 4031ae08745Sheppo * to cons queue 4041ae08745Sheppo */ 4051ae08745Sheppo 4061ae08745Sheppo rv = vntsd_que_rm(&groupp->no_cons_clientpq, clientp); 4071ae08745Sheppo assert(rv == VNTSD_SUCCESS); 4081ae08745Sheppo 4091ae08745Sheppo rv = vntsd_que_append(&consp->clientpq, clientp); 4101ae08745Sheppo (void) mutex_unlock(&groupp->lock); 4111ae08745Sheppo 4121ae08745Sheppo if (rv != VNTSD_SUCCESS) { 4131ae08745Sheppo if (consp->clientpq->handle == clientp) { 4141ae08745Sheppo /* writer */ 4151ae08745Sheppo (void) close(consp->vcc_fd); 4161ae08745Sheppo consp->vcc_fd = -1; 4171ae08745Sheppo } 4181ae08745Sheppo 4191ae08745Sheppo (void) mutex_unlock(&consp->lock); 4201ae08745Sheppo return (rv); 4211ae08745Sheppo } 4221ae08745Sheppo 4231ae08745Sheppo (void) mutex_unlock(&consp->lock); 4241ae08745Sheppo 4251ae08745Sheppo if (consp->clientpq->handle == clientp) { 4261ae08745Sheppo /* create a write thread */ 4271ae08745Sheppo rv = create_write_thread(consp); 4281ae08745Sheppo if (rv != VNTSD_SUCCESS) { 4291ae08745Sheppo return (rv); 4301ae08745Sheppo } 4311ae08745Sheppo } 4321ae08745Sheppo 4331ae08745Sheppo /* write connecting message */ 4341ae08745Sheppo if ((rv = write_connect_msg(clientp, consp->group->group_name, 4351ae08745Sheppo consp->domain_name)) != VNTSD_SUCCESS) { 4361ae08745Sheppo return (rv); 4371ae08745Sheppo } 4381ae08745Sheppo 4391ae08745Sheppo /* process input from client */ 4401ae08745Sheppo rv = vntsd_read(clientp); 4411ae08745Sheppo 4421ae08745Sheppo /* client disconnected from the console */ 4431ae08745Sheppo (void) mutex_lock(&groupp->lock); 4441ae08745Sheppo 4451ae08745Sheppo /* remove client from console queue */ 4461ae08745Sheppo (void) mutex_lock(&consp->lock); 4471ae08745Sheppo rv1 = vntsd_que_rm(&consp->clientpq, clientp); 4481ae08745Sheppo assert(rv1 == VNTSD_SUCCESS); 4491ae08745Sheppo 4501ae08745Sheppo /* append client to group's no console selected queue */ 4511ae08745Sheppo rv1 = vntsd_que_append(&groupp->no_cons_clientpq, clientp); 4521ae08745Sheppo (void) mutex_unlock(&groupp->lock); 4531ae08745Sheppo 4541ae08745Sheppo if (consp->clientpq == NULL) { 4551ae08745Sheppo /* clean up console since there is no client connected to it */ 4561ae08745Sheppo assert(consp->vcc_fd != -1); 4571ae08745Sheppo 4581ae08745Sheppo /* close vcc port */ 4591ae08745Sheppo (void) close(consp->vcc_fd); 4601ae08745Sheppo consp->vcc_fd = -1; 4611ae08745Sheppo 4621ae08745Sheppo /* force write thread to exit */ 4631ae08745Sheppo assert(consp->wr_tid != (thread_t)-1); 4641ae08745Sheppo (void) thr_kill(consp->wr_tid, SIGUSR1); 4651ae08745Sheppo (void) mutex_unlock(&consp->lock); 4661ae08745Sheppo (void) thr_join(consp->wr_tid, NULL, NULL); 4671ae08745Sheppo (void) mutex_lock(&consp->lock); 4681ae08745Sheppo } 4691ae08745Sheppo 4701ae08745Sheppo if (consp->status & VNTSD_CONS_SIG_WAIT) { 4711ae08745Sheppo /* console is waiting for client to disconnect */ 4721ae08745Sheppo (void) cond_signal(&consp->cvp); 4731ae08745Sheppo } 4741ae08745Sheppo 4751ae08745Sheppo (void) mutex_unlock(&consp->lock); 4761ae08745Sheppo 4771ae08745Sheppo return (rv1 == VNTSD_SUCCESS ? rv : rv1); 4781ae08745Sheppo 4791ae08745Sheppo } 4801ae08745Sheppo 4811ae08745Sheppo /* read command line input */ 4821ae08745Sheppo static int 4831ae08745Sheppo read_cmd(vntsd_client_t *clientp, char *prompt, char *cmd) 4841ae08745Sheppo { 4851ae08745Sheppo int rv; 4861ae08745Sheppo 4871ae08745Sheppo /* disable daemon special command */ 4881ae08745Sheppo (void) mutex_lock(&clientp->lock); 4891ae08745Sheppo clientp->status |= VNTSD_CLIENT_DISABLE_DAEMON_CMD; 4901ae08745Sheppo (void) mutex_unlock(&clientp->lock); 4911ae08745Sheppo 4921ae08745Sheppo if ((rv = vntsd_write_client(clientp, vntsd_eol, VNTSD_EOL_LEN)) 4931ae08745Sheppo != VNTSD_SUCCESS) { 4941ae08745Sheppo return (rv); 4951ae08745Sheppo } 4961ae08745Sheppo 4971ae08745Sheppo if ((rv = vntsd_write_client(clientp, prompt, strlen(prompt))) 4981ae08745Sheppo != VNTSD_SUCCESS) { 4991ae08745Sheppo return (rv); 5001ae08745Sheppo } 5011ae08745Sheppo 5021ae08745Sheppo if ((rv = vntsd_read_data(clientp, cmd)) != VNTSD_SUCCESS) { 5031ae08745Sheppo return (rv); 5041ae08745Sheppo } 5051ae08745Sheppo if (*cmd == BS) { 5061ae08745Sheppo return (VNTSD_SUCCESS); 5071ae08745Sheppo } 5081ae08745Sheppo 5091ae08745Sheppo rv = vntsd_write_client(clientp, cmd, 1); 5101ae08745Sheppo 5111ae08745Sheppo *cmd = tolower(*cmd); 5121ae08745Sheppo 5131ae08745Sheppo return (rv); 5141ae08745Sheppo } 5151ae08745Sheppo 5161ae08745Sheppo /* reset client for selecting a console in the group */ 5171ae08745Sheppo static void 5181ae08745Sheppo client_init(vntsd_client_t *clientp) 5191ae08745Sheppo { 5201ae08745Sheppo (void) mutex_lock(&clientp->lock); 5211ae08745Sheppo clientp->cons = NULL; 5221ae08745Sheppo clientp->status = 0; 5231ae08745Sheppo (void) mutex_unlock(&clientp->lock); 5241ae08745Sheppo } 5251ae08745Sheppo 5261ae08745Sheppo /* clean up client and exit the thread */ 5271ae08745Sheppo static void 5281ae08745Sheppo client_fini(vntsd_group_t *groupp, vntsd_client_t *clientp) 5291ae08745Sheppo { 5301ae08745Sheppo 5311ae08745Sheppo assert(groupp); 5321ae08745Sheppo assert(clientp); 5331ae08745Sheppo 5341ae08745Sheppo /* disconnct client from tcp port */ 5351ae08745Sheppo assert(clientp->sockfd != -1); 5361ae08745Sheppo (void) close(clientp->sockfd); 5371ae08745Sheppo 5381ae08745Sheppo (void) mutex_lock(&groupp->lock); 5391ae08745Sheppo (void) vntsd_que_rm(&groupp->no_cons_clientpq, clientp); 5401ae08745Sheppo 5411ae08745Sheppo if ((groupp->no_cons_clientpq == NULL) && 5421ae08745Sheppo (groupp->status & VNTSD_GROUP_SIG_WAIT)) { 5431ae08745Sheppo /* group is waiting to be deleted */ 5441ae08745Sheppo groupp->status &= ~VNTSD_GROUP_SIG_WAIT; 5451ae08745Sheppo (void) cond_signal(&groupp->cvp); 5461ae08745Sheppo } 5471ae08745Sheppo (void) mutex_unlock(&groupp->lock); 5481ae08745Sheppo 5491ae08745Sheppo (void) mutex_destroy(&clientp->lock); 5501ae08745Sheppo free(clientp); 5511ae08745Sheppo 5521ae08745Sheppo thr_exit(0); 5531ae08745Sheppo } 5541ae08745Sheppo 5551ae08745Sheppo /* check client's status. exit if client quits or fatal errors */ 5561ae08745Sheppo static void 5571ae08745Sheppo console_chk_status(vntsd_group_t *groupp, vntsd_client_t *clientp, int status) 5581ae08745Sheppo { 5591ae08745Sheppo char err_msg[VNTSD_LINE_LEN]; 5601ae08745Sheppo 5611ae08745Sheppo D1(stderr, "t@%d console_chk_status() status=%d " 5621ae08745Sheppo "client status=%x num consoles=%d \n", 5631ae08745Sheppo thr_self(), status, clientp->status, groupp->num_cons); 5641ae08745Sheppo 5651ae08745Sheppo (void) snprintf(err_msg, VNTSD_LINE_LEN, "console_chk_status client%d" 5661ae08745Sheppo " num_cos=%d", clientp->sockfd, groupp->num_cons); 5671ae08745Sheppo 5681ae08745Sheppo if (groupp->num_cons == 0) { 5691ae08745Sheppo /* no more console in the group */ 5701ae08745Sheppo client_fini(groupp, clientp); 5711ae08745Sheppo } 5721ae08745Sheppo 5731ae08745Sheppo if (status == VNTSD_STATUS_INTR) { 5741ae08745Sheppo /* reason for signal? */ 5751ae08745Sheppo status = vntsd_cons_chk_intr(clientp); 5761ae08745Sheppo } 5771ae08745Sheppo 5781ae08745Sheppo switch (status) { 5791ae08745Sheppo 5801ae08745Sheppo case VNTSD_STATUS_CLIENT_QUIT: 5811ae08745Sheppo client_fini(groupp, clientp); 5821ae08745Sheppo return; 5831ae08745Sheppo 5841ae08745Sheppo case VNTSD_STATUS_RESELECT_CONS: 5851ae08745Sheppo assert(clientp->cons); 5861ae08745Sheppo if ((groupp->num_cons == 1) && 5871ae08745Sheppo (groupp->conspq->handle == clientp->cons)) { 5881ae08745Sheppo /* no other selection available */ 5891ae08745Sheppo client_fini(groupp, clientp); 5901ae08745Sheppo } else { 5911ae08745Sheppo client_init(clientp); 5921ae08745Sheppo } 5931ae08745Sheppo return; 5941ae08745Sheppo 5951ae08745Sheppo case VNTSD_STATUS_VCC_IO_ERR: 5961ae08745Sheppo if ((clientp->status & VNTSD_CLIENT_CONS_DELETED) == 0) { 5971ae08745Sheppo /* check if console was deleted */ 5981ae08745Sheppo status = vntsd_vcc_err(clientp->cons); 5991ae08745Sheppo } 6001ae08745Sheppo 6011ae08745Sheppo if (status != VNTSD_STATUS_CONTINUE) { 6021ae08745Sheppo /* console was deleted */ 6031ae08745Sheppo if (groupp->num_cons == 1) { 6041ae08745Sheppo client_fini(groupp, clientp); 6051ae08745Sheppo } 6061ae08745Sheppo } 6071ae08745Sheppo 6081ae08745Sheppo /* console is ok */ 6091ae08745Sheppo client_init(clientp); 6101ae08745Sheppo return; 6111ae08745Sheppo 6121ae08745Sheppo case VNTSD_STATUS_MOV_CONS_FORWARD: 6131ae08745Sheppo case VNTSD_STATUS_MOV_CONS_BACKWARD: 6141ae08745Sheppo if (groupp->num_cons == 1) { 6151ae08745Sheppo /* same console */ 6161ae08745Sheppo return; 6171ae08745Sheppo } 6181ae08745Sheppo 6191ae08745Sheppo /* get selected console */ 6201ae08745Sheppo (void) mutex_lock(&(clientp->cons->group->lock)); 6211ae08745Sheppo clientp->cons = vntsd_que_pos(clientp->cons->group->conspq, 6221ae08745Sheppo clientp->cons, 6231ae08745Sheppo (status == VNTSD_STATUS_MOV_CONS_FORWARD)?(1):(-1)); 6241ae08745Sheppo (void) mutex_unlock(&(clientp->cons->group->lock)); 6251ae08745Sheppo return; 6261ae08745Sheppo 6271ae08745Sheppo case VNTSD_SUCCESS: 6281ae08745Sheppo case VNTSD_STATUS_CONTINUE: 6291ae08745Sheppo case VNTSD_STATUS_NO_CONS: 6301ae08745Sheppo client_init(clientp); 6311ae08745Sheppo return; 6321ae08745Sheppo 6331ae08745Sheppo case VNTSD_ERR_INVALID_INPUT: 6341ae08745Sheppo return; 6351ae08745Sheppo 6361ae08745Sheppo default: 6371ae08745Sheppo /* fatal error */ 6381ae08745Sheppo vntsd_log(status, err_msg); 6391ae08745Sheppo client_fini(groupp, clientp); 6401ae08745Sheppo return; 6411ae08745Sheppo } 6421ae08745Sheppo } 6431ae08745Sheppo 6441ae08745Sheppo /* console thread */ 6451ae08745Sheppo void * 6461ae08745Sheppo vntsd_console_thread(vntsd_thr_arg_t *argp) 6471ae08745Sheppo { 6481ae08745Sheppo vntsd_group_t *groupp; 6491ae08745Sheppo vntsd_cons_t *consp; 6501ae08745Sheppo vntsd_client_t *clientp; 6511ae08745Sheppo 6521ae08745Sheppo char buf[MAXHOSTNAMELEN]; 6531ae08745Sheppo char prompt[72]; 6541ae08745Sheppo char cmd; 6551ae08745Sheppo int rv = VNTSD_SUCCESS; 6561ae08745Sheppo int num_cons; 6571ae08745Sheppo 6581ae08745Sheppo 6591ae08745Sheppo groupp = (vntsd_group_t *)argp->handle; 6601ae08745Sheppo clientp = (vntsd_client_t *)argp->arg; 6611ae08745Sheppo 6621ae08745Sheppo assert(groupp); 6631ae08745Sheppo assert(clientp); 6641ae08745Sheppo 6651ae08745Sheppo /* check if group is removed */ 6661ae08745Sheppo 6671ae08745Sheppo D1(stderr, "t@%d get_client_sel@%lld:client@%d\n", thr_self(), 6681ae08745Sheppo groupp->tcp_port, clientp->sockfd); 6691ae08745Sheppo 6701ae08745Sheppo bzero(buf, MAXHOSTNAMELEN); 6711ae08745Sheppo 6721ae08745Sheppo /* host name */ 6731ae08745Sheppo if (gethostname(buf, MAXHOSTNAMELEN)) { 6741ae08745Sheppo vntsd_log(VNTSD_STATUS_NO_HOST_NAME, "vntsd_console_thread()"); 6751ae08745Sheppo (void) snprintf(buf, sizeof (buf), "unkown host"); 6761ae08745Sheppo } 6771ae08745Sheppo 6781ae08745Sheppo if (snprintf(prompt, sizeof (prompt), 679*8e6a2a04Slm66018 "%s-vnts-%s: h, l, c{id}, n{name}, q:", 6801ae08745Sheppo buf, groupp->group_name) >= sizeof (prompt)) { 6811ae08745Sheppo /* long prompt doesn't fit, use short one */ 6821ae08745Sheppo (void) snprintf(prompt, sizeof (prompt), 683*8e6a2a04Slm66018 "vnts: h, l, c{id}, n{name}, q:"); 6841ae08745Sheppo } 6851ae08745Sheppo 6861ae08745Sheppo 6871ae08745Sheppo for (;;) { 6881ae08745Sheppo cmd = ' '; 6891ae08745Sheppo D1(stderr, "t@%d console_thread()@%lld:client@%d\n", thr_self(), 6901ae08745Sheppo groupp->tcp_port, clientp->sockfd); 6911ae08745Sheppo 6921ae08745Sheppo num_cons = vntsd_chk_group_total_cons(groupp); 6931ae08745Sheppo 6941ae08745Sheppo if ((num_cons > 1) && (clientp->cons == NULL)) { 6951ae08745Sheppo /* console to connect to */ 6961ae08745Sheppo rv = read_cmd(clientp, prompt, &cmd); 6971ae08745Sheppo /* check error and may exit */ 6981ae08745Sheppo console_chk_status(groupp, clientp, rv); 6991ae08745Sheppo } 7001ae08745Sheppo 7011ae08745Sheppo switch (cmd) { 7021ae08745Sheppo 7031ae08745Sheppo case 'l': 7041ae08745Sheppo 7051ae08745Sheppo /* list domain names */ 7061ae08745Sheppo rv = list_all_domains(groupp, clientp); 7071ae08745Sheppo break; 7081ae08745Sheppo 7091ae08745Sheppo 7101ae08745Sheppo case 'q': 7111ae08745Sheppo 7121ae08745Sheppo rv = VNTSD_STATUS_CLIENT_QUIT; 7131ae08745Sheppo break; 7141ae08745Sheppo 715*8e6a2a04Slm66018 case ' ': 716*8e6a2a04Slm66018 717*8e6a2a04Slm66018 if (clientp->cons == NULL) { 718*8e6a2a04Slm66018 if (num_cons == 1) { 719*8e6a2a04Slm66018 /* by pass selecting console */ 720*8e6a2a04Slm66018 consp = (vntsd_cons_t *) 721*8e6a2a04Slm66018 (groupp->conspq->handle); 722*8e6a2a04Slm66018 } else { 723*8e6a2a04Slm66018 continue; 724*8e6a2a04Slm66018 } 725*8e6a2a04Slm66018 726*8e6a2a04Slm66018 } else { 727*8e6a2a04Slm66018 consp = clientp->cons; 728*8e6a2a04Slm66018 } 729*8e6a2a04Slm66018 730*8e6a2a04Slm66018 /* connect to console */ 731*8e6a2a04Slm66018 rv = connect_cons(consp, clientp); 732*8e6a2a04Slm66018 7331ae08745Sheppo break; 7341ae08745Sheppo 735*8e6a2a04Slm66018 case 'c': 736*8e6a2a04Slm66018 case 'n': 7371ae08745Sheppo /* select console */ 7381ae08745Sheppo if (clientp->cons == NULL) { 739*8e6a2a04Slm66018 rv = select_cons(groupp, &consp, clientp, cmd); 7401ae08745Sheppo if (rv == VNTSD_ERR_INVALID_INPUT) { 7411ae08745Sheppo rv = display_help(clientp); 7421ae08745Sheppo break; 7431ae08745Sheppo } 7441ae08745Sheppo } else { 7451ae08745Sheppo consp = clientp->cons; 7461ae08745Sheppo } 7471ae08745Sheppo assert(consp); 7481ae08745Sheppo 7491ae08745Sheppo /* connect to console */ 7501ae08745Sheppo rv = connect_cons(consp, clientp); 7511ae08745Sheppo D1(stderr, "t@%d console_thread()" 7521ae08745Sheppo "connect_cons returns %d\n", 7531ae08745Sheppo thr_self(), rv); 7541ae08745Sheppo break; 7551ae08745Sheppo 756*8e6a2a04Slm66018 case 'h': 757*8e6a2a04Slm66018 default: 758*8e6a2a04Slm66018 rv = display_help(clientp); 759*8e6a2a04Slm66018 break; 760*8e6a2a04Slm66018 7611ae08745Sheppo } 762*8e6a2a04Slm66018 7631ae08745Sheppo /* check error and may exit */ 7641ae08745Sheppo console_chk_status(groupp, clientp, rv); 7651ae08745Sheppo } 7661ae08745Sheppo 7671ae08745Sheppo /*NOTREACHED*/ 7681ae08745Sheppo return (NULL); 7691ae08745Sheppo } 770