console.c 5.8 KB
Newer Older
1 2 3
/*
 * console.c: A dumb serial console client
 *
4
 * Copyright (C) 2007, 2008 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 *
 * Daniel Berrange <berrange@redhat.com>
 */

23
#include <config.h>
24

25 26
#ifndef __MINGW32__

27 28 29 30 31 32 33 34 35 36 37 38 39
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <poll.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>

#include "console.h"
#include "internal.h"
40
#include "logging.h"
D
Daniel P. Berrange 已提交
41
#include "util.h"
42 43 44 45 46 47 48 49 50

/* ie  Ctrl-]  as per telnet */
#define CTRL_CLOSE_BRACKET '\35'

static int got_signal = 0;
static void do_signal(int sig ATTRIBUTE_UNUSED) {
    got_signal = 1;
}

51 52 53 54 55 56 57 58 59 60 61 62 63
#ifndef HAVE_CFMAKERAW
static void
cfmakeraw (struct termios *attr)
{
    attr->c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP
                         | INLCR | IGNCR | ICRNL | IXON);
    attr->c_oflag &= ~OPOST;
    attr->c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
    attr->c_cflag &= ~(CSIZE | PARENB);
    attr->c_cflag |= CS8;
}
#endif /* !HAVE_CFMAKERAW */

64
int vshRunConsole(const char *tty) {
65 66 67 68 69 70 71 72 73 74 75
    int ttyfd, ret = -1;
    struct termios ttyattr, rawattr;
    void (*old_sigquit)(int);
    void (*old_sigterm)(int);
    void (*old_sigint)(int);
    void (*old_sighup)(int);
    void (*old_sigpipe)(int);


    /* We do not want this to become the controlling TTY */
    if ((ttyfd = open(tty, O_NOCTTY | O_RDWR)) < 0) {
76 77
        VIR_ERROR(_("unable to open tty %s: %s\n"),
                  tty, strerror(errno));
78 79 80
        return -1;
    }

81
    /* Put STDIN into raw mode so that stuff typed
82 83 84 85 86
       does not echo to the screen (the TTY reads will
       result in it being echoed back already), and
       also ensure Ctrl-C, etc is blocked, and misc
       other bits */
    if (tcgetattr(STDIN_FILENO, &ttyattr) < 0) {
87 88
        VIR_ERROR(_("unable to get tty attributes: %s\n"),
                  strerror(errno));
89 90 91 92 93 94 95
        goto closetty;
    }

    rawattr = ttyattr;
    cfmakeraw(&rawattr);

    if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &rawattr) < 0) {
96 97
        VIR_ERROR(_("unable to set tty attributes: %s\n"),
                  strerror(errno));
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
        goto closetty;
    }


    /* Trap all common signals so that we can safely restore
       the original terminal settings on STDIN before the
       process exits - people don't like being left with a
       messed up terminal ! */
    old_sigquit = signal(SIGQUIT, do_signal);
    old_sigterm = signal(SIGTERM, do_signal);
    old_sigint = signal(SIGINT, do_signal);
    old_sighup = signal(SIGHUP, do_signal);
    old_sigpipe = signal(SIGPIPE, do_signal);
    got_signal = 0;


    /* Now lets process STDIN & tty forever.... */
    for (; !got_signal ;) {
        unsigned int i;
        struct pollfd fds[] = {
            { STDIN_FILENO, POLLIN, 0 },
            { ttyfd, POLLIN, 0 },
        };

        /* Wait for data to be available for reading on
           STDIN or the tty */
        if (poll(fds, (sizeof(fds)/sizeof(struct pollfd)), -1) < 0) {
            if (got_signal)
                goto cleanup;

            if (errno == EINTR || errno == EAGAIN)
                continue;

131
            VIR_ERROR(_("failure waiting for I/O: %s\n"), strerror(errno));
132 133 134 135 136 137 138 139 140 141 142 143 144
            goto cleanup;
        }

        for (i = 0 ; i < (sizeof(fds)/sizeof(struct pollfd)) ; i++) {
            if (!fds[i].revents)
                continue;

            /* Process incoming data available for read */
            if (fds[i].revents & POLLIN) {
                char buf[4096];
                int got, sent = 0, destfd;

                if ((got = read(fds[i].fd, buf, sizeof(buf))) < 0) {
145 146
                    VIR_ERROR(_("failure reading input: %s\n"),
                              strerror(errno));
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
                    goto cleanup;
                }

                /* Quit if end of file, or we got the Ctrl-] key */
                if (!got ||
                    (got == 1 &&
                     buf[0] == CTRL_CLOSE_BRACKET))
                    goto done;

                /* Data from stdin goes to the TTY,
                   data from the TTY goes to STDOUT */
                if (fds[i].fd == STDIN_FILENO)
                    destfd = ttyfd;
                else
                    destfd = STDOUT_FILENO;

                while (sent < got) {
                    int done;
165 166
                    if ((done = safewrite(destfd, buf + sent, got - sent))
                        <= 0) {
167 168
                        VIR_ERROR(_("failure writing output: %s\n"),
                                  strerror(errno));
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
                        goto cleanup;
                    }
                    sent += done;
                }
            } else { /* Any other flag from poll is an error condition */
                goto cleanup;
            }
        }
    }
 done:
    ret = 0;

 cleanup:

    /* Restore original signal handlers */
    signal(SIGQUIT, old_sigpipe);
    signal(SIGQUIT, old_sighup);
    signal(SIGQUIT, old_sigint);
    signal(SIGQUIT, old_sigterm);
    signal(SIGQUIT, old_sigquit);

    /* Put STDIN back into the (sane?) state we found
       it in before starting */
    tcsetattr(STDIN_FILENO, TCSAFLUSH, &ttyattr);

 closetty:
    close(ttyfd);

    return ret;
}

200
#endif /* !__MINGW32__ */