read.c revision 823fe29b97090ae4962ebcc65cdaf0a757eb4851
2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A/*
2N/A * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A/*
2N/A * read thread - Read from tcp client and write to vcc driver. There are one
2N/A * writer and multiple readers per console. The first client who connects to
2N/A * a console get write access. An error message is returned to readers if they
2N/A * attemp to input commands. Read thread accepts special daemon commands from
2N/A * all clients.
2N/A */
2N/A
2N/A#include <stdio.h>
2N/A#include <stdlib.h>
2N/A#include <string.h>
2N/A#include <unistd.h>
2N/A#include <sys/types.h>
2N/A#include <sys/socket.h>
2N/A#include <netinet/in.h>
2N/A#include <thread.h>
2N/A#include <synch.h>
2N/A#include <signal.h>
2N/A#include <assert.h>
2N/A#include <ctype.h>
2N/A#include <syslog.h>
2N/A#include <libintl.h>
2N/A#include "vntsd.h"
2N/A#include "chars.h"
2N/A
2N/A/* write_vcc() - write to vcc virtual console */
2N/Astatic int
2N/Awrite_vcc(vntsd_client_t *clientp, char c)
2N/A{
2N/A int n;
2N/A
2N/A
2N/A assert(clientp);
2N/A assert(clientp->cons);
2N/A
2N/A n = write(clientp->cons->vcc_fd, &c, 1);
2N/A
2N/A if (n < 0) {
2N/A /* write error */
2N/A if (errno == EINTR) {
2N/A return (vntsd_cons_chk_intr(clientp));
2N/A }
2N/A
2N/A return (VNTSD_STATUS_VCC_IO_ERR);
2N/A }
2N/A
2N/A assert(n != 0);
2N/A return (VNTSD_SUCCESS);
2N/A
2N/A}
2N/A
2N/A/*
2N/A * acquire_writer() the client is going to be writer.
2N/A * insert the client to the head of the console client queue.
2N/A */
2N/Astatic int
2N/Aacquire_writer(vntsd_client_t *clientp)
2N/A{
2N/A vntsd_cons_t *consp;
2N/A vntsd_client_t *writerp;
2N/A int rv;
2N/A
2N/A D1(stderr, "t@%d:acuire_writer :client@%d\n", thr_self(),
2N/A clientp->sockfd);
2N/A
2N/A assert(clientp != NULL);
2N/A consp = clientp->cons;
2N/A
2N/A assert(consp);
2N/A
2N/A (void) mutex_lock(&consp->lock);
2N/A
2N/A assert(consp->clientpq != NULL);
2N/A if (consp->clientpq->handle == clientp) {
2N/A /* clientp is a writer already */
2N/A (void) mutex_unlock(&consp->lock);
2N/A return (VNTSD_SUCCESS);
2N/A }
2N/A
2N/A /* current writer */
2N/A writerp = (vntsd_client_t *)(consp->clientpq->handle);
2N/A
2N/A (void) mutex_lock(&writerp->lock);
2N/A
2N/A rv = vntsd_que_rm(&(consp->clientpq), clientp);
2N/A assert(rv == VNTSD_SUCCESS);
2N/A
2N/A (void) mutex_lock(&clientp->lock);
2N/A
2N/A /* move client to be first in the console queue */
2N/A consp->clientpq->handle = clientp;
2N/A
2N/A /* move previous writer to be the second in the queue */
2N/A rv = vntsd_que_insert_after(consp->clientpq, clientp, writerp);
2N/A
2N/A (void) mutex_unlock(&consp->lock);
2N/A (void) mutex_unlock(&writerp->lock);
2N/A (void) mutex_unlock(&clientp->lock);
2N/A
2N/A if (rv != VNTSD_SUCCESS) {
2N/A return (rv);
2N/A }
2N/A
2N/A /* write warning message to the writer */
2N/A
2N/A if ((rv = vntsd_write_line(writerp,
2N/A gettext("Warning: Console connection forced into read-only mode")))
2N/A != VNTSD_SUCCESS) {
2N/A return (rv);
2N/A }
2N/A
2N/A return (VNTSD_SUCCESS);
2N/A}
2N/A
2N/A/* interrupt handler */
int
vntsd_cons_chk_intr(vntsd_client_t *clientp)
{
if (clientp->status & VNTSD_CLIENT_TIMEOUT) {
return (VNTSD_STATUS_CLIENT_QUIT);
}
if (clientp->status & VNTSD_CLIENT_CONS_DELETED) {
return (VNTSD_STATUS_RESELECT_CONS);
}
if (clientp->status & VNTSD_CLIENT_IO_ERR) {
return (VNTSD_STATUS_CLIENT_QUIT);
}
return (VNTSD_STATUS_CONTINUE);
}
/* read from client */
static int
read_char(vntsd_client_t *clientp, char *c)
{
int rv;
for (; ; ) {
rv = vntsd_read_data(clientp, c);
switch (rv) {
case VNTSD_STATUS_ACQUIRE_WRITER:
clientp->prev_char = 0;
rv = acquire_writer(clientp);
if (rv != VNTSD_SUCCESS) {
return (rv);
}
break;
case VNTSD_SUCCESS:
/*
* Based on telnet protocol, when an <eol> is entered,
* vntsd receives <0x0d,0x00>. However, console expects
* <0xd> only. We need to filter out <0x00>.
*/
if (clientp->prev_char == 0xd && *c == 0) {
clientp->prev_char = *c;
break;
}
clientp->prev_char = *c;
return (rv);
default:
assert(rv != VNTSD_STATUS_CONTINUE);
clientp->prev_char = 0;
return (rv);
}
}
}
/* vntsd_read worker */
int
vntsd_read(vntsd_client_t *clientp)
{
char c;
int rv;
assert(clientp);
D3(stderr, "t@%d vntsd_read@%d\n", thr_self(), clientp->sockfd);
for (; ; ) {
/* client input */
rv = read_char(clientp, &c);
if (rv == VNTSD_STATUS_INTR) {
rv = vntsd_cons_chk_intr(clientp);
}
if (rv != VNTSD_SUCCESS) {
return (rv);
}
assert(clientp->cons);
if (clientp->cons->clientpq->handle != clientp) {
/* reader - print error message */
if ((c != CR) && (c != LF)) {
rv = vntsd_write_line(clientp,
gettext(VNTSD_NO_WRITE_ACCESS_MSG));
/* check errors and may exit */
if (rv == VNTSD_STATUS_INTR) {
rv = vntsd_cons_chk_intr(clientp);
}
if (rv != VNTSD_SUCCESS) {
return (rv);
}
}
continue;
}
rv = vntsd_ctrl_cmd(clientp, c);
switch (rv) {
case VNTSD_STATUS_CONTINUE:
continue;
break;
case VNTSD_STATUS_INTR:
rv = vntsd_cons_chk_intr(clientp);
if (rv != VNTSD_SUCCESS) {
return (rv);
}
break;
case VNTSD_SUCCESS:
break;
default:
return (rv);
}
/* write to vcc */
rv = write_vcc(clientp, c);
if (rv == VNTSD_STATUS_INTR) {
rv = vntsd_cons_chk_intr(clientp);
}
if (rv != VNTSD_SUCCESS) {
return (rv);
}
}
/*NOTREACHED*/
return (NULL);
}