summaryrefslogtreecommitdiffstats
path: root/src/tsnif.c
blob: 57731f22ed0d914493de3d14522024566e956ae8 (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303

#include <stdio.h>
#include <sys/select.h>
#include <unistd.h>
#include <termios.h>
#include <getopt.h>
#include <errno.h>
#include <setjmp.h>
#include <signal.h>

#include "autoconf.h"
#include "intf.h"
#include "storage.h"


static struct tsnif_handle handle;
static struct tsnif_term term;

static struct tsnif_storage_opts storage_opts = {
	.flags    = TSNIF_STORAGE_OPT_WRITE,
	.size_max = 1024*1024,
};
static struct tsnif_storage_handle storage_handle;

static int killed = 0;
static int store = 0;
static int type = -1;
static int idx = -1;
static int display_slave  = 1;
static int display_master = 0;
static char *storage_file = NULL;

static int display_pty(int flags)
{
	if ((flags == TSNIF_FLAGS_PTY_SLAVE) &&
	    !display_slave)
		return 0;

	if ((flags == TSNIF_FLAGS_PTY_MASTER) &&
	    !display_master)
		return 0;

	return 1;
}

static int store_data(struct tsnif_data *data)
{
	struct tsnif_storage_rec rec = {
		.ptr = data->ptr,
		.len = data->len,
		.flags = data->flags,
	};

	return tsnif_storage_write(&storage_handle, &rec);
}

static int data_cb(struct tsnif_term* term, struct tsnif_data *data)
{
	/* rule out unwanted PTY channel */
	if ((term->type == TSNIF_TYPE_PTY) &&
	    !display_pty(data->flags))
		return 0;

	fwrite(data->ptr, data->len, 1, stdout);
	fflush(NULL);

	if (store)
		return store_data(data);

	return 0;
}

static int err_cb(struct tsnif_term *term, int err)
{
	return err;
}

static int release_cb(struct tsnif_term *term)
{
	return -1;
}

static int notify_cb(struct tsnif_term *term, int action)
{
	return 0;
}

struct tsnif_ops ops = {
	.cb_data	= data_cb,
	.cb_err		= err_cb,
	.cb_release	= release_cb,
	.cb_notify	= notify_cb,
};

static void usage()
{
	printf("tsnif -t <terminal type> -i <terminal index> [ -s<file> ]\n");
	printf("  where types can be 'tty', 'ttys' or 'pty'\n");
	_exit(-1);
}

static void sig_handler(int sig)
{
	printf("killed\n");
	killed = 1;
}

int set_term(int set_init)
{
	struct termios new_settings;
	static struct termios init_settings;

	if (set_init)
		return tcsetattr(0, TCSANOW, &init_settings);

	tcgetattr(0, &init_settings);
	new_settings = init_settings;
	new_settings.c_lflag &= ~ICANON;
	new_settings.c_lflag &= ~ECHO;

	return tcsetattr(0, TCSANOW, &new_settings);
}

static int get_type(char *optarg)
{
	int i;
	char *types[TSNIF_TYPE_MAX] = {
		"tty", "ttys", "pty",
	};

	for(i = 0; i < TSNIF_TYPE_MAX; i++)
		if (!strcmp(optarg, types[i]))
			break;

	return i;
}

static int get_args(int argc, char **argv)
{
	while (1) {
		int c;
		int option_index = 0;
		static struct option long_options[] = {
			{"type", required_argument, 0, 't'},
			{"idx", required_argument, 0, 'i'},
			{"store", required_argument, 0, 's'},
			{"version", no_argument, 0, 'v'},
			{"debug", no_argument, 0, 'd'},
			{"help", no_argument, 0, 'h'},
			{0, 0, 0, 0}
		};

		c = getopt_long(argc, argv, "t:i:s:vdh",
			long_options, &option_index);

		if (c == -1)
			break;

		switch (c) {
		case 't':
			type = get_type(optarg);
			if (type == TSNIF_TYPE_MAX)
				return -1;
			break;

                case 'i':
			errno = 0;
			idx = strtol(optarg, (char **) NULL, 10);
			if (errno) {
				perror("strtol failed");
				printf("strtol failed\n");
				return -1;
			}
                        break;

                case 's':
			storage_file = optarg;
			store = 1;
                        break;

		case 'v':
			printf("tsnif "CONFIG_TSNIF_VER"\n");
			break;

		case 'd':
			tsnif_debug = 1;
			break;

		case 'h':
			usage();
			break;

		default:
			printf("unknown option '%c'", c);
		} /* switch(c) */
	} /* while(1) */

	if ((type == -1) || (idx == -1))
		return -1;

	return 0;
}

static int process_input(void)
{
	int c = getchar();
	switch(c) {
	case 's':
		display_slave = !display_slave;
		break;

	case 'm':
		display_master = !display_master;
		break;

	case 'q':
		return -1;

	default:
		return 0;
	}

	printf("\ntsnif display [%s%s]\n",
		display_master ? "M" : "",
		display_slave  ? "S"  : "");
	return 0;
}

int main(int argc, char **argv)
{
	int err, ret;
	jmp_buf env;

	if (get_args(argc, argv))
		usage();

	err = tsnif_init(&handle, &ops);
	if (err)
		return err;

	if ((ret = setjmp(env))) {
		if (ret > 2) {
			set_term(1);
			if (store)
				tsnif_storage_close(&storage_handle);
		}

		if (ret > 1) {
			tsnif_detach(&term);
		}

		tsnif_close(&handle);
		printf("done err = %d\n", err);
		return err;
	}

	/* attach the term */
	err = tsnif_term_add(&handle, &term, type, idx);
	if (err)
		longjmp(env, 1);

	err = tsnif_attach(&term);
	if (err)
		longjmp(env, 1);

	/* initialize storage unit if needed */
	if (store) {
		err = tsnif_storage_init(&storage_handle, &storage_opts, storage_file);
		if (err)
			longjmp(env, 2);
	}

	set_term(0);
        signal(SIGINT, sig_handler);

	while(!killed) {
		fd_set rfds;
		struct timeval tv = { 1, 0};
		int ts_fd = tsnif_fd(&handle), ret;
		int in_fd = 0;

		FD_ZERO(&rfds);
		FD_SET(ts_fd, &rfds);
		FD_SET(in_fd, &rfds);

		ret = select(ts_fd + 1, &rfds, NULL, NULL, &tv);
		if (ret == -1) {
			perror("select()");
			continue;
		} else if (!ret)
			continue;

		if (FD_ISSET(ts_fd, &rfds) &&
		    tsnif_process(&handle))
			longjmp(env, 3);

		if (FD_ISSET(in_fd, &rfds) &&
		    process_input())
			longjmp(env, 3);
	}

	longjmp(env, 3);
}