4545 <refnamediv >
4646 <refname >sd_journal_get_fd</refname >
4747 <refname >sd_journal_get_events</refname >
48- <refname >sd_journal_reliable_fd </refname >
48+ <refname >sd_journal_get_timeout </refname >
4949 <refname >sd_journal_process</refname >
5050 <refname >sd_journal_wait</refname >
51+ <refname >sd_journal_reliable_fd</refname >
5152 <refname >SD_JOURNAL_NOP</refname >
5253 <refname >SD_JOURNAL_APPEND</refname >
5354 <refname >SD_JOURNAL_INVALIDATE</refname >
7071 </funcprototype >
7172
7273 <funcprototype >
73- <funcdef >int <function >sd_journal_reliable_fd </function ></funcdef >
74+ <funcdef >int <function >sd_journal_get_timeout </function ></funcdef >
7475 <paramdef >sd_journal* <parameter >j</parameter ></paramdef >
76+ <paramdef >uint64_t* <parameter >timeout_usec</parameter ></paramdef >
7577 </funcprototype >
7678
7779 <funcprototype >
8587 <paramdef >uint64_t <parameter >timeout_usec</parameter ></paramdef >
8688 </funcprototype >
8789
90+ <funcprototype >
91+ <funcdef >int <function >sd_journal_reliable_fd</function ></funcdef >
92+ <paramdef >sd_journal* <parameter >j</parameter ></paramdef >
93+ </funcprototype >
94+
8895 </funcsynopsis >
8996 </refsynopsisdiv >
9097
103110 events mask to watch for. The call takes one argument:
104111 the journal context object. Note that not all file
105112 systems are capable of generating the necessary events
106- for wakeups from this file descriptor to be enirely
107- reliable. In particular network files systems do not
108- generate suitable file change events in all cases. In
109- such a case an application should not rely alone on
110- wake-ups from this file descriptor but wake up and
111- recheck the journal in regular time intervals, for
112- example every 2s. To detect cases where this is
113- necessary, use
113+ for wakeups from this file descriptor for changes to
114+ be noticed immediately. In particular network files
115+ systems do not generate suitable file change events in
116+ all cases. Cases like this can be detected with
114117 <function >sd_journal_reliable_fd()</function >,
115- below.</para >
118+ below. <function >sd_journal_get_timeout()</function >
119+ will ensure in these cases that wake-ups happen
120+ frequently enough for changes to be noticed, although
121+ with a certain latency.</para >
116122
117123 <para ><function >sd_journal_get_events()</function >
118124 will return the <function >poll()</function > mask to
122128 the <literal >.events</literal > field of
123129 <literal >struct pollfd</literal >.</para >
124130
125- <para ><function >sd_journal_reliable_fd()</function >
126- may be used to check whether the wakeup events from
127- the file descriptor returned by
128- <function >sd_journal_get_fd</function > are sufficient
129- to track changes to the journal. If this call returns
130- 0, it is necessary to regularly recheck for journal
131- changes (suggestion: every 2s). If this call returns a
132- positive integer this is not necessary, and wakeups
133- from the file descriptor returned by
134- <function >sd_journal_get_fd()</function > are
135- sufficient as only source for wake-ups.</para >
131+ <para ><function >sd_journal_get_timeout()</function >
132+ will return a timeout value for usage in <function >poll()</function >. This returns a value in microseconds since the epoch of CLOCK_MONOTONIC for timing out <function >poll()</function > in <literal >timeout_usec</literal >. See
133+ <citerefentry ><refentrytitle >clock_gettime</refentrytitle ><manvolnum >2</manvolnum ></citerefentry >
134+ for details about
135+ <literal >CLOCK_MONOTONIC</literal >. If there's no
136+ timeout to wait for this will fill in
137+ <literal >(uint64_t) -1</literal > instead. Note that
138+ <function >poll()</function > takes a relative timeout
139+ in milliseconds rather than an absolute timeout in
140+ microseconds. To convert the absolute 'us' timeout into
141+ relative 'ms', use code like the following:</para >
142+
143+ <programlisting >uint64_t t;
144+ int msec;
145+ sd_journal_get_timeout(m, & t);
146+ if (t == (uint64_t) -1)
147+ msec = -1;
148+ else {
149+ struct timespec ts;
150+ uint64_t n;
151+ clock_getttime(CLOCK_MONOTONIC, & ts);
152+ n = (uint64_t) ts.tv_sec * 1000000 + ts.tv_nsec / 1000;
153+ msec = t > n ? (int) ((t - n + 999) / 1000) : 0;
154+ }</programlisting >
155+
156+ <para >The code above does not do any error checking
157+ for brevity's sake. The calculated <literal >msec</literal >
158+ integer can be passed directly as
159+ <function >poll()</function >'s timeout
160+ parameter.</para >
136161
137162 <para >After each <function >poll()</function > wake-up
138163 <function >sd_journal_process()</function > needs to be
143168 <para >A synchronous alternative for using
144169 <function >sd_journal_get_fd()</function >,
145170 <function >sd_journal_get_events()</function >,
146- <function >sd_journal_reliable_fd ()</function > and
171+ <function >sd_journal_get_timeout ()</function > and
147172 <function >sd_journal_process()</function > is
148173 <function >sd_journal_wait()</function >. It will
149- synchronously wait until the journal gets changed,
150- possibly using a 2s time-out if this is necessary (see
151- above). In either way the maximum time this call
152- sleeps may be controlled with the
153- <parameter >timeout_usec</parameter > parameter. Pass
154- <literal >(uint64_t) -1</literal > to wait
155- indefinitely. Internally this call simply combines
156- <function >sd_journal_get_fd()</function >,
174+ synchronously wait until the journal gets changed. The
175+ maximum time this call sleeps may be controlled with
176+ the <parameter >timeout_usec</parameter >
177+ parameter. Pass <literal >(uint64_t) -1</literal > to
178+ wait indefinitely. Internally this call simply
179+ combines <function >sd_journal_get_fd()</function >,
157180 <function >sd_journal_get_events()</function >,
158- <function >sd_journal_reliable_fd ()</function >,
181+ <function >sd_journal_get_timeout ()</function >,
159182 <function >poll()</function > and
160183 <function >sd_journal_process()</function > into
161184 one.</para >
185+
186+ <para ><function >sd_journal_reliable_fd()</function >
187+ may be used to check whether the wakeup events from
188+ the file descriptor returned by
189+ <function >sd_journal_get_fd()</function > are known to
190+ be immediately triggered. On certain file systems
191+ where file change events from the OS are not available
192+ (such as NFS) changes need to be polled for
193+ repeatedly, and hence are detected only with a certain
194+ latency. This call will return a positive value if the
195+ journal changes are detected immediately and zero when
196+ they need to be polled for and hence might be noticed
197+ only with a certain latency. Note that there's usually
198+ no need to invoke this function directly as
199+ <function >sd_journal_get_timeout()</function > on these
200+ file systems will ask for timeouts explicitly
201+ anyway.</para >
162202 </refsect1 >
163203
164204 <refsect1 >
176216 <para ><function >sd_journal_reliable_fd()</function >
177217 returns a positive integer if the file descriptor
178218 returned by <function >sd_journal_get_fd()</function >
179- is sufficient as sole wake-up source for journal
180- change events. Returns 0 if it is not sufficient and
181- the journal needs to be checked manually in regular
182- time intervals for changes. Returns a negative
183- errno-style error code on failure.</para >
219+ will generate wake-ups immediately for all journal
220+ changes. Returns 0 if there might be a latency
221+ involved.</para >
184222
185223 <para ><function >sd_journal_process()</function > and
186224 <function >sd_journal_wait()</function > return one of
@@ -270,9 +308,22 @@ int main(int argc, char *argv[]) {
270308
271309int wait_for_changes(sd_journal *j) {
272310 struct pollfd pollfd;
311+ int msec;
312+
313+ sd_journal_get_timeout(m, & t);
314+ if (t == (uint64_t) -1)
315+ msec = -1;
316+ else {
317+ struct timespec ts;
318+ uint64_t n;
319+ clock_getttime(CLOCK_MONOTONIC, & ts);
320+ n = (uint64_t) ts.tv_sec * 1000000 + ts.tv_nsec / 1000;
321+ msec = t > n ? (int) ((t - n + 999) / 1000) : 0;
322+ }
323+
273324 pollfd.fd = sd_journal_get_fd(j);
274325 pollfd.events = sd_journal_get_events(j);
275- poll(& pollfd, 1, sd_journal_reliable_fd(j) > 0 ? -1 : 2000 );
326+ poll(& pollfd, 1, msec );
276327 return sd_journal_process(j);
277328}
278329 </programlisting >
@@ -286,7 +337,8 @@ int wait_for_changes(sd_journal *j) {
286337 <citerefentry ><refentrytitle >sd-journal</refentrytitle ><manvolnum >3</manvolnum ></citerefentry >,
287338 <citerefentry ><refentrytitle >sd_journal_open</refentrytitle ><manvolnum >3</manvolnum ></citerefentry >,
288339 <citerefentry ><refentrytitle >sd_journal_next</refentrytitle ><manvolnum >3</manvolnum ></citerefentry >,
289- <citerefentry ><refentrytitle >poll</refentrytitle ><manvolnum >2</manvolnum ></citerefentry >
340+ <citerefentry ><refentrytitle >poll</refentrytitle ><manvolnum >2</manvolnum ></citerefentry >,
341+ <citerefentry ><refentrytitle >clock_gettime</refentrytitle ><manvolnum >2</manvolnum ></citerefentry >
290342 </para >
291343 </refsect1 >
292344
0 commit comments