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