From 23367a9167c0ee1efb700a474e1a40ac28856da1 Mon Sep 17 00:00:00 2001 From: Jiri Olsa Date: Thu, 22 Apr 2010 13:07:36 +0200 Subject: adding tsnid storage module --- src/Makefile | 7 ++- src/tsnifd-storage.c | 149 +++++++++++++++++++++++++++++++++++++++++++++++++ src/tsnifd.c | 153 +++------------------------------------------------ src/tsnifd.h | 23 ++++++++ 4 files changed, 183 insertions(+), 149 deletions(-) create mode 100644 src/tsnifd-storage.c create mode 100644 src/tsnifd.h 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 +#include +#include +#include + +#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 */ -- cgit