summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJiri Olsa <Jiri Olsa jolsa@redhat.com>2010-04-22 13:07:36 +0200
committerJiri Olsa <Jiri Olsa jolsa@redhat.com>2010-04-22 13:07:36 +0200
commit23367a9167c0ee1efb700a474e1a40ac28856da1 (patch)
tree1f8658bb8fea5f0d099780901ae4179f9ef88e28
parente47aa812a3aa8dd3a73d66cce60afeacf76d9646 (diff)
downloadtsnif-23367a9167c0ee1efb700a474e1a40ac28856da1.tar.gz
tsnif-23367a9167c0ee1efb700a474e1a40ac28856da1.tar.xz
tsnif-23367a9167c0ee1efb700a474e1a40ac28856da1.zip
adding tsnid storage moduleHEADmaster
-rw-r--r--src/Makefile7
-rw-r--r--src/tsnifd-storage.c149
-rw-r--r--src/tsnifd.c153
-rw-r--r--src/tsnifd.h23
4 files changed, 183 insertions, 149 deletions
diff --git a/src/Makefile b/src/Makefile
index 8da59bc..fb38799 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -42,9 +42,10 @@ $(TSNIF_REPLAY): $(TSNIF_REPLAY_OBJS)
TSNIFD=tsnifd
TSNIFD_OBJS= \
- src/tsnifd.o \
- $(INTF_OBJS) \
- $(STORAGE_OBJS) \
+ src/tsnifd.o \
+ src/tsnifd-storage.o \
+ $(INTF_OBJS) \
+ $(STORAGE_OBJS) \
$(DEBUG_OBJS)
$(TSNIFD): $(TSNIFD_OBJS)
diff --git a/src/tsnifd-storage.c b/src/tsnifd-storage.c
new file mode 100644
index 0000000..152221f
--- /dev/null
+++ b/src/tsnifd-storage.c
@@ -0,0 +1,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;
+}
diff --git a/src/tsnifd.c b/src/tsnifd.c
index dc90ce6..52393d5 100644
--- a/src/tsnifd.c
+++ b/src/tsnifd.c
@@ -14,133 +14,20 @@
#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,
-};
+#include "tsnifd.h"
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);
@@ -162,21 +49,13 @@ static int terminal_add(struct tsnif_term *term)
return err;
}
- file = file_get(term->type, term->idx);
- if (!file) {
+ err = storage_init(t);
+ if (err) {
tsnif_term_del(&handle, &t->term);
free(t);
- return -1;
+ return err;
}
- 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);
}
@@ -189,36 +68,18 @@ static int terminal_del(struct tsnif_term *term)
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);
-
+ storage_close(t);
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);
+ return storage_data(t, data);
}
static int err_cb(struct tsnif_term *term, int err)
@@ -316,7 +177,7 @@ static int get_args(int argc, char **argv)
break;
case 's':
- store_dir = optarg;
+ storage_dir = optarg;
break;
case 'V':
diff --git a/src/tsnifd.h b/src/tsnifd.h
new file mode 100644
index 0000000..dcbe30d
--- /dev/null
+++ b/src/tsnifd.h
@@ -0,0 +1,23 @@
+#ifndef TSNIFD_H
+#define TSNIFD_H
+
+#include "storage.h"
+#include "intf.h"
+
+extern char *storage_dir;
+
+struct terminal {
+ /* interface term */
+ struct tsnif_term term;
+
+ /* storage */
+ char *file;
+ struct tsnif_storage_opts storage_opts;
+ struct tsnif_storage_handle storage_handle;
+};
+
+int storage_init(struct terminal *t);
+int storage_close(struct terminal *t);
+int storage_data(struct terminal *t, struct tsnif_data *data);
+
+#endif /* TSNIFD_H */