1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
|
/*
* Copyright © 2013 Red Hat Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
* Author: Hans de Goede <hdegoede@redhat.com>
*/
#ifdef HAVE_XORG_CONFIG_H
#include <xorg-config.h>
#endif
#include <dbus/dbus.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include "os.h"
#include "dbus-core.h"
#include "xf86.h"
#include "xf86platformBus.h"
#include "xf86Xinput.h"
#include "systemd-logind.h"
#define DBUS_TIMEOUT 500 /* Wait max 0.5 seconds */
struct systemd_logind_info {
DBusConnection *conn;
char *session;
Bool active;
Bool vt_active;
};
static struct systemd_logind_info logind_info;
static InputInfoPtr
systemd_logind_find_info_ptr_by_devnum(InputInfoPtr start,
int major, int minor)
{
InputInfoPtr pInfo;
for (pInfo = start; pInfo; pInfo = pInfo->next)
if (pInfo->major == major && pInfo->minor == minor &&
(pInfo->flags & XI86_SERVER_FD))
return pInfo;
return NULL;
}
static void
systemd_logind_set_input_fd_for_all_devs(int major, int minor, int fd,
Bool enable)
{
InputInfoPtr pInfo;
pInfo = systemd_logind_find_info_ptr_by_devnum(xf86InputDevs, major, minor);
while (pInfo) {
pInfo->fd = fd;
pInfo->options = xf86ReplaceIntOption(pInfo->options, "fd", fd);
if (enable)
xf86EnableInputDeviceForVTSwitch(pInfo);
pInfo = systemd_logind_find_info_ptr_by_devnum(pInfo->next, major, minor);
}
}
int
systemd_logind_take_fd(int _major, int _minor, const char *path,
Bool *paused_ret)
{
struct systemd_logind_info *info = &logind_info;
InputInfoPtr pInfo;
DBusError error;
DBusMessage *msg = NULL;
DBusMessage *reply = NULL;
dbus_int32_t major = _major;
dbus_int32_t minor = _minor;
dbus_bool_t paused;
int fd = -1;
if (!info->session || major == 0)
return -1;
/* logind does not support mouse devs (with evdev we don't need them) */
if (strstr(path, "mouse"))
return -1;
/* Check if we already have an InputInfo entry with this major, minor
* (shared device-nodes happen ie with Wacom tablets). */
pInfo = systemd_logind_find_info_ptr_by_devnum(xf86InputDevs, major, minor);
if (pInfo) {
LogMessage(X_INFO, "systemd-logind: returning pre-existing fd for %s %u:%u\n",
path, major, minor);
*paused_ret = FALSE;
return pInfo->fd;
}
dbus_error_init(&error);
msg = dbus_message_new_method_call("org.freedesktop.login1", info->session,
"org.freedesktop.login1.Session", "TakeDevice");
if (!msg) {
LogMessage(X_ERROR, "systemd-logind: out of memory\n");
goto cleanup;
}
if (!dbus_message_append_args(msg, DBUS_TYPE_UINT32, &major,
DBUS_TYPE_UINT32, &minor,
DBUS_TYPE_INVALID)) {
LogMessage(X_ERROR, "systemd-logind: out of memory\n");
goto cleanup;
}
reply = dbus_connection_send_with_reply_and_block(info->conn, msg,
DBUS_TIMEOUT, &error);
if (!reply) {
LogMessage(X_ERROR, "systemd-logind: failed to take device %s: %s\n",
path, error.message);
goto cleanup;
}
if (!dbus_message_get_args(reply, &error,
DBUS_TYPE_UNIX_FD, &fd,
DBUS_TYPE_BOOLEAN, &paused,
DBUS_TYPE_INVALID)) {
LogMessage(X_ERROR, "systemd-logind: TakeDevice %s: %s\n",
path, error.message);
goto cleanup;
}
*paused_ret = paused;
LogMessage(X_INFO, "systemd-logind: got fd for %s %u:%u fd %d paused %d\n",
path, major, minor, fd, paused);
cleanup:
if (msg)
dbus_message_unref(msg);
if (reply)
dbus_message_unref(reply);
dbus_error_free(&error);
return fd;
}
void
systemd_logind_release_fd(int _major, int _minor)
{
struct systemd_logind_info *info = &logind_info;
InputInfoPtr pInfo;
DBusError error;
DBusMessage *msg = NULL;
DBusMessage *reply = NULL;
dbus_int32_t major = _major;
dbus_int32_t minor = _minor;
int matches = 0;
if (!info->session || major == 0)
return;
/* Only release the fd if there is only 1 InputInfo left for this major
* and minor, otherwise other InputInfo's are still referencing the fd. */
pInfo = systemd_logind_find_info_ptr_by_devnum(xf86InputDevs, major, minor);
while (pInfo) {
matches++;
pInfo = systemd_logind_find_info_ptr_by_devnum(pInfo->next, major, minor);
}
if (matches > 1) {
LogMessage(X_INFO, "systemd-logind: not releasing fd for %u:%u, still in use\n", major, minor);
return;
}
LogMessage(X_INFO, "systemd-logind: releasing fd for %u:%u\n", major, minor);
dbus_error_init(&error);
msg = dbus_message_new_method_call("org.freedesktop.login1", info->session,
"org.freedesktop.login1.Session", "ReleaseDevice");
if (!msg) {
LogMessage(X_ERROR, "systemd-logind: out of memory\n");
goto cleanup;
}
if (!dbus_message_append_args(msg, DBUS_TYPE_UINT32, &major,
DBUS_TYPE_UINT32, &minor,
DBUS_TYPE_INVALID)) {
LogMessage(X_ERROR, "systemd-logind: out of memory\n");
goto cleanup;
}
reply = dbus_connection_send_with_reply_and_block(info->conn, msg,
DBUS_TIMEOUT, &error);
if (!reply)
LogMessage(X_ERROR, "systemd-logind: failed to release device: %s\n",
error.message);
cleanup:
if (msg)
dbus_message_unref(msg);
if (reply)
dbus_message_unref(reply);
dbus_error_free(&error);
}
int
systemd_logind_controls_session(void)
{
return logind_info.session ? 1 : 0;
}
void
systemd_logind_vtenter(void)
{
struct systemd_logind_info *info = &logind_info;
InputInfoPtr pInfo;
int i;
if (!info->session)
return; /* Not using systemd-logind */
if (!info->active)
return; /* Session not active */
if (info->vt_active)
return; /* Already did vtenter */
for (i = 0; i < xf86_num_platform_devices; i++) {
if (xf86_platform_devices[i].flags & XF86_PDEV_PAUSED)
break;
}
if (i != xf86_num_platform_devices)
return; /* Some drm nodes are still paused wait for resume */
xf86VTEnter();
info->vt_active = TRUE;
/* Activate any input devices which were resumed before the drm nodes */
for (pInfo = xf86InputDevs; pInfo; pInfo = pInfo->next)
if ((pInfo->flags & XI86_SERVER_FD) && pInfo->fd != -1)
xf86EnableInputDeviceForVTSwitch(pInfo);
/* Do delayed input probing, this must be done after the above enabling */
xf86InputEnableVTProbe();
}
static void
systemd_logind_ack_pause(struct systemd_logind_info *info,
dbus_int32_t minor, dbus_int32_t major)
{
DBusError error;
DBusMessage *msg = NULL;
DBusMessage *reply = NULL;
dbus_error_init(&error);
msg = dbus_message_new_method_call("org.freedesktop.login1", info->session,
"org.freedesktop.login1.Session", "PauseDeviceComplete");
if (!msg) {
LogMessage(X_ERROR, "systemd-logind: out of memory\n");
goto cleanup;
}
if (!dbus_message_append_args(msg, DBUS_TYPE_UINT32, &major,
DBUS_TYPE_UINT32, &minor,
DBUS_TYPE_INVALID)) {
LogMessage(X_ERROR, "systemd-logind: out of memory\n");
goto cleanup;
}
reply = dbus_connection_send_with_reply_and_block(info->conn, msg,
DBUS_TIMEOUT, &error);
if (!reply)
LogMessage(X_ERROR, "systemd-logind: failed to ack pause: %s\n",
error.message);
cleanup:
if (msg)
dbus_message_unref(msg);
if (reply)
dbus_message_unref(reply);
dbus_error_free(&error);
}
static DBusHandlerResult
message_filter(DBusConnection * connection, DBusMessage * message, void *data)
{
struct systemd_logind_info *info = data;
struct xf86_platform_device *pdev = NULL;
InputInfoPtr pInfo = NULL;
int ack = 0, pause = 0, fd = -1;
DBusError error;
dbus_int32_t major, minor;
char *pause_str;
dbus_error_init(&error);
if (dbus_message_is_signal(message,
"org.freedesktop.DBus", "NameOwnerChanged")) {
char *name, *old_owner, *new_owner;
dbus_message_get_args(message, &error,
DBUS_TYPE_STRING, &name,
DBUS_TYPE_STRING, &old_owner,
DBUS_TYPE_STRING, &new_owner, DBUS_TYPE_INVALID);
if (dbus_error_is_set(&error)) {
LogMessage(X_ERROR, "systemd-logind: NameOwnerChanged: %s\n",
error.message);
dbus_error_free(&error);
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
if (name && strcmp(name, "org.freedesktop.login1") == 0)
FatalError("systemd-logind disappeared (stopped/restarted?)\n");
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
if (strcmp(dbus_message_get_path(message), info->session) != 0)
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
if (dbus_message_is_signal(message, "org.freedesktop.login1.Session",
"PauseDevice")) {
if (!dbus_message_get_args(message, &error,
DBUS_TYPE_UINT32, &major,
DBUS_TYPE_UINT32, &minor,
DBUS_TYPE_STRING, &pause_str,
DBUS_TYPE_INVALID)) {
LogMessage(X_ERROR, "systemd-logind: PauseDevice: %s\n",
error.message);
dbus_error_free(&error);
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
if (strcmp(pause_str, "pause") == 0) {
pause = 1;
ack = 1;
}
else if (strcmp(pause_str, "force") == 0) {
pause = 1;
}
else if (strcmp(pause_str, "gone") == 0) {
/* Device removal is handled through udev */
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
else {
LogMessage(X_WARNING, "systemd-logind: unknown pause type: %s\n",
pause_str);
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
}
else if (dbus_message_is_signal(message, "org.freedesktop.login1.Session",
"ResumeDevice")) {
if (!dbus_message_get_args(message, &error,
DBUS_TYPE_UINT32, &major,
DBUS_TYPE_UINT32, &minor,
DBUS_TYPE_UNIX_FD, &fd,
DBUS_TYPE_INVALID)) {
LogMessage(X_ERROR, "systemd-logind: ResumeDevice: %s\n",
error.message);
dbus_error_free(&error);
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
} else
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
LogMessage(X_INFO, "systemd-logind: got %s for %u:%u\n",
pause ? "pause" : "resume", major, minor);
pdev = xf86_find_platform_device_by_devnum(major, minor);
if (!pdev)
pInfo = systemd_logind_find_info_ptr_by_devnum(xf86InputDevs,
major, minor);
if (!pdev && !pInfo) {
LogMessage(X_WARNING, "systemd-logind: could not find dev %u:%u\n",
major, minor);
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
if (pause) {
/* Our VT_PROCESS usage guarantees we've already given up the vt */
info->active = info->vt_active = FALSE;
/* Note the actual vtleave has already been handled by xf86Events.c */
if (pdev)
pdev->flags |= XF86_PDEV_PAUSED;
else {
close(pInfo->fd);
systemd_logind_set_input_fd_for_all_devs(major, minor, -1, FALSE);
}
if (ack)
systemd_logind_ack_pause(info, major, minor);
}
else {
/* info->vt_active gets set by systemd_logind_vtenter() */
info->active = TRUE;
if (pdev)
pdev->flags &= ~XF86_PDEV_PAUSED;
else
systemd_logind_set_input_fd_for_all_devs(major, minor, fd,
info->vt_active);
/* Always call vtenter(), in case there are only legacy video devs */
systemd_logind_vtenter();
}
return DBUS_HANDLER_RESULT_HANDLED;
}
static void
connect_hook(DBusConnection *connection, void *data)
{
struct systemd_logind_info *info = data;
DBusError error;
DBusMessage *msg = NULL;
DBusMessage *reply = NULL;
dbus_int32_t arg;
char *session = NULL;
dbus_error_init(&error);
msg = dbus_message_new_method_call("org.freedesktop.login1",
"/org/freedesktop/login1", "org.freedesktop.login1.Manager",
"GetSessionByPID");
if (!msg) {
LogMessage(X_ERROR, "systemd-logind: out of memory\n");
goto cleanup;
}
arg = getpid();
if (!dbus_message_append_args(msg, DBUS_TYPE_UINT32, &arg,
DBUS_TYPE_INVALID)) {
LogMessage(X_ERROR, "systemd-logind: out of memory\n");
goto cleanup;
}
reply = dbus_connection_send_with_reply_and_block(connection, msg,
DBUS_TIMEOUT, &error);
if (!reply) {
LogMessage(X_ERROR, "systemd-logind: failed to get session: %s\n",
error.message);
goto cleanup;
}
dbus_message_unref(msg);
if (!dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &session,
DBUS_TYPE_INVALID)) {
LogMessage(X_ERROR, "systemd-logind: GetSessionByPID: %s\n",
error.message);
goto cleanup;
}
session = XNFstrdup(session);
dbus_message_unref(reply);
reply = NULL;
msg = dbus_message_new_method_call("org.freedesktop.login1",
session, "org.freedesktop.login1.Session", "TakeControl");
if (!msg) {
LogMessage(X_ERROR, "systemd-logind: out of memory\n");
goto cleanup;
}
arg = FALSE; /* Don't forcibly take over over the session */
if (!dbus_message_append_args(msg, DBUS_TYPE_BOOLEAN, &arg,
DBUS_TYPE_INVALID)) {
LogMessage(X_ERROR, "systemd-logind: out of memory\n");
goto cleanup;
}
reply = dbus_connection_send_with_reply_and_block(connection, msg,
DBUS_TIMEOUT, &error);
if (!reply) {
LogMessage(X_ERROR, "systemd-logind: TakeControl failed: %s\n",
error.message);
goto cleanup;
}
dbus_bus_add_match(connection,
"type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged',path='/org/freedesktop/DBus'",
&error);
if (dbus_error_is_set(&error)) {
LogMessage(X_ERROR, "systemd-logind: could not add match: %s\n",
error.message);
goto cleanup;
}
/*
* HdG: This is not useful with systemd <= 208 since the signal only
* contains invalidated property names there, rather than property, val
* pairs as it should. Instead we just use the first resume / pause now.
*/
#if 0
snprintf(match, sizeof(match),
"type='signal',sender='org.freedesktop.login1',interface='org.freedesktop.DBus.Properties',member='PropertiesChanged',path='%s'",
session);
dbus_bus_add_match(connection, match, &error);
if (dbus_error_is_set(&error)) {
LogMessage(X_ERROR, "systemd-logind: could not add match: %s\n",
error.message);
goto cleanup;
}
#endif
if (!dbus_connection_add_filter(connection, message_filter, info, NULL)) {
LogMessage(X_ERROR, "systemd-logind: could not add filter: %s\n",
error.message);
goto cleanup;
}
LogMessage(X_INFO, "systemd-logind: took control of session %s\n",
session);
info->conn = connection;
info->session = session;
info->vt_active = info->active = TRUE; /* The server owns the vt during init */
session = NULL;
cleanup:
free(session);
if (msg)
dbus_message_unref(msg);
if (reply)
dbus_message_unref(reply);
dbus_error_free(&error);
}
static void
systemd_logind_release_control(struct systemd_logind_info *info)
{
DBusError error;
DBusMessage *msg = NULL;
DBusMessage *reply = NULL;
dbus_error_init(&error);
msg = dbus_message_new_method_call("org.freedesktop.login1",
info->session, "org.freedesktop.login1.Session", "ReleaseControl");
if (!msg) {
LogMessage(X_ERROR, "systemd-logind: out of memory\n");
goto cleanup;
}
reply = dbus_connection_send_with_reply_and_block(info->conn, msg,
DBUS_TIMEOUT, &error);
if (!reply) {
LogMessage(X_ERROR, "systemd-logind: ReleaseControl failed: %s\n",
error.message);
goto cleanup;
}
cleanup:
if (msg)
dbus_message_unref(msg);
if (reply)
dbus_message_unref(reply);
dbus_error_free(&error);
}
static void
disconnect_hook(void *data)
{
struct systemd_logind_info *info = data;
free(info->session);
info->session = NULL;
info->conn = NULL;
}
static struct dbus_core_hook core_hook = {
.connect = connect_hook,
.disconnect = disconnect_hook,
.data = &logind_info,
};
int
systemd_logind_init(void)
{
return dbus_core_add_hook(&core_hook);
}
void
systemd_logind_fini(void)
{
if (logind_info.session)
systemd_logind_release_control(&logind_info);
dbus_core_remove_hook(&core_hook);
}
|