summaryrefslogtreecommitdiffstats
path: root/src/tsnifd-storage.c
blob: 152221f0337b9866a769335cccc313e12614e299 (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

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>

#include "tsnifd.h"
#include "debug.h"

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

char *storage_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(storage_dir))
		return NULL;

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

	len = snprintf(path, MAXPATH, "%s/%s", storage_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;
}

int storage_data(struct terminal *t, 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(&t->storage_handle, &rec);
}

int storage_init(struct terminal *t)
{
	char *file;

	file = file_get(t->term.type, t->term.idx);
	if (!file)
		return -1;

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

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

	return tsnif_storage_init(&t->storage_handle,
				 &t->storage_opts, file);
}

int storage_close(struct terminal *t)
{
	tsnif_storage_close(&t->storage_handle);
	file_put(t->file);
	free(t->file);
	return 0;
}