HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/libsystemd/sd-journal/sd-journal.c
Git blob: 3208ce9733f473c236071d28f6f34639076b6b14
Lines 1001–2000 of 3,837
uint64_t cp, n_entries;
int r;
assert(j);
assert(f);
/* Rate-limit tail timestamp refreshes during iteration. Calling this unconditionally is
* O(N x files) volatile mmap overhead that makes large 'journalctl -n N' queries unusably
* slow. Periodic refresh keeps cross-boot ordering reasonably fresh and provides a fallback
* for any missed inotify events. */
if (ratelimit_below(&f->tail_timestamp_ratelimit))
(void) journal_file_read_tail_timestamp(j, f);
n_entries = le64toh(f->header->n_entries);
/* If we hit EOF before, we don't need to look into this file again
* unless direction changed or new entries appeared. */
if (f->last_direction == direction &&
f->location_type == (direction == DIRECTION_DOWN ? LOCATION_TAIL : LOCATION_HEAD) &&
n_entries == f->last_n_entries)
return 0;
f->last_n_entries = n_entries;
if (f->last_direction == direction && f->current_offset > 0) {
/* LOCATION_SEEK here means we did the work in a previous
* iteration and the current location already points to a
* candidate entry. */
if (f->location_type != LOCATION_SEEK) {
r = next_with_matches(j, f, direction, &c, &cp);
if (r <= 0)
return r;
journal_file_save_location(f, c, cp);
}
} else {
r = find_location_with_matches(j, f, direction, &c, &cp);
/* LOCATION_SEEK specified to j->current_location.type here means that this is called first
* after sd_journal_seek_monotonic_usec() or friends was called. In that case, this file may
* not contain any matching entries with the user-specified location, but another file may
* contain them. If so, the second call of this function will use the seqnum, and we may find
* an entry in _this_ file with the seqnum. To prevent the second call of this function exits
* earlier by the first 'if' block of this function, do not save the direction if the current
* location is LOCATION_SEEK. */
if (r > 0 || j->current_location.type != LOCATION_SEEK)
f->last_direction = direction;
else
assert(f->last_direction == _DIRECTION_INVALID);
if (r <= 0)
return r;
journal_file_save_location(f, c, cp);
}
/* OK, we found the spot, now let's advance until an entry
* that is actually different from what we were previously
* looking at. This is necessary to handle entries which exist
* in two (or more) journal files, and which shall all be
* suppressed but one. */
for (;;) {
bool found;
if (j->current_location.type == LOCATION_DISCRETE) {
r = compare_with_location(j, f, &j->current_location, j->current_file);
found = direction == DIRECTION_DOWN ? r > 0 : r < 0;
} else
found = true;
if (found)
return 1;
r = next_with_matches(j, f, direction, &c, &cp);
if (r <= 0)
return r;
journal_file_save_location(f, c, cp);
}
}
static int compare_locations(sd_journal *j, JournalFile *af, JournalFile *bf) {
int r;
assert(j);
assert(af);
assert(af->header);
assert(bf);
assert(bf->header);
assert(af->location_type == LOCATION_SEEK);
assert(bf->location_type == LOCATION_SEEK);
/* If contents, timestamps and seqnum match, these entries are identical. */
if (sd_id128_equal(af->current_boot_id, bf->current_boot_id) &&
af->current_monotonic == bf->current_monotonic &&
af->current_realtime == bf->current_realtime &&
af->current_xor_hash == bf->current_xor_hash &&
sd_id128_equal(af->header->seqnum_id, bf->header->seqnum_id) &&
af->current_seqnum == bf->current_seqnum)
return 0;
if (sd_id128_equal(af->header->seqnum_id, bf->header->seqnum_id)) {
/* If this is from the same seqnum source, compare seqnums */
r = CMP(af->current_seqnum, bf->current_seqnum);
if (r != 0)
return r;
/* Wow! This is weird, different data but the same seqnums? Something is borked, but let's
* make the best of it and compare by time. */
}
if (sd_id128_equal(af->current_boot_id, bf->current_boot_id))
/* If the boot id matches, compare monotonic time */
r = CMP(af->current_monotonic, bf->current_monotonic);
else
/* If they don't match try to compare boot IDs */
r = compare_boot_ids(j, af->current_boot_id, bf->current_boot_id);
if (r != 0)
return r;
/* Otherwise, compare UTC time */
r = CMP(af->current_realtime, bf->current_realtime);
if (r != 0)
return r;
/* Finally, compare by contents */
return CMP(af->current_xor_hash, bf->current_xor_hash);
}
static int real_journal_next(sd_journal *j, direction_t direction) {
unsigned n_files;
const void **files;
int r;
assert_return(j, -EINVAL);
assert_return(!journal_origin_changed(j), -ECHILD);
for (;;) {
JournalFile *new_file = NULL, *exact_match = NULL;
Object *o;
r = iterated_cache_get(j->files_cache, NULL, &files, &n_files);
if (r < 0)
return r;
FOREACH_ARRAY(_f, files, n_files) {
JournalFile *f = (JournalFile*) *_f;
bool found;
r = next_beyond_location(j, f, direction);
if (r < 0) {
log_debug_errno(r, "Can't iterate through %s, ignoring: %m", f->path);
remove_file_real(j, f);
continue;
} else if (r == 0) {
f->location_type = direction == DIRECTION_DOWN ?
LOCATION_TAIL : LOCATION_HEAD;
continue;
}
if (!new_file)
found = true;
else {
r = compare_locations(j, f, new_file);
found = direction == DIRECTION_DOWN ? r < 0 : r > 0;
}
if (found)
new_file = f;
/* Track the file that holds the cursor's exact entry (matching seqnum_id and
* seqnum). On systems without a reliable (or missing) RTC, compare_boot_ids() can
* produce incorrect cross-boot ordering. After the loop, we detect when
* compare_locations() preferred the wrong file and override the choice if needed.
*
* See https://github.com/systemd/systemd/issues/31516 */
if (j->current_location.type == LOCATION_SEEK &&
j->current_location.seqnum_set &&
sd_id128_equal(f->header->seqnum_id, j->current_location.seqnum_id) &&
f->current_seqnum == j->current_location.seqnum)
exact_match = f;
}
if (exact_match)
new_file = exact_match;
if (!new_file)
return 0;
r = journal_file_move_to_object(new_file, OBJECT_ENTRY, new_file->current_offset, &o);
if (r < 0) {
/* Vacuuming can unlink and deallocate the file we just picked, which surfaces as
* one of these errors. Confirm the file is really gone before dropping it: -EIDRM
* already comes from the journal_file_fstat() inside the lookup, the others need a
* stat of our own. */
if (!IN_SET(r, -EADDRNOTAVAIL, -EBADMSG, -EIDRM, -EIO))
return r;
if (r != -EIDRM && journal_file_fstat(new_file) != -EIDRM)
return r;
log_debug_errno(r, "Can't read selected entry from removed journal file '%s', ignoring: %m",
new_file->path);
remove_file_real(j, new_file);
/* Removing the selected file guarantees that the next iteration makes progress. */
continue;
}
set_location(j, new_file, o);
return 1;
}
}
_public_ int sd_journal_next(sd_journal *j) {
return real_journal_next(j, DIRECTION_DOWN);
}
_public_ int sd_journal_previous(sd_journal *j) {
return real_journal_next(j, DIRECTION_UP);
}
_public_ int sd_journal_step_one(sd_journal *j, int advanced) {
assert_return(j, -EINVAL);
if (j->current_location.type == LOCATION_HEAD)
return sd_journal_next(j);
if (j->current_location.type == LOCATION_TAIL)
return sd_journal_previous(j);
return real_journal_next(j, advanced ? DIRECTION_DOWN : DIRECTION_UP);
}
static int real_journal_next_skip(sd_journal *j, direction_t direction, uint64_t skip) {
int c = 0, r;
assert_return(j, -EINVAL);
assert_return(!journal_origin_changed(j), -ECHILD);
assert_return(skip <= INT_MAX, -ERANGE);
if (skip == 0) {
/* If this is not a discrete skip, then at least
* resolve the current location */
if (j->current_location.type != LOCATION_DISCRETE) {
r = real_journal_next(j, direction);
if (r < 0)
return r;
}
return 0;
}
do {
r = real_journal_next(j, direction);
if (r < 0)
return r;
if (r == 0)
return c;
skip--;
c++;
} while (skip > 0);
return c;
}
_public_ int sd_journal_next_skip(sd_journal *j, uint64_t skip) {
return real_journal_next_skip(j, DIRECTION_DOWN, skip);
}
_public_ int sd_journal_previous_skip(sd_journal *j, uint64_t skip) {
return real_journal_next_skip(j, DIRECTION_UP, skip);
}
_public_ int sd_journal_get_cursor(sd_journal *j, char **ret) {
Object *o;
int r;
assert_return(j, -EINVAL);
assert_return(!journal_origin_changed(j), -ECHILD);
if (!j->current_file || j->current_file->current_offset <= 0)
return -EADDRNOTAVAIL;
r = journal_file_move_to_object(j->current_file, OBJECT_ENTRY, j->current_file->current_offset, &o);
if (r < 0)
return r;
if (!ret)
return 0;
if (asprintf(ret,
"s=%s;i=%"PRIx64";b=%s;m=%"PRIx64";t=%"PRIx64";x=%"PRIx64,
SD_ID128_TO_STRING(j->current_file->header->seqnum_id), le64toh(o->entry.seqnum),
SD_ID128_TO_STRING(o->entry.boot_id), le64toh(o->entry.monotonic),
le64toh(o->entry.realtime),
le64toh(o->entry.xor_hash)) < 0)
return -ENOMEM;
return 0;
}
_public_ int sd_journal_seek_cursor(sd_journal *j, const char *cursor) {
unsigned long long seqnum, monotonic, realtime, xor_hash;
bool seqnum_id_set = false,
seqnum_set = false,
boot_id_set = false,
monotonic_set = false,
realtime_set = false,
xor_hash_set = false;
sd_id128_t seqnum_id, boot_id;
int r;
assert_return(j, -EINVAL);
assert_return(!journal_origin_changed(j), -ECHILD);
assert_return(!isempty(cursor), -EINVAL);
for (const char *p = cursor;;) {
_cleanup_free_ char *word = NULL;
r = extract_first_word(&p, &word, ";", EXTRACT_DONT_COALESCE_SEPARATORS);
if (r < 0)
return r;
if (r == 0)
break;
if (word[0] == '\0' || word[1] != '=')
return -EINVAL;
switch (word[0]) {
case 's':
seqnum_id_set = true;
r = sd_id128_from_string(word + 2, &seqnum_id);
if (r < 0)
return r;
break;
case 'i':
seqnum_set = true;
if (sscanf(word + 2, "%llx", &seqnum) != 1)
return -EINVAL;
break;
case 'b':
boot_id_set = true;
r = sd_id128_from_string(word + 2, &boot_id);
if (r < 0)
return r;
break;
case 'm':
monotonic_set = true;
if (sscanf(word + 2, "%llx", &monotonic) != 1)
return -EINVAL;
break;
case 't':
realtime_set = true;
if (sscanf(word + 2, "%llx", &realtime) != 1)
return -EINVAL;
break;
case 'x':
xor_hash_set = true;
if (sscanf(word + 2, "%llx", &xor_hash) != 1)
return -EINVAL;
break;
}
}
if ((!seqnum_set || !seqnum_id_set) &&
(!monotonic_set || !boot_id_set) &&
!realtime_set)
return -EINVAL;
detach_location(j);
j->current_location = (Location) {
.type = LOCATION_SEEK,
};
if (realtime_set) {
j->current_location.realtime = (uint64_t) realtime;
j->current_location.realtime_set = true;
}
if (seqnum_set && seqnum_id_set) {
j->current_location.seqnum = (uint64_t) seqnum;
j->current_location.seqnum_id = seqnum_id;
j->current_location.seqnum_set = true;
}
if (monotonic_set && boot_id_set) {
j->current_location.monotonic = (uint64_t) monotonic;
j->current_location.boot_id = boot_id;
j->current_location.monotonic_set = true;
}
if (xor_hash_set) {
j->current_location.xor_hash = (uint64_t) xor_hash;
j->current_location.xor_hash_set = true;
}
return 0;
}
_public_ int sd_journal_test_cursor(sd_journal *j, const char *cursor) {
int r;
Object *o;
assert_return(j, -EINVAL);
assert_return(!journal_origin_changed(j), -ECHILD);
assert_return(!isempty(cursor), -EINVAL);
if (!j->current_file || j->current_file->current_offset <= 0)
return -EADDRNOTAVAIL;
r = journal_file_move_to_object(j->current_file, OBJECT_ENTRY, j->current_file->current_offset, &o);
if (r < 0)
return r;
for (;;) {
_cleanup_free_ char *item = NULL;
unsigned long long ll;
sd_id128_t id;
r = extract_first_word(&cursor, &item, ";", EXTRACT_DONT_COALESCE_SEPARATORS);
if (r < 0)
return r;
if (r == 0)
break;
if (strlen(item) < 2 || item[1] != '=')
return -EINVAL;
switch (item[0]) {
case 's':
r = sd_id128_from_string(item+2, &id);
if (r < 0)
return r;
if (!sd_id128_equal(id, j->current_file->header->seqnum_id))
return 0;
break;
case 'i':
if (sscanf(item+2, "%llx", &ll) != 1)
return -EINVAL;
if (ll != le64toh(o->entry.seqnum))
return 0;
break;
case 'b':
r = sd_id128_from_string(item+2, &id);
if (r < 0)
return r;
if (!sd_id128_equal(id, o->entry.boot_id))
return 0;
break;
case 'm':
if (sscanf(item+2, "%llx", &ll) != 1)
return -EINVAL;
if (ll != le64toh(o->entry.monotonic))
return 0;
break;
case 't':
if (sscanf(item+2, "%llx", &ll) != 1)
return -EINVAL;
if (ll != le64toh(o->entry.realtime))
return 0;
break;
case 'x':
if (sscanf(item+2, "%llx", &ll) != 1)
return -EINVAL;
if (ll != le64toh(o->entry.xor_hash))
return 0;
break;
}
}
return 1;
}
_public_ int sd_journal_seek_monotonic_usec(sd_journal *j, sd_id128_t boot_id, uint64_t usec) {
assert_return(j, -EINVAL);
assert_return(!journal_origin_changed(j), -ECHILD);
detach_location(j);
j->current_location = (Location) {
.type = LOCATION_SEEK,
.boot_id = boot_id,
.monotonic = usec,
.monotonic_set = true,
};
return 0;
}
_public_ int sd_journal_seek_realtime_usec(sd_journal *j, uint64_t usec) {
assert_return(j, -EINVAL);
assert_return(!journal_origin_changed(j), -ECHILD);
detach_location(j);
j->current_location = (Location) {
.type = LOCATION_SEEK,
.realtime = usec,
.realtime_set = true,
};
return 0;
}
_public_ int sd_journal_seek_head(sd_journal *j) {
assert_return(j, -EINVAL);
assert_return(!journal_origin_changed(j), -ECHILD);
detach_location(j);
j->current_location = (Location) {
.type = LOCATION_HEAD,
};
return 0;
}
_public_ int sd_journal_seek_tail(sd_journal *j) {
assert_return(j, -EINVAL);
assert_return(!journal_origin_changed(j), -ECHILD);
detach_location(j);
j->current_location = (Location) {
.type = LOCATION_TAIL,
};
return 0;
}
static void check_network(sd_journal *j, int fd) {
assert(j);
if (j->on_network)
return;
j->on_network = fd_is_network_fs(fd);
}
static bool file_has_type_prefix(const char *prefix, const char *filename) {
const char *full, *tilded, *atted;
full = strjoina(prefix, ".journal");
tilded = strjoina(full, "~");
atted = strjoina(prefix, "@");
return STR_IN_SET(filename, full, tilded) ||
startswith(filename, atted);
}
static bool file_type_wanted(int flags, const char *filename) {
assert(filename);
if (!ENDSWITH_SET(filename, ".journal", ".journal~"))
return false;
/* no flags set → every type is OK */
if (!(flags & (SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER)))
return true;
if (FLAGS_SET(flags, SD_JOURNAL_CURRENT_USER)) {
char prefix[5 + DECIMAL_STR_MAX(uid_t) + 1];
xsprintf(prefix, "user-" UID_FMT, getuid());
if (file_has_type_prefix(prefix, filename))
return true;
/* If SD_JOURNAL_CURRENT_USER is specified and we are invoked under a system UID, then
* automatically enable SD_JOURNAL_SYSTEM too, because journald will actually put system user
* data into the system journal. */
if (uid_for_system_journal(getuid()))
flags |= SD_JOURNAL_SYSTEM;
}
if (FLAGS_SET(flags, SD_JOURNAL_SYSTEM) && file_has_type_prefix("system", filename))
return true;
return false;
}
static bool path_has_prefix(sd_journal *j, const char *path, const char *prefix) {
assert(j);
assert(path);
assert(prefix);
if (j->toplevel_fd >= 0)
return false;
return path_startswith(path, prefix);
}
static void track_file_disposition(sd_journal *j, JournalFile *f) {
assert(j);
assert(f);
if (!j->has_runtime_files && path_has_prefix(j, f->path, "/run"))
j->has_runtime_files = true;
else if (!j->has_persistent_files && path_has_prefix(j, f->path, "/var"))
j->has_persistent_files = true;
}
static int add_any_file(
sd_journal *j,
int fd,
const char *path) {
_cleanup_close_ int our_fd = -EBADF;
_cleanup_free_ char *resolved_path = NULL;
JournalFile *f;
struct stat st;
int r;
assert(j);
assert(fd >= 0 || path);
if (fd < 0) {
assert(path); /* For gcc. */
if (j->toplevel_fd >= 0)
/* If there's a top-level fd defined make the path relative, explicitly, since otherwise
* openat() ignores the first argument. */
fd = our_fd = openat(j->toplevel_fd, skip_leading_slash(path), O_RDONLY|O_CLOEXEC|O_NONBLOCK);
else
fd = our_fd = open(path, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
if (fd < 0) {
r = log_debug_errno(errno, "Failed to open journal file %s: %m", path);
goto error;
}
r = fd_nonblock(fd, false);
if (r < 0) {
r = log_debug_errno(errno, "Failed to turn off O_NONBLOCK for %s: %m", path);
goto error;
}
r = fd_get_path(fd, &resolved_path);
if (r < 0) {
r = log_debug_errno(r, "Failed to resolve path '%s': %m", path);
goto error;
}
path = resolved_path;
}
if (fstat(fd, &st) < 0) {
r = log_debug_errno(errno, "Failed to fstat %s: %m", path ?: "fd");
goto error;
}
r = stat_verify_regular(&st);
if (r < 0) {
log_debug_errno(r, "Refusing to open %s: %m", path ?: "fd");
goto error;
}
if (path) {
f = ordered_hashmap_get(j->files, path);
if (f) {
if (stat_inode_same(&f->last_stat, &st)) {
/* We already track this file, under the same path and with the same
* device/inode numbers, it's hence really the same. Mark this file as seen
* in this generation. This is used to GC old files in process_q_overflow()
* to detect journal files that are still there and discern them from those
* which are gone. */
f->last_seen_generation = j->generation;
(void) journal_file_read_tail_timestamp(j, f);
return 0;
}
/* So we tracked a file under this name, but it has a different inode/device. In that
* case, it got replaced (probably due to rotation?), let's drop it hence from our
* list. */
remove_file_real(j, f);
f = NULL;
}
}
if (ordered_hashmap_size(j->files) >= JOURNAL_FILES_MAX) {
r = log_debug_errno(SYNTHETIC_ERRNO(ETOOMANYREFS),
"Too many open journal files, not adding %s.", path ?: "fd");
goto error;
}
r = journal_file_open(fd, path, O_RDONLY, 0, 0, 0, NULL, j->mmap, NULL, &f);
if (r < 0) {
log_debug_errno(r, "Failed to open journal file %s: %m", path ?: "from fd");
goto error;
}
/* journal_file_dump(f); */
/* journal_file_open() generates an replacement fname if necessary, so we can use f->path. */
r = ordered_hashmap_put(j->files, f->path, f);
if (r < 0) {
f->close_fd = false; /* Make sure journal_file_close() doesn't close the caller's fd
* (or our own). The caller or we will do that ourselves. */
(void) journal_file_close(f);
goto error;
}
TAKE_FD(our_fd); /* the fd is now owned by the JournalFile object */
f->last_seen_generation = j->generation;
track_file_disposition(j, f);
check_network(j, f->fd);
(void) journal_file_read_tail_timestamp(j, f);
j->current_invalidate_counter++;
log_debug("File %s added.", f->path);
return 0;
error:
(void) journal_put_error(j, r, path); /* path==NULL is OK. */
return r;
}
int journal_get_directories(sd_journal *j, char ***ret) {
_cleanup_strv_free_ char **paths = NULL;
JournalFile *f;
const char *p;
size_t n = SIZE_MAX;
int r;
assert(j);
assert(ret);
/* This returns parent directories of opened journal files. */
ORDERED_HASHMAP_FOREACH_KEY(f, p, j->files) {
_cleanup_free_ char *d = NULL;
/* Ignore paths generated from fd. */
if (path_startswith(p, "/proc/"))
continue;
r = path_extract_directory(p, &d);
if (r < 0)
return r;
if (path_strv_contains(paths, d))
continue;
r = strv_extend_with_size(&paths, &n, d);
if (r < 0)
return r;
}
*ret = TAKE_PTR(paths);
return 0;
}
static int add_file_by_name(
sd_journal *j,
const char *prefix,
const char *filename) {
_cleanup_free_ char *path = NULL;
assert(j);
assert(prefix);
assert(filename);
if (j->no_new_files)
return 0;
if (!file_type_wanted(j->flags, filename))
return 0;
path = path_join(prefix, filename);
if (!path)
return -ENOMEM;
return add_any_file(j, /* fd= */ -EBADF, path);
}
static int remove_file_by_name(
sd_journal *j,
const char *prefix,
const char *filename) {
_cleanup_free_ char *path = NULL;
JournalFile *f;
assert(j);
assert(prefix);
assert(filename);
path = path_join(prefix, filename);
if (!path)
return -ENOMEM;
f = ordered_hashmap_get(j->files, path);
if (!f)
return 0;
remove_file_real(j, f);
return 1;
}
static void remove_file_real(sd_journal *j, JournalFile *f) {
assert(j);
assert(f);
(void) ordered_hashmap_remove(j->files, f->path);
log_debug("File %s removed.", f->path);
if (j->current_file == f) {
j->current_file = NULL;
j->current_field = 0;
}
if (j->unique_file == f) {
/* Jump to the next unique_file or NULL if that one was last */
j->unique_file = ordered_hashmap_next(j->files, j->unique_file->path);
j->unique_offset = 0;
if (!j->unique_file)
j->unique_file_lost = true;
}
if (j->fields_file == f) {
j->fields_file = ordered_hashmap_next(j->files, j->fields_file->path);
j->fields_offset = 0;
if (!j->fields_file)
j->fields_file_lost = true;
}
journal_file_unlink_newest_by_boot_id(j, f);
(void) journal_file_close(f);
j->current_invalidate_counter++;
}
static int dirname_is_machine_id(const char *fn) {
sd_id128_t id, machine;
const char *e;
int r;
/* Returns true if the specified directory name matches the local machine ID */
r = sd_id128_get_machine(&machine);
if (r < 0)
return r;
e = strchr(fn, '.');
if (e) {
const char *k;
/* Looks like it has a namespace suffix. Verify that. */
if (!log_namespace_name_valid(e + 1))
return false;
k = strndupa_safe(fn, e - fn);
r = sd_id128_from_string(k, &id);
} else
r = sd_id128_from_string(fn, &id);
if (r < 0)
return r;
return sd_id128_equal(id, machine);
}
static int dirname_has_namespace(const char *fn, const char *namespace) {
const char *e;
/* Returns true if the specified directory name matches the specified namespace */
e = strchr(fn, '.');
if (e) {
const char *k;
if (!namespace)
return false;
if (!streq(e + 1, namespace))
return false;
k = strndupa_safe(fn, e - fn);
return id128_is_valid(k);
}
if (namespace)
return false;
return id128_is_valid(fn);
}
static bool dirent_is_journal_file(const struct dirent *de) {
assert(de);
/* Returns true if the specified directory entry looks like a journal file we might be interested in */
if (!IN_SET(de->d_type, DT_REG, DT_LNK, DT_UNKNOWN))
return false;
return endswith(de->d_name, ".journal") ||
endswith(de->d_name, ".journal~");
}
static bool dirent_is_journal_subdir(const struct dirent *de) {
const char *e, *n;
assert(de);
/* returns true if the specified directory entry looks like a directory that might contain journal
* files we might be interested in, i.e. is either a 128-bit ID or a 128-bit ID suffixed by a
* namespace. */
if (!IN_SET(de->d_type, DT_DIR, DT_LNK, DT_UNKNOWN))
return false;
e = strchr(de->d_name, '.');
if (!e)
return id128_is_valid(de->d_name); /* No namespace */
n = strndupa_safe(de->d_name, e - de->d_name);
if (!id128_is_valid(n))
return false;
return log_namespace_name_valid(e + 1);
}
static int directory_open(sd_journal *j, const char *path, DIR **ret) {
DIR *d;
assert(j);
assert(path);
assert(ret);
if (j->toplevel_fd < 0)
d = opendir(path);
else
/* Open the specified directory relative to the toplevel fd. Enforce that the path specified is
* relative, by dropping the initial slash */
d = xopendirat(j->toplevel_fd, skip_leading_slash(path), 0);
if (!d)
return -errno;
*ret = d;
return 0;
}
static Directory* directory_free(Directory *d) {
if (!d)
return NULL;
if (d->journal) {
if (d->wd > 0 &&
hashmap_remove_value(d->journal->directories_by_wd, INT_TO_PTR(d->wd), d) &&
d->journal->inotify_fd >= 0)
(void) inotify_rm_watch(d->journal->inotify_fd, d->wd);
if (d->path)
hashmap_remove_value(d->journal->directories_by_path, d->path, d);
}
if (d->path) {
if (d->is_root)
log_debug("Root directory %s removed.", d->path);
else
log_debug("Directory %s removed.", d->path);
free(d->path);
}
return mfree(d);
}
DEFINE_TRIVIAL_CLEANUP_FUNC(Directory*, directory_free);
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
directories_by_path_hash_ops,
char,
path_hash_func,
path_compare,
Directory,
directory_free);
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
directories_by_wd_hash_ops,
void,
trivial_hash_func,
trivial_compare_func,