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
|
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <inttypes.h>
#include <time.h>
#include <fcntl.h>
#include <ctype.h>
#include <errno.h>
#include <termios.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/sysmacros.h>
#include "config.h"
#ifdef HAVE_SYSTEMD
# include <systemd/sd-bus.h>
#endif
#include "logind.h"
#ifdef HAVE_SYSTEMD
/* ---------------------------------------------------------------------- */
static int logind_debug = 0;
static sd_bus *logind_dbus = NULL;
/* ---------------------------------------------------------------------- */
static int logind_take_control(void)
{
sd_bus_error error = SD_BUS_ERROR_NULL;
sd_bus_message *m = NULL;
int r;
if (!logind_dbus)
return -1;
if (logind_debug)
fprintf(stderr, "call : TakeControl()\n");
r = sd_bus_call_method(logind_dbus,
"org.freedesktop.login1",
"/org/freedesktop/login1/session/self",
"org.freedesktop.login1.Session",
"TakeControl",
&error,
&m,
"b",
false);
if (r < 0) {
fprintf(stderr, "error : TakeControl failed: %s\n",
error.message);
sd_bus_error_free(&error);
}
sd_bus_message_unref(m);
return r;
}
static int logind_release_control(void)
{
sd_bus_error error = SD_BUS_ERROR_NULL;
sd_bus_message *m = NULL;
int r;
if (!logind_dbus)
return -1;
if (logind_debug)
fprintf(stderr, "call : ReleaseControl()\n");
r = sd_bus_call_method(logind_dbus,
"org.freedesktop.login1",
"/org/freedesktop/login1/session/self",
"org.freedesktop.login1.Session",
"ReleaseControl",
&error,
&m,
"");
if (r < 0) {
fprintf(stderr, "error : ReleaseControl failed: %s\n",
error.message);
sd_bus_error_free(&error);
}
sd_bus_message_unref(m);
return r;
}
/* ---------------------------------------------------------------------- */
void logind_init(void)
{
const char *session_id, *seat;
int r;
if (logind_dbus)
return;
seat = getenv("XDG_SEAT");
session_id = getenv("XDG_SESSION_ID");
if (!seat || !session_id)
return;
r = sd_bus_open_system(&logind_dbus);
if (r < 0) {
fprintf(stderr, "Failed to connect to system bus: %s\n", strerror(-r));
return;
}
r = logind_take_control();
if (r < 0)
goto err;
return;
err:
sd_bus_unref(logind_dbus);
logind_dbus = NULL;
}
void logind_fini(void)
{
if (!logind_dbus)
return;
logind_release_control();
sd_bus_unref(logind_dbus);
logind_dbus = NULL;
}
int logind_open(const char *path)
{
sd_bus_error error = SD_BUS_ERROR_NULL;
sd_bus_message *m = NULL;
struct stat st;
unsigned int maj, min;
int inactive;
int handle, fd, r;
if (!logind_dbus)
return -1;
r = stat(path, &st);
if (r < 0) {
fprintf(stderr, "stat %s failed: %s\n", path, strerror(errno));
return -1;
}
maj = major(st.st_rdev);
min = minor(st.st_rdev);
if (logind_debug)
fprintf(stderr, "call : TakeDevice(%d,%d)\n", maj, min);
r = sd_bus_call_method(logind_dbus,
"org.freedesktop.login1",
"/org/freedesktop/login1/session/self",
"org.freedesktop.login1.Session",
"TakeDevice",
&error,
&m,
"uu",
maj,
min);
if (r < 0) {
fprintf(stderr, "error : TakeDevice failed: %s\n",
error.message);
sd_bus_error_free(&error);
return -1;
}
handle = -1;
inactive = -1;
r = sd_bus_message_read(m, "hb", &handle, &inactive);
if (r < 0) {
fd = -1;
fprintf(stderr, "error : Parsing TakeDevice reply failed: %s\n",
strerror(-r));
} else {
fd = fcntl(handle, F_DUPFD_CLOEXEC, 0);
fprintf(stderr, "open %s: got fd %d via logind.\n",
path, fd);
}
sd_bus_message_unref(m);
return fd;
}
#else
/* ---------------------------------------------------------------------- */
void logind_init(void)
{
}
void logind_fini(void)
{
}
int logind_open(const char *path)
{
fprintf(stderr, "%s(%s): compiled without logind support.\n",
__func__, path);
return -1;
}
#endif
|