dryang / rpms / systemd

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