2012-07-13 23:04:38 +04:00
<?xml version='1.0'?> <!-- * - nxml - * -->
< !DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
"http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
<!--
This file is part of systemd.
Copyright 2012 Lennart Poettering
systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http: / / w w w . g n u . o r g / l i c e n s e s /> .
-->
<refentry id= "sd_journal_get_fd" >
<refentryinfo >
<title > sd_journal_get_fd</title>
<productname > systemd</productname>
<authorgroup >
<author >
<contrib > Developer</contrib>
<firstname > Lennart</firstname>
<surname > Poettering</surname>
<email > lennart@poettering.net</email>
</author>
</authorgroup>
</refentryinfo>
<refmeta >
<refentrytitle > sd_journal_get_fd</refentrytitle>
<manvolnum > 3</manvolnum>
</refmeta>
<refnamediv >
<refname > sd_journal_get_fd</refname>
2013-04-04 19:22:28 +04:00
<refname > sd_journal_get_events</refname>
2013-04-04 22:07:48 +04:00
<refname > sd_journal_get_timeout</refname>
2012-07-13 23:54:19 +04:00
<refname > sd_journal_process</refname>
<refname > sd_journal_wait</refname>
2013-04-04 22:07:48 +04:00
<refname > sd_journal_reliable_fd</refname>
2012-07-14 01:38:32 +04:00
<refname > SD_JOURNAL_NOP</refname>
<refname > SD_JOURNAL_APPEND</refname>
<refname > SD_JOURNAL_INVALIDATE</refname>
2012-07-13 23:04:38 +04:00
<refpurpose > Journal change notification
interface</refpurpose>
</refnamediv>
<refsynopsisdiv >
<funcsynopsis >
<funcsynopsisinfo > #include < systemd/sd-journal.h> </funcsynopsisinfo>
<funcprototype >
<funcdef > int <function > sd_journal_get_fd</function> </funcdef>
<paramdef > sd_journal* <parameter > j</parameter> </paramdef>
</funcprototype>
2013-04-04 19:22:28 +04:00
<funcprototype >
<funcdef > int <function > sd_journal_get_events</function> </funcdef>
<paramdef > sd_journal* <parameter > j</parameter> </paramdef>
</funcprototype>
2012-10-26 22:05:19 +04:00
<funcprototype >
2013-04-04 22:07:48 +04:00
<funcdef > int <function > sd_journal_get_timeout</function> </funcdef>
2012-10-26 22:05:19 +04:00
<paramdef > sd_journal* <parameter > j</parameter> </paramdef>
2013-04-04 22:07:48 +04:00
<paramdef > uint64_t* <parameter > timeout_usec</parameter> </paramdef>
2012-10-26 22:05:19 +04:00
</funcprototype>
2012-07-13 23:04:38 +04:00
<funcprototype >
<funcdef > int <function > sd_journal_process</function> </funcdef>
<paramdef > sd_journal* <parameter > j</parameter> </paramdef>
</funcprototype>
<funcprototype >
<funcdef > int <function > sd_journal_wait</function> </funcdef>
<paramdef > sd_journal* <parameter > j</parameter> </paramdef>
<paramdef > uint64_t <parameter > timeout_usec</parameter> </paramdef>
</funcprototype>
2013-04-04 22:07:48 +04:00
<funcprototype >
<funcdef > int <function > sd_journal_reliable_fd</function> </funcdef>
<paramdef > sd_journal* <parameter > j</parameter> </paramdef>
</funcprototype>
2012-07-13 23:04:38 +04:00
</funcsynopsis>
</refsynopsisdiv>
<refsect1 >
<title > Description</title>
<para > <function > sd_journal_get_fd()</function> returns
a file descriptor that may be asynchronously polled in
2013-04-04 19:22:28 +04:00
an external event loop and is signaled as 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> . Use
<function > sd_journal_get_events()</function> for an
events mask to watch for. The call takes one argument:
the journal context object. Note that not all file
systems are capable of generating the necessary events
2013-04-04 22:07:48 +04:00
for wakeups from this file descriptor for changes to
be noticed immediately. In particular network files
systems do not generate suitable file change events in
all cases. Cases like this can be detected with
2012-10-26 22:05:19 +04:00
<function > sd_journal_reliable_fd()</function> ,
2013-04-04 22:07:48 +04:00
below. <function > sd_journal_get_timeout()</function>
will ensure in these cases that wake-ups happen
frequently enough for changes to be noticed, although
with a certain latency.</para>
2012-10-26 22:05:19 +04:00
2013-04-04 19:22:28 +04:00
<para > <function > sd_journal_get_events()</function>
2013-04-04 19:38:08 +04:00
will return the <function > poll()</function> mask to
wait for. This function will return a combination of
2013-06-27 03:47:34 +04:00
<constant > POLLIN</constant> and
<constant > POLLOUT</constant> and similar to fill into
2013-04-04 19:38:08 +04:00
the <literal > .events</literal> field of
2013-06-27 03:47:34 +04:00
<varname > struct pollfd</varname> .</para>
2013-04-04 19:22:28 +04:00
2013-04-04 22:07:48 +04:00
<para > <function > sd_journal_get_timeout()</function>
2013-06-27 03:47:34 +04:00
will return a timeout value for usage in
<function > poll()</function> . This returns a value in
microseconds since the epoch of
<constant > CLOCK_MONOTONIC</constant> for timing out
<function > poll()</function> in
<varname > timeout_usec</varname> . See
2013-04-04 22:07:48 +04:00
<citerefentry > <refentrytitle > clock_gettime</refentrytitle> <manvolnum > 2</manvolnum> </citerefentry>
for details about
2013-06-27 03:47:34 +04:00
<constant > CLOCK_MONOTONIC</constant> . If there's no
2013-04-04 22:07:48 +04:00
timeout to wait for this will fill in
2013-06-27 03:47:34 +04:00
<constant > (uint64_t) -1</constant> instead. Note that
2013-04-04 22:07:48 +04:00
<function > poll()</function> takes a relative timeout
in milliseconds rather than an absolute timeout in
2013-06-27 03:47:34 +04:00
microseconds. To convert the absolute 'us' timeout
into relative 'ms', use code like the
following:</para>
2013-04-04 22:07:48 +04:00
<programlisting > uint64_t t;
int msec;
sd_journal_get_timeout(m, & t);
if (t == (uint64_t) -1)
msec = -1;
else {
struct timespec ts;
uint64_t n;
clock_getttime(CLOCK_MONOTONIC, & ts);
n = (uint64_t) ts.tv_sec * 1000000 + ts.tv_nsec / 1000;
msec = t > n ? (int) ((t - n + 999) / 1000) : 0;
}</programlisting>
<para > The code above does not do any error checking
2013-06-27 03:47:34 +04:00
for brevity's sake. The calculated <varname > msec</varname>
2013-04-04 22:07:48 +04:00
integer can be passed directly as
<function > poll()</function> 's timeout
parameter.</para>
2012-07-13 23:04:38 +04:00
2013-04-04 19:38:08 +04:00
<para > After each <function > poll()</function> wake-up
2012-07-13 23:04:38 +04:00
<function > sd_journal_process()</function> needs to be
2013-04-04 19:22:28 +04:00
called to process events. This call will also indicate
2012-07-13 23:04:38 +04:00
what kind of change has been detected (see below; note
that spurious wake-ups are possible).</para>
<para > A synchronous alternative for using
2012-10-26 22:05:19 +04:00
<function > sd_journal_get_fd()</function> ,
2013-04-04 19:22:28 +04:00
<function > sd_journal_get_events()</function> ,
2013-04-04 22:07:48 +04:00
<function > sd_journal_get_timeout()</function> and
2012-07-13 23:04:38 +04:00
<function > sd_journal_process()</function> is
<function > sd_journal_wait()</function> . It will
2013-04-04 22:07:48 +04:00
synchronously wait until the journal gets changed. The
maximum time this call sleeps may be controlled with
the <parameter > timeout_usec</parameter>
2013-06-27 03:47:34 +04:00
parameter. Pass <constant > (uint64_t) -1</constant> to
2013-04-04 22:07:48 +04:00
wait indefinitely. Internally this call simply
combines <function > sd_journal_get_fd()</function> ,
2013-04-04 19:22:28 +04:00
<function > sd_journal_get_events()</function> ,
2013-04-04 22:07:48 +04:00
<function > sd_journal_get_timeout()</function> ,
2012-07-13 23:04:38 +04:00
<function > poll()</function> and
<function > sd_journal_process()</function> into
one.</para>
2013-04-04 22:07:48 +04:00
<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 known to
be immediately triggered. On certain file systems
where file change events from the OS are not available
(such as NFS) changes need to be polled for
repeatedly, and hence are detected only with a certain
latency. This call will return a positive value if the
journal changes are detected immediately and zero when
they need to be polled for and hence might be noticed
only with a certain latency. Note that there's usually
no need to invoke this function directly as
<function > sd_journal_get_timeout()</function> on these
file systems will ask for timeouts explicitly
anyway.</para>
2012-07-13 23:04:38 +04:00
</refsect1>
<refsect1 >
<title > Return Value</title>
2013-04-04 19:22:28 +04:00
<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_get_events()</function>
2013-06-27 03:47:34 +04:00
returns a combination of <constant > POLLIN</constant> ,
<constant > POLLOUT</constant> and suchlike on success or
2013-04-04 19:38:08 +04:00
a negative errno-style error code.</para>
2012-07-13 23:04:38 +04:00
2012-10-26 22:05:19 +04:00
<para > <function > sd_journal_reliable_fd()</function>
returns a positive integer if the file descriptor
returned by <function > sd_journal_get_fd()</function>
2013-04-04 22:07:48 +04:00
will generate wake-ups immediately for all journal
changes. Returns 0 if there might be a latency
involved.</para>
2012-10-26 22:05:19 +04:00
2012-07-13 23:04:38 +04:00
<para > <function > sd_journal_process()</function> and
<function > sd_journal_wait()</function> return one of
2013-06-27 03:47:34 +04:00
<constant > SD_JOURNAL_NOP</constant> ,
<constant > SD_JOURNAL_APPEND</constant> or
<constant > SD_JOURNAL_INVALIDATE</constant> on success or
2012-07-13 23:04:38 +04:00
a negative errno-style error code. If
2013-06-27 03:47:34 +04:00
<constant > SD_JOURNAL_NOP</constant> is returned the
2012-07-13 23:04:38 +04:00
journal didn't change since the last invocation. If
2013-06-27 03:47:34 +04:00
<constant > SD_JOURNAL_APPEND</constant> is returned new
2012-07-13 23:04:38 +04:00
entries have been appended to the end of the
2013-06-27 03:47:34 +04:00
journal. If <constant > SD_JOURNAL_INVALIDATE</constant>
2012-07-13 23:04:38 +04:00
journal files were added or removed (possibly due to
rotation). In the latter event live-view UIs should
probably refresh their entire display while in the
2013-06-27 03:47:34 +04:00
case of <constant > SD_JOURNAL_APPEND</constant> it is
2012-07-13 23:04:38 +04:00
sufficient to simply continue reading at the previous
end of the journal.</para>
</refsect1>
<refsect1 >
<title > Notes</title>
<para > The <function > sd_journal_get_fd()</function> ,
2013-04-04 19:22:28 +04:00
<function > sd_journal_get_events()</function> ,
2012-10-26 22:05:19 +04:00
<function > sd_journal_reliable_fd()</function> ,
2012-07-13 23:04:38 +04:00
<function > sd_journal_process()</function> and
<function > sd_journal_wait()</function> interfaces are
available as shared library, which can be compiled and
linked to with the
2013-06-27 03:47:34 +04:00
<constant > libsystemd-journal</constant>
2012-07-13 23:04:38 +04:00
<citerefentry > <refentrytitle > pkg-config</refentrytitle> <manvolnum > 1</manvolnum> </citerefentry>
file.</para>
</refsect1>
2012-10-26 22:05:19 +04:00
<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 (;;) {
2013-03-07 02:07:42 +04:00
const void *d;
2012-10-26 22:05:19 +04:00
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;
}
2013-03-07 02:07:42 +04:00
printf("%.*s\n", (int) l, (const char*) d);
2012-10-26 22:05:19 +04:00
}
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;
2013-04-04 22:07:48 +04:00
int msec;
sd_journal_get_timeout(m, & t);
if (t == (uint64_t) -1)
msec = -1;
else {
struct timespec ts;
uint64_t n;
clock_getttime(CLOCK_MONOTONIC, & ts);
n = (uint64_t) ts.tv_sec * 1000000 + ts.tv_nsec / 1000;
msec = t > n ? (int) ((t - n + 999) / 1000) : 0;
}
2013-03-07 02:07:42 +04:00
pollfd.fd = sd_journal_get_fd(j);
2013-04-04 19:22:28 +04:00
pollfd.events = sd_journal_get_events(j);
2013-04-04 22:07:48 +04:00
poll(& pollfd, 1, msec);
2012-10-26 22:05:19 +04:00
return sd_journal_process(j);
}
</programlisting>
</refsect1>
2012-07-13 23:04:38 +04:00
<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> ,
2012-10-26 22:05:19 +04:00
<citerefentry > <refentrytitle > sd_journal_next</refentrytitle> <manvolnum > 3</manvolnum> </citerefentry> ,
2013-04-04 22:07:48 +04:00
<citerefentry > <refentrytitle > poll</refentrytitle> <manvolnum > 2</manvolnum> </citerefentry> ,
<citerefentry > <refentrytitle > clock_gettime</refentrytitle> <manvolnum > 2</manvolnum> </citerefentry>
2012-07-13 23:04:38 +04:00
</para>
</refsect1>
</refentry>