1
0
Fork 0
mirror of https://github.com/pygos/init.git synced 2024-11-22 19:19:47 +01:00

usyslogd: cleanup log file filename handling

Signed-off-by: David Oberhollenzer <david.oberhollenzer@tele2.at>
This commit is contained in:
David Oberhollenzer 2018-08-13 22:06:34 +02:00
parent e15208097c
commit 0624f95de6
3 changed files with 28 additions and 36 deletions

View file

@ -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;
}

View file

@ -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);

View file

@ -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;