summaryrefslogtreecommitdiff
path: root/src/uvt_tty_null.c
blob: 0c04942d897b87441b80cfe3ae5aa22f5fdbe95f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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
131
132
133
134
135
/*
 * UVT - Userspace Virtual Terminals
 *
 * Copyright (c) 2011-2013 David Herrmann <dh.herrmann@gmail.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files
 * (the "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

/*
 * Null TTY
 * This tty simply discards all incoming messages and never produces any
 * outgoing messages. Ioctls return static data or fail with some generic error
 * code if they would modify internal state that we cannot emulate easily.
 */

#include <eloop.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "shl_llog.h"
#include "uvt.h"
#include "uvt_internal.h"

#define LLOG_SUBSYSTEM "uvt_tty_null"

struct uvt_tty_null {
	unsigned long ref;
	struct uvt_ctx *ctx;
	llog_submit_t llog;
	void *llog_data;
};

static void tty_null_ref(void *data)
{
	uvt_tty_null_ref(data);
}

static void tty_null_unref(void *data)
{
	uvt_tty_null_unref(data);
}

static int tty_null_register_cb(void *data, uvt_tty_cb cb, void *cb_data)
{
	return 0;
}

static void tty_null_unregister_cb(void *data, uvt_tty_cb cb, void *cb_data)
{
}

static int tty_null_read(void *data, uint8_t *buf, size_t size)
{
	return -EAGAIN;
}

static int tty_null_write(void *data, const uint8_t *buf, size_t size)
{
	return size;
}

static unsigned int tty_null_poll(void *data)
{
	return UVT_TTY_WRITE;
}

const struct uvt_tty_ops uvt_tty_null_ops = {
	.ref = tty_null_ref,
	.unref = tty_null_unref,
	.register_cb = tty_null_register_cb,
	.unregister_cb = tty_null_unregister_cb,

	.read = tty_null_read,
	.write = tty_null_write,
	.poll = tty_null_poll,
};

int uvt_tty_null_new(struct uvt_tty_null **out, struct uvt_ctx *ctx)
{
	struct uvt_tty_null *tty;

	if (!ctx)
		return -EINVAL;
	if (!out)
		return llog_EINVAL(ctx);

	tty = malloc(sizeof(*tty));
	if (!tty)
		return llog_ENOMEM(ctx);
	memset(tty, 0, sizeof(*tty));
	tty->ref = 1;
	tty->ctx = ctx;
	tty->llog = tty->ctx->llog;
	tty->llog_data = tty->ctx->llog_data;

	uvt_ctx_ref(tty->ctx);
	*out = tty;
	return 0;
}

void uvt_tty_null_ref(struct uvt_tty_null *tty)
{
	if (!tty || !tty->ref)
		return;

	++tty->ref;
}

void uvt_tty_null_unref(struct uvt_tty_null *tty)
{
	if (!tty || !tty->ref || --tty->ref)
		return;

	uvt_ctx_unref(tty->ctx);
	free(tty);
}