/*
- * Copyright (C) 2012-2024 Tobias Brunner
+ * Copyright (C) 2012-2026 Tobias Brunner
* Copyright (C) 2006 Martin Willi
*
* Copyright (C) secunet Security Networks AG
#include <string.h>
#include <time.h>
#include <errno.h>
+#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
}
else
{
- file = fopen(this->filename, append ? "a" : "w");
- if (file == NULL)
+ int fd, flags = O_WRONLY | O_CREAT | (append ? O_APPEND : O_TRUNC);
+
+ fd = open(this->filename, flags, 0660);
+ if (fd < 0)
{
- DBG1(DBG_DMN, "opening file %s for logging failed: %s",
+ DBG1(DBG_DMN, "opening file '%s' for logging failed: %s",
this->filename, strerror(errno));
return;
}
#ifdef HAVE_CHOWN
if (lib->caps->check(lib->caps, CAP_CHOWN))
{
- if (chown(this->filename, lib->caps->get_uid(lib->caps),
- lib->caps->get_gid(lib->caps)) != 0)
+ if (fchown(fd, lib->caps->get_uid(lib->caps),
+ lib->caps->get_gid(lib->caps)) != 0)
{
- DBG1(DBG_NET, "changing owner/group for '%s' failed: %s",
+ DBG1(DBG_DMN, "changing owner/group for '%s' failed: %s",
this->filename, strerror(errno));
}
}
else
{
- if (chown(this->filename, -1, lib->caps->get_gid(lib->caps)) != 0)
+ if (fchown(fd, -1, lib->caps->get_gid(lib->caps)) != 0)
{
- DBG1(DBG_NET, "changing group for '%s' failed: %s",
+ DBG1(DBG_DMN, "changing group for '%s' failed: %s",
this->filename, strerror(errno));
}
}
#endif /* HAVE_CHOWN */
+ file = fdopen(fd, append ? "a" : "w");
+ if (!file)
+ {
+ DBG1(DBG_DMN, "opening file %s for logging failed: %s",
+ this->filename, strerror(errno));
+ close(fd);
+ return;
+ }
#ifdef HAVE_SETLINEBUF
if (flush_line)
{