1 <?xml version='1.0'?> <!--*-nxml-*-->
2 <!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
3 "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
6 SPDX-License-Identifier: LGPL-2.1+
8 This file is part of systemd.
10 Copyright 2010 Lennart Poettering
13 <refentry id="sd_login_monitor_new" conditional='HAVE_PAM'
14 xmlns:xi="http://www.w3.org/2001/XInclude">
17 <title>sd_login_monitor_new</title>
18 <productname>systemd</productname>
22 <contrib>Developer</contrib>
23 <firstname>Lennart</firstname>
24 <surname>Poettering</surname>
25 <email>lennart@poettering.net</email>
31 <refentrytitle>sd_login_monitor_new</refentrytitle>
32 <manvolnum>3</manvolnum>
36 <refname>sd_login_monitor_new</refname>
37 <refname>sd_login_monitor_unref</refname>
38 <refname>sd_login_monitor_unrefp</refname>
39 <refname>sd_login_monitor_flush</refname>
40 <refname>sd_login_monitor_get_fd</refname>
41 <refname>sd_login_monitor_get_events</refname>
42 <refname>sd_login_monitor_get_timeout</refname>
43 <refname>sd_login_monitor</refname>
44 <refpurpose>Monitor login sessions, seats, users and virtual machines/containers</refpurpose>
49 <funcsynopsisinfo>#include <systemd/sd-login.h></funcsynopsisinfo>
52 <funcdef>int <function>sd_login_monitor_new</function></funcdef>
53 <paramdef>const char *<parameter>category</parameter></paramdef>
54 <paramdef>sd_login_monitor **<parameter>ret</parameter></paramdef>
58 <funcdef>sd_login_monitor *<function>sd_login_monitor_unref</function></funcdef>
59 <paramdef>sd_login_monitor *<parameter>m</parameter></paramdef>
63 <funcdef>void <function>sd_login_monitor_unrefp</function></funcdef>
64 <paramdef>sd_login_monitor **<parameter>m</parameter></paramdef>
68 <funcdef>int <function>sd_login_monitor_flush</function></funcdef>
69 <paramdef>sd_login_monitor *<parameter>m</parameter></paramdef>
73 <funcdef>int <function>sd_login_monitor_get_fd</function></funcdef>
74 <paramdef>sd_login_monitor *<parameter>m</parameter></paramdef>
78 <funcdef>int <function>sd_login_monitor_get_events</function></funcdef>
79 <paramdef>sd_login_monitor *<parameter>m</parameter></paramdef>
83 <funcdef>int <function>sd_login_monitor_get_timeout</function></funcdef>
84 <paramdef>sd_login_monitor *<parameter>m</parameter></paramdef>
85 <paramdef>uint64_t *<parameter>timeout_usec</parameter></paramdef>
92 <title>Description</title>
94 <para><function>sd_login_monitor_new()</function> may be used to
95 monitor login sessions, users, seats, and virtual
96 machines/containers. Via a monitor object a file descriptor can be
97 integrated into an application defined event loop which is woken
98 up each time a user logs in, logs out or a seat is added or
99 removed, or a session, user, seat or virtual machine/container
100 changes state otherwise. The first parameter takes a string which
101 can be <literal>seat</literal> (to get only notifications about
102 seats being added, removed or changed), <literal>session</literal>
103 (to get only notifications about sessions being created or removed
104 or changed), <literal>uid</literal> (to get only notifications
105 when a user changes state in respect to logins) or
106 <literal>machine</literal> (to get only notifications when a
107 virtual machine or container is started or stopped). If
108 notifications shall be generated in all these conditions,
109 <constant>NULL</constant> may be passed. Note that in the future
110 additional categories may be defined. The second parameter returns
111 a monitor object and needs to be freed with the
112 <function>sd_login_monitor_unref()</function> call after
115 <para><function>sd_login_monitor_unref()</function> may be used to
116 destroy a monitor object. Note that this will invalidate any file
117 descriptor returned by
118 <function>sd_login_monitor_get_fd()</function>.</para>
120 <para><function>sd_login_monitor_unrefp()</function> is similar to
121 <function>sd_login_monitor_unref()</function> but takes a pointer
122 to a pointer to an <type>sd_login_monitor</type> object. This call
123 is useful in conjunction with GCC's and LLVM's <ulink
124 url="https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html">Clean-up
125 Variable Attribute</ulink>. Note that this function is defined as
126 inline function. Use a declaration like the following, in order to
127 allocate a login monitor object that is freed automatically as the
128 code block is left:</para>
131 __attribute__((cleanup(sd_login_monitor_unrefp)) sd_login_monitor *m = NULL;
134 r = sd_login_monitor_default(&m);
136 fprintf(stderr, "Failed to allocate login monitor object: %s\n", strerror(-r));
140 <para><function>sd_login_monitor_flush()</function> may be used to
141 reset the wakeup state of the monitor object. Whenever an event
142 causes the monitor to wake up the event loop via the file
143 descriptor this function needs to be called to reset the wake-up
144 state. If this call is not invoked, the file descriptor will
145 immediately wake up the event loop again.</para>
147 <para><function>sd_login_monitor_unref()</function> and
148 <function>sd_login_monitor_unrefp()</function> execute no
149 operation if the passed in monitor object is
150 <constant>NULL</constant>.</para>
152 <para><function>sd_login_monitor_get_fd()</function> may be used
153 to retrieve the file descriptor of the monitor object that may be
154 integrated in an application defined event loop, based around
155 <citerefentry><refentrytitle>poll</refentrytitle><manvolnum>2</manvolnum></citerefentry>
156 or a similar interface. The application should include the
157 returned file descriptor as wake-up source for the events mask
158 returned by <function>sd_login_monitor_get_events()</function>. It
159 should pass a timeout value as returned by
160 <function>sd_login_monitor_get_timeout()</function>. Whenever a
161 wake-up is triggered the file descriptor needs to be reset via
162 <function>sd_login_monitor_flush()</function>. An application
163 needs to reread the login state with a function like
164 <citerefentry><refentrytitle>sd_get_seats</refentrytitle><manvolnum>3</manvolnum></citerefentry>
165 or similar to determine what changed.</para>
167 <para><function>sd_login_monitor_get_events()</function> will
168 return the <function>poll()</function> mask to wait for. This
169 function will return a combination of <constant>POLLIN</constant>,
170 <constant>POLLOUT</constant> and similar to fill into the
171 <literal>.events</literal> field of <varname>struct
172 pollfd</varname>.</para>
174 <para><function>sd_login_monitor_get_timeout()</function> will
175 return a timeout value for usage in <function>poll()</function>.
176 This returns a value in microseconds since the epoch of
177 <constant>CLOCK_MONOTONIC</constant> for timing out
178 <function>poll()</function> in <varname>timeout_usec</varname>.
180 <citerefentry><refentrytitle>clock_gettime</refentrytitle><manvolnum>2</manvolnum></citerefentry>
181 for details about <constant>CLOCK_MONOTONIC</constant>. If there
182 is no timeout to wait for this will fill in <constant>(uint64_t)
183 -1</constant> instead. Note that <function>poll()</function> takes
184 a relative timeout in milliseconds rather than an absolute timeout
185 in microseconds. To convert the absolute 'µs' timeout into
186 relative 'ms', use code like the following:</para>
188 <programlisting>uint64_t t;
190 sd_login_monitor_get_timeout(m, &t);
191 if (t == (uint64_t) -1)
196 clock_gettime(CLOCK_MONOTONIC, &ts);
197 n = (uint64_t) ts.tv_sec * 1000000 + ts.tv_nsec / 1000;
198 msec = t > n ? (int) ((t - n + 999) / 1000) : 0;
201 <para>The code above does not do any error checking for brevity's
202 sake. The calculated <varname>msec</varname> integer can be passed
203 directly as <function>poll()</function>'s timeout
208 <title>Return Value</title>
211 <function>sd_login_monitor_new()</function>,
212 <function>sd_login_monitor_flush()</function> and
213 <function>sd_login_monitor_get_timeout()</function>
214 return 0 or a positive integer. On success,
215 <function>sd_login_monitor_get_fd()</function> returns
216 a Unix file descriptor. On success,
217 <function>sd_login_monitor_get_events()</function>
218 returns a combination of <constant>POLLIN</constant>,
219 <constant>POLLOUT</constant> and suchlike. On failure,
220 these calls return a negative errno-style error
223 <para><function>sd_login_monitor_unref()</function>
224 always returns <constant>NULL</constant>.</para>
228 <title>Errors</title>
230 <para>Returned errors may indicate the following problems:</para>
235 <term><constant>-EINVAL</constant></term>
237 <listitem><para>An input parameter was invalid (out of range,
238 or NULL, where that is not accepted). The specified category to
239 watch is not known.</para></listitem>
243 <term><constant>-ENOMEM</constant></term>
245 <listitem><para>Memory allocation failed.</para></listitem>
250 <xi:include href="libelogind-pkgconfig.xml" />
253 <title>See Also</title>
256 <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
257 <citerefentry><refentrytitle>sd-login</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
258 <citerefentry><refentrytitle>sd_get_seats</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
259 <citerefentry><refentrytitle>poll</refentrytitle><manvolnum>2</manvolnum></citerefentry>,
260 <citerefentry><refentrytitle>clock_gettime</refentrytitle><manvolnum>2</manvolnum></citerefentry>