console.c 10.9 KB
Newer Older
1 2 3
/*
 * console.c: A dumb serial console client
 *
4
 * Copyright (C) 2007, 2008, 2010 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
#ifndef WIN32
26

27 28 29 30 31 32 33 34 35 36
# 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>
37

38
# include "internal.h"
39
# include "console.h"
40 41
# include "logging.h"
# include "util.h"
42
# include "files.h"
43 44 45
# include "memory.h"
# include "virterror_internal.h"

46
# include "event.h"
47 48

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

51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
# define VIR_FROM_THIS VIR_FROM_NONE

struct virConsoleBuffer {
    size_t length;
    size_t offset;
    char *data;
};

typedef struct virConsole virConsole;
typedef virConsole *virConsolePtr;
struct virConsole {
    virStreamPtr st;
    bool quit;

    int stdinWatch;
    int stdoutWatch;

    struct virConsoleBuffer streamToTerminal;
    struct virConsoleBuffer terminalToStream;
};

72 73 74 75 76
static int got_signal = 0;
static void do_signal(int sig ATTRIBUTE_UNUSED) {
    got_signal = 1;
}

77
# ifndef HAVE_CFMAKERAW
78 79 80 81 82 83 84 85 86 87
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;
}
88
# endif /* !HAVE_CFMAKERAW */
89

90 91 92 93 94 95
static void
virConsoleShutdown(virConsolePtr con)
{
    con->quit = true;
    virStreamEventRemoveCallback(con->st);
    if (con->stdinWatch != -1)
96
        virEventRemoveHandle(con->stdinWatch);
97
    if (con->stdinWatch != -1)
98
        virEventRemoveHandle(con->stdoutWatch);
99 100 101 102
    con->stdinWatch = -1;
    con->stdoutWatch = -1;
}

103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
static void
virConsoleEventOnStream(virStreamPtr st,
                        int events, void *opaque)
{
    virConsolePtr con = opaque;

    if (events & VIR_STREAM_EVENT_READABLE) {
        size_t avail = con->streamToTerminal.length -
            con->streamToTerminal.offset;
        int got;

        if (avail < 1024) {
            if (VIR_REALLOC_N(con->streamToTerminal.data,
                              con->streamToTerminal.length + 1024) < 0) {
                virReportOOMError();
118
                virConsoleShutdown(con);
119 120 121 122 123 124 125 126 127 128 129 130 131
                return;
            }
            con->streamToTerminal.length += 1024;
            avail += 1024;
        }

        got = virStreamRecv(st,
                            con->streamToTerminal.data +
                            con->streamToTerminal.offset,
                            avail);
        if (got == -2)
            return; /* blocking */
        if (got <= 0) {
132
            virConsoleShutdown(con);
133 134 135 136
            return;
        }
        con->streamToTerminal.offset += got;
        if (con->streamToTerminal.offset)
137 138
            virEventUpdateHandle(con->stdoutWatch,
                                 VIR_EVENT_HANDLE_WRITABLE);
139 140 141 142 143 144 145 146 147 148 149 150
    }

    if (events & VIR_STREAM_EVENT_WRITABLE &&
        con->terminalToStream.offset) {
        ssize_t done;
        size_t avail;
        done = virStreamSend(con->st,
                             con->terminalToStream.data,
                             con->terminalToStream.offset);
        if (done == -2)
            return; /* blocking */
        if (done < 0) {
151
            virConsoleShutdown(con);
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
            return;
        }
        memmove(con->terminalToStream.data,
                con->terminalToStream.data + done,
                con->terminalToStream.offset - done);
        con->terminalToStream.offset -= done;

        avail = con->terminalToStream.length - con->terminalToStream.offset;
        if (avail > 1024) {
            if (VIR_REALLOC_N(con->terminalToStream.data,
                              con->terminalToStream.offset + 1024) < 0)
            {}
            con->terminalToStream.length = con->terminalToStream.offset + 1024;
        }
    }
    if (!con->terminalToStream.offset)
        virStreamEventUpdateCallback(con->st,
                                     VIR_STREAM_EVENT_READABLE);

    if (events & VIR_STREAM_EVENT_ERROR ||
        events & VIR_STREAM_EVENT_HANGUP) {
173
        virConsoleShutdown(con);
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
    }
}

static void
virConsoleEventOnStdin(int watch ATTRIBUTE_UNUSED,
                       int fd ATTRIBUTE_UNUSED,
                       int events,
                       void *opaque)
{
    virConsolePtr con = opaque;

    if (events & VIR_EVENT_HANDLE_READABLE) {
        size_t avail = con->terminalToStream.length -
            con->terminalToStream.offset;
        int got;

        if (avail < 1024) {
            if (VIR_REALLOC_N(con->terminalToStream.data,
                              con->terminalToStream.length + 1024) < 0) {
                virReportOOMError();
194
                virConsoleShutdown(con);
195 196 197 198 199 200 201 202 203 204 205 206
                return;
            }
            con->terminalToStream.length += 1024;
            avail += 1024;
        }

        got = read(fd,
                   con->terminalToStream.data +
                   con->terminalToStream.offset,
                   avail);
        if (got < 0) {
            if (errno != EAGAIN) {
207
                virConsoleShutdown(con);
208 209 210 211
            }
            return;
        }
        if (got == 0) {
212
            virConsoleShutdown(con);
213 214 215
            return;
        }
        if (con->terminalToStream.data[con->terminalToStream.offset] == CTRL_CLOSE_BRACKET) {
216
            virConsoleShutdown(con);
217 218 219 220 221 222 223 224 225 226 227 228
            return;
        }

        con->terminalToStream.offset += got;
        if (con->terminalToStream.offset)
            virStreamEventUpdateCallback(con->st,
                                         VIR_STREAM_EVENT_READABLE |
                                         VIR_STREAM_EVENT_WRITABLE);
    }

    if (events & VIR_EVENT_HANDLE_ERROR ||
        events & VIR_EVENT_HANDLE_HANGUP) {
229
        virConsoleShutdown(con);
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
    }
}

static void
virConsoleEventOnStdout(int watch ATTRIBUTE_UNUSED,
                        int fd,
                        int events,
                        void *opaque)
{
    virConsolePtr con = opaque;

    if (events & VIR_EVENT_HANDLE_WRITABLE &&
        con->streamToTerminal.offset) {
        ssize_t done;
        size_t avail;
        done = write(fd,
                     con->streamToTerminal.data,
                     con->streamToTerminal.offset);
        if (done < 0) {
            if (errno != EAGAIN) {
250
                virConsoleShutdown(con);
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
            }
            return;
        }
        memmove(con->streamToTerminal.data,
                con->streamToTerminal.data + done,
                con->streamToTerminal.offset - done);
        con->streamToTerminal.offset -= done;

        avail = con->streamToTerminal.length - con->streamToTerminal.offset;
        if (avail > 1024) {
            if (VIR_REALLOC_N(con->streamToTerminal.data,
                              con->streamToTerminal.offset + 1024) < 0)
            {}
            con->streamToTerminal.length = con->streamToTerminal.offset + 1024;
        }
    }

    if (!con->streamToTerminal.offset)
269
        virEventUpdateHandle(con->stdoutWatch, 0);
270 271 272

    if (events & VIR_EVENT_HANDLE_ERROR ||
        events & VIR_EVENT_HANDLE_HANGUP) {
273
        virConsoleShutdown(con);
274 275 276 277 278 279 280
    }
}


int vshRunConsole(virDomainPtr dom, const char *devname)
{
    int ret = -1;
281 282 283 284 285 286
    struct termios ttyattr, rawattr;
    void (*old_sigquit)(int);
    void (*old_sigterm)(int);
    void (*old_sigint)(int);
    void (*old_sighup)(int);
    void (*old_sigpipe)(int);
287
    virConsolePtr con = NULL;
288

289
    /* Put STDIN into raw mode so that stuff typed
290 291 292 293 294
       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) {
295
        VIR_ERROR(_("unable to get tty attributes: %s"),
296
                  strerror(errno));
297
        return -1;
298 299 300 301 302 303
    }

    rawattr = ttyattr;
    cfmakeraw(&rawattr);

    if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &rawattr) < 0) {
304
        VIR_ERROR(_("unable to set tty attributes: %s"),
305
                  strerror(errno));
306
        goto resettty;
307 308 309 310 311 312 313 314 315 316 317 318 319 320
    }


    /* 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;

321 322 323 324
    if (VIR_ALLOC(con) < 0) {
        virReportOOMError();
        goto cleanup;
    }
325

326 327 328 329 330 331 332 333
    con->st = virStreamNew(virDomainGetConnect(dom),
                           VIR_STREAM_NONBLOCK);
    if (!con->st)
        goto cleanup;

    if (virDomainOpenConsole(dom, devname, con->st, 0) < 0)
        goto cleanup;

334 335 336 337 338 339 340 341 342 343
    con->stdinWatch = virEventAddHandle(STDIN_FILENO,
                                        VIR_EVENT_HANDLE_READABLE,
                                        virConsoleEventOnStdin,
                                        con,
                                        NULL);
    con->stdoutWatch = virEventAddHandle(STDOUT_FILENO,
                                         0,
                                         virConsoleEventOnStdout,
                                         con,
                                         NULL);
344 345 346 347 348 349 350 351

    virStreamEventAddCallback(con->st,
                              VIR_STREAM_EVENT_READABLE,
                              virConsoleEventOnStream,
                              con,
                              NULL);

    while (!con->quit) {
352
        if (virEventRunDefaultImpl() < 0)
353 354
            break;
    }
355 356 357 358 359

    ret = 0;

 cleanup:

360 361 362 363 364 365
    if (con) {
        if (con->st)
            virStreamFree(con->st);
        VIR_FREE(con);
    }

366
    /* Restore original signal handlers */
W
Wen Congyang 已提交
367 368 369 370
    signal(SIGPIPE, old_sigpipe);
    signal(SIGHUP, old_sighup);
    signal(SIGINT, old_sigint);
    signal(SIGTERM, old_sigterm);
371 372
    signal(SIGQUIT, old_sigquit);

373
resettty:
374 375 376 377 378 379 380
    /* Put STDIN back into the (sane?) state we found
       it in before starting */
    tcsetattr(STDIN_FILENO, TCSAFLUSH, &ttyattr);

    return ret;
}

381
#endif /* !WIN32 */