daandemeyer / rpms / systemd

Forked from rpms/systemd 2 years ago
Clone
923a60
From 5ec508cc5c13d831c93ce98d84b1d9cedb0117a7 Mon Sep 17 00:00:00 2001
923a60
From: Jan Synacek <jsynacek@redhat.com>
923a60
Date: Mon, 1 Feb 2016 09:23:58 +0100
923a60
Subject: [PATCH] sd-journal: introduce has_runtime_files and
923a60
 has_persistent_files
923a60
923a60
Also introduce sd_journal_has_runtime_files() and
923a60
sd_journal_has_persistent_files() to the public API. These functions
923a60
can be used to easily find out if the open journal files are runtime
923a60
and/or persistent.
923a60
923a60
Cherry-picked from: 39fd5b08a73f144a20202a665bd25cad51d8a90b
923a60
Resolves: #1082179
923a60
---
923a60
 Makefile-man.am                      |  7 ++
923a60
 man/sd-journal.xml                   |  8 ++-
923a60
 man/sd_journal_has_runtime_files.xml | 95 ++++++++++++++++++++++++++++
923a60
 src/journal/journal-internal.h       |  2 +
923a60
 src/journal/sd-journal.c             | 29 ++++++---
923a60
 src/systemd/sd-journal.h             |  3 +
923a60
 6 files changed, 133 insertions(+), 11 deletions(-)
923a60
 create mode 100644 man/sd_journal_has_runtime_files.xml
923a60
923a60
diff --git a/Makefile-man.am b/Makefile-man.am
923a60
index 497be6612c..7ec709c8b8 100644
923a60
--- a/Makefile-man.am
923a60
+++ b/Makefile-man.am
923a60
@@ -40,6 +40,7 @@ MANPAGES += \
923a60
 	man/sd_journal_get_fd.3 \
923a60
 	man/sd_journal_get_realtime_usec.3 \
923a60
 	man/sd_journal_get_usage.3 \
923a60
+	man/sd_journal_has_runtime_files.3 \
923a60
 	man/sd_journal_next.3 \
923a60
 	man/sd_journal_open.3 \
923a60
 	man/sd_journal_print.3 \
923a60
@@ -176,6 +177,7 @@ MANPAGES_ALIAS += \
923a60
 	man/sd_journal_get_events.3 \
923a60
 	man/sd_journal_get_monotonic_usec.3 \
923a60
 	man/sd_journal_get_timeout.3 \
923a60
+	man/sd_journal_has_persistent_files.3 \
923a60
 	man/sd_journal_next_skip.3 \
923a60
 	man/sd_journal_open_container.3 \
923a60
 	man/sd_journal_open_directory.3 \
923a60
@@ -287,6 +289,7 @@ man/sd_journal_get_data_threshold.3: man/sd_journal_get_data.3
923a60
 man/sd_journal_get_events.3: man/sd_journal_get_fd.3
923a60
 man/sd_journal_get_monotonic_usec.3: man/sd_journal_get_realtime_usec.3
923a60
 man/sd_journal_get_timeout.3: man/sd_journal_get_fd.3
923a60
+man/sd_journal_has_persistent_files.3: man/sd_journal_has_runtime_files.3
923a60
 man/sd_journal_next_skip.3: man/sd_journal_next.3
923a60
 man/sd_journal_open_container.3: man/sd_journal_open.3
923a60
 man/sd_journal_open_directory.3: man/sd_journal_open.3
923a60
@@ -500,6 +503,9 @@ man/sd_journal_get_monotonic_usec.html: man/sd_journal_get_realtime_usec.html
923a60
 man/sd_journal_get_timeout.html: man/sd_journal_get_fd.html
923a60
 	$(html-alias)
923a60
 
923a60
+man/sd_journal_has_persistent_files.html: man/sd_journal_has_runtime_files.html
923a60
+	$(html-alias)
923a60
+
923a60
 man/sd_journal_next_skip.html: man/sd_journal_next.html
923a60
 	$(html-alias)
923a60
 
923a60
@@ -1727,6 +1733,7 @@ EXTRA_DIST += \
923a60
 	man/sd_journal_get_fd.xml \
923a60
 	man/sd_journal_get_realtime_usec.xml \
923a60
 	man/sd_journal_get_usage.xml \
923a60
+	man/sd_journal_has_runtime_files.xml \
923a60
 	man/sd_journal_next.xml \
923a60
 	man/sd_journal_open.xml \
923a60
 	man/sd_journal_print.xml \
923a60
diff --git a/man/sd-journal.xml b/man/sd-journal.xml
923a60
index 9b1a52207f..a1185d372b 100644
923a60
--- a/man/sd-journal.xml
923a60
+++ b/man/sd-journal.xml
923a60
@@ -81,9 +81,11 @@
923a60
     <citerefentry><refentrytitle>sd_journal_get_cutoff_realtime_usec</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
923a60
     <citerefentry><refentrytitle>sd_journal_get_cutoff_monotonic_usec</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
923a60
     <citerefentry><refentrytitle>sd_journal_get_usage</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
923a60
-    <citerefentry><refentrytitle>sd_journal_get_catalog</refentrytitle><manvolnum>3</manvolnum></citerefentry>
923a60
+    <citerefentry><refentrytitle>sd_journal_get_catalog</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
923a60
+    <citerefentry><refentrytitle>sd_journal_get_fd</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
923a60
+    <citerefentry><refentrytitle>sd_journal_has_runtime_files</refentrytitle><manvolnum>3</manvolnum></citerefentry>
923a60
     and
923a60
-    <citerefentry><refentrytitle>sd_journal_get_fd</refentrytitle><manvolnum>3</manvolnum></citerefentry>
923a60
+    <citerefentry><refentrytitle>sd_journal_has_persistent_files</refentrytitle><manvolnum>3</manvolnum></citerefentry>
923a60
     for more information about the functions implemented.</para>
923a60
 
923a60
     <para>Command line access for submitting entries to the journal is
923a60
@@ -116,6 +118,8 @@
923a60
       <citerefentry><refentrytitle>sd_journal_get_fd</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
923a60
       <citerefentry><refentrytitle>sd_journal_query_unique</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
923a60
       <citerefentry><refentrytitle>sd_journal_get_catalog</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
923a60
+      <citerefentry><refentrytitle>sd_journal_has_runtime_files</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
923a60
+      <citerefentry><refentrytitle>sd_journal_has_persistent_files</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
923a60
       <citerefentry><refentrytitle>journalctl</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
923a60
       <citerefentry><refentrytitle>sd-id128</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
923a60
       <citerefentry project='die-net'><refentrytitle>pkg-config</refentrytitle><manvolnum>1</manvolnum></citerefentry>
923a60
diff --git a/man/sd_journal_has_runtime_files.xml b/man/sd_journal_has_runtime_files.xml
923a60
new file mode 100644
923a60
index 0000000000..237e649206
923a60
--- /dev/null
923a60
+++ b/man/sd_journal_has_runtime_files.xml
923a60
@@ -0,0 +1,95 @@
923a60
+ 
923a60
+
923a60
+  "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
923a60
+
923a60
+
923a60
+  This file is part of systemd.
923a60
+
923a60
+  Copyright 2016 Jan Synáček
923a60
+
923a60
+  systemd is free software; you can redistribute it and/or modify it
923a60
+  under the terms of the GNU Lesser General Public License as published by
923a60
+  the Free Software Foundation; either version 2.1 of the License, or
923a60
+  (at your option) any later version.
923a60
+
923a60
+  systemd is distributed in the hope that it will be useful, but
923a60
+  WITHOUT ANY WARRANTY; without even the implied warranty of
923a60
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
923a60
+  Lesser General Public License for more details.
923a60
+
923a60
+  You should have received a copy of the GNU Lesser General Public License
923a60
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
923a60
+-->
923a60
+
923a60
+<refentry id="sd_journal_has_runtime_files">
923a60
+
923a60
+  <refentryinfo>
923a60
+    <title>sd_journal_has_runtime_files</title>
923a60
+    <productname>systemd</productname>
923a60
+
923a60
+    <authorgroup>
923a60
+      <author>
923a60
+        <contrib>Developer</contrib>
923a60
+        <firstname>Jan</firstname>
923a60
+        <surname>Synáček</surname>
923a60
+        <email>jan.synacek@gmail.com</email>
923a60
+      </author>
923a60
+    </authorgroup>
923a60
+  </refentryinfo>
923a60
+
923a60
+  <refmeta>
923a60
+    <refentrytitle>sd_journal_has_runtime_files</refentrytitle>
923a60
+    <manvolnum>3</manvolnum>
923a60
+  </refmeta>
923a60
+
923a60
+  <refnamediv>
923a60
+    <refname>sd_journal_has_runtime_files</refname>
923a60
+    <refname>sd_journal_has_persistent_files</refname>
923a60
+    <refpurpose>Query availability of runtime or persistent journal files.</refpurpose>
923a60
+  </refnamediv>
923a60
+
923a60
+  <refsynopsisdiv>
923a60
+    <funcsynopsis>
923a60
+      <funcsynopsisinfo>#include <systemd/sd-journal.h></funcsynopsisinfo>
923a60
+
923a60
+      <funcprototype>
923a60
+        <funcdef>int <function>sd_journal_has_runtime_files</function></funcdef>
923a60
+        <paramdef>sd_journal *<parameter>j</parameter></paramdef>
923a60
+      </funcprototype>
923a60
+
923a60
+      <funcprototype>
923a60
+        <funcdef>int <function>sd_journal_has_persistent_files</function></funcdef>
923a60
+        <paramdef>sd_journal *<parameter>j</parameter></paramdef>
923a60
+      </funcprototype>
923a60
+
923a60
+    </funcsynopsis>
923a60
+  </refsynopsisdiv>
923a60
+
923a60
+  <refsect1>
923a60
+    <title>Description</title>
923a60
+
923a60
+    <para><function>sd_journal_has_runtime_files()</function> returns a positive value
923a60
+    if runtime journal files (present in /run/systemd/journal/) have been found.
923a60
+    Otherwise returns 0.</para>
923a60
+
923a60
+    <para><function>sd_journal_has_persistent_files()</function> returns a positive value
923a60
+    if persistent journal files (present in /var/log/journal/) have been found.
923a60
+    Otherwise returns 0.</para>
923a60
+  </refsect1>
923a60
+
923a60
+  <refsect1>
923a60
+    <title>Return value</title>
923a60
+    <para>Both <function>sd_journal_has_runtime_files()</function>
923a60
+    and <function>sd_journal_has_persistent_files()</function> return -EINVAL
923a60
+    if their argument is NULL.
923a60
+    </para>
923a60
+  </refsect1>
923a60
+
923a60
+  <refsect1>
923a60
+    <title>See Also</title>
923a60
+    <para>
923a60
+      <citerefentry><refentrytitle>sd-journal</refentrytitle><manvolnum>3</manvolnum></citerefentry>
923a60
+    </para>
923a60
+  </refsect1>
923a60
+
923a60
+</refentry>
923a60
diff --git a/src/journal/journal-internal.h b/src/journal/journal-internal.h
923a60
index b51ecdb600..115d7776da 100644
923a60
--- a/src/journal/journal-internal.h
923a60
+++ b/src/journal/journal-internal.h
923a60
@@ -115,6 +115,8 @@ struct sd_journal {
923a60
                                   removed, and there were no more
923a60
                                   files, so sd_j_enumerate_unique
923a60
                                   will return a value equal to 0. */
923a60
+        bool has_runtime_files:1;
923a60
+        bool has_persistent_files:1;
923a60
 
923a60
         size_t data_threshold;
923a60
 
923a60
diff --git a/src/journal/sd-journal.c b/src/journal/sd-journal.c
923a60
index 9b9e8ac859..20456c3a12 100644
923a60
--- a/src/journal/sd-journal.c
923a60
+++ b/src/journal/sd-journal.c
923a60
@@ -1230,8 +1230,7 @@ static int add_any_file(sd_journal *j, const char *path) {
923a60
 }
923a60
 
923a60
 static int add_file(sd_journal *j, const char *prefix, const char *filename) {
923a60
-        _cleanup_free_ char *path = NULL;
923a60
-        int r;
923a60
+        char *path = NULL;
923a60
 
923a60
         assert(j);
923a60
         assert(prefix);
923a60
@@ -1241,14 +1240,14 @@ static int add_file(sd_journal *j, const char *prefix, const char *filename) {
923a60
             !file_type_wanted(j->flags, filename))
923a60
                 return 0;
923a60
 
923a60
-        path = strjoin(prefix, "/", filename, NULL);
923a60
-        if (!path)
923a60
-                return -ENOMEM;
923a60
+        path = strjoina(prefix, "/", filename);
923a60
 
923a60
-        r = add_any_file(j, path);
923a60
-        if (r == -ENOENT)
923a60
-                return 0;
923a60
-        return r;
923a60
+        if (!j->has_runtime_files && path_startswith(path, "/run/log/journal"))
923a60
+                j->has_runtime_files = true;
923a60
+        else if (!j->has_persistent_files && path_startswith(path, "/var/log/journal"))
923a60
+                j->has_persistent_files = true;
923a60
+
923a60
+        return add_any_file(j, path);
923a60
 }
923a60
 
923a60
 static int remove_file(sd_journal *j, const char *prefix, const char *filename) {
923a60
@@ -2616,3 +2615,15 @@ _public_ int sd_journal_get_data_threshold(sd_journal *j, size_t *sz) {
923a60
         *sz = j->data_threshold;
923a60
         return 0;
923a60
 }
923a60
+
923a60
+_public_ int sd_journal_has_runtime_files(sd_journal *j) {
923a60
+        assert_return(j, -EINVAL);
923a60
+
923a60
+        return j->has_runtime_files;
923a60
+}
923a60
+
923a60
+_public_ int sd_journal_has_persistent_files(sd_journal *j) {
923a60
+        assert_return(j, -EINVAL);
923a60
+
923a60
+        return j->has_persistent_files;
923a60
+}
923a60
diff --git a/src/systemd/sd-journal.h b/src/systemd/sd-journal.h
923a60
index 00237a2158..d5fd46eea4 100644
923a60
--- a/src/systemd/sd-journal.h
923a60
+++ b/src/systemd/sd-journal.h
923a60
@@ -138,6 +138,9 @@ int sd_journal_reliable_fd(sd_journal *j);
923a60
 int sd_journal_get_catalog(sd_journal *j, char **text);
923a60
 int sd_journal_get_catalog_for_message_id(sd_id128_t id, char **text);
923a60
 
923a60
+int sd_journal_has_runtime_files(sd_journal *j);
923a60
+int sd_journal_has_persistent_files(sd_journal *j);
923a60
+
923a60
 /* the inverse condition avoids ambiguity of danling 'else' after the macro */
923a60
 #define SD_JOURNAL_FOREACH(j)                                           \
923a60
         if (sd_journal_seek_head(j) < 0) { }                            \