console.c revision 1ae0874509b6811fdde1dfd46f0d93fd09867a3f
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 */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo 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
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo bufp = gettext("[c[c ]]{id} -- connect to console of domain {id}");
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo if ((rv = vntsd_write_line(clientp, bufp)) != VNTSD_SUCCESS) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo return (rv);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo return (VNTSD_SUCCESS);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo}
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo/* select a console to connect */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppostatic int
1ae0874509b6811fdde1dfd46f0d93fd09867a3fhepposelect_cons(vntsd_group_t *groupp, int num_cons, vntsd_cons_t **consp,
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo vntsd_client_t *clientp, char c)
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo{
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo int cons_no = -2;
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 if (num_cons == 1) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /* by pass selecting console */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo *consp = (vntsd_cons_t *)(groupp->conspq->handle);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo return (VNTSD_SUCCESS);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo if (isdigit(c)) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /* {id} input */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo cons_no = c - '0';
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo } else if (c == 'c') {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /* c{id} or c {id} input */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo cons_no = -1;
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo } else if (!isspace(c)) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo return (VNTSD_ERR_INVALID_INPUT);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /* get client selections */
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++) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo if (cons_no == -1) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /* c{id} */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo cons_no = atoi(buf + i);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo break;
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo if (isspace(buf[i]) && cons_no == -2) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /* skip space */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo continue;
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo if (buf[i] == 'c') {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /* c{id} or c {id} */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo cons_no = -1;
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo } else if (buf[i] == CR) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo break;
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo } else {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo return (VNTSD_ERR_INVALID_INPUT);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
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
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo if (consp->clientpq == NULL) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /* first connect to console - a writer */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo assert(consp->vcc_fd == -1);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /* open vcc */
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 /* close vcc port */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (void) close(consp->vcc_fd);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo 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}
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);
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)) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /* group is waiting to be deleted */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo groupp->status &= ~VNTSD_GROUP_SIG_WAIT;
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
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo if (groupp->num_cons == 0) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /* no more console in the group */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo client_fini(groupp, clientp);
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:
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo client_fini(groupp, clientp);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo return;
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo case VNTSD_STATUS_RESELECT_CONS:
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo assert(clientp->cons);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo if ((groupp->num_cons == 1) &&
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (groupp->conspq->handle == clientp->cons)) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /* no other selection available */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo client_fini(groupp, clientp);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo } else {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo client_init(clientp);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
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 */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo status = vntsd_vcc_err(clientp->cons);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo if (status != VNTSD_STATUS_CONTINUE) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /* console was deleted */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo if (groupp->num_cons == 1) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo client_fini(groupp, clientp);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
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 */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo return;
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /* get selected console */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (void) mutex_lock(&(clientp->cons->group->lock));
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo clientp->cons = vntsd_que_pos(clientp->cons->group->conspq,
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo clientp->cons,
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (status == VNTSD_STATUS_MOV_CONS_FORWARD)?(1):(-1));
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (void) mutex_unlock(&(clientp->cons->group->lock));
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo return;
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo case VNTSD_SUCCESS:
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo case VNTSD_STATUS_CONTINUE:
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo case VNTSD_STATUS_NO_CONS:
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo client_init(clientp);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo return;
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo case VNTSD_ERR_INVALID_INPUT:
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo return;
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo default:
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /* fatal error */
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),
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo "%s-vnts-%s: h,l,{id},c{id},c {id},q:",
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo buf, groupp->group_name) >= sizeof (prompt)) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /* long prompt doesn't fit, use short one */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo (void) snprintf(prompt, sizeof (prompt),
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo "vnts: h,l,{id},c{id},c {id}, 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);
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
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo case 'h':
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo rv = display_help(clientp);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo break;
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo default:
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /* select console */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo if (clientp->cons == NULL) {
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo rv = select_cons(groupp, num_cons,
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo &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
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /* check error and may exit */
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo console_chk_status(groupp, clientp, rv);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo }
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo /*NOTREACHED*/
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo return (NULL);
1ae0874509b6811fdde1dfd46f0d93fd09867a3fheppo}