]> git.ipfire.org Git - thirdparty/systemd.git/blame - man/sd_login_monitor_new.xml
man: fix link markup
[thirdparty/systemd.git] / man / sd_login_monitor_new.xml
CommitLineData
0b3b020a 1<?xml version='1.0'?> <!--*-nxml-*-->
3a54a157 2<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
12b42c76 3 "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
0307f791 4<!-- SPDX-License-Identifier: LGPL-2.1+ -->
0b3b020a 5
7d6b2723
ZJS
6<refentry id="sd_login_monitor_new" conditional='HAVE_PAM'
7 xmlns:xi="http://www.w3.org/2001/XInclude">
0b3b020a 8
798d3a52
ZJS
9 <refentryinfo>
10 <title>sd_login_monitor_new</title>
11 <productname>systemd</productname>
798d3a52
ZJS
12 </refentryinfo>
13
14 <refmeta>
15 <refentrytitle>sd_login_monitor_new</refentrytitle>
16 <manvolnum>3</manvolnum>
17 </refmeta>
18
19 <refnamediv>
20 <refname>sd_login_monitor_new</refname>
21 <refname>sd_login_monitor_unref</refname>
4afd3348 22 <refname>sd_login_monitor_unrefp</refname>
798d3a52
ZJS
23 <refname>sd_login_monitor_flush</refname>
24 <refname>sd_login_monitor_get_fd</refname>
25 <refname>sd_login_monitor_get_events</refname>
26 <refname>sd_login_monitor_get_timeout</refname>
27 <refname>sd_login_monitor</refname>
28 <refpurpose>Monitor login sessions, seats, users and virtual machines/containers</refpurpose>
29 </refnamediv>
30
31 <refsynopsisdiv>
32 <funcsynopsis>
33 <funcsynopsisinfo>#include &lt;systemd/sd-login.h&gt;</funcsynopsisinfo>
34
35 <funcprototype>
36 <funcdef>int <function>sd_login_monitor_new</function></funcdef>
37 <paramdef>const char *<parameter>category</parameter></paramdef>
38 <paramdef>sd_login_monitor **<parameter>ret</parameter></paramdef>
39 </funcprototype>
40
41 <funcprototype>
42 <funcdef>sd_login_monitor *<function>sd_login_monitor_unref</function></funcdef>
43 <paramdef>sd_login_monitor *<parameter>m</parameter></paramdef>
44 </funcprototype>
45
4afd3348
LP
46 <funcprototype>
47 <funcdef>void <function>sd_login_monitor_unrefp</function></funcdef>
48 <paramdef>sd_login_monitor **<parameter>m</parameter></paramdef>
49 </funcprototype>
50
798d3a52
ZJS
51 <funcprototype>
52 <funcdef>int <function>sd_login_monitor_flush</function></funcdef>
53 <paramdef>sd_login_monitor *<parameter>m</parameter></paramdef>
54 </funcprototype>
55
56 <funcprototype>
57 <funcdef>int <function>sd_login_monitor_get_fd</function></funcdef>
58 <paramdef>sd_login_monitor *<parameter>m</parameter></paramdef>
59 </funcprototype>
60
61 <funcprototype>
62 <funcdef>int <function>sd_login_monitor_get_events</function></funcdef>
63 <paramdef>sd_login_monitor *<parameter>m</parameter></paramdef>
64 </funcprototype>
65
66 <funcprototype>
67 <funcdef>int <function>sd_login_monitor_get_timeout</function></funcdef>
68 <paramdef>sd_login_monitor *<parameter>m</parameter></paramdef>
69 <paramdef>uint64_t *<parameter>timeout_usec</parameter></paramdef>
70 </funcprototype>
71
72 </funcsynopsis>
73 </refsynopsisdiv>
74
75 <refsect1>
76 <title>Description</title>
77
78 <para><function>sd_login_monitor_new()</function> may be used to
79 monitor login sessions, users, seats, and virtual
80 machines/containers. Via a monitor object a file descriptor can be
81 integrated into an application defined event loop which is woken
82 up each time a user logs in, logs out or a seat is added or
83 removed, or a session, user, seat or virtual machine/container
84 changes state otherwise. The first parameter takes a string which
85 can be <literal>seat</literal> (to get only notifications about
86 seats being added, removed or changed), <literal>session</literal>
87 (to get only notifications about sessions being created or removed
88 or changed), <literal>uid</literal> (to get only notifications
89 when a user changes state in respect to logins) or
90 <literal>machine</literal> (to get only notifications when a
91 virtual machine or container is started or stopped). If
92 notifications shall be generated in all these conditions,
93 <constant>NULL</constant> may be passed. Note that in the future
94 additional categories may be defined. The second parameter returns
95 a monitor object and needs to be freed with the
96 <function>sd_login_monitor_unref()</function> call after
97 use.</para>
98
99 <para><function>sd_login_monitor_unref()</function> may be used to
100 destroy a monitor object. Note that this will invalidate any file
101 descriptor returned by
102 <function>sd_login_monitor_get_fd()</function>.</para>
103
4afd3348
LP
104 <para><function>sd_login_monitor_unrefp()</function> is similar to
105 <function>sd_login_monitor_unref()</function> but takes a pointer
106 to a pointer to an <type>sd_login_monitor</type> object. This call
107 is useful in conjunction with GCC's and LLVM's <ulink
108 url="https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html">Clean-up
109 Variable Attribute</ulink>. Note that this function is defined as
110 inline function. Use a declaration like the following, in order to
111 allocate a login monitor object that is freed automatically as the
112 code block is left:</para>
113
114 <programlisting>{
f4e1a425 115 __attribute__((cleanup(sd_login_monitor_unrefp))) sd_login_monitor *m = NULL;
787f78b6
ZJS
116 int r;
117
118 r = sd_login_monitor_default(&amp;m);
119 if (r &lt; 0)
120 fprintf(stderr, "Failed to allocate login monitor object: %s\n", strerror(-r));
121
4afd3348
LP
122}</programlisting>
123
798d3a52
ZJS
124 <para><function>sd_login_monitor_flush()</function> may be used to
125 reset the wakeup state of the monitor object. Whenever an event
126 causes the monitor to wake up the event loop via the file
127 descriptor this function needs to be called to reset the wake-up
128 state. If this call is not invoked, the file descriptor will
129 immediately wake up the event loop again.</para>
130
4afd3348
LP
131 <para><function>sd_login_monitor_unref()</function> and
132 <function>sd_login_monitor_unrefp()</function> execute no
133 operation if the passed in monitor object is
134 <constant>NULL</constant>.</para>
135
798d3a52
ZJS
136 <para><function>sd_login_monitor_get_fd()</function> may be used
137 to retrieve the file descriptor of the monitor object that may be
138 integrated in an application defined event loop, based around
139 <citerefentry><refentrytitle>poll</refentrytitle><manvolnum>2</manvolnum></citerefentry>
140 or a similar interface. The application should include the
141 returned file descriptor as wake-up source for the events mask
142 returned by <function>sd_login_monitor_get_events()</function>. It
143 should pass a timeout value as returned by
144 <function>sd_login_monitor_get_timeout()</function>. Whenever a
145 wake-up is triggered the file descriptor needs to be reset via
146 <function>sd_login_monitor_flush()</function>. An application
147 needs to reread the login state with a function like
148 <citerefentry><refentrytitle>sd_get_seats</refentrytitle><manvolnum>3</manvolnum></citerefentry>
149 or similar to determine what changed.</para>
150
151 <para><function>sd_login_monitor_get_events()</function> will
152 return the <function>poll()</function> mask to wait for. This
153 function will return a combination of <constant>POLLIN</constant>,
154 <constant>POLLOUT</constant> and similar to fill into the
155 <literal>.events</literal> field of <varname>struct
156 pollfd</varname>.</para>
157
158 <para><function>sd_login_monitor_get_timeout()</function> will
159 return a timeout value for usage in <function>poll()</function>.
160 This returns a value in microseconds since the epoch of
161 <constant>CLOCK_MONOTONIC</constant> for timing out
162 <function>poll()</function> in <varname>timeout_usec</varname>.
163 See
164 <citerefentry><refentrytitle>clock_gettime</refentrytitle><manvolnum>2</manvolnum></citerefentry>
165 for details about <constant>CLOCK_MONOTONIC</constant>. If there
166 is no timeout to wait for this will fill in <constant>(uint64_t)
167 -1</constant> instead. Note that <function>poll()</function> takes
168 a relative timeout in milliseconds rather than an absolute timeout
707b66c6 169 in microseconds. To convert the absolute 'µs' timeout into
798d3a52
ZJS
170 relative 'ms', use code like the following:</para>
171
172 <programlisting>uint64_t t;
667c24a6
LP
173int msec;
174sd_login_monitor_get_timeout(m, &amp;t);
175if (t == (uint64_t) -1)
787f78b6 176 msec = -1;
667c24a6 177else {
787f78b6
ZJS
178 struct timespec ts;
179 uint64_t n;
180 clock_gettime(CLOCK_MONOTONIC, &amp;ts);
181 n = (uint64_t) ts.tv_sec * 1000000 + ts.tv_nsec / 1000;
182 msec = t > n ? (int) ((t - n + 999) / 1000) : 0;
667c24a6
LP
183}</programlisting>
184
798d3a52
ZJS
185 <para>The code above does not do any error checking for brevity's
186 sake. The calculated <varname>msec</varname> integer can be passed
187 directly as <function>poll()</function>'s timeout
188 parameter.</para>
189 </refsect1>
190
191 <refsect1>
192 <title>Return Value</title>
193
194 <para>On success,
195 <function>sd_login_monitor_new()</function>,
196 <function>sd_login_monitor_flush()</function> and
197 <function>sd_login_monitor_get_timeout()</function>
198 return 0 or a positive integer. On success,
199 <function>sd_login_monitor_get_fd()</function> returns
200 a Unix file descriptor. On success,
201 <function>sd_login_monitor_get_events()</function>
202 returns a combination of <constant>POLLIN</constant>,
203 <constant>POLLOUT</constant> and suchlike. On failure,
204 these calls return a negative errno-style error
205 code.</para>
206
207 <para><function>sd_login_monitor_unref()</function>
208 always returns <constant>NULL</constant>.</para>
798d3a52 209
b1de39de
ZJS
210 <refsect2>
211 <title>Errors</title>
707b66c6 212
b1de39de 213 <para>Returned errors may indicate the following problems:</para>
707b66c6 214
b1de39de 215 <variablelist>
707b66c6 216
b1de39de
ZJS
217 <varlistentry>
218 <term><constant>-EINVAL</constant></term>
707b66c6 219
b1de39de
ZJS
220 <listitem><para>An input parameter was invalid (out of range, or <constant>NULL</constant>, where
221 that is not accepted). The specified category to watch is not known.</para></listitem>
222 </varlistentry>
707b66c6 223
b1de39de
ZJS
224 <varlistentry>
225 <term><constant>-ENOMEM</constant></term>
707b66c6 226
b1de39de
ZJS
227 <listitem><para>Memory allocation failed.</para></listitem>
228 </varlistentry>
229 </variablelist>
230 </refsect2>
707b66c6
LP
231 </refsect1>
232
7d6b2723 233 <xi:include href="libsystemd-pkgconfig.xml" />
798d3a52
ZJS
234
235 <refsect1>
236 <title>See Also</title>
237
238 <para>
239 <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
240 <citerefentry><refentrytitle>sd-login</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
241 <citerefentry><refentrytitle>sd_get_seats</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
242 <citerefentry><refentrytitle>poll</refentrytitle><manvolnum>2</manvolnum></citerefentry>,
243 <citerefentry><refentrytitle>clock_gettime</refentrytitle><manvolnum>2</manvolnum></citerefentry>
244 </para>
245 </refsect1>
0b3b020a
LP
246
247</refentry>