mirror of
https://github.com/systemd/systemd
synced 2024-10-15 04:24:19 +00:00
sd-journal: introduce SD_JOURNAL_TAKE_DIRECTORY_FD flag for sd_journal_open_directory_fd()
If it is called with the flag, then the provided file descriptor will be owned by the sd_journal object, and will be closed in sd_journal_close().
This commit is contained in:
parent
12fff85d55
commit
4a45a2e0e3
|
@ -32,6 +32,7 @@
|
||||||
<refname>SD_JOURNAL_OS_ROOT</refname>
|
<refname>SD_JOURNAL_OS_ROOT</refname>
|
||||||
<refname>SD_JOURNAL_ALL_NAMESPACES</refname>
|
<refname>SD_JOURNAL_ALL_NAMESPACES</refname>
|
||||||
<refname>SD_JOURNAL_INCLUDE_DEFAULT_NAMESPACE</refname>
|
<refname>SD_JOURNAL_INCLUDE_DEFAULT_NAMESPACE</refname>
|
||||||
|
<refname>SD_JOURNAL_TAKE_DIRECTORY_FD</refname>
|
||||||
<refpurpose>Open the system journal for reading</refpurpose>
|
<refpurpose>Open the system journal for reading</refpurpose>
|
||||||
</refnamediv>
|
</refnamediv>
|
||||||
|
|
||||||
|
@ -136,7 +137,13 @@
|
||||||
|
|
||||||
<para><function>sd_journal_open_directory_fd()</function> is similar to
|
<para><function>sd_journal_open_directory_fd()</function> is similar to
|
||||||
<function>sd_journal_open_directory()</function>, but takes a file descriptor referencing a directory in the file
|
<function>sd_journal_open_directory()</function>, but takes a file descriptor referencing a directory in the file
|
||||||
system instead of an absolute file system path.</para>
|
system instead of an absolute file system path. In addtion to the flags accepted by
|
||||||
|
<function>sd_journal_open_directory()</function>, this function also accepts
|
||||||
|
<constant>SD_JOURNAL_TAKE_DIRECTORY_FD</constant>. If <constant>SD_JOURNAL_TAKE_DIRECTORY_FD</constant> is
|
||||||
|
specified, the function will take the ownership of the specified file descriptor on success, and it will be
|
||||||
|
closed by <function>sd_journal_close()</function>, hence the caller of the function must not close the file
|
||||||
|
descriptor. When the flag is not specified, <function>sd_journal_close()</function> will not close the file
|
||||||
|
descriptor, so the caller should close it after <function>sd_journal_close()</function>.</para>
|
||||||
|
|
||||||
<para><function>sd_journal_open_files()</function> is similar to <function>sd_journal_open()</function> but takes a
|
<para><function>sd_journal_open_files()</function> is similar to <function>sd_journal_open()</function> but takes a
|
||||||
<constant>NULL</constant>-terminated list of file paths to open. All files will be opened and interleaved
|
<constant>NULL</constant>-terminated list of file paths to open. All files will be opened and interleaved
|
||||||
|
|
|
@ -2177,13 +2177,16 @@ _public_ int sd_journal_open_files(sd_journal **ret, const char **paths, int fla
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#define OPEN_DIRECTORY_FD_ALLOWED_FLAGS \
|
#define OPEN_DIRECTORY_FD_ALLOWED_FLAGS \
|
||||||
(SD_JOURNAL_OS_ROOT | \
|
(SD_JOURNAL_OS_ROOT | \
|
||||||
SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER )
|
SD_JOURNAL_SYSTEM | \
|
||||||
|
SD_JOURNAL_CURRENT_USER | \
|
||||||
|
SD_JOURNAL_TAKE_DIRECTORY_FD)
|
||||||
|
|
||||||
_public_ int sd_journal_open_directory_fd(sd_journal **ret, int fd, int flags) {
|
_public_ int sd_journal_open_directory_fd(sd_journal **ret, int fd, int flags) {
|
||||||
_cleanup_(sd_journal_closep) sd_journal *j = NULL;
|
_cleanup_(sd_journal_closep) sd_journal *j = NULL;
|
||||||
struct stat st;
|
struct stat st;
|
||||||
|
bool take_fd;
|
||||||
int r;
|
int r;
|
||||||
|
|
||||||
assert_return(ret, -EINVAL);
|
assert_return(ret, -EINVAL);
|
||||||
|
@ -2196,7 +2199,8 @@ _public_ int sd_journal_open_directory_fd(sd_journal **ret, int fd, int flags) {
|
||||||
if (!S_ISDIR(st.st_mode))
|
if (!S_ISDIR(st.st_mode))
|
||||||
return -EBADFD;
|
return -EBADFD;
|
||||||
|
|
||||||
j = journal_new(flags, NULL, NULL);
|
take_fd = FLAGS_SET(flags, SD_JOURNAL_TAKE_DIRECTORY_FD);
|
||||||
|
j = journal_new(flags & ~SD_JOURNAL_TAKE_DIRECTORY_FD, NULL, NULL);
|
||||||
if (!j)
|
if (!j)
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
|
|
||||||
|
@ -2209,6 +2213,8 @@ _public_ int sd_journal_open_directory_fd(sd_journal **ret, int fd, int flags) {
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
return r;
|
return r;
|
||||||
|
|
||||||
|
SET_FLAG(j->flags, SD_JOURNAL_TAKE_DIRECTORY_FD, take_fd);
|
||||||
|
|
||||||
*ret = TAKE_PTR(j);
|
*ret = TAKE_PTR(j);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -2288,6 +2294,9 @@ _public_ void sd_journal_close(sd_journal *j) {
|
||||||
hashmap_free(j->directories_by_path);
|
hashmap_free(j->directories_by_path);
|
||||||
hashmap_free(j->directories_by_wd);
|
hashmap_free(j->directories_by_wd);
|
||||||
|
|
||||||
|
if (FLAGS_SET(j->flags, SD_JOURNAL_TAKE_DIRECTORY_FD))
|
||||||
|
safe_close(j->toplevel_fd);
|
||||||
|
|
||||||
safe_close(j->inotify_fd);
|
safe_close(j->inotify_fd);
|
||||||
|
|
||||||
if (j->mmap) {
|
if (j->mmap) {
|
||||||
|
|
|
@ -71,6 +71,7 @@ enum {
|
||||||
SD_JOURNAL_OS_ROOT = 1 << 4,
|
SD_JOURNAL_OS_ROOT = 1 << 4,
|
||||||
SD_JOURNAL_ALL_NAMESPACES = 1 << 5, /* Show all namespaces, not just the default or specified one */
|
SD_JOURNAL_ALL_NAMESPACES = 1 << 5, /* Show all namespaces, not just the default or specified one */
|
||||||
SD_JOURNAL_INCLUDE_DEFAULT_NAMESPACE = 1 << 6, /* Show default namespace in addition to specified one */
|
SD_JOURNAL_INCLUDE_DEFAULT_NAMESPACE = 1 << 6, /* Show default namespace in addition to specified one */
|
||||||
|
SD_JOURNAL_TAKE_DIRECTORY_FD = 1 << 7, /* sd_journal_open_directory_fd() will take ownership of the provided file descriptor. */
|
||||||
|
|
||||||
SD_JOURNAL_SYSTEM_ONLY _sd_deprecated_ = SD_JOURNAL_SYSTEM /* old name */
|
SD_JOURNAL_SYSTEM_ONLY _sd_deprecated_ = SD_JOURNAL_SYSTEM /* old name */
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue