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