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