| 1 |
'\" t |
| 2 |
.TH "SD_LOGIN_MONITOR_NEW" "3" "" "systemd 261~rc3" "sd_login_monitor_new" |
| 3 |
.\" ----------------------------------------------------------------- |
| 4 |
.\" * Define some portability stuff |
| 5 |
.\" ----------------------------------------------------------------- |
| 6 |
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 7 |
.\" http://bugs.debian.org/507673 |
| 8 |
.\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html |
| 9 |
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 10 |
.ie \n(.g .ds Aq \(aq |
| 11 |
.el .ds Aq ' |
| 12 |
.\" ----------------------------------------------------------------- |
| 13 |
.\" * set default formatting |
| 14 |
.\" ----------------------------------------------------------------- |
| 15 |
.\" disable hyphenation |
| 16 |
.nh |
| 17 |
.\" disable justification (adjust text to left margin only) |
| 18 |
.ad l |
| 19 |
.\" ----------------------------------------------------------------- |
| 20 |
.\" * MAIN CONTENT STARTS HERE * |
| 21 |
.\" ----------------------------------------------------------------- |
| 22 |
.SH "NAME" |
| 23 |
sd_login_monitor_new, sd_login_monitor_unref, sd_login_monitor_unrefp, sd_login_monitor_flush, sd_login_monitor_get_fd, sd_login_monitor_get_events, sd_login_monitor_get_timeout, sd_login_monitor \- Monitor login sessions, seats, users and virtual machines/containers |
| 24 |
.SH "SYNOPSIS" |
| 25 |
.sp |
| 26 |
.ft B |
| 27 |
.nf |
| 28 |
#include <systemd/sd\-login\&.h> |
| 29 |
.fi |
| 30 |
.ft |
| 31 |
.HP \w'int\ sd_login_monitor_new('u |
| 32 |
.BI "int sd_login_monitor_new(const\ char\ *" "category" ", sd_login_monitor\ **" "ret" ");" |
| 33 |
.HP \w'sd_login_monitor\ *sd_login_monitor_unref('u |
| 34 |
.BI "sd_login_monitor *sd_login_monitor_unref(sd_login_monitor\ *" "m" ");" |
| 35 |
.HP \w'void\ sd_login_monitor_unrefp('u |
| 36 |
.BI "void sd_login_monitor_unrefp(sd_login_monitor\ **" "m" ");" |
| 37 |
.HP \w'int\ sd_login_monitor_flush('u |
| 38 |
.BI "int sd_login_monitor_flush(sd_login_monitor\ *" "m" ");" |
| 39 |
.HP \w'int\ sd_login_monitor_get_fd('u |
| 40 |
.BI "int sd_login_monitor_get_fd(sd_login_monitor\ *" "m" ");" |
| 41 |
.HP \w'int\ sd_login_monitor_get_events('u |
| 42 |
.BI "int sd_login_monitor_get_events(sd_login_monitor\ *" "m" ");" |
| 43 |
.HP \w'int\ sd_login_monitor_get_timeout('u |
| 44 |
.BI "int sd_login_monitor_get_timeout(sd_login_monitor\ *" "m" ", uint64_t\ *" "timeout_usec" ");" |
| 45 |
.SH "DESCRIPTION" |
| 46 |
.PP |
| 47 |
\fBsd_login_monitor_new()\fR |
| 48 |
may be used to monitor login sessions, users, seats, and virtual machines/containers\&. Via a monitor object a file descriptor can be integrated into an application defined event loop which is woken up each time a user logs in, logs out or a seat is added or removed, or a session, user, seat or virtual machine/container changes state otherwise\&. The first parameter takes a string which can be |
| 49 |
"seat" |
| 50 |
(to get only notifications about seats being added, removed or changed), |
| 51 |
"session" |
| 52 |
(to get only notifications about sessions being created or removed or changed), |
| 53 |
"uid" |
| 54 |
(to get only notifications when a user changes state in respect to logins) or |
| 55 |
"machine" |
| 56 |
(to get only notifications when a virtual machine or container is started or stopped)\&. If notifications shall be generated in all these conditions, |
| 57 |
\fBNULL\fR |
| 58 |
may be passed\&. Note that in the future additional categories may be defined\&. The second parameter returns a monitor object and needs to be freed with the |
| 59 |
\fBsd_login_monitor_unref()\fR |
| 60 |
call after use\&. |
| 61 |
.PP |
| 62 |
\fBsd_login_monitor_unref()\fR |
| 63 |
may be used to destroy a monitor object\&. Note that this will invalidate any file descriptor returned by |
| 64 |
\fBsd_login_monitor_get_fd()\fR\&. |
| 65 |
.PP |
| 66 |
\fBsd_login_monitor_unrefp()\fR |
| 67 |
is similar to |
| 68 |
\fBsd_login_monitor_unref()\fR |
| 69 |
but takes a pointer to a pointer to an |
| 70 |
\fBsd_login_monitor\fR |
| 71 |
object\&. This call is useful in conjunction with GCC\*(Aqs and LLVM\*(Aqs |
| 72 |
\m[blue]\fBClean\-up Variable Attribute\fR\m[]\&\s-2\u[1]\d\s+2\&. Note that this function is defined as inline function\&. Use a declaration like the following, in order to allocate a login monitor object that is freed automatically as the code block is left: |
| 73 |
.sp |
| 74 |
.if n \{\ |
| 75 |
.RS 4 |
| 76 |
.\} |
| 77 |
.nf |
| 78 |
{ |
| 79 |
__attribute__((cleanup(sd_login_monitor_unrefp))) sd_login_monitor *m = NULL; |
| 80 |
int r; |
| 81 |
\&... |
| 82 |
r = sd_login_monitor_new(NULL, &m); |
| 83 |
if (r < 0) { |
| 84 |
errno = \-r; |
| 85 |
fprintf(stderr, "Failed to allocate login monitor object: %m\en"); |
| 86 |
} |
| 87 |
\&... |
| 88 |
} |
| 89 |
.fi |
| 90 |
.if n \{\ |
| 91 |
.RE |
| 92 |
.\} |
| 93 |
.PP |
| 94 |
\fBsd_login_monitor_flush()\fR |
| 95 |
may be used to reset the wakeup state of the monitor object\&. Whenever an event causes the monitor to wake up the event loop via the file descriptor this function needs to be called to reset the wake\-up state\&. If this call is not invoked, the file descriptor will immediately wake up the event loop again\&. |
| 96 |
.PP |
| 97 |
\fBsd_login_monitor_unref()\fR |
| 98 |
and |
| 99 |
\fBsd_login_monitor_unrefp()\fR |
| 100 |
execute no operation if the passed in monitor object is |
| 101 |
\fBNULL\fR\&. |
| 102 |
.PP |
| 103 |
\fBsd_login_monitor_get_fd()\fR |
| 104 |
may be used to retrieve the file descriptor of the monitor object that may be integrated in an application defined event loop, based around |
| 105 |
\fBpoll\fR(2) |
| 106 |
or a similar interface\&. The application should include the returned file descriptor as wake\-up source for the events mask returned by |
| 107 |
\fBsd_login_monitor_get_events()\fR\&. It should pass a timeout value as returned by |
| 108 |
\fBsd_login_monitor_get_timeout()\fR\&. Whenever a wake\-up is triggered the file descriptor needs to be reset via |
| 109 |
\fBsd_login_monitor_flush()\fR\&. An application needs to reread the login state with a function like |
| 110 |
\fBsd_get_seats\fR(3) |
| 111 |
or similar to determine what changed\&. |
| 112 |
.PP |
| 113 |
\fBsd_login_monitor_get_events()\fR |
| 114 |
will return the |
| 115 |
\fBpoll()\fR |
| 116 |
mask to wait for\&. This function will return a combination of |
| 117 |
\fBPOLLIN\fR, |
| 118 |
\fBPOLLOUT\fR |
| 119 |
and similar to fill into the |
| 120 |
"\&.events" |
| 121 |
field of |
| 122 |
\fIstruct pollfd\fR\&. |
| 123 |
.PP |
| 124 |
\fBsd_login_monitor_get_timeout()\fR |
| 125 |
will return a timeout value for usage in |
| 126 |
\fBpoll()\fR\&. This returns a value in microseconds since the epoch of |
| 127 |
\fBCLOCK_MONOTONIC\fR |
| 128 |
for timing out |
| 129 |
\fBpoll()\fR |
| 130 |
in |
| 131 |
\fItimeout_usec\fR\&. See |
| 132 |
\fBclock_gettime\fR(2) |
| 133 |
for details about |
| 134 |
\fBCLOCK_MONOTONIC\fR\&. If there is no timeout to wait for this will fill in |
| 135 |
\fB(uint64_t) \-1\fR |
| 136 |
instead\&. Note that |
| 137 |
\fBpoll()\fR |
| 138 |
takes a relative timeout in milliseconds rather than an absolute timeout in microseconds\&. To convert the absolute \*(Aqμs\*(Aq timeout into relative \*(Aqms\*(Aq, use code like the following: |
| 139 |
.sp |
| 140 |
.if n \{\ |
| 141 |
.RS 4 |
| 142 |
.\} |
| 143 |
.nf |
| 144 |
uint64_t t; |
| 145 |
int msec; |
| 146 |
sd_login_monitor_get_timeout(m, &t); |
| 147 |
if (t == (uint64_t) \-1) |
| 148 |
msec = \-1; |
| 149 |
else { |
| 150 |
struct timespec ts; |
| 151 |
uint64_t n; |
| 152 |
clock_gettime(CLOCK_MONOTONIC, &ts); |
| 153 |
n = (uint64_t) ts\&.tv_sec * 1000000 + ts\&.tv_nsec / 1000; |
| 154 |
msec = t > n ? (int) ((t \- n + 999) / 1000) : 0; |
| 155 |
} |
| 156 |
.fi |
| 157 |
.if n \{\ |
| 158 |
.RE |
| 159 |
.\} |
| 160 |
.PP |
| 161 |
The code above does not do any error checking for brevity\*(Aqs sake\&. The calculated |
| 162 |
\fImsec\fR |
| 163 |
integer can be passed directly as |
| 164 |
\fBpoll()\fR\*(Aqs timeout parameter\&. |
| 165 |
.SH "RETURN VALUE" |
| 166 |
.PP |
| 167 |
On success, |
| 168 |
\fBsd_login_monitor_new()\fR, |
| 169 |
\fBsd_login_monitor_flush()\fR |
| 170 |
and |
| 171 |
\fBsd_login_monitor_get_timeout()\fR |
| 172 |
return 0 or a positive integer\&. On success, |
| 173 |
\fBsd_login_monitor_get_fd()\fR |
| 174 |
returns a Unix file descriptor\&. On success, |
| 175 |
\fBsd_login_monitor_get_events()\fR |
| 176 |
returns a combination of |
| 177 |
\fBPOLLIN\fR, |
| 178 |
\fBPOLLOUT\fR |
| 179 |
and suchlike\&. On failure, these calls return a negative errno\-style error code\&. |
| 180 |
.PP |
| 181 |
\fBsd_login_monitor_unref()\fR |
| 182 |
always returns |
| 183 |
\fBNULL\fR\&. |
| 184 |
.SS "Errors" |
| 185 |
.PP |
| 186 |
Returned errors may indicate the following problems: |
| 187 |
.PP |
| 188 |
\fB\-EINVAL\fR |
| 189 |
.RS 4 |
| 190 |
An input parameter was invalid (out of range, or |
| 191 |
\fBNULL\fR, where that is not accepted)\&. The specified category to watch is not known\&. |
| 192 |
.RE |
| 193 |
.PP |
| 194 |
\fB\-ENOMEM\fR |
| 195 |
.RS 4 |
| 196 |
Memory allocation failed\&. |
| 197 |
.RE |
| 198 |
.SH "NOTES" |
| 199 |
.PP |
| 200 |
Functions described here are available as a shared library, which can be compiled against and linked to with the |
| 201 |
\fBlibsystemd\fR\ \&\fBpkg-config\fR(1) |
| 202 |
file\&. |
| 203 |
.PP |
| 204 |
The code described here uses |
| 205 |
\fBgetenv\fR(3), which is declared to be not multi\-thread\-safe\&. This means that the code calling the functions described here must not call |
| 206 |
\fBsetenv\fR(3) |
| 207 |
from a parallel thread\&. It is recommended to only do calls to |
| 208 |
\fBsetenv()\fR |
| 209 |
from an early phase of the program when no other threads have been started\&. |
| 210 |
.SH "HISTORY" |
| 211 |
.PP |
| 212 |
\fBsd_login_monitor_get_events()\fR |
| 213 |
and |
| 214 |
\fBsd_login_monitor_get_timeout()\fR |
| 215 |
were added in version 201\&. |
| 216 |
.PP |
| 217 |
\fBsd_login_monitor_unrefp()\fR |
| 218 |
was added in version 229\&. |
| 219 |
.SH "SEE ALSO" |
| 220 |
.PP |
| 221 |
\fBsystemd\fR(1), \fBsd-login\fR(3), \fBsd_get_seats\fR(3), \fBpoll\fR(2), \fBclock_gettime\fR(2) |
| 222 |
.SH "NOTES" |
| 223 |
.IP " 1." 4 |
| 224 |
Clean-up Variable Attribute |
| 225 |
.RS 4 |
| 226 |
\%https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html |
| 227 |
.RE |