diff options
author | Jiri Olsa <Jiri Olsa jolsa@redhat.com> | 2010-04-22 13:07:36 +0200 |
---|---|---|
committer | Jiri Olsa <Jiri Olsa jolsa@redhat.com> | 2010-04-22 13:07:36 +0200 |
commit | 23367a9167c0ee1efb700a474e1a40ac28856da1 (patch) | |
tree | 1f8658bb8fea5f0d099780901ae4179f9ef88e28 /src/tsnifd.c | |
parent | e47aa812a3aa8dd3a73d66cce60afeacf76d9646 (diff) | |
download | tsnif-master.tar.gz tsnif-master.tar.xz tsnif-master.zip |
Diffstat (limited to 'src/tsnifd.c')
-rw-r--r-- | src/tsnifd.c | 153 |
1 files changed, 7 insertions, 146 deletions
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': |