[systemd-commits] 7 commits - Makefile.am man/sd_journal_get_fd.xml man/systemd-coredumpctl.xml src/journal src/shared src/systemd
Lennart Poettering
lennart at kemper.freedesktop.org
Fri Oct 26 16:20:30 PDT 2012
Makefile.am | 2
man/sd_journal_get_fd.xml | 133 ++++++++++++++++++--
man/systemd-coredumpctl.xml | 13 +-
src/journal/coredumpctl.c | 236 +++++++++++++++++++++++++++++--------
src/journal/journal-file.c | 5
src/journal/journal-internal.h | 2
src/journal/libsystemd-journal.sym | 5
src/journal/sd-journal.c | 45 +++++++
src/shared/missing.h | 4
src/shared/util.c | 47 +++----
src/shared/util.h | 6
src/systemd/sd-journal.h | 1
12 files changed, 407 insertions(+), 92 deletions(-)
New commits:
commit e8988fc2a26cda58a2088c2884f4c3802c25dfc8
Author: Lennart Poettering <lennart at poettering.net>
Date: Sat Oct 27 01:20:01 2012 +0200
util: return the remaining string in startswith()
diff --git a/src/shared/util.c b/src/shared/util.c
index a73443d..db8e75b 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -216,45 +216,38 @@ char* endswith(const char *s, const char *postfix) {
return (char*) s + sl - pl;
}
-bool startswith(const char *s, const char *prefix) {
- size_t sl, pl;
+char* startswith(const char *s, const char *prefix) {
+ const char *a, *b;
assert(s);
assert(prefix);
- sl = strlen(s);
- pl = strlen(prefix);
-
- if (pl == 0)
- return true;
-
- if (sl < pl)
- return false;
+ a = s, b = prefix;
+ for (;;) {
+ if (*b == 0)
+ return (char*) a;
+ if (*a != *b)
+ return NULL;
- return memcmp(s, prefix, pl) == 0;
+ a++, b++;
+ }
}
-bool startswith_no_case(const char *s, const char *prefix) {
- size_t sl, pl;
- unsigned i;
+char* startswith_no_case(const char *s, const char *prefix) {
+ const char *a, *b;
assert(s);
assert(prefix);
- sl = strlen(s);
- pl = strlen(prefix);
-
- if (pl == 0)
- return true;
-
- if (sl < pl)
- return false;
-
- for(i = 0; i < pl; ++i)
- if (tolower(s[i]) != tolower(prefix[i]))
- return false;
+ a = s, b = prefix;
+ for (;;) {
+ if (*b == 0)
+ return (char*) a;
+ if (tolower(*a) != tolower(*b))
+ return NULL;
- return true;
+ a++, b++;
+ }
}
bool first_word(const char *s, const char *word) {
diff --git a/src/shared/util.h b/src/shared/util.h
index 8935bee..f6a4c1e 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -142,8 +142,8 @@ static inline bool isempty(const char *p) {
}
char *endswith(const char *s, const char *postfix);
-bool startswith(const char *s, const char *prefix);
-bool startswith_no_case(const char *s, const char *prefix);
+char *startswith(const char *s, const char *prefix);
+char *startswith_no_case(const char *s, const char *prefix);
bool first_word(const char *s, const char *word);
commit ada45c785fc7b0cbe0adb9cbf641943410f4953f
Author: Lennart Poettering <lennart at poettering.net>
Date: Sat Oct 27 01:19:47 2012 +0200
coredumpctl: add 'gdb' verb to start gdb right-away on a collected coredump
diff --git a/man/systemd-coredumpctl.xml b/man/systemd-coredumpctl.xml
index 378d40f..bbb4214 100644
--- a/man/systemd-coredumpctl.xml
+++ b/man/systemd-coredumpctl.xml
@@ -121,6 +121,16 @@
</para></listitem>
</varlistentry>
+
+ <varlistentry>
+ <term><command>gdb</command></term>
+
+ <listitem><para>Invoke the GNU
+ debugger on the last coredump matching
+ specified characteristics.
+ </para></listitem>
+ </varlistentry>
+
</variablelist>
</refsect1>
@@ -179,7 +189,8 @@
<refsect1>
<title>See Also</title>
<para>
- <citerefentry><refentrytitle>systemd-journald.service</refentrytitle><manvolnum>8</manvolnum></citerefentry>
+ <citerefentry><refentrytitle>systemd-journald.service</refentrytitle><manvolnum>8</manvolnum></citerefentry>,
+ <citerefentry><refentrytitle>gdb</refentrytitle><manvolnum>1</manvolnum></citerefentry>
</para>
</refsect1>
diff --git a/src/journal/coredumpctl.c b/src/journal/coredumpctl.c
index 7cf6b45..5b494e3 100644
--- a/src/journal/coredumpctl.c
+++ b/src/journal/coredumpctl.c
@@ -22,6 +22,8 @@
#include <stdio.h>
#include <string.h>
#include <getopt.h>
+#include <fcntl.h>
+#include <unistd.h>
#include <systemd/sd-journal.h>
@@ -36,6 +38,7 @@ static enum {
ACTION_NONE,
ACTION_LIST,
ACTION_DUMP,
+ ACTION_GDB,
} arg_action = ACTION_LIST;
static Set *matches = NULL;
@@ -191,6 +194,8 @@ static int parse_argv(int argc, char *argv[]) {
arg_action = ACTION_LIST;
else if (streq(cmd, "dump"))
arg_action = ACTION_DUMP;
+ else if (streq(cmd, "gdb"))
+ arg_action = ACTION_GDB;
else {
log_error("Unknown action '%s'", cmd);
return -EINVAL;
@@ -304,13 +309,9 @@ static int dump_list(sd_journal *j) {
return 0;
}
-static int dump_core(sd_journal* j) {
- const char *data;
- size_t len, ret;
+static int focus(sd_journal *j) {
int r;
- assert(j);
-
r = sd_journal_seek_tail(j);
if (r == 0)
r = sd_journal_previous(j);
@@ -318,17 +319,23 @@ static int dump_core(sd_journal* j) {
log_error("Failed to search journal: %s", strerror(-r));
return r;
}
-
if (r == 0) {
log_error("No match found");
return -ESRCH;
}
+ return r;
+}
- r = sd_journal_get_data(j, "COREDUMP", (const void**) &data, &len);
- if (r != 0) {
- log_error("retrieve COREDUMP field: %s", strerror(-r));
+static int dump_core(sd_journal* j) {
+ const void *data;
+ size_t len, ret;
+ int r;
+
+ assert(j);
+
+ r = focus(j);
+ if (r < 0)
return r;
- }
print_entry(output ? stdout : stderr, j, false);
@@ -337,9 +344,17 @@ static int dump_core(sd_journal* j) {
return -ENOTTY;
}
+ r = sd_journal_get_data(j, "COREDUMP", (const void**) &data, &len);
+ if (r < 0) {
+ log_error("Failed to retrieve COREDUMP field: %s", strerror(-r));
+ return r;
+ }
+
assert(len >= 9);
+ data = (const uint8_t*) data + 9;
+ len -= 9;
- ret = fwrite(data+9, len-9, 1, output ? output : stdout);
+ ret = fwrite(data, len, 1, output ? output : stdout);
if (ret != 1) {
log_error("dumping coredump: %m (%zu)", ret);
return -errno;
@@ -352,6 +367,100 @@ static int dump_core(sd_journal* j) {
return 0;
}
+static int run_gdb(sd_journal *j) {
+ char path[] = "/var/tmp/coredump-XXXXXX";
+ const void *data;
+ size_t len;
+ ssize_t sz;
+ pid_t pid;
+ _cleanup_free_ char *exe = NULL;
+ int r;
+ _cleanup_close_ int fd = -1;
+ siginfo_t st;
+
+ assert(j);
+
+ r = focus(j);
+ if (r < 0)
+ return r;
+
+ print_entry(stdout, j, false);
+
+ r = sd_journal_get_data(j, "COREDUMP_EXE", (const void**) &data, &len);
+ if (r < 0) {
+ log_error("Failed to retrieve COREDUMP_EXE field: %s", strerror(-r));
+ return r;
+ }
+
+ assert(len >= 13);
+ data = (const uint8_t*) data + 13;
+ len -= 13;
+
+ exe = strndup(data, len);
+ if (!exe)
+ return log_oom();
+
+ if (endswith(exe, " (deleted)")) {
+ log_error("Binary already deleted.");
+ return -ENOENT;
+ }
+
+ r = sd_journal_get_data(j, "COREDUMP", (const void**) &data, &len);
+ if (r < 0) {
+ log_error("Failed to retrieve COREDUMP field: %s", strerror(-r));
+ return r;
+ }
+
+ assert(len >= 9);
+ data = (const uint8_t*) data + 9;
+ len -= 9;
+
+ fd = mkostemp(path, O_WRONLY);
+ if (fd < 0) {
+ log_error("Failed to create temporary file: %m");
+ return -errno;
+ }
+
+ sz = write(fd, data, len);
+ if (sz < 0) {
+ log_error("Failed to write temporary file: %s", strerror(errno));
+ r = -errno;
+ goto finish;
+ }
+ if (sz != (ssize_t) len) {
+ log_error("Short write to temporary file.");
+ r = -EIO;
+ goto finish;
+ }
+
+ close_nointr_nofail(fd);
+ fd = -1;
+
+ pid = fork();
+ if (pid < 0) {
+ log_error("Failed to fork(): %m");
+ r = -errno;
+ goto finish;
+ }
+ if (pid == 0) {
+ execlp("gdb", "gdb", exe, path, NULL);
+ log_error("Failed to invoke gdb: %m");
+ _exit(1);
+ }
+
+ r = wait_for_terminate(pid, &st);
+ if (r < 0) {
+ log_error("Failed to wait for gdb: %m");
+ goto finish;
+ }
+
+ r = st.si_code == CLD_EXITED ? st.si_status : 255;
+
+finish:
+ unlink(path);
+ return r;
+}
+
int main(int argc, char *argv[]) {
sd_journal *j = NULL;
const char* match;
@@ -387,16 +496,23 @@ int main(int argc, char *argv[]) {
}
switch(arg_action) {
+
case ACTION_LIST:
if (!arg_no_pager)
pager_open();
r = dump_list(j);
break;
+
case ACTION_DUMP:
r = dump_core(j);
break;
- case ACTION_NONE:
+
+ case ACTION_GDB:
+ r = run_gdb(j);
+ break;
+
+ default:
assert_not_reached("Shouldn't be here");
}
@@ -411,5 +527,5 @@ end:
if (output)
fclose(output);
- return r == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
+ return r >= 0 ? r : EXIT_FAILURE;
}
commit 684341b073c4c5f7339e14ebc6a3e0df849e4eb7
Author: Lennart Poettering <lennart at poettering.net>
Date: Fri Oct 26 20:34:39 2012 +0200
coredumpctl: show timestamps in list
diff --git a/src/journal/coredumpctl.c b/src/journal/coredumpctl.c
index 9dac169..7cf6b45 100644
--- a/src/journal/coredumpctl.c
+++ b/src/journal/coredumpctl.c
@@ -232,12 +232,15 @@ static int retrieve(const void *data,
return 0;
}
-static void print_entry(FILE* file, sd_journal *j, int had_header) {
+static int print_entry(FILE* file, sd_journal *j, int had_header) {
const char _cleanup_free_
*pid = NULL, *uid = NULL, *gid = NULL,
*sgnl = NULL, *exe = NULL;
const void *d;
size_t l;
+ usec_t t;
+ char buf[FORMAT_TIMESTAMP_MAX];
+ int r;
SD_JOURNAL_FOREACH_DATA(j, d, l) {
retrieve(d, l, "COREDUMP_PID", &pid);
@@ -253,24 +256,36 @@ static void print_entry(FILE* file, sd_journal *j, int had_header) {
}
if (!pid && !uid && !gid && !sgnl && !exe) {
- log_warning("empty coredump log entry");
- return;
+ log_warning("Empty coredump log entry");
+ return -EINVAL;
+ }
+
+ r = sd_journal_get_realtime_usec(j, &t);
+ if (r < 0) {
+ log_error("Failed to get realtime timestamp: %s", strerror(-r));
+ return r;
}
+ format_timestamp(buf, sizeof(buf), t);
+
if (!had_header)
- fprintf(file, "%*s %*s %*s %*s %s\n",
+ fprintf(file, "%-*s %*s %*s %*s %*s %s\n",
+ FORMAT_TIMESTAMP_MAX-1, "TIME",
6, "PID",
5, "UID",
5, "GID",
- 3, "sig",
- "exe");
+ 3, "SIG",
+ "EXE");
- fprintf(file, "%*s %*s %*s %*s %s\n",
+ fprintf(file, "%*s %*s %*s %*s %*s %s\n",
+ FORMAT_TIMESTAMP_MAX-1, buf,
6, pid,
5, uid,
5, gid,
3, sgnl,
exe);
+
+ return 0;
}
static int dump_list(sd_journal *j) {
@@ -282,7 +297,7 @@ static int dump_list(sd_journal *j) {
print_entry(stdout, j, found++);
if (!found) {
- log_error("no coredumps found");
+ log_notice("No coredumps found");
return -ESRCH;
}
diff --git a/src/shared/util.h b/src/shared/util.h
index a9c39b8..8935bee 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -79,7 +79,7 @@ union dirent_storage {
#define QUOTES "\"\'"
#define COMMENTS "#;\n"
-#define FORMAT_TIMESTAMP_MAX 64
+#define FORMAT_TIMESTAMP_MAX (5+11+9+4+1)
#define FORMAT_TIMESTAMP_PRETTY_MAX 256
#define FORMAT_TIMESPAN_MAX 64
#define FORMAT_BYTES_MAX 8
commit 34741aa3e2ee1e67a4cc735b7492aec13f0d822c
Author: Lennart Poettering <lennart at poettering.net>
Date: Fri Oct 26 20:25:36 2012 +0200
journal: special case the trivial cache chain cache entry
diff --git a/src/journal/journal-file.c b/src/journal/journal-file.c
index 6c9deac..3df099d 100644
--- a/src/journal/journal-file.c
+++ b/src/journal/journal-file.c
@@ -1328,6 +1328,11 @@ static void chain_cache_put(
uint64_t total) {
if (!ci) {
+ /* If the chain item to cache for this chain is the
+ * first one it's not worth caching anything */
+ if (array == first)
+ return;
+
if (hashmap_size(h) >= CHAIN_CACHE_MAX)
ci = hashmap_steal_first(h);
else {
commit 8bc8ab83c874847343e695cdd0e25873ad8bc5d4
Author: Lennart Poettering <lennart at poettering.net>
Date: Fri Oct 26 20:25:10 2012 +0200
coredumpctl: optimize journal entry parsing a bit by enumerating only once
diff --git a/src/journal/coredumpctl.c b/src/journal/coredumpctl.c
index 51d333c..9dac169 100644
--- a/src/journal/coredumpctl.c
+++ b/src/journal/coredumpctl.c
@@ -207,19 +207,23 @@ static int parse_argv(int argc, char *argv[]) {
return 0;
}
-static int retrieve(sd_journal *j, const char *name, const char **var) {
- const void *data;
- size_t len, field;
- int r;
+static int retrieve(const void *data,
+ size_t len,
+ const char *name,
+ const char **var) {
- r = sd_journal_get_data(j, name, &data, &len);
- if (r < 0) {
- log_warning("Failed to retrieve %s", name);
- return r;
- }
+ size_t field;
+
+ field = strlen(name) + 1; /* name + "=" */
+
+ if (len < field)
+ return 0;
- field = strlen(name) + 1; // name + "="
- assert(len >= field);
+ if (memcmp(data, name, field - 1) != 0)
+ return 0;
+
+ if (((const char*) data)[field - 1] != '=')
+ return 0;
*var = strndup((const char*)data + field, len - field);
if (!var)
@@ -232,16 +236,21 @@ static void print_entry(FILE* file, sd_journal *j, int had_header) {
const char _cleanup_free_
*pid = NULL, *uid = NULL, *gid = NULL,
*sgnl = NULL, *exe = NULL;
-
- retrieve(j, "COREDUMP_PID", &pid);
- retrieve(j, "COREDUMP_UID", &uid);
- retrieve(j, "COREDUMP_GID", &gid);
- retrieve(j, "COREDUMP_SIGNAL", &sgnl);
- retrieve(j, "COREDUMP_EXE", &exe);
- if (!exe)
- retrieve(j, "COREDUMP_COMM", &exe);
- if (!exe)
- retrieve(j, "COREDUMP_CMDLINE", &exe);
+ const void *d;
+ size_t l;
+
+ SD_JOURNAL_FOREACH_DATA(j, d, l) {
+ retrieve(d, l, "COREDUMP_PID", &pid);
+ retrieve(d, l, "COREDUMP_PID", &pid);
+ retrieve(d, l, "COREDUMP_UID", &uid);
+ retrieve(d, l, "COREDUMP_GID", &gid);
+ retrieve(d, l, "COREDUMP_SIGNAL", &sgnl);
+ retrieve(d, l, "COREDUMP_EXE", &exe);
+ if (!exe)
+ retrieve(d, l, "COREDUMP_COMM", &exe);
+ if (!exe)
+ retrieve(d, l, "COREDUMP_CMDLINE", &exe);
+ }
if (!pid && !uid && !gid && !sgnl && !exe) {
log_warning("empty coredump log entry");
@@ -295,10 +304,10 @@ static int dump_core(sd_journal* j) {
return r;
}
- if (r == 0) {
- log_error("No match found");
- return -ESRCH;
- }
+ if (r == 0) {
+ log_error("No match found");
+ return -ESRCH;
+ }
r = sd_journal_get_data(j, "COREDUMP", (const void**) &data, &len);
if (r != 0) {
@@ -354,8 +363,6 @@ int main(int argc, char *argv[]) {
}
SET_FOREACH(match, matches, it) {
- log_info("Matching: %s", match);
-
r = sd_journal_add_match(j, match, strlen(match));
if (r != 0) {
log_error("Failed to add match '%s': %s",
commit ccc403587ce78639daa6cf3cf220575011e88d3a
Author: Lennart Poettering <lennart at poettering.net>
Date: Fri Oct 26 20:23:28 2012 +0200
coredumpctl: initialize global vars
diff --git a/src/journal/coredumpctl.c b/src/journal/coredumpctl.c
index 5c442ff..51d333c 100644
--- a/src/journal/coredumpctl.c
+++ b/src/journal/coredumpctl.c
@@ -38,10 +38,10 @@ static enum {
ACTION_DUMP,
} arg_action = ACTION_LIST;
-Set *matches;
-FILE* output;
+static Set *matches = NULL;
+static FILE* output = NULL;
-int arg_no_pager;
+static int arg_no_pager = false;
static Set *new_matches(void) {
Set *set;
commit 85210bffd8363e491b4c31f2d09404f9869ad0c7
Author: Lennart Poettering <lennart at poettering.net>
Date: Fri Oct 26 20:05:19 2012 +0200
journal: provide an API that allows client to figure out whether they need to recheck the journal manually for changes in regular intervals
Network file systems generally do not offer inotify() that would work
across the network. We hence cannot rely on inotify() exclusiely in
those case. Provide an API to determine these cases, and suggest doing
manual regular rechecks.
Note that this is not complete yet, as we need to rescan journal dirs on
network file systems explicitly to find new/removed files
diff --git a/Makefile.am b/Makefile.am
index 0bcde9c..634c67d 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -576,6 +576,7 @@ MANPAGES_ALIAS = \
man/SD_JOURNAL_FOREACH_DATA.3 \
man/sd_journal_get_monotonic_usec.3 \
man/sd_journal_get_cutoff_monotonic_usec.3 \
+ man/sd_journal_reliable_fd.3 \
man/sd_journal_process.3 \
man/sd_journal_wait.3 \
man/SD_JOURNAL_NOP.3 \
@@ -649,6 +650,7 @@ man/sd_journal_restart_data.3: man/sd_journal_get_data.3
man/SD_JOURNAL_FOREACH_DATA.3: man/sd_journal_get_data.3
man/sd_journal_get_monotonic_usec.3: man/sd_journal_get_realtime_usec.3
man/sd_journal_get_cutoff_monotonic_usec.3: man/sd_journal_get_cutoff_realtime_usec.3
+man/sd_journal_reliable_fd.3: man/sd_journal_get_fd.3
man/sd_journal_process.3: man/sd_journal_get_fd.3
man/sd_journal_wait.3: man/sd_journal_get_fd.3
man/SD_JOURNAL_NOP.3: man/sd_journal_get_fd.3
diff --git a/man/sd_journal_get_fd.xml b/man/sd_journal_get_fd.xml
index e3b5eaa..189d213 100644
--- a/man/sd_journal_get_fd.xml
+++ b/man/sd_journal_get_fd.xml
@@ -44,6 +44,7 @@
<refnamediv>
<refname>sd_journal_get_fd</refname>
+ <refname>sd_journal_reliable_fd</refname>
<refname>sd_journal_process</refname>
<refname>sd_journal_wait</refname>
<refname>SD_JOURNAL_NOP</refname>
@@ -63,6 +64,11 @@
</funcprototype>
<funcprototype>
+ <funcdef>int <function>sd_journal_reliable_fd</function></funcdef>
+ <paramdef>sd_journal* <parameter>j</parameter></paramdef>
+ </funcprototype>
+
+ <funcprototype>
<funcdef>int <function>sd_journal_process</function></funcdef>
<paramdef>sd_journal* <parameter>j</parameter></paramdef>
</funcprototype>
@@ -82,12 +88,36 @@
<para><function>sd_journal_get_fd()</function> returns
a file descriptor that may be asynchronously polled in
an external event loop and is signaled readable as
- soon as the journal changes, for example because new
- entries were added. The file descriptor is suitable
- for usage in
+ soon as the journal changes, because new entries or
+ files were added, rotation took place, or files have
+ been deleted, and similar. The file descriptor is
+ suitable for usage in
<citerefentry><refentrytitle>poll</refentrytitle><manvolnum>2</manvolnum></citerefentry>
- where it will yield POLLIN on all changes. The call
- takes one argument: the journal context object.</para>
+ where it will yield POLLIN on changes. The call takes
+ one argument: the journal context object. Note that
+ not all file systems are capable of generating the
+ necessary events for wakeups from this file descriptor
+ to be enirely reliable. In particular network files
+ systems do not generate suitable file change events in
+ all cases. In such a case an application should not
+ rely alone on wake-ups from this file descriptor but
+ wake up and recheck the journal in regular time
+ intervals, for example every 2s. To detect
+ cases where this is necessary, use
+ <function>sd_journal_reliable_fd()</function>,
+ below.</para>
+
+ <para><function>sd_journal_reliable_fd()</function>
+ may be used to check whether the wakeup events from
+ the file descriptor returned by
+ <function>sd_journal_get_fd</function> are sufficient
+ to track changes to the journal. If this call returns
+ 0, it is necessary to regularly recheck for journal
+ changes (suggestion: every 2s). If this call returns a
+ positive integer this is not necessary, and wakeups
+ from the file descriptor returned by
+ <function>sd_journal_get_fd()</function> are
+ sufficient as only source for wake-ups.</para>
<para>After each POLLIN wake-up
<function>sd_journal_process()</function> needs to be
@@ -97,14 +127,19 @@
that spurious wake-ups are possible).</para>
<para>A synchronous alternative for using
- <function>sd_journal_get_fd()</function> and
+ <function>sd_journal_get_fd()</function>,
+ <function>sd_journal_reliable_fd()</function> and
<function>sd_journal_process()</function> is
<function>sd_journal_wait()</function>. It will
- synchronously wait until the journal gets changed up
- to a certain time-out as specified in its second
- argument. Pass <literal>(uint64_t) -1</literal> to
- wait indefinitely. Internally this call simply
- combines <function>sd_journal_get_fd()</function>,
+ synchronously wait until the journal gets changed,
+ possibly using a 2s time-out if this is necessary (see
+ above). In either way the maximum time this call
+ sleeps may be controlled with the
+ <parameter>timeout_usec</parameter> parameter. Pass
+ <literal>(uint64_t) -1</literal> to wait
+ indefinitely. Internally this call simply combines
+ <function>sd_journal_get_fd()</function>,
+ <function>sd_journal_reliable_fd()</function>,
<function>poll()</function> and
<function>sd_journal_process()</function> into
one.</para>
@@ -117,6 +152,15 @@
<para><function>sd_journal_get_fd()</function> returns a valid file descriptor on success or a negative errno-style error
code.</para>
+ <para><function>sd_journal_reliable_fd()</function>
+ returns a positive integer if the file descriptor
+ returned by <function>sd_journal_get_fd()</function>
+ is sufficient as sole wake-up source for journal
+ change events. Returns 0 if it is not sufficient and
+ the journal needs to be checked manually in regular
+ time intervals for changes. Returns a negative
+ errno-style error code on failure.</para>
+
<para><function>sd_journal_process()</function> and
<function>sd_journal_wait()</function> return one of
<literal>SD_JOURNAL_NOP</literal>,
@@ -140,6 +184,7 @@
<title>Notes</title>
<para>The <function>sd_journal_get_fd()</function>,
+ <function>sd_journal_reliable_fd()</function>,
<function>sd_journal_process()</function> and
<function>sd_journal_wait()</function> interfaces are
available as shared library, which can be compiled and
@@ -150,13 +195,77 @@
</refsect1>
<refsect1>
+ <title>Examples</title>
+
+ <para>Iterating through the journal, in a live view tracking all changes:</para>
+
+ <programlisting>#include <stdio.h>
+#include <string.h>
+#include <systemd/sd-journal.h>
+
+int main(int argc, char *argv[]) {
+ int r;
+ sd_journal *j;
+ r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
+ if (r < 0) {
+ fprintf(stderr, "Failed to open journal: %s\n", strerror(-r));
+ return 1;
+ }
+ for (;;) {
+ const char *d;
+ size_t l;
+ r = sd_journal_next(j);
+ if (r < 0) {
+ fprintf(stderr, "Failed to iterate to next entry: %s\n", strerror(-r));
+ break;
+ }
+ if (r == 0) {
+ /* Reached the end, let's wait for changes, and try again */
+ r = sd_journal_wait(j, (uint64_t) -1);
+ if (r < 0) {
+ fprintf(stderr, "Failed to wait for changes: %s\n", strerror(-r));
+ break;
+ }
+ continue;
+ }
+ r = sd_journal_get_data(j, "MESSAGE", &d, &l);
+ if (r < 0) {
+ fprintf(stderr, "Failed to read message field: %s\n", strerror(-r));
+ continue;
+ }
+ printf("%.*s\n", (int) l, d);
+ }
+ sd_journal_close(j);
+ return 0;
+}</programlisting>
+
+ <para>Waiting with <function>poll()</function> (this
+ example lacks all error checking for the sake of
+ simplicity):</para>
+
+ <programlisting>#include <sys/poll.h>
+#include <systemd/sd-journal.h>
+
+int wait_for_changes(sd_journal *j) {
+ struct pollfd pollfd;
+ pollfd.fd = sd_journal_get_fd();
+ pollfd.events = POLLIN;
+ poll(&pollfd, 1, sd_journal_reliable_fd() > 0 ? -1 : 2000);
+ return sd_journal_process(j);
+}
+ </programlisting>
+ </refsect1>
+
+
+ <refsect1>
<title>See Also</title>
<para>
<citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
<citerefentry><refentrytitle>sd-journal</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
<citerefentry><refentrytitle>sd_journal_open</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
- <citerefentry><refentrytitle>sd_journal_next</refentrytitle><manvolnum>3</manvolnum></citerefentry>
+ <citerefentry><refentrytitle>sd_journal_next</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
+ <citerefentry><refentrytitle>poll</refentrytitle><manvolnum>2</manvolnum></citerefentry>
</para>
</refsect1>
diff --git a/src/journal/journal-internal.h b/src/journal/journal-internal.h
index f68ca99..75a4129 100644
--- a/src/journal/journal-internal.h
+++ b/src/journal/journal-internal.h
@@ -119,6 +119,8 @@ struct sd_journal {
char *unique_field;
JournalFile *unique_file;
uint64_t unique_offset;
+
+ bool on_network;
};
char *journal_make_match_string(sd_journal *j);
diff --git a/src/journal/libsystemd-journal.sym b/src/journal/libsystemd-journal.sym
index 96293b8..ad78fcc 100644
--- a/src/journal/libsystemd-journal.sym
+++ b/src/journal/libsystemd-journal.sym
@@ -80,3 +80,8 @@ global:
sd_journal_enumerate_unique;
sd_journal_restart_unique;
} LIBSYSTEMD_JOURNAL_190;
+
+LIBSYSTEMD_JOURNAL_196 {
+global:
+ sd_journal_fd_reliable;
+} LIBSYSTEMD_JOURNAL_195;
diff --git a/src/journal/sd-journal.c b/src/journal/sd-journal.c
index d5d2d78..a346691 100644
--- a/src/journal/sd-journal.c
+++ b/src/journal/sd-journal.c
@@ -25,6 +25,8 @@
#include <unistd.h>
#include <sys/inotify.h>
#include <sys/poll.h>
+#include <sys/vfs.h>
+#include <linux/magic.h>
#include "sd-journal.h"
#include "journal-def.h"
@@ -35,9 +37,12 @@
#include "lookup3.h"
#include "compress.h"
#include "journal-internal.h"
+#include "missing.h"
#define JOURNAL_FILES_MAX 1024
+#define JOURNAL_FILES_RECHECK_USEC (2 * USEC_PER_SEC)
+
static void detach_location(sd_journal *j) {
Iterator i;
JournalFile *f;
@@ -1184,6 +1189,25 @@ _public_ int sd_journal_seek_tail(sd_journal *j) {
return 0;
}
+static void check_network(sd_journal *j, int fd) {
+ struct statfs sfs;
+
+ assert(j);
+
+ if (j->on_network)
+ return;
+
+ if (fstatfs(fd, &sfs) < 0)
+ return;
+
+ j->on_network =
+ sfs.f_type == CIFS_MAGIC_NUMBER ||
+ sfs.f_type == CODA_SUPER_MAGIC ||
+ sfs.f_type == NCP_SUPER_MAGIC ||
+ sfs.f_type == NFS_SUPER_MAGIC ||
+ sfs.f_type == SMB_SUPER_MAGIC;
+}
+
static int add_file(sd_journal *j, const char *prefix, const char *filename) {
char *path;
int r;
@@ -1233,6 +1257,8 @@ static int add_file(sd_journal *j, const char *prefix, const char *filename) {
return r;
}
+ check_network(j, f->fd);
+
j->current_invalidate_counter ++;
log_debug("File %s got added.", f->path);
@@ -1366,6 +1392,8 @@ static int add_directory(sd_journal *j, const char *prefix, const char *dirname)
}
}
+ check_network(j, dirfd(d));
+
closedir(d);
return 0;
@@ -1453,6 +1481,8 @@ static int add_root_directory(sd_journal *j, const char *p) {
}
}
+ check_network(j, dirfd(d));
+
closedir(d);
return 0;
@@ -2079,6 +2109,14 @@ _public_ int sd_journal_wait(sd_journal *j, uint64_t timeout_usec) {
return determine_change(j);
}
+ if (j->on_network) {
+ /* If we are on the network we need to regularly check
+ * for changes manually */
+
+ if (timeout_usec == (uint64_t) -1 || timeout_usec > JOURNAL_FILES_RECHECK_USEC)
+ timeout_usec = JOURNAL_FILES_RECHECK_USEC;
+ }
+
do {
r = fd_wait_for_event(j->inotify_fd, POLLIN, timeout_usec);
} while (r == -EINTR);
@@ -2344,3 +2382,10 @@ _public_ void sd_journal_restart_unique(sd_journal *j) {
j->unique_file = NULL;
j->unique_offset = 0;
}
+
+_public_ int sd_journal_reliable_fd(sd_journal *j) {
+ if (!j)
+ return -EINVAL;
+
+ return !j->on_network;
+}
diff --git a/src/shared/missing.h b/src/shared/missing.h
index f58a609..3777cf6 100644
--- a/src/shared/missing.h
+++ b/src/shared/missing.h
@@ -249,3 +249,7 @@ static inline int name_to_handle_at(int fd, const char *name, struct file_handle
# error neither secure_getenv nor __secure_getenv are available
# endif
#endif
+
+#ifndef CIFS_MAGIC_NUMBER
+#define CIFS_MAGIC_NUMBER 0xFF534D42
+#endif
diff --git a/src/systemd/sd-journal.h b/src/systemd/sd-journal.h
index 55281b1..7241173 100644
--- a/src/systemd/sd-journal.h
+++ b/src/systemd/sd-journal.h
@@ -124,6 +124,7 @@ int sd_journal_enumerate_unique(sd_journal *j, const void **data, size_t *l);
void sd_journal_restart_unique(sd_journal *j);
int sd_journal_get_fd(sd_journal *j);
+int sd_journal_reliable_fd(sd_journal *j);
int sd_journal_process(sd_journal *j);
int sd_journal_wait(sd_journal *j, uint64_t timeout_usec);
More information about the systemd-commits
mailing list