aboutsummaryrefslogtreecommitdiff
path: root/syslogd/logfile.c
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@tele2.at>2018-06-17 16:48:50 +0200
committerDavid Oberhollenzer <david.oberhollenzer@tele2.at>2018-06-17 18:20:53 +0200
commit9878e69ebe5b18676f8f5e26ef57d2e47b02e44d (patch)
tree0205ed51f570e2db855fb34b51fe3e7d5a806b9d /syslogd/logfile.c
parent8a8f49a501fd109bcb722afff7eb2c7852a04107 (diff)
usyslogd: store logs in subdirectory based on service identifier
Signed-off-by: David Oberhollenzer <david.oberhollenzer@tele2.at>
Diffstat (limited to 'syslogd/logfile.c')
-rw-r--r--syslogd/logfile.c39
1 files changed, 33 insertions, 6 deletions
diff --git a/syslogd/logfile.c b/syslogd/logfile.c
index 5687750..56f8d53 100644
--- a/syslogd/logfile.c
+++ b/syslogd/logfile.c
@@ -15,39 +15,66 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
+#include <sys/types.h>
+#include <sys/stat.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include <fcntl.h>
+#include <errno.h>
#include "logfile.h"
-logfile_t *logfile_create(const char *name, int facility)
+logfile_t *logfile_create(const char *ident, const char *name, int facility)
{
+ int dfd = AT_FDCWD;
logfile_t *file;
+ size_t size;
- file = calloc(1, sizeof(*file) + strlen(name) + 1);
+ size = sizeof(*file) + 1;
+ if (ident != NULL)
+ size += strlen(ident);
+
+ file = calloc(1, size);
if (file == NULL) {
perror("calloc");
return NULL;
}
- strcpy(file->name, name);
+ 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;
+ }
+ }
+
file->facility = facility;
- file->fd = open(file->name, O_WRONLY | O_CREAT, 0640);
- if (file->fd < 0)
+ file->fd = openat(dfd, name, O_WRONLY | O_CREAT, 0640);
+ if (file->fd < 0) {
+ perror(name);
goto fail;
+ }
if (lseek(file->fd, 0, SEEK_END))
goto fail;
+ if (dfd != AT_FDCWD)
+ close(dfd);
return file;
fail:
- perror(file->name);
+ if (dfd != AT_FDCWD)
+ close(dfd);
free(file);
return NULL;
}