summaryrefslogtreecommitdiffstats
path: root/src/tsnifd.c
blob: 8a6c8ebe797afd06c61d153d7ebb6d52887f5fec (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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389

#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 <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <time.h>

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


static struct tsnif_storage_opts init_storage_opts = {
	.flags    = TSNIF_STORAGE_OPT_WRITE,
	.size_max = 1024*1024,
};

static struct tsnif_handle handle;

struct terminal {
	/* interface term */
	struct tsnif_term term;

	/* storage */
	char *file;
	struct tsnif_storage_opts   storage_opts;
	struct tsnif_storage_handle storage_handle;
};

static int foreground  = 0;
static int killed      = 0;
static char *store_dir = "/var/log/tsnid";

static char *types[TSNIF_TYPE_MAX] = {
	"tty", "ttys", "pty"
};

static char *type_get(int type)
{
	if ((type >= TSNIF_TYPE_MAX) ||
	    (type < 0))
		return NULL;

	return types[type];
}

static int dir_get(char *path)
{
	struct stat st;
	int err;

	if (!stat(path, &st))
		return 0;

	err = mkdir(path, S_IRUSR | S_IWUSR | S_IXUSR);
	if (err) {
		perror("mkdir failed");
		return err;
	}

	return 0;

}

static char *file_get(int type, int idx)
{
	struct timeval tv;
	struct tm *tm;
#define MAXPATH 1024
	static char path[MAXPATH];
	char *type_str;
	int len;

	if (dir_get(store_dir))
		return NULL;

	type_str = type_get(type);
	if (!type_str)
		return NULL;

	len = snprintf(path, MAXPATH, "%s/%s", store_dir, type_str);

	if (dir_get(path))
		return NULL;

	if (-1 == gettimeofday(&tv, NULL)) {
		perror("gettimeofday failed");
		return NULL;
	}

	tm = localtime(&tv.tv_sec);
	if (!tm) {
		perror("localtime failed");
		return NULL;
	}

	snprintf(path + len, MAXPATH - len,
			"/%d-%02d.%02d.%02d_%02d:%02d:%02d.%03lu-alive",
			idx,
			tm->tm_mday,
			tm->tm_mon + 1,
			(tm->tm_year + 1900) % 100,
			tm->tm_hour,
			tm->tm_min,
			tm->tm_sec,
			tv.tv_usec / 1000);

	return path;
}

static int file_put(char *name)
{
	char *new = strdup(name);
	char *p;
	int err;

	p = strrchr(new, '-');
	if (!p)
		return -1;

	sprintf(p, "%s", ".tsnif");

	TSNIF_DEBUG(APP, "putting to %s\n", new);

	err = rename(name, new);
	free(new);
	return err;
}

static int terminal_add(struct tsnif_term *term)
{
	struct terminal *t;
	int err;
	char *file;

	TSNIF_DEBUG(APP, "type %d, idx %d\n", term->type, term->idx);

	/* XXX debug gets only PTY XXX */
	if (tsnif_debug) {
		if (term->type != TSNIF_TYPE_PTY)
			return 0;
	}

	t = malloc(sizeof(*t));
	if (!t)
		return -ENOMEM;

	memset(t, 0x0, sizeof(*t));

	err = tsnif_term_add(&handle, &t->term, term->type, term->idx);
	if (err) {
		free(t);
		return err;
	}

	file = file_get(term->type, term->idx);
	if (!file) {
		tsnif_term_del(&handle, &t->term);
		free(t);
		return -1;
	}

	TSNIF_DEBUG(APP, "storing to %s\n", file);

	t->file = strdup(file);
	t->storage_opts = init_storage_opts;

	err = tsnif_storage_init(&t->storage_handle,
				 &t->storage_opts, file);

	return tsnif_attach(&t->term);
}

static int terminal_del(struct tsnif_term *term)
{
	struct terminal *t;

	TSNIF_DEBUG(APP, "type %d, idx %d\n", term->type, term->idx);

	tsnif_term_del(&handle, term);

	t = container_of(term, struct terminal, term);

	tsnif_storage_close(&t->storage_handle);
	file_put(t->file);
	free(t->file);

	free(t);

	return 0;
}

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

	return tsnif_storage_write(h, &rec);
}

static int data_cb(struct tsnif_term* term, struct tsnif_data *data)
{
	struct terminal *t;

	t = container_of(term, struct terminal, term);

	return store_data(&t->storage_handle, data);
}

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

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

static int notify_cb(struct tsnif_term *term, int action)
{
	int err = 0;

	switch(action) {
	case TSNIF_CMD_TTY_RELEASE:
		printf("not watched terminal released - type %d, idx %d\n",
			term->type, term->idx);
		break;

	case TSNIF_CMD_TTY_CREATE:
	case TSNIF_CMD_TTY_LIST:
		err = terminal_add(term);
		break;

	default:
		err = -EINVAL;
	}
	return err;
}

static int release_terms(void)
{
	struct tsnif_term *term, *t;

	tsnif_for_each(term, t, (&handle)) {
		/* The ACK action (release cb) is never going
		 * to happen, since we are going down, * so we
		 * need to run terminal_del manually */
		tsnif_detach(term);
		terminal_del(term);
	}

	return 0;
}

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

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

static void usage()
{
	printf("tsnifd [-dfvhs]\n");
	_exit(-1);
}

static int get_args(int argc, char **argv)
{
	while (1) {
		int c;
		int option_index = 0;
		static struct option long_options[] = {
			{"version", no_argument, 0, 'v'},
			{"debug", no_argument, 0, 'd'},
			{"foreground", no_argument, 0, 'f'},
			{"store-dir", required_argument, 0, 's'},
			{"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 'v':
			printf("tsnif "CONFIG_TSNIF_VER"\n");
			break;

		case 'd':
			tsnif_debug = 1;
			break;

		case 'f':
			foreground = 1;
			break;

		case 's':
			store_dir = optarg;
			break;

		case 'h':
			usage();
			break;

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

	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 > 1)
			release_terms();

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

	err = tsnif_list(&handle);
	if (err)
		longjmp(env, 1);

	signal(SIGINT, sig_handler);

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

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

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

		if (FD_ISSET(ts_fd, &rfds)) {
			err = tsnif_process(&handle);
			if (err)
				longjmp(env, 2);
		}
	}

	longjmp(env, 2);
}