journalctl: Make temporary files directory configurable (#3574)

journalctl: Use env variable TMPDIR to save temporary files
This commit is contained in:
ottopotto 2016-06-30 17:59:06 +03:00 committed by Lennart Poettering
parent 30b42a9a36
commit 34a8f0811c
4 changed files with 89 additions and 3 deletions

View File

@ -38,6 +38,7 @@
#include "mkdir.h"
#include "parse-util.h"
#include "path-util.h"
#include "stat-util.h"
#include "stdio-util.h"
#include "string-util.h"
#include "strv.h"
@ -495,6 +496,34 @@ int get_files_in_directory(const char *path, char ***list) {
return n;
}
int var_tmp(char **ret) {
const char *tmp_dir = NULL;
const char *env_tmp_dir = NULL;
char *c = NULL;
int r;
assert(ret);
env_tmp_dir = getenv("TMPDIR");
if (env_tmp_dir != NULL) {
r = is_dir(env_tmp_dir, true);
if (r < 0 && r != -ENOENT)
return r;
if (r > 0)
tmp_dir = env_tmp_dir;
}
if (!tmp_dir)
tmp_dir = "/var/tmp";
c = strdup(tmp_dir);
if (!c)
return -ENOMEM;
*ret = c;
return 0;
}
int inotify_add_watch_fd(int fd, int what, uint32_t mask) {
char path[strlen("/proc/self/fd/") + DECIMAL_STR_MAX(int) + 1];
int r;

View File

@ -61,6 +61,8 @@ int mkfifo_atomic(const char *path, mode_t mode);
int get_files_in_directory(const char *path, char ***list);
int var_tmp(char **ret);
#define INOTIFY_EVENT_MAX (sizeof(struct inotify_event) + NAME_MAX + 1)
#define FOREACH_INOTIFY_EVENT(e, buffer, sz) \

View File

@ -26,6 +26,7 @@
#include "compress.h"
#include "fd-util.h"
#include "fileio.h"
#include "fs-util.h"
#include "journal-authenticate.h"
#include "journal-def.h"
#include "journal-file.h"
@ -825,6 +826,8 @@ int journal_file_verify(
int data_fd = -1, entry_fd = -1, entry_array_fd = -1;
unsigned i;
bool found_last = false;
_cleanup_free_ char *tmp_dir = NULL;
#ifdef HAVE_GCRYPT
uint64_t last_tag = 0;
#endif
@ -843,19 +846,25 @@ int journal_file_verify(
} else if (f->seal)
return -ENOKEY;
data_fd = open_tmpfile_unlinkable("/var/tmp", O_RDWR | O_CLOEXEC);
r = var_tmp(&tmp_dir);
if (r < 0) {
log_error_errno(r, "Failed to determine temporary directory: %m");
goto fail;
}
data_fd = open_tmpfile_unlinkable(tmp_dir, O_RDWR | O_CLOEXEC);
if (data_fd < 0) {
r = log_error_errno(data_fd, "Failed to create data file: %m");
goto fail;
}
entry_fd = open_tmpfile_unlinkable("/var/tmp", O_RDWR | O_CLOEXEC);
entry_fd = open_tmpfile_unlinkable(tmp_dir, O_RDWR | O_CLOEXEC);
if (entry_fd < 0) {
r = log_error_errno(entry_fd, "Failed to create entry file: %m");
goto fail;
}
entry_array_fd = open_tmpfile_unlinkable("/var/tmp", O_RDWR | O_CLOEXEC);
entry_array_fd = open_tmpfile_unlinkable(tmp_dir, O_RDWR | O_CLOEXEC);
if (entry_array_fd < 0) {
r = log_error_errno(entry_array_fd,
"Failed to create entry array file: %m");

View File

@ -82,10 +82,56 @@ static void test_get_files_in_directory(void) {
assert_se(get_files_in_directory(".", NULL) >= 0);
}
static void test_var_tmp(void) {
char *tmp_dir = NULL;
char *tmpdir_backup = NULL;
const char *default_var_tmp = NULL;
const char *var_name;
bool do_overwrite = true;
default_var_tmp = "/var/tmp";
var_name = "TMPDIR";
if (getenv(var_name) != NULL) {
tmpdir_backup = strdup(getenv(var_name));
assert_se(tmpdir_backup != NULL);
}
unsetenv(var_name);
var_tmp(&tmp_dir);
assert_se(!strcmp(tmp_dir, default_var_tmp));
free(tmp_dir);
setenv(var_name, "/tmp", do_overwrite);
assert_se(!strcmp(getenv(var_name), "/tmp"));
var_tmp(&tmp_dir);
assert_se(!strcmp(tmp_dir, "/tmp"));
free(tmp_dir);
setenv(var_name, "/88_does_not_exist_88", do_overwrite);
assert_se(!strcmp(getenv(var_name), "/88_does_not_exist_88"));
var_tmp(&tmp_dir);
assert_se(!strcmp(tmp_dir, default_var_tmp));
free(tmp_dir);
if (tmpdir_backup != NULL) {
setenv(var_name, tmpdir_backup, do_overwrite);
assert_se(!strcmp(getenv(var_name), tmpdir_backup));
free(tmpdir_backup);
}
}
int main(int argc, char *argv[]) {
test_unlink_noerrno();
test_readlink_and_make_absolute();
test_get_files_in_directory();
test_var_tmp();
return 0;
}