summaryrefslogtreecommitdiff
path: root/usr/src/cmd/vntsd/write.c
diff options
context:
space:
mode:
Diffstat (limited to 'usr/src/cmd/vntsd/write.c')
-rw-r--r--usr/src/cmd/vntsd/write.c251
1 files changed, 251 insertions, 0 deletions
diff --git a/usr/src/cmd/vntsd/write.c b/usr/src/cmd/vntsd/write.c
new file mode 100644
index 0000000000..16f07029c5
--- /dev/null
+++ b/usr/src/cmd/vntsd/write.c
@@ -0,0 +1,251 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License (the "License").
+ * You may not use this file except in compliance with the License.
+ *
+ * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+ * or http://www.opensolaris.org/os/licensing.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+ * If applicable, add the following below this CDDL HEADER, with the
+ * fields enclosed by brackets "[]" replaced with your own identifying
+ * information: Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ */
+/*
+ * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
+ * Use is subject to license terms.
+ */
+#pragma ident "%Z%%M% %I% %E% SMI"
+
+/*
+ * write thread - read from vcc console and write to tcp client. There are one
+ * writer and multiple readers per console. The first client who connects to
+ * a console get write access.
+ * Writer thread writes vcc data to all tcp clients that connected to
+ * the console.
+ */
+
+#include <stdio.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <thread.h>
+#include <synch.h>
+#include <signal.h>
+#include <assert.h>
+#include <poll.h>
+#include <syslog.h>
+#include <libintl.h>
+#include "vntsd.h"
+#include "chars.h"
+
+/*
+ * check the state of write thread. exit if no more client connects to the
+ * console.
+ */
+static void
+write_chk_status(vntsd_cons_t *consp, int status)
+{
+
+ if ((consp->status & VNTSD_CONS_DELETED) || (consp->clientpq == NULL)) {
+ thr_exit(0);
+ }
+
+ switch (status) {
+ case VNTSD_STATUS_VCC_IO_ERR:
+ assert(consp->group != NULL);
+ if (vntsd_vcc_err(consp) != VNTSD_STATUS_CONTINUE) {
+ thr_exit(0);
+ }
+ break;
+ case VNTSD_STATUS_INTR:
+ thr_exit(0);
+ default:
+ break;
+
+ }
+}
+
+/*
+ * skip_terminal_null()
+ * scan terminal null character sequence (0x5e 0x40)
+ * return number of characters in the buf after skipping terminal null
+ * sequence.
+ */
+static int
+skip_terminal_null(char *buf, int buf_sz, int sz)
+{
+ int i, j;
+ static int term_null_seq = 0;
+
+ assert(sz >= 0);
+
+ if (buf_sz < sz+1) {
+ return (-1);
+ }
+
+ if (term_null_seq) {
+ /* skip 0x5e previously */
+ term_null_seq = 0;
+
+ if (buf[0] != 0x40) {
+ /* not terminal null sequence put 0x5e back */
+ for (i = sz; i > 0; i--) {
+ buf[i] = buf[i-1];
+ }
+
+ buf[0] = 0x5e;
+
+ sz++;
+ } else {
+ /* skip terminal null sequence */
+ sz--;
+
+ if (sz == 0) {
+ return (sz);
+ }
+
+ for (i = 0; i < sz; i++) {
+ buf[i] = buf[i+1];
+ }
+ }
+ }
+
+ for (; ; ) {
+ for (i = 0; i < sz; i++) {
+ if (buf[i] == '\0') {
+ return (i);
+ }
+
+ if (buf[i] == 0x5e) {
+ /* possible terminal null sequence */
+ if (i == sz -1) {
+ /* last character in buffer */
+ term_null_seq = 1;
+ sz--;
+ buf[i] = 0;
+ return (sz);
+ }
+
+ if (buf[i+1] == 0x40) {
+ /* found terminal null sequence */
+ sz -= 2;
+ for (j = i; j < sz -i; j++) {
+ buf[j] = buf[j+2];
+ }
+ break;
+ }
+
+ if (buf[i+1] == '\0') {
+ buf[i] = 0;
+ term_null_seq = 1;
+ return (i);
+ }
+
+ }
+ }
+
+ if (i == sz) {
+ /* end of scan */
+ return (sz);
+ }
+ }
+}
+
+/* read data from vcc */
+static int
+read_vcc(vntsd_cons_t *consp, char *buf, ssize_t *sz)
+{
+ /* read from vcc */
+ *sz = read(consp->vcc_fd, buf, VNTSD_MAX_BUF_SIZE);
+
+ if (errno == EINTR) {
+ return (VNTSD_STATUS_INTR);
+ }
+
+ if ((*sz > 0)) {
+ return (VNTSD_SUCCESS);
+ }
+ return (VNTSD_STATUS_VCC_IO_ERR);
+}
+
+static int s_sz;
+/* write to a client */
+static boolean_t
+write_all_clients(vntsd_client_t *clientp, char *buf)
+{
+ int rv;
+
+ rv = vntsd_write_client(clientp, buf, s_sz);
+ if (rv != VNTSD_SUCCESS) {
+ (void) mutex_lock(&clientp->lock);
+ clientp->status |= VNTSD_CLIENT_IO_ERR;
+ assert(clientp->cons);
+ (void) thr_kill(clientp->cons_tid, NULL);
+ (void) mutex_unlock(&clientp->lock);
+ }
+ return (B_FALSE);
+
+}
+
+/* vntsd_write_thread() */
+void*
+vntsd_write_thread(vntsd_cons_t *consp)
+{
+ char buf[VNTSD_MAX_BUF_SIZE+1];
+ int sz;
+ int rv;
+
+ D1(stderr, "t@%d vntsd_write@%d\n", thr_self(), consp->vcc_fd);
+
+ assert(consp);
+ write_chk_status(consp, VNTSD_SUCCESS);
+
+ for (; ; ) {
+ bzero(buf, VNTSD_MAX_BUF_SIZE +1);
+
+ /* read data */
+ rv = read_vcc(consp, buf, &sz);
+
+ write_chk_status(consp, rv);
+
+ if (sz <= 0) {
+ continue;
+ }
+
+ /* has data */
+ if ((s_sz = skip_terminal_null(buf, sz+1, sz)) == 0) {
+ /* terminal null sequence */
+ continue;
+ }
+
+ assert(s_sz > 0);
+
+ /*
+ * output data to all clients connected
+ * to this console
+ */
+
+ (void) mutex_lock(&consp->lock);
+ (void) vntsd_que_find(consp->clientpq,
+ (compare_func_t)write_all_clients, buf);
+ (void) mutex_unlock(&consp->lock);
+
+ write_chk_status(consp, VNTSD_SUCCESS);
+
+ }
+
+ /*NOTREACHED*/
+ return (NULL);
+}