Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -4399,8 +4399,8 @@ systemd_journal_upload_CFLAGS = \
$(LIBCURL_CFLAGS)

systemd_journal_upload_LDADD = \
libsystemd-internal.la \
libsystemd-journal-internal.la \
libsystemd-internal.la \
libsystemd-shared.la \
$(LIBCURL_LIBS)

Expand Down Expand Up @@ -4644,6 +4644,8 @@ libsystemd_journal_internal_la_SOURCES = \
src/systemd/_sd-common.h \
src/journal/journal-file.c \
src/journal/journal-file.h \
src/journal/journal-dir.c \
src/journal/journal-dir.h \
src/journal/journal-vacuum.c \
src/journal/journal-vacuum.h \
src/journal/journal-verify.c \
Expand Down
12 changes: 6 additions & 6 deletions src/journal-remote/journal-remote-write.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ static int do_rotate(JournalFile **f, bool compress, bool seal) {
int r = journal_file_rotate(f, compress, seal);
if (r < 0) {
if (*f)
log_error_errno(r, "Failed to rotate %s: %m", (*f)->path);
log_error_errno(r, "Failed to rotate %s/%s: %m", (*f)->directory->path, (*f)->filename);
else
log_error_errno(r, "Failed to create rotated journal: %m");
}
Expand Down Expand Up @@ -93,7 +93,7 @@ Writer* writer_free(Writer *w) {
return NULL;

if (w->journal) {
log_debug("Closing journal file %s.", w->journal->path);
log_debug("Closing journal file %s/%s.", w->journal->directory->path, w->journal->filename);
journal_file_close(w->journal);
}

Expand Down Expand Up @@ -136,8 +136,8 @@ int writer_write(Writer *w,
assert(iovw->count > 0);

if (journal_file_rotate_suggested(w->journal, 0)) {
log_info("%s: Journal header limits reached or header out-of-date, rotating",
w->journal->path);
log_info("%s/%s: Journal header limits reached or header out-of-date, rotating",
w->journal->directory->path, w->journal->filename);
r = do_rotate(&w->journal, compress, seal);
if (r < 0)
return r;
Expand All @@ -151,12 +151,12 @@ int writer_write(Writer *w,
return 1;
}

log_debug_errno(r, "%s: Write failed, rotating: %m", w->journal->path);
log_debug_errno(r, "%s/%s: Write failed, rotating: %m", w->journal->directory->path, w->journal->filename);
r = do_rotate(&w->journal, compress, seal);
if (r < 0)
return r;
else
log_debug("%s: Successfully rotated journal", w->journal->path);
log_debug("%s/%s: Successfully rotated journal", w->journal->directory->path, w->journal->filename);

log_debug("Retrying write.");
r = journal_file_append_entry(w->journal, ts, iovw->iovec, iovw->count,
Expand Down
34 changes: 22 additions & 12 deletions src/journal-remote/journal-remote.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,20 @@ static int spawn_getter(const char *getter, const char *url) {
#define filename_escape(s) xescape((s), "/ ")

static int open_output(Writer *w, const char* host) {
_cleanup_free_ char *_output = NULL;
const char *output;
_cleanup_free_ char *directory = NULL;
_cleanup_free_ char *filename = NULL;
JournalDirectory *dir;
int r;

switch (arg_split_mode) {
case JOURNAL_WRITE_SPLIT_NONE:
case JOURNAL_WRITE_SPLIT_NONE: {
const char *output;

output = arg_output ?: REMOTE_JOURNAL_PATH "/remote.journal";
directory = dirname_malloc(output);
filename = basename_malloc(output);
break;
}

case JOURNAL_WRITE_SPLIT_HOST: {
_cleanup_free_ char *name;
Expand All @@ -180,31 +186,35 @@ static int open_output(Writer *w, const char* host) {
if (!name)
return log_oom();

r = asprintf(&_output, "%s/remote-%s.journal",
arg_output ?: REMOTE_JOURNAL_PATH,
name);
directory = strdup(arg_output ?: REMOTE_JOURNAL_PATH);
r = asprintf(&filename, "remote-%s.journal", name);
if (r < 0)
return log_oom();

output = _output;
break;
}

default:
assert_not_reached("what?");
}

r = journal_file_open_reliably(output,
r = journal_directory_open(directory, &dir);
if (r < 0) {
log_error_errno(r, "Failed to open journal directory %s: %m",
directory);
return r;
}
r = journal_file_open_reliably(dir, filename,
O_RDWR|O_CREAT, 0640,
arg_compress, arg_seal,
&w->metrics,
w->mmap,
NULL, &w->journal);
dir = journal_directory_unref(dir);
if (r < 0)
log_error_errno(r, "Failed to open output journal %s: %m",
output);
log_error_errno(r, "Failed to open output journal %s/%s: %m",
directory, filename);
else
log_debug("Opened output file %s", w->journal->path);
log_debug("Opened output file %s/%s", w->journal->directory->path, w->journal->filename);
return r;
}

Expand Down
141 changes: 141 additions & 0 deletions src/journal/journal-dir.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/

/***
This file is part of systemd.

Copyright 2015 Endocode AG

systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.

systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/

#include "journal-dir.h"
#include "macro.h"
#include "util.h"

static int journal_directory_new_steal(char *path, int fd, JournalDirectory **dir) {
JournalDirectory *d;

assert(path);
assert(fd >= 0);
assert(dir);

d = new0(JournalDirectory, 1);
if (!d)
return -ENOMEM;
d->path = path;
d->fd = fd;
d->n_ref = 1;
*dir = d;
return 0;
}

int journal_directory_open(const char *path, JournalDirectory **dir)
{
_cleanup_free_ char *p = NULL;
_cleanup_close_ int fd = -1;
int r;

assert(path);
assert(dir);

fd = open(path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW);
if (fd < 0)
return -errno;

p = strdup(path);
if (!p)
return -ENOMEM;

r = journal_directory_new_steal(p, fd, dir);
if (r < 0)
return r;
p = NULL;
fd = -1;
return 0;
}

int journal_directory_new(const char *path, int fd, JournalDirectory **dir)
{
_cleanup_free_ char *p = NULL;
_cleanup_close_ int dfd = -1;
int r;

assert(path);
assert(fd >= 0);
assert(dir);

dfd = fcntl(fd, F_DUPFD_CLOEXEC, 3);
if (dfd < 0)
return -errno;

p = strdup(path);
if (!p)
return -ENOMEM;

r = journal_directory_new_steal(p, dfd, dir);
if (r < 0)
return r;
p = NULL;
dfd = -1;
return 0;
}

JournalDirectory *journal_directory_ref(JournalDirectory *dir)
{
assert(dir);
assert(dir->n_ref > 0);

dir->n_ref ++;
return dir;
}

JournalDirectory *journal_directory_unref(JournalDirectory *dir)
{
if (dir) {
PROTECT_ERRNO;

assert(dir->n_ref > 0);

dir->n_ref --;
if (!dir->n_ref) {
safe_close(dir->fd);
free(dir->path);
free(dir);
}
}

return NULL;
}

int journal_directory_opendir(JournalDirectory *dir, DIR **de)
{
int fd;
DIR* d;

assert(dir);
assert(de);

fd = fcntl(dir->fd, F_DUPFD_CLOEXEC, 3);
if (fd < 0)
return -errno;

d = fdopendir(fd);
if (!d) {
safe_close(fd);
return -errno;
}

*de = d;
return 0;
}
37 changes: 37 additions & 0 deletions src/journal/journal-dir.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/

#pragma once

/***
This file is part of systemd.

Copyright 2015 Endocode AG

systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.

systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/

#include <sys/types.h>
#include <dirent.h>

typedef struct JournalDirectory {
char *path;
int fd;
int n_ref;
} JournalDirectory;

int journal_directory_open(const char *path, JournalDirectory **dir);
int journal_directory_new(const char *path, int fd, JournalDirectory **dir);
JournalDirectory *journal_directory_ref(JournalDirectory *dir);
JournalDirectory *journal_directory_unref(JournalDirectory *dir);
int journal_directory_opendir(JournalDirectory *dir, DIR **de);
Loading