console.c revision 4d39be2b45b5ac811d28452e6eb629ac64aebfc4
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo/*
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo * CDDL HEADER START
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo *
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo * The contents of this file are subject to the terms of the
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo * Common Development and Distribution License (the "License").
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo * You may not use this file except in compliance with the License.
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo *
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo * or http://www.opensolaris.org/os/licensing.
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo * See the License for the specific language governing permissions
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo * and limitations under the License.
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo *
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo * When distributing Covered Code, include this CDDL HEADER in each
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo * If applicable, add the following below this CDDL HEADER, with the
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo * fields enclosed by brackets "[]" replaced with your own identifying
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo * information: Portions Copyright [yyyy] [name of copyright owner]
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo *
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo * CDDL HEADER END
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo/*
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo * Use is subject to license terms.
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo#pragma ident "%Z%%M% %I% %E% SMI"
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo/*
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo * Listen thread creates a console thread whenever there is a tcp client
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo * made a conection to its port. In the console thread, if there are
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo * multiple consoles in the group, client will be asked for a console selection.
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo * a write thread for a console is created when first client connects to a
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo * selected console and console thread becomes read thread for the client.
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo#include <stdio.h>
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo#include <stdlib.h>
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo#include <string.h>
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo#include <unistd.h>
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo#include <sys/types.h>
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo#include <sys/socket.h>
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo#include <netinet/in.h>
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo#include <thread.h>
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo#include <synch.h>
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo#include <signal.h>
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo#include <assert.h>
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo#include <ctype.h>
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo#include <syslog.h>
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo#include <libintl.h>
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo#include <netdb.h>
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo#include "vntsd.h"
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo#include "chars.h"
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo/* display domain names in the group */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppostatic boolean_t
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppodisplay_domain_name(vntsd_cons_t *consp, int *fd)
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo{
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo char buf[VNTSD_LINE_LEN];
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo char *status;
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo if (consp->clientpq != NULL) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo status = gettext("connected");
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo } else if (consp->status & VNTSD_CONS_DELETED) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo status = gettext("removing...");
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo } else {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo status = gettext("online");
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (void) snprintf(buf, sizeof (buf), "%-20d%-30s%-25s%s",
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo consp->cons_no, consp->domain_name, status, vntsd_eol);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo return (vntsd_write_fd(*fd, buf, strlen(buf)) != VNTSD_SUCCESS);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo}
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo/* output connected message to tcp client */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppostatic int
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppowrite_connect_msg(vntsd_client_t *clientp, char *group_name,
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo char *domain_name)
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo{
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo int rv = VNTSD_SUCCESS;
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo char buf[VNTSD_LINE_LEN];
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo if ((rv = vntsd_write_client(clientp, vntsd_eol, VNTSD_EOL_LEN)) !=
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo VNTSD_SUCCESS) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo return (rv);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (void) snprintf(buf, sizeof (buf),
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo gettext("Connecting to console \"%s\" in group \"%s\" ...."),
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo domain_name, group_name);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo if ((rv = vntsd_write_line(clientp, buf)) != VNTSD_SUCCESS) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo return (rv);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo if ((rv = vntsd_write_line(clientp,
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo gettext("Press ~? for control options .."))) !=
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo VNTSD_SUCCESS) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo return (rv);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo return (VNTSD_SUCCESS);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo}
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppostatic int
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppocreate_write_thread(vntsd_cons_t *consp)
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo{
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo assert(consp);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /* create write thread for the console */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (void) mutex_lock(&consp->lock);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo if (thr_create(NULL, 0, (thr_func_t)vntsd_write_thread,
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (void *)consp, NULL, &consp->wr_tid)) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo DERR(stderr, "t@%d create_rd_wr_thread@%d: "
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo "create write thread failed\n",
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo thr_self(), consp->cons_no);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (void) close(consp->vcc_fd);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo consp->vcc_fd = -1;
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (void) mutex_unlock(&consp->lock);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo return (VNTSD_ERR_CREATE_WR_THR);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (void) mutex_unlock(&consp->lock);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo return (VNTSD_SUCCESS);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo}
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo/* Display all domain consoles in a group. */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppostatic int
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppolist_all_domains(vntsd_group_t *groupp, vntsd_client_t *clientp)
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo{
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo char vntsd_line[VNTSD_LINE_LEN];
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo int rv = VNTSD_SUCCESS;
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo if ((rv = vntsd_write_client(clientp, vntsd_eol, VNTSD_EOL_LEN))
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo != VNTSD_SUCCESS) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo return (rv);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /*
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo * TRANSLATION_NOTE
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo * The following three strings of the form "DOMAIN .." are table
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo * headers and should be all uppercase.
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (void) snprintf(vntsd_line, sizeof (vntsd_line),
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo "%-20s%-30s%-25s",
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo gettext("DOMAIN ID"), gettext("DOMAIN NAME"),
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo gettext("DOMAIN STATE"));
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo if ((rv = vntsd_write_line(clientp, vntsd_line)) != VNTSD_SUCCESS) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo return (rv);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (void) mutex_lock(&groupp->lock);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo if (vntsd_que_find(groupp->conspq, (compare_func_t)display_domain_name,
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo &(clientp->sockfd)) != NULL) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo rv = VNTSD_ERR_WRITE_CLIENT;
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (void) mutex_unlock(&groupp->lock);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo return (rv);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo}
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo/* display help */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppostatic int
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppodisplay_help(vntsd_client_t *clientp)
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo{
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo int rv = VNTSD_SUCCESS;
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo char *bufp;
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo if ((rv = vntsd_write_client(clientp, vntsd_eol, VNTSD_EOL_LEN))
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo != VNTSD_SUCCESS) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo return (rv);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /*
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo * TRANSLATION_NOTE
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo * The following three strings of the form ". -- ..." are help
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo * messages for single character commands. Do not translate the
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo * character before the --.
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo */
8e6a2a040587479821d1e682a28bcef7e75f19a6lm bufp = gettext("h -- this help");
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo if ((rv = vntsd_write_line(clientp, bufp)) != VNTSD_SUCCESS) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo return (rv);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo bufp = gettext("l -- list of consoles");
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo if ((rv = vntsd_write_line(clientp, bufp)) != VNTSD_SUCCESS) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo return (rv);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo bufp = gettext("q -- quit");
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo if ((rv = vntsd_write_line(clientp, bufp)) != VNTSD_SUCCESS) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo return (rv);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /*
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo * TRANSLATION_NOTE
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo * In the following string, "id" is a short mnemonic for
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo * "identifier" and both occurrences should be translated.
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
8e6a2a040587479821d1e682a28bcef7e75f19a6lm bufp = gettext("c{id}, n{name} -- connect to a console of domain {id}"
8e6a2a040587479821d1e682a28bcef7e75f19a6lm " or domain {name}");
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo if ((rv = vntsd_write_line(clientp, bufp)) != VNTSD_SUCCESS) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo return (rv);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo return (VNTSD_SUCCESS);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo}
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
8e6a2a040587479821d1e682a28bcef7e75f19a6lm/* cons_by_name() - find a console structure according to a ldom's name */
8e6a2a040587479821d1e682a28bcef7e75f19a6lmstatic boolean_t
8e6a2a040587479821d1e682a28bcef7e75f19a6lmcons_by_name(vntsd_cons_t *consp, char *name)
8e6a2a040587479821d1e682a28bcef7e75f19a6lm{
8e6a2a040587479821d1e682a28bcef7e75f19a6lm if (consp->status & VNTSD_CONS_DELETED) {
8e6a2a040587479821d1e682a28bcef7e75f19a6lm return (B_FALSE);
8e6a2a040587479821d1e682a28bcef7e75f19a6lm }
8e6a2a040587479821d1e682a28bcef7e75f19a6lm return (strcmp(consp->domain_name, name) == 0);
8e6a2a040587479821d1e682a28bcef7e75f19a6lm}
8e6a2a040587479821d1e682a28bcef7e75f19a6lm
8e6a2a040587479821d1e682a28bcef7e75f19a6lm/* name_to_cons_no - convert a ldom's name to its consno */
8e6a2a040587479821d1e682a28bcef7e75f19a6lmstatic int
8e6a2a040587479821d1e682a28bcef7e75f19a6lmname_to_cons_no(vntsd_group_t *groupp, char *name)
8e6a2a040587479821d1e682a28bcef7e75f19a6lm{
8e6a2a040587479821d1e682a28bcef7e75f19a6lm vntsd_cons_t *consp;
8e6a2a040587479821d1e682a28bcef7e75f19a6lm
8e6a2a040587479821d1e682a28bcef7e75f19a6lm consp = (vntsd_cons_t *)vntsd_que_find(groupp->conspq,
8e6a2a040587479821d1e682a28bcef7e75f19a6lm (compare_func_t)cons_by_name, name);
8e6a2a040587479821d1e682a28bcef7e75f19a6lm
8e6a2a040587479821d1e682a28bcef7e75f19a6lm if (consp == NULL) {
8e6a2a040587479821d1e682a28bcef7e75f19a6lm return (-1);
8e6a2a040587479821d1e682a28bcef7e75f19a6lm }
8e6a2a040587479821d1e682a28bcef7e75f19a6lm
8e6a2a040587479821d1e682a28bcef7e75f19a6lm return (consp->cons_no);
8e6a2a040587479821d1e682a28bcef7e75f19a6lm}
8e6a2a040587479821d1e682a28bcef7e75f19a6lm
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo/* select a console to connect */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppostatic int
8e6a2a040587479821d1e682a28bcef7e75f19a6lmselect_cons(vntsd_group_t *groupp, vntsd_cons_t **consp,
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo vntsd_client_t *clientp, char c)
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo{
8e6a2a040587479821d1e682a28bcef7e75f19a6lm int cons_no = -1;
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo int n;
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo int i;
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo char buf[VNTSD_LINE_LEN];
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo int rv;
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (void) mutex_lock(&groupp->lock);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo if (groupp->num_cons == 0) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (void) mutex_unlock(&groupp->lock);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /* no console in this group */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo return (VNTSD_STATUS_NO_CONS);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (void) mutex_unlock(&groupp->lock);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
8e6a2a040587479821d1e682a28bcef7e75f19a6lm /* c{id} or n{name} */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo n = VNTSD_LINE_LEN;
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo if ((rv = vntsd_read_line(clientp, buf, &n)) != VNTSD_SUCCESS) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo return (rv);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /* parse command */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo for (i = 0; i < n; i++) {
8e6a2a040587479821d1e682a28bcef7e75f19a6lm switch (c) {
8e6a2a040587479821d1e682a28bcef7e75f19a6lm
8e6a2a040587479821d1e682a28bcef7e75f19a6lm case 'c':
8e6a2a040587479821d1e682a28bcef7e75f19a6lm /* c{id} or c {id} */
8e6a2a040587479821d1e682a28bcef7e75f19a6lm if (isspace(buf[i])) {
8e6a2a040587479821d1e682a28bcef7e75f19a6lm continue;
8e6a2a040587479821d1e682a28bcef7e75f19a6lm }
8e6a2a040587479821d1e682a28bcef7e75f19a6lm
8e6a2a040587479821d1e682a28bcef7e75f19a6lm if (!isdigit(buf[i])) {
8e6a2a040587479821d1e682a28bcef7e75f19a6lm return (VNTSD_ERR_INVALID_INPUT);
8e6a2a040587479821d1e682a28bcef7e75f19a6lm }
8e6a2a040587479821d1e682a28bcef7e75f19a6lm
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo cons_no = atoi(buf + i);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo break;
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
8e6a2a040587479821d1e682a28bcef7e75f19a6lm case 'n':
8e6a2a040587479821d1e682a28bcef7e75f19a6lm /* n{name) or n {name} */
8e6a2a040587479821d1e682a28bcef7e75f19a6lm if (isspace(buf[i])) {
8e6a2a040587479821d1e682a28bcef7e75f19a6lm continue;
8e6a2a040587479821d1e682a28bcef7e75f19a6lm }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
8e6a2a040587479821d1e682a28bcef7e75f19a6lm buf[n-1] = 0;
8e6a2a040587479821d1e682a28bcef7e75f19a6lm cons_no = name_to_cons_no(groupp, buf+i);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo break;
8e6a2a040587479821d1e682a28bcef7e75f19a6lm
8e6a2a040587479821d1e682a28bcef7e75f19a6lm default:
8e6a2a040587479821d1e682a28bcef7e75f19a6lm /* should never get here */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo return (VNTSD_ERR_INVALID_INPUT);
8e6a2a040587479821d1e682a28bcef7e75f19a6lm
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
8e6a2a040587479821d1e682a28bcef7e75f19a6lm
8e6a2a040587479821d1e682a28bcef7e75f19a6lm /* got user selection */
8e6a2a040587479821d1e682a28bcef7e75f19a6lm break;
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo if (cons_no < 0) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo return (VNTSD_ERR_INVALID_INPUT);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /* get selected console */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (void) mutex_lock(&groupp->lock);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo *consp = (vntsd_cons_t *)vntsd_que_find(groupp->conspq,
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (compare_func_t)vntsd_cons_by_consno, &cons_no);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo if (*consp == NULL) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /* during console selection, the console has been deleted */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (void) mutex_unlock(&groupp->lock);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo return (VNTSD_ERR_INVALID_INPUT);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo if ((*consp)->status & VNTSD_CONS_DELETED) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo return (VNTSD_ERR_INVALID_INPUT);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (void) mutex_unlock(&groupp->lock);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo return (VNTSD_SUCCESS);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo}
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo/* compare if there is a match console in the gorup */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppostatic boolean_t
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppofind_cons_in_group(vntsd_cons_t *consp_in_group, vntsd_cons_t *consp)
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo{
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo if (consp_in_group == consp) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo return (B_TRUE);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo } else {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo return (B_FALSE);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo}
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo/* connect a client to a console */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppostatic int
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppoconnect_cons(vntsd_cons_t *consp, vntsd_client_t *clientp)
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo{
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo int rv, rv1;
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo vntsd_group_t *groupp;
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo assert(consp);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo groupp = consp->group;
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo assert(groupp);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo assert(clientp);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (void) mutex_lock(&groupp->lock);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /* check if console is valid */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo consp = vntsd_que_find(groupp->conspq,
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (compare_func_t)find_cons_in_group, consp);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo if (consp == NULL) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (void) mutex_unlock(&groupp->lock);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo return (VNTSD_STATUS_NO_CONS);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo if (consp->status & VNTSD_CONS_DELETED) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (void) mutex_unlock(&groupp->lock);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo return (VNTSD_STATUS_NO_CONS);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (void) mutex_lock(&consp->lock);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (void) mutex_lock(&clientp->lock);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo clientp->cons = consp;
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /* enable daemon cmd */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo clientp->status &= ~VNTSD_CLIENT_DISABLE_DAEMON_CMD;
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
7636cb21f250f0485ca6052ffadc80ace93e6358lm if (consp->clientpq == NULL && consp->vcc_fd == -1) {
7636cb21f250f0485ca6052ffadc80ace93e6358lm
7636cb21f250f0485ca6052ffadc80ace93e6358lm /*
7636cb21f250f0485ca6052ffadc80ace93e6358lm * the first connection to a console - a writer
7636cb21f250f0485ca6052ffadc80ace93e6358lm * and the console has not opened.
7636cb21f250f0485ca6052ffadc80ace93e6358lm */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo consp->vcc_fd = vntsd_open_vcc(consp->dev_name, consp->cons_no);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo if (consp->vcc_fd < 0) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (void) mutex_unlock(&clientp->lock);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (void) mutex_unlock(&consp->lock);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (void) mutex_unlock(&groupp->lock);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo assert(consp->group);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo return (vntsd_vcc_err(consp));
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (void) mutex_unlock(&clientp->lock);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /*
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo * move the client from group's no console selected queue
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo * to cons queue
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo rv = vntsd_que_rm(&groupp->no_cons_clientpq, clientp);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo assert(rv == VNTSD_SUCCESS);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo rv = vntsd_que_append(&consp->clientpq, clientp);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (void) mutex_unlock(&groupp->lock);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo if (rv != VNTSD_SUCCESS) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo if (consp->clientpq->handle == clientp) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /* writer */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (void) close(consp->vcc_fd);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo consp->vcc_fd = -1;
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (void) mutex_unlock(&consp->lock);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo return (rv);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (void) mutex_unlock(&consp->lock);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo if (consp->clientpq->handle == clientp) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /* create a write thread */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo rv = create_write_thread(consp);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo if (rv != VNTSD_SUCCESS) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo return (rv);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /* write connecting message */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo if ((rv = write_connect_msg(clientp, consp->group->group_name,
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo consp->domain_name)) != VNTSD_SUCCESS) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo return (rv);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /* process input from client */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo rv = vntsd_read(clientp);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /* client disconnected from the console */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (void) mutex_lock(&groupp->lock);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /* remove client from console queue */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (void) mutex_lock(&consp->lock);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo rv1 = vntsd_que_rm(&consp->clientpq, clientp);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo assert(rv1 == VNTSD_SUCCESS);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /* append client to group's no console selected queue */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo rv1 = vntsd_que_append(&groupp->no_cons_clientpq, clientp);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (void) mutex_unlock(&groupp->lock);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo if (consp->clientpq == NULL) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /* clean up console since there is no client connected to it */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo assert(consp->vcc_fd != -1);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /* force write thread to exit */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo assert(consp->wr_tid != (thread_t)-1);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (void) thr_kill(consp->wr_tid, SIGUSR1);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (void) mutex_unlock(&consp->lock);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (void) thr_join(consp->wr_tid, NULL, NULL);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (void) mutex_lock(&consp->lock);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo if (consp->status & VNTSD_CONS_SIG_WAIT) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /* console is waiting for client to disconnect */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (void) cond_signal(&consp->cvp);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (void) mutex_unlock(&consp->lock);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo return (rv1 == VNTSD_SUCCESS ? rv : rv1);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo}
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo/* read command line input */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppostatic int
1ae0874509b6811fdde1dfd46f0d93fd09867a3fhepporead_cmd(vntsd_client_t *clientp, char *prompt, char *cmd)
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo{
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo int rv;
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /* disable daemon special command */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (void) mutex_lock(&clientp->lock);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo clientp->status |= VNTSD_CLIENT_DISABLE_DAEMON_CMD;
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (void) mutex_unlock(&clientp->lock);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo if ((rv = vntsd_write_client(clientp, vntsd_eol, VNTSD_EOL_LEN))
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo != VNTSD_SUCCESS) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo return (rv);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo if ((rv = vntsd_write_client(clientp, prompt, strlen(prompt)))
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo != VNTSD_SUCCESS) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo return (rv);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo if ((rv = vntsd_read_data(clientp, cmd)) != VNTSD_SUCCESS) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo return (rv);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo if (*cmd == BS) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo return (VNTSD_SUCCESS);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo rv = vntsd_write_client(clientp, cmd, 1);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo *cmd = tolower(*cmd);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo return (rv);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo}
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo/* reset client for selecting a console in the group */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppostatic void
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppoclient_init(vntsd_client_t *clientp)
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo{
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (void) mutex_lock(&clientp->lock);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo clientp->cons = NULL;
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo clientp->status = 0;
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (void) mutex_unlock(&clientp->lock);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo}
7636cb21f250f0485ca6052ffadc80ace93e6358lm/* is there any connection to a given console? */
7636cb21f250f0485ca6052ffadc80ace93e6358lmstatic boolean_t
7636cb21f250f0485ca6052ffadc80ace93e6358lmis_client_que_empty(vntsd_cons_t *consp)
7636cb21f250f0485ca6052ffadc80ace93e6358lm{
7636cb21f250f0485ca6052ffadc80ace93e6358lm boolean_t has_client = B_FALSE;
7636cb21f250f0485ca6052ffadc80ace93e6358lm
7636cb21f250f0485ca6052ffadc80ace93e6358lm (void) mutex_lock(&consp->lock);
7636cb21f250f0485ca6052ffadc80ace93e6358lm
7636cb21f250f0485ca6052ffadc80ace93e6358lm if (consp->clientpq != NULL)
7636cb21f250f0485ca6052ffadc80ace93e6358lm has_client = B_TRUE;
7636cb21f250f0485ca6052ffadc80ace93e6358lm
7636cb21f250f0485ca6052ffadc80ace93e6358lm (void) mutex_unlock(&consp->lock);
7636cb21f250f0485ca6052ffadc80ace93e6358lm
7636cb21f250f0485ca6052ffadc80ace93e6358lm return (has_client);
7636cb21f250f0485ca6052ffadc80ace93e6358lm}
7636cb21f250f0485ca6052ffadc80ace93e6358lm
7636cb21f250f0485ca6052ffadc80ace93e6358lm/*
7636cb21f250f0485ca6052ffadc80ace93e6358lm * close one opened console.
7636cb21f250f0485ca6052ffadc80ace93e6358lm * This function is passed to vntsd_que_walk to close one console.
7636cb21f250f0485ca6052ffadc80ace93e6358lm * The function returns B_FALSE so that vntsd_que_walk will
7636cb21f250f0485ca6052ffadc80ace93e6358lm * continue to apply the function to all consoles in the group.
7636cb21f250f0485ca6052ffadc80ace93e6358lm */
7636cb21f250f0485ca6052ffadc80ace93e6358lmstatic boolean_t
7636cb21f250f0485ca6052ffadc80ace93e6358lmclose_one_vcc_fd(vntsd_cons_t *consp)
7636cb21f250f0485ca6052ffadc80ace93e6358lm{
7636cb21f250f0485ca6052ffadc80ace93e6358lm (void) mutex_lock(&consp->lock);
7636cb21f250f0485ca6052ffadc80ace93e6358lm
7636cb21f250f0485ca6052ffadc80ace93e6358lm if (consp->vcc_fd != -1) {
7636cb21f250f0485ca6052ffadc80ace93e6358lm (void) close(consp->vcc_fd);
7636cb21f250f0485ca6052ffadc80ace93e6358lm consp->vcc_fd = -1;
7636cb21f250f0485ca6052ffadc80ace93e6358lm }
7636cb21f250f0485ca6052ffadc80ace93e6358lm
7636cb21f250f0485ca6052ffadc80ace93e6358lm (void) mutex_unlock(&consp->lock);
7636cb21f250f0485ca6052ffadc80ace93e6358lm
7636cb21f250f0485ca6052ffadc80ace93e6358lm return (B_FALSE);
7636cb21f250f0485ca6052ffadc80ace93e6358lm}
7636cb21f250f0485ca6052ffadc80ace93e6358lm
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo/* clean up client and exit the thread */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppostatic void
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppoclient_fini(vntsd_group_t *groupp, vntsd_client_t *clientp)
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo{
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo assert(groupp);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo assert(clientp);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /* disconnct client from tcp port */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo assert(clientp->sockfd != -1);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (void) close(clientp->sockfd);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (void) mutex_lock(&groupp->lock);
7636cb21f250f0485ca6052ffadc80ace93e6358lm
7636cb21f250f0485ca6052ffadc80ace93e6358lm /*
7636cb21f250f0485ca6052ffadc80ace93e6358lm * close all consoles in the group if the client is the
7636cb21f250f0485ca6052ffadc80ace93e6358lm * last one connected to the group
7636cb21f250f0485ca6052ffadc80ace93e6358lm */
7636cb21f250f0485ca6052ffadc80ace93e6358lm if (vntsd_que_walk(groupp->conspq, (el_func_t)is_client_que_empty) ==
7636cb21f250f0485ca6052ffadc80ace93e6358lm VNTSD_SUCCESS) {
7636cb21f250f0485ca6052ffadc80ace93e6358lm (void) vntsd_que_walk(groupp->conspq,
7636cb21f250f0485ca6052ffadc80ace93e6358lm (el_func_t)close_one_vcc_fd);
7636cb21f250f0485ca6052ffadc80ace93e6358lm }
7636cb21f250f0485ca6052ffadc80ace93e6358lm
7636cb21f250f0485ca6052ffadc80ace93e6358lm
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (void) vntsd_que_rm(&groupp->no_cons_clientpq, clientp);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo if ((groupp->no_cons_clientpq == NULL) &&
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (groupp->status & VNTSD_GROUP_SIG_WAIT)) {
4d39be2b45b5ac811d28452e6eb629ac64aebfc4sg /*
4d39be2b45b5ac811d28452e6eb629ac64aebfc4sg * group is waiting to be deleted. - signal the group's
4d39be2b45b5ac811d28452e6eb629ac64aebfc4sg * listen thread - the VNTSD_GROUP_SIG_WAIT state will
4d39be2b45b5ac811d28452e6eb629ac64aebfc4sg * be cleared when the listen thread exits.
4d39be2b45b5ac811d28452e6eb629ac64aebfc4sg */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (void) cond_signal(&groupp->cvp);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (void) mutex_unlock(&groupp->lock);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (void) mutex_destroy(&clientp->lock);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo free(clientp);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo thr_exit(0);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo}
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo/* check client's status. exit if client quits or fatal errors */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppostatic void
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppoconsole_chk_status(vntsd_group_t *groupp, vntsd_client_t *clientp, int status)
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo{
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo char err_msg[VNTSD_LINE_LEN];
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo D1(stderr, "t@%d console_chk_status() status=%d "
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo "client status=%x num consoles=%d \n",
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo thr_self(), status, clientp->status, groupp->num_cons);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (void) snprintf(err_msg, VNTSD_LINE_LEN, "console_chk_status client%d"
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo " num_cos=%d", clientp->sockfd, groupp->num_cons);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
4d39be2b45b5ac811d28452e6eb629ac64aebfc4sg /*
4d39be2b45b5ac811d28452e6eb629ac64aebfc4sg * obtain group lock to protect groupp->num_cons.
4d39be2b45b5ac811d28452e6eb629ac64aebfc4sg * When groupp->num_cons == 0, close client and exit the tread.
4d39be2b45b5ac811d28452e6eb629ac64aebfc4sg */
4d39be2b45b5ac811d28452e6eb629ac64aebfc4sg (void) mutex_lock(&groupp->lock);
4d39be2b45b5ac811d28452e6eb629ac64aebfc4sg
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo if (groupp->num_cons == 0) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /* no more console in the group */
4d39be2b45b5ac811d28452e6eb629ac64aebfc4sg (void) mutex_unlock(&groupp->lock);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo client_fini(groupp, clientp);
4d39be2b45b5ac811d28452e6eb629ac64aebfc4sg return;
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo if (status == VNTSD_STATUS_INTR) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /* reason for signal? */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo status = vntsd_cons_chk_intr(clientp);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo switch (status) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo case VNTSD_STATUS_CLIENT_QUIT:
4d39be2b45b5ac811d28452e6eb629ac64aebfc4sg (void) mutex_unlock(&groupp->lock);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo client_fini(groupp, clientp);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo return;
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo case VNTSD_STATUS_RESELECT_CONS:
4d39be2b45b5ac811d28452e6eb629ac64aebfc4sg
4d39be2b45b5ac811d28452e6eb629ac64aebfc4sg if (clientp->cons == NULL) {
4d39be2b45b5ac811d28452e6eb629ac64aebfc4sg /*
4d39be2b45b5ac811d28452e6eb629ac64aebfc4sg * domain was deleted before client connects to it
4d39be2b45b5ac811d28452e6eb629ac64aebfc4sg * connect to other console in the same group
4d39be2b45b5ac811d28452e6eb629ac64aebfc4sg */
4d39be2b45b5ac811d28452e6eb629ac64aebfc4sg (void) mutex_unlock(&groupp->lock);
4d39be2b45b5ac811d28452e6eb629ac64aebfc4sg client_init(clientp);
4d39be2b45b5ac811d28452e6eb629ac64aebfc4sg return;
4d39be2b45b5ac811d28452e6eb629ac64aebfc4sg }
4d39be2b45b5ac811d28452e6eb629ac64aebfc4sg
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo if ((groupp->num_cons == 1) &&
4d39be2b45b5ac811d28452e6eb629ac64aebfc4sg ((clientp->status & VNTSD_CLIENT_CONS_DELETED) ||
4d39be2b45b5ac811d28452e6eb629ac64aebfc4sg (groupp->conspq->handle == clientp->cons))) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /* no other selection available */
4d39be2b45b5ac811d28452e6eb629ac64aebfc4sg (void) mutex_unlock(&groupp->lock);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo client_fini(groupp, clientp);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo } else {
4d39be2b45b5ac811d28452e6eb629ac64aebfc4sg (void) mutex_unlock(&groupp->lock);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo client_init(clientp);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
4d39be2b45b5ac811d28452e6eb629ac64aebfc4sg
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo return;
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo case VNTSD_STATUS_VCC_IO_ERR:
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo if ((clientp->status & VNTSD_CLIENT_CONS_DELETED) == 0) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /* check if console was deleted */
4d39be2b45b5ac811d28452e6eb629ac64aebfc4sg (void) mutex_unlock(&groupp->lock);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo status = vntsd_vcc_err(clientp->cons);
4d39be2b45b5ac811d28452e6eb629ac64aebfc4sg (void) mutex_lock(&groupp->lock);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo if (status != VNTSD_STATUS_CONTINUE) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /* console was deleted */
4d39be2b45b5ac811d28452e6eb629ac64aebfc4sg if (groupp->num_cons <= 1) {
4d39be2b45b5ac811d28452e6eb629ac64aebfc4sg (void) mutex_unlock(&groupp->lock);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo client_fini(groupp, clientp);
4d39be2b45b5ac811d28452e6eb629ac64aebfc4sg return;
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
4d39be2b45b5ac811d28452e6eb629ac64aebfc4sg (void) mutex_unlock(&groupp->lock);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /* console is ok */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo client_init(clientp);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo return;
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo case VNTSD_STATUS_MOV_CONS_FORWARD:
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo case VNTSD_STATUS_MOV_CONS_BACKWARD:
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo if (groupp->num_cons == 1) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /* same console */
4d39be2b45b5ac811d28452e6eb629ac64aebfc4sg (void) mutex_unlock(&groupp->lock);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo return;
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /* get selected console */
4d39be2b45b5ac811d28452e6eb629ac64aebfc4sg clientp->cons = vntsd_que_pos(groupp->conspq,
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo clientp->cons,
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (status == VNTSD_STATUS_MOV_CONS_FORWARD)?(1):(-1));
4d39be2b45b5ac811d28452e6eb629ac64aebfc4sg (void) mutex_unlock(&groupp->lock);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo return;
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo case VNTSD_SUCCESS:
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo case VNTSD_STATUS_CONTINUE:
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo case VNTSD_STATUS_NO_CONS:
4d39be2b45b5ac811d28452e6eb629ac64aebfc4sg (void) mutex_unlock(&groupp->lock);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo client_init(clientp);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo return;
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo case VNTSD_ERR_INVALID_INPUT:
4d39be2b45b5ac811d28452e6eb629ac64aebfc4sg (void) mutex_unlock(&groupp->lock);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo return;
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo default:
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /* fatal error */
4d39be2b45b5ac811d28452e6eb629ac64aebfc4sg (void) mutex_unlock(&groupp->lock);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo vntsd_log(status, err_msg);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo client_fini(groupp, clientp);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo return;
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo}
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo/* console thread */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppovoid *
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppovntsd_console_thread(vntsd_thr_arg_t *argp)
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo{
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo vntsd_group_t *groupp;
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo vntsd_cons_t *consp;
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo vntsd_client_t *clientp;
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo char buf[MAXHOSTNAMELEN];
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo char prompt[72];
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo char cmd;
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo int rv = VNTSD_SUCCESS;
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo int num_cons;
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo groupp = (vntsd_group_t *)argp->handle;
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo clientp = (vntsd_client_t *)argp->arg;
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo assert(groupp);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo assert(clientp);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /* check if group is removed */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo D1(stderr, "t@%d get_client_sel@%lld:client@%d\n", thr_self(),
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo groupp->tcp_port, clientp->sockfd);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo bzero(buf, MAXHOSTNAMELEN);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /* host name */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo if (gethostname(buf, MAXHOSTNAMELEN)) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo vntsd_log(VNTSD_STATUS_NO_HOST_NAME, "vntsd_console_thread()");
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (void) snprintf(buf, sizeof (buf), "unkown host");
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo if (snprintf(prompt, sizeof (prompt),
8e6a2a040587479821d1e682a28bcef7e75f19a6lm "%s-vnts-%s: h, l, c{id}, n{name}, q:",
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo buf, groupp->group_name) >= sizeof (prompt)) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /* long prompt doesn't fit, use short one */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (void) snprintf(prompt, sizeof (prompt),
8e6a2a040587479821d1e682a28bcef7e75f19a6lm "vnts: h, l, c{id}, n{name}, q:");
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo for (;;) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo cmd = ' ';
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo D1(stderr, "t@%d console_thread()@%lld:client@%d\n", thr_self(),
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo groupp->tcp_port, clientp->sockfd);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo num_cons = vntsd_chk_group_total_cons(groupp);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo if ((num_cons > 1) && (clientp->cons == NULL)) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /* console to connect to */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo rv = read_cmd(clientp, prompt, &cmd);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /* check error and may exit */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo console_chk_status(groupp, clientp, rv);
4d39be2b45b5ac811d28452e6eb629ac64aebfc4sg
4d39be2b45b5ac811d28452e6eb629ac64aebfc4sg /* any console is removed from group? */
4d39be2b45b5ac811d28452e6eb629ac64aebfc4sg num_cons = vntsd_chk_group_total_cons(groupp);
4d39be2b45b5ac811d28452e6eb629ac64aebfc4sg if (num_cons <= 1) {
4d39be2b45b5ac811d28452e6eb629ac64aebfc4sg cmd = ' ';
4d39be2b45b5ac811d28452e6eb629ac64aebfc4sg }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo switch (cmd) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo case 'l':
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /* list domain names */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo rv = list_all_domains(groupp, clientp);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo break;
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo case 'q':
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo rv = VNTSD_STATUS_CLIENT_QUIT;
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo break;
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
8e6a2a040587479821d1e682a28bcef7e75f19a6lm case ' ':
8e6a2a040587479821d1e682a28bcef7e75f19a6lm
4d39be2b45b5ac811d28452e6eb629ac64aebfc4sg if (num_cons == 0)
4d39be2b45b5ac811d28452e6eb629ac64aebfc4sg /* no console in the group */
4d39be2b45b5ac811d28452e6eb629ac64aebfc4sg break;
4d39be2b45b5ac811d28452e6eb629ac64aebfc4sg
8e6a2a040587479821d1e682a28bcef7e75f19a6lm if (clientp->cons == NULL) {
8e6a2a040587479821d1e682a28bcef7e75f19a6lm if (num_cons == 1) {
8e6a2a040587479821d1e682a28bcef7e75f19a6lm /* by pass selecting console */
8e6a2a040587479821d1e682a28bcef7e75f19a6lm consp = (vntsd_cons_t *)
8e6a2a040587479821d1e682a28bcef7e75f19a6lm (groupp->conspq->handle);
8e6a2a040587479821d1e682a28bcef7e75f19a6lm } else {
8e6a2a040587479821d1e682a28bcef7e75f19a6lm continue;
8e6a2a040587479821d1e682a28bcef7e75f19a6lm }
8e6a2a040587479821d1e682a28bcef7e75f19a6lm
8e6a2a040587479821d1e682a28bcef7e75f19a6lm } else {
8e6a2a040587479821d1e682a28bcef7e75f19a6lm consp = clientp->cons;
8e6a2a040587479821d1e682a28bcef7e75f19a6lm }
8e6a2a040587479821d1e682a28bcef7e75f19a6lm
8e6a2a040587479821d1e682a28bcef7e75f19a6lm /* connect to console */
8e6a2a040587479821d1e682a28bcef7e75f19a6lm rv = connect_cons(consp, clientp);
8e6a2a040587479821d1e682a28bcef7e75f19a6lm
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo break;
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
8e6a2a040587479821d1e682a28bcef7e75f19a6lm case 'c':
8e6a2a040587479821d1e682a28bcef7e75f19a6lm case 'n':
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /* select console */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo if (clientp->cons == NULL) {
8e6a2a040587479821d1e682a28bcef7e75f19a6lm rv = select_cons(groupp, &consp, clientp, cmd);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo if (rv == VNTSD_ERR_INVALID_INPUT) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo rv = display_help(clientp);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo break;
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo } else {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo consp = clientp->cons;
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo assert(consp);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /* connect to console */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo rv = connect_cons(consp, clientp);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo D1(stderr, "t@%d console_thread()"
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo "connect_cons returns %d\n",
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo thr_self(), rv);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo break;
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
8e6a2a040587479821d1e682a28bcef7e75f19a6lm case 'h':
8e6a2a040587479821d1e682a28bcef7e75f19a6lm default:
8e6a2a040587479821d1e682a28bcef7e75f19a6lm rv = display_help(clientp);
8e6a2a040587479821d1e682a28bcef7e75f19a6lm break;
8e6a2a040587479821d1e682a28bcef7e75f19a6lm
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
8e6a2a040587479821d1e682a28bcef7e75f19a6lm
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /* check error and may exit */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo console_chk_status(groupp, clientp, rv);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /*NOTREACHED*/
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo return (NULL);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo}