diff options
author | David Oberhollenzer <david.oberhollenzer@tele2.at> | 2018-08-13 22:06:34 +0200 |
---|---|---|
committer | David Oberhollenzer <david.oberhollenzer@tele2.at> | 2018-08-13 22:06:34 +0200 |
commit | 0624f95de639a41e8a47c264cdb7387a386a4dcd (patch) | |
tree | e2385f329e659eace10c91af28bad30c31c16627 /syslogd | |
parent | e15208097cf3e97c8165536c2005e53961227621 (diff) |
usyslogd: cleanup log file filename handling
Signed-off-by: David Oberhollenzer <david.oberhollenzer@tele2.at>
Diffstat (limited to 'syslogd')
-rw-r--r-- | syslogd/logfile.c | 37 | ||||
-rw-r--r-- | syslogd/logfile.h | 4 | ||||
-rw-r--r-- | syslogd/main.c | 23 |
3 files changed, 28 insertions, 36 deletions
diff --git a/syslogd/logfile.c b/syslogd/logfile.c index 56f8d53..adc7e3c 100644 --- a/syslogd/logfile.c +++ b/syslogd/logfile.c @@ -28,53 +28,32 @@ #include "logfile.h" -logfile_t *logfile_create(const char *ident, const char *name, int facility) +logfile_t *logfile_create(const char *filename, int facility) { - int dfd = AT_FDCWD; - logfile_t *file; - size_t size; + logfile_t *file = calloc(1, sizeof(*file) + strlen(filename) + 1); - size = sizeof(*file) + 1; - if (ident != NULL) - size += strlen(ident); - - file = calloc(1, size); if (file == NULL) { perror("calloc"); return NULL; } - if (ident != NULL) { - strcpy(file->ident, ident); - if (mkdir(file->ident, 0750) != 0 && errno != EEXIST) { - perror(file->ident); - goto fail; - } - - dfd = open(file->ident, O_DIRECTORY | O_RDONLY); - if (dfd < 0) { - perror(file->ident); - goto fail; - } - } + strcpy(file->filename, filename); file->facility = facility; - file->fd = openat(dfd, name, O_WRONLY | O_CREAT, 0640); + file->fd = open(file->filename, O_WRONLY | O_CREAT, 0640); if (file->fd < 0) { - perror(name); + perror(file->filename); goto fail; } if (lseek(file->fd, 0, SEEK_END)) - goto fail; + goto fail_fd; - if (dfd != AT_FDCWD) - close(dfd); return file; +fail_fd: + close(file->fd); fail: - if (dfd != AT_FDCWD) - close(dfd); free(file); return NULL; } diff --git a/syslogd/logfile.h b/syslogd/logfile.h index 3a596ba..ccbfd5d 100644 --- a/syslogd/logfile.h +++ b/syslogd/logfile.h @@ -23,10 +23,10 @@ typedef struct logfile_t { int facility; int fd; - char ident[]; + char filename[]; } logfile_t; -logfile_t *logfile_create(const char *ident, const char *name, int facility); +logfile_t *logfile_create(const char *filename, int facility); void logfile_destroy(logfile_t *file); diff --git a/syslogd/main.c b/syslogd/main.c index 263c87d..b98c414 100644 --- a/syslogd/main.c +++ b/syslogd/main.c @@ -121,9 +121,10 @@ static void signal_setup(void) static int print_to_log(const syslog_msg_t *msg) { const char *fac_name, *lvl_str; - char timebuf[32]; + char timebuf[32], *filename; logfile_t *log; struct tm tm; + size_t len; fac_name = enum_to_name(facilities, msg->facility); if (fac_name == NULL) @@ -133,17 +134,29 @@ static int print_to_log(const syslog_msg_t *msg) if (lvl_str == NULL) return -1; + if (msg->ident) { + len = strlen(msg->ident) + 1 + strlen(fac_name) + 1; + filename = alloca(len); + sprintf(filename, "%s/%s", msg->ident, fac_name); + } else { + filename = (char *)fac_name; + } + for (log = logfiles; log != NULL; log = log->next) { if (log->facility != msg->facility) continue; - if (msg->ident == NULL && log->ident[0] == '\0') - break; - if (msg->ident != NULL && strcmp(msg->ident, log->ident) == 0) + if (strcmp(filename, log->filename) == 0) break; } if (log == NULL) { - log = logfile_create(msg->ident, fac_name, msg->facility); + if (msg->ident != NULL && mkdir(msg->ident, 0750) != 0 && + errno != EEXIST) { + perror(msg->ident); + return -1; + } + + log = logfile_create(filename, msg->facility); if (log == NULL) return -1; log->next = logfiles; |