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