| 1 |
<?xml version='1.0'?> <!--*-nxml-*--> |
| 2 |
<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN" |
| 3 |
"http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd"> |
| 4 |
<!-- SPDX-License-Identifier: LGPL-2.1-or-later --> |
| 5 |
|
| 6 |
<refentry id="sd_login_monitor_new" conditional='HAVE_PAM' |
| 7 |
xmlns:xi="http://www.w3.org/2001/XInclude"> |
| 8 |
|
| 9 |
<refentryinfo> |
| 10 |
<title>sd_login_monitor_new</title> |
| 11 |
<productname>systemd</productname> |
| 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> |
| 22 |
<refname>sd_login_monitor_unrefp</refname> |
| 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 <systemd/sd-login.h></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 |
|
| 46 |
<funcprototype> |
| 47 |
<funcdef>void <function>sd_login_monitor_unrefp</function></funcdef> |
| 48 |
<paramdef>sd_login_monitor **<parameter>m</parameter></paramdef> |
| 49 |
</funcprototype> |
| 50 |
|
| 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 |
|
| 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>{ |
| 115 |
__attribute__((cleanup(sd_login_monitor_unrefp))) sd_login_monitor *m = NULL; |
| 116 |
int r; |
| 117 |
… |
| 118 |
r = sd_login_monitor_new(NULL, &m); |
| 119 |
if (r < 0) { |
| 120 |
errno = -r; |
| 121 |
fprintf(stderr, "Failed to allocate login monitor object: %m\n"); |
| 122 |
} |
| 123 |
… |
| 124 |
}</programlisting> |
| 125 |
|
| 126 |
<para><function>sd_login_monitor_flush()</function> may be used to |
| 127 |
reset the wakeup state of the monitor object. Whenever an event |
| 128 |
causes the monitor to wake up the event loop via the file |
| 129 |
descriptor this function needs to be called to reset the wake-up |
| 130 |
state. If this call is not invoked, the file descriptor will |
| 131 |
immediately wake up the event loop again.</para> |
| 132 |
|
| 133 |
<para><function>sd_login_monitor_unref()</function> and |
| 134 |
<function>sd_login_monitor_unrefp()</function> execute no |
| 135 |
operation if the passed in monitor object is |
| 136 |
<constant>NULL</constant>.</para> |
| 137 |
|
| 138 |
<para><function>sd_login_monitor_get_fd()</function> may be used |
| 139 |
to retrieve the file descriptor of the monitor object that may be |
| 140 |
integrated in an application defined event loop, based around |
| 141 |
<citerefentry><refentrytitle>poll</refentrytitle><manvolnum>2</manvolnum></citerefentry> |
| 142 |
or a similar interface. The application should include the |
| 143 |
returned file descriptor as wake-up source for the events mask |
| 144 |
returned by <function>sd_login_monitor_get_events()</function>. It |
| 145 |
should pass a timeout value as returned by |
| 146 |
<function>sd_login_monitor_get_timeout()</function>. Whenever a |
| 147 |
wake-up is triggered the file descriptor needs to be reset via |
| 148 |
<function>sd_login_monitor_flush()</function>. An application |
| 149 |
needs to reread the login state with a function like |
| 150 |
<citerefentry><refentrytitle>sd_get_seats</refentrytitle><manvolnum>3</manvolnum></citerefentry> |
| 151 |
or similar to determine what changed.</para> |
| 152 |
|
| 153 |
<para><function>sd_login_monitor_get_events()</function> will |
| 154 |
return the <function>poll()</function> mask to wait for. This |
| 155 |
function will return a combination of <constant>POLLIN</constant>, |
| 156 |
<constant>POLLOUT</constant> and similar to fill into the |
| 157 |
<literal>.events</literal> field of <varname>struct |
| 158 |
pollfd</varname>.</para> |
| 159 |
|
| 160 |
<para><function>sd_login_monitor_get_timeout()</function> will |
| 161 |
return a timeout value for usage in <function>poll()</function>. |
| 162 |
This returns a value in microseconds since the epoch of |
| 163 |
<constant>CLOCK_MONOTONIC</constant> for timing out |
| 164 |
<function>poll()</function> in <varname>timeout_usec</varname>. |
| 165 |
See |
| 166 |
<citerefentry><refentrytitle>clock_gettime</refentrytitle><manvolnum>2</manvolnum></citerefentry> |
| 167 |
for details about <constant>CLOCK_MONOTONIC</constant>. If there |
| 168 |
is no timeout to wait for this will fill in <constant>(uint64_t) |
| 169 |
-1</constant> instead. Note that <function>poll()</function> takes |
| 170 |
a relative timeout in milliseconds rather than an absolute timeout |
| 171 |
in microseconds. To convert the absolute 'μs' timeout into |
| 172 |
relative 'ms', use code like the following:</para> |
| 173 |
|
| 174 |
<programlisting>uint64_t t; |
| 175 |
int msec; |
| 176 |
sd_login_monitor_get_timeout(m, &t); |
| 177 |
if (t == (uint64_t) -1) |
| 178 |
msec = -1; |
| 179 |
else { |
| 180 |
struct timespec ts; |
| 181 |
uint64_t n; |
| 182 |
clock_gettime(CLOCK_MONOTONIC, &ts); |
| 183 |
n = (uint64_t) ts.tv_sec * 1000000 + ts.tv_nsec / 1000; |
| 184 |
msec = t > n ? (int) ((t - n + 999) / 1000) : 0; |
| 185 |
}</programlisting> |
| 186 |
|
| 187 |
<para>The code above does not do any error checking for brevity's |
| 188 |
sake. The calculated <varname>msec</varname> integer can be passed |
| 189 |
directly as <function>poll()</function>'s timeout |
| 190 |
parameter.</para> |
| 191 |
</refsect1> |
| 192 |
|
| 193 |
<refsect1> |
| 194 |
<title>Return Value</title> |
| 195 |
|
| 196 |
<para>On success, |
| 197 |
<function>sd_login_monitor_new()</function>, |
| 198 |
<function>sd_login_monitor_flush()</function> and |
| 199 |
<function>sd_login_monitor_get_timeout()</function> |
| 200 |
return 0 or a positive integer. On success, |
| 201 |
<function>sd_login_monitor_get_fd()</function> returns |
| 202 |
a Unix file descriptor. On success, |
| 203 |
<function>sd_login_monitor_get_events()</function> |
| 204 |
returns a combination of <constant>POLLIN</constant>, |
| 205 |
<constant>POLLOUT</constant> and suchlike. On failure, |
| 206 |
these calls return a negative errno-style error |
| 207 |
code.</para> |
| 208 |
|
| 209 |
<para><function>sd_login_monitor_unref()</function> |
| 210 |
always returns <constant>NULL</constant>.</para> |
| 211 |
|
| 212 |
<refsect2> |
| 213 |
<title>Errors</title> |
| 214 |
|
| 215 |
<para>Returned errors may indicate the following problems:</para> |
| 216 |
|
| 217 |
<variablelist> |
| 218 |
|
| 219 |
<varlistentry> |
| 220 |
<term><constant>-EINVAL</constant></term> |
| 221 |
|
| 222 |
<listitem><para>An input parameter was invalid (out of range, or <constant>NULL</constant>, where |
| 223 |
that is not accepted). The specified category to watch is not known.</para></listitem> |
| 224 |
</varlistentry> |
| 225 |
|
| 226 |
<varlistentry> |
| 227 |
<term><constant>-ENOMEM</constant></term> |
| 228 |
|
| 229 |
<listitem><para>Memory allocation failed.</para></listitem> |
| 230 |
</varlistentry> |
| 231 |
</variablelist> |
| 232 |
</refsect2> |
| 233 |
</refsect1> |
| 234 |
|
| 235 |
<xi:include href="libsystemd-pkgconfig.xml" /> |
| 236 |
|
| 237 |
<refsect1> |
| 238 |
<title>History</title> |
| 239 |
<para><function>sd_login_monitor_get_events()</function> and |
| 240 |
<function>sd_login_monitor_get_timeout()</function> were added in version 201.</para> |
| 241 |
<para><function>sd_login_monitor_unrefp()</function> was added in version 229.</para> |
| 242 |
</refsect1> |
| 243 |
|
| 244 |
<refsect1> |
| 245 |
<title>See Also</title> |
| 246 |
|
| 247 |
<para><simplelist type="inline"> |
| 248 |
<member><citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry></member> |
| 249 |
<member><citerefentry><refentrytitle>sd-login</refentrytitle><manvolnum>3</manvolnum></citerefentry></member> |
| 250 |
<member><citerefentry><refentrytitle>sd_get_seats</refentrytitle><manvolnum>3</manvolnum></citerefentry></member> |
| 251 |
<member><citerefentry><refentrytitle>poll</refentrytitle><manvolnum>2</manvolnum></citerefentry></member> |
| 252 |
<member><citerefentry><refentrytitle>clock_gettime</refentrytitle><manvolnum>2</manvolnum></citerefentry></member> |
| 253 |
</simplelist></para> |
| 254 |
</refsect1> |
| 255 |
|
| 256 |
</refentry> |