blob: 00ada7a5cb028fb494500421e558c50a4e3c96fd [file] [log] [blame]
/**
* Copyright © 2016 IBM Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define _GNU_SOURCE
#include <assert.h>
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "console-server.h"
struct tty_handler {
struct handler handler;
struct console *console;
struct poller *poller;
int fd;
};
static struct tty_handler *to_tty_handler(struct handler *handler)
{
return container_of(handler, struct tty_handler, handler);
}
static enum poller_ret tty_poll(struct handler *handler,
int events, void __attribute__((unused)) *data)
{
struct tty_handler *th = to_tty_handler(handler);
uint8_t buf[4096];
ssize_t len;
if (!(events & POLLIN))
return POLLER_OK;
len = read(th->fd, buf, sizeof(buf));
if (len <= 0) {
th->poller = NULL;
close(th->fd);
return POLLER_REMOVE;
}
console_data_out(th->console, buf, len);
return POLLER_OK;
}
static int tty_init(struct handler *handler, struct console *console,
struct config *config __attribute__((unused)))
{
struct tty_handler *th = to_tty_handler(handler);
const char *tty_name;
char *tty_path;
int rc, flags;
tty_name = config_get_value(config, "local-tty");
if (!tty_name)
return -1;
rc = asprintf(&tty_path, "/dev/%s", tty_name);
if (!rc)
return -1;
th->fd = open(tty_path, O_RDWR);
if (th->fd < 0) {
warn("Can't open %s; disabling local tty", tty_name);
free(tty_path);
return -1;
}
free(tty_path);
/* initial tty setup */
flags = fcntl(th->fd, F_GETFL, 0);
flags |= FNDELAY;
fcntl(th->fd, F_SETFL, flags);
th->poller = console_register_poller(console, handler, tty_poll,
th->fd, POLLIN, NULL);
th->console = console;
return 0;
}
static int tty_data(struct handler *handler, uint8_t *buf, size_t len)
{
struct tty_handler *th = to_tty_handler(handler);
return write_buf_to_fd(th->fd, buf, len);
}
static void tty_fini(struct handler *handler)
{
struct tty_handler *th = to_tty_handler(handler);
if (th->poller)
console_unregister_poller(th->console, th->poller);
close(th->fd);
}
static struct tty_handler tty_handler = {
.handler = {
.name = "tty",
.init = tty_init,
.data_in = tty_data,
.fini = tty_fini,
},
};
console_register_handler(&tty_handler.handler);