|
|
|
@ -14,16 +14,13 @@
|
|
|
|
|
|
|
|
|
|
#define MODNAME "files" |
|
|
|
|
|
|
|
|
|
#define FREE(x) { free(x); x = NULL; } |
|
|
|
|
#define FCLOSE(x) { if (x) fclose(x); x = NULL; } |
|
|
|
|
|
|
|
|
|
typedef struct f2b_file_t { |
|
|
|
|
struct f2b_file_t *next; |
|
|
|
|
uint32_t lines; |
|
|
|
|
uint32_t stag; |
|
|
|
|
struct stat st; |
|
|
|
|
FILE *fd; |
|
|
|
|
char path[]; |
|
|
|
|
char path[PATH_MAX]; |
|
|
|
|
struct stat st; |
|
|
|
|
} f2b_file_t; |
|
|
|
|
|
|
|
|
|
struct _config { |
|
|
|
@ -31,39 +28,40 @@ struct _config {
|
|
|
|
|
f2b_file_t *files; |
|
|
|
|
f2b_file_t *current; |
|
|
|
|
int flags; |
|
|
|
|
bool missingok; |
|
|
|
|
char path[]; |
|
|
|
|
char path[256]; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
#include "source.c" |
|
|
|
|
|
|
|
|
|
static bool |
|
|
|
|
file_open(const cfg_t *cfg, f2b_file_t *file) { |
|
|
|
|
file_open(f2b_file_t *file, const char *path) { |
|
|
|
|
FILE *fd; |
|
|
|
|
struct stat st; |
|
|
|
|
char buf[PATH_MAX] = ""; |
|
|
|
|
|
|
|
|
|
assert(file != NULL); |
|
|
|
|
assert(path != NULL || file->path[0] != '\0'); |
|
|
|
|
|
|
|
|
|
if (stat(file->path, &st) != 0) |
|
|
|
|
strlcpy(buf, path ? path : file->path, sizeof(buf)); |
|
|
|
|
|
|
|
|
|
memset(file, 0x0, sizeof(f2b_file_t)); |
|
|
|
|
|
|
|
|
|
if (stat(buf, &st) != 0) |
|
|
|
|
return false; |
|
|
|
|
|
|
|
|
|
if (!(S_ISREG(st.st_mode) || S_ISFIFO(st.st_mode))) { |
|
|
|
|
log_msg(cfg, error, "not a regular file/fifo: %s", file->path); |
|
|
|
|
if (!(S_ISREG(st.st_mode) || S_ISFIFO(st.st_mode))) |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if ((fd = fopen(file->path, "r")) == NULL) { |
|
|
|
|
log_msg(cfg, error, "can't open file: %s -- %s", file->path, strerror(errno)); |
|
|
|
|
if ((fd = fopen(buf, "r")) == NULL) |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (S_ISREG(st.st_mode) && fseek(fd, 0, SEEK_END) < 0) { |
|
|
|
|
log_msg(cfg, error, "fseek error: %s -- %s", file->path, strerror(errno)); |
|
|
|
|
FCLOSE(fd); |
|
|
|
|
fclose(fd); |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
memcpy(&file->st, &st, sizeof(st)); |
|
|
|
|
strlcpy(file->path, buf, sizeof(file->path)); |
|
|
|
|
file->fd = fd; |
|
|
|
|
file->stag = fnv_32a_str(file->path, FNV1_32A_INIT); |
|
|
|
|
|
|
|
|
@ -74,7 +72,11 @@ static void
|
|
|
|
|
file_close(f2b_file_t *file) { |
|
|
|
|
assert(file != NULL); |
|
|
|
|
|
|
|
|
|
FCLOSE(file->fd); |
|
|
|
|
if (file->fd == NULL) |
|
|
|
|
return; |
|
|
|
|
|
|
|
|
|
fclose(file->fd); |
|
|
|
|
file->fd = NULL; |
|
|
|
|
file->lines = 0; |
|
|
|
|
memset(&file->st, 0, sizeof(struct stat)); |
|
|
|
|
} |
|
|
|
@ -88,7 +90,7 @@ file_rotated(const cfg_t *cfg, f2b_file_t *file) {
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
|
|
if (stat(file->path, &st) != 0) { |
|
|
|
|
log_msg(cfg, error, "stat(): %s -- %s", file->path, strerror(errno)); |
|
|
|
|
log_msg(cfg, error, "file stat error: %s", strerror(errno)); |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -130,21 +132,15 @@ file_getline(const cfg_t *cfg, f2b_file_t *file, char *buf, size_t bufsize) {
|
|
|
|
|
cfg_t * |
|
|
|
|
create(const char *init) { |
|
|
|
|
cfg_t *cfg = NULL; |
|
|
|
|
|
|
|
|
|
if (init == NULL) return NULL; |
|
|
|
|
|
|
|
|
|
size_t ilen = strlen(init) + 1; |
|
|
|
|
if (ilen <= 1) return NULL; |
|
|
|
|
|
|
|
|
|
if ((cfg = calloc(1, sizeof(cfg_t) + ilen)) == NULL) |
|
|
|
|
if ((cfg = calloc(1, sizeof(cfg_t))) == NULL) |
|
|
|
|
return NULL; |
|
|
|
|
cfg->logcb = &logcb_stub; |
|
|
|
|
cfg->flags |= MOD_TYPE_SOURCE; |
|
|
|
|
cfg->flags |= MOD_NEED_FILTER; |
|
|
|
|
|
|
|
|
|
cfg->logcb = &logcb_stub; |
|
|
|
|
strlcpy(cfg->path, init, ilen); |
|
|
|
|
if (init != NULL && strlen(init) > 0) { |
|
|
|
|
strlcpy(cfg->path, init, sizeof(cfg->path)); |
|
|
|
|
cfg->flags |= MOD_IS_READY; |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
return cfg; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -154,10 +150,10 @@ config(cfg_t *cfg, const char *key, const char *value) {
|
|
|
|
|
assert(key != NULL); |
|
|
|
|
assert(value != NULL); |
|
|
|
|
|
|
|
|
|
if (strcmp(key, "missingok") == 0) { |
|
|
|
|
cfg->missingok = (strcmp(value, "yes") == 0) ? true : false; |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
/* no options */ |
|
|
|
|
(void)(cfg); /* suppress warning for unused variable 'ip' */ |
|
|
|
|
(void)(key); /* suppress warning for unused variable 'ip' */ |
|
|
|
|
(void)(value); /* suppress warning for unused variable 'ip' */ |
|
|
|
|
|
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
@ -165,32 +161,25 @@ config(cfg_t *cfg, const char *key, const char *value) {
|
|
|
|
|
bool |
|
|
|
|
start(cfg_t *cfg) { |
|
|
|
|
f2b_file_t *file = NULL; |
|
|
|
|
int glob_flags = GLOB_MARK | GLOB_NOESCAPE; |
|
|
|
|
glob_t globbuf; |
|
|
|
|
|
|
|
|
|
assert(cfg != NULL); |
|
|
|
|
|
|
|
|
|
if (cfg->missingok) |
|
|
|
|
glob_flags |= GLOB_NOCHECK; /* allow no matches */ |
|
|
|
|
|
|
|
|
|
if (glob(cfg->path, glob_flags, NULL, &globbuf) != 0) |
|
|
|
|
if (glob(cfg->path, GLOB_MARK | GLOB_NOESCAPE, NULL, &globbuf) != 0) |
|
|
|
|
return NULL; |
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < globbuf.gl_pathc; i++) { |
|
|
|
|
size_t slen = strlen(globbuf.gl_pathv[i]) + 1; |
|
|
|
|
if ((file = calloc(1, sizeof(f2b_file_t) + slen)) == NULL) { |
|
|
|
|
log_msg(cfg, error, "can't allocate memory: %s", strerror(errno)); |
|
|
|
|
if ((file = calloc(1, sizeof(f2b_file_t))) == NULL) |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
strlcpy(file->path, globbuf.gl_pathv[i], slen); |
|
|
|
|
if (!file_open(cfg, file) && !cfg->missingok) { |
|
|
|
|
FREE(file); |
|
|
|
|
if (file_open(file, globbuf.gl_pathv[i]) == false) { |
|
|
|
|
log_msg(cfg, error, "can't open file: %s -- %s", globbuf.gl_pathv[i], strerror(errno)); |
|
|
|
|
free(file); |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
if (cfg->files) { |
|
|
|
|
file->next = cfg->files; |
|
|
|
|
if (cfg->files == NULL) { |
|
|
|
|
cfg->files = file; |
|
|
|
|
} else { |
|
|
|
|
file->next = cfg->files; |
|
|
|
|
cfg->files = file; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -208,7 +197,7 @@ stop(cfg_t *cfg) {
|
|
|
|
|
for (; cfg->files != NULL; cfg->files = next) { |
|
|
|
|
next = cfg->files->next; |
|
|
|
|
file_close(cfg->files); |
|
|
|
|
FREE(cfg->files); |
|
|
|
|
free(cfg->files); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return true; |
|
|
|
@ -224,10 +213,13 @@ next(cfg_t *cfg, char *buf, size_t bufsize, bool reset) {
|
|
|
|
|
cfg->current = cfg->files; |
|
|
|
|
|
|
|
|
|
for (f2b_file_t *file = cfg->current; file != NULL; file = file->next) { |
|
|
|
|
if (file->fd != NULL && file_rotated(cfg, file)) |
|
|
|
|
if (file_rotated(cfg, file)) |
|
|
|
|
file_close(file); |
|
|
|
|
if (file->fd == NULL && !file_open(cfg, file)) |
|
|
|
|
if (file->fd == NULL) { |
|
|
|
|
if (!file_open(file, NULL)) |
|
|
|
|
log_msg(cfg, error, "can't open file: %s", file->path); |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
if (file_getline(cfg, file, buf, bufsize)) |
|
|
|
|
return file->stag; |
|
|
|
|
} |
|
|
|
@ -237,11 +229,14 @@ next(cfg_t *cfg, char *buf, size_t bufsize, bool reset) {
|
|
|
|
|
|
|
|
|
|
bool |
|
|
|
|
stats(cfg_t *cfg, char *buf, size_t bufsize) { |
|
|
|
|
struct tm tm; |
|
|
|
|
char tmp[PATH_MAX + 512]; |
|
|
|
|
time_t mtime; |
|
|
|
|
char mtime[30]; |
|
|
|
|
const char *fmt = |
|
|
|
|
"- path: %s\n" |
|
|
|
|
" info: fd=%d inode=%d size=%ld pos=%ld tag=%08X readl=%lu mtime=%lu\n"; |
|
|
|
|
" mtime: %s\n" |
|
|
|
|
" file: fd=%d inode=%d size=%ld pos=%ld tag=%08X\n" |
|
|
|
|
" read: %lu lines\n"; |
|
|
|
|
int fd, ino; off_t sz; long pos; |
|
|
|
|
assert(cfg != NULL); |
|
|
|
|
assert(buf != NULL); |
|
|
|
@ -253,11 +248,13 @@ stats(cfg_t *cfg, char *buf, size_t bufsize) {
|
|
|
|
|
for (f2b_file_t *f = cfg->files; f != NULL; f = f->next) { |
|
|
|
|
if (f->fd) { |
|
|
|
|
fd = fileno(f->fd), ino = f->st.st_ino, sz = f->st.st_size, pos = ftell(f->fd); |
|
|
|
|
mtime = f->st.st_mtime; |
|
|
|
|
localtime_r(&f->st.st_mtime, &tm); |
|
|
|
|
strftime(mtime, sizeof(mtime), "%Y-%m-%d %H:%M:%S", &tm); |
|
|
|
|
} else { |
|
|
|
|
fd = -1, ino = -1, sz = 0, pos = -1, mtime = 0; |
|
|
|
|
fd = -1, ino = -1, sz = 0, pos = -1; |
|
|
|
|
strlcpy(mtime, "-", sizeof(mtime)); |
|
|
|
|
} |
|
|
|
|
snprintf(tmp, sizeof(tmp), fmt, f->path, fd, ino, sz, pos, f->stag, f->lines, mtime); |
|
|
|
|
snprintf(tmp, sizeof(tmp), fmt, f->path, mtime, fd, ino, sz, pos, f->stag, f->lines); |
|
|
|
|
strlcat(buf, tmp, bufsize); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -268,5 +265,5 @@ void
|
|
|
|
|
destroy(cfg_t *cfg) { |
|
|
|
|
assert(cfg != NULL); |
|
|
|
|
|
|
|
|
|
FREE(cfg); |
|
|
|
|
free(cfg); |
|
|
|
|
} |
|
|
|
|