aboutsummaryrefslogtreecommitdiffstats
path: root/logind.c
blob: a2e9add9031f3d3d5a6325b0b89473a72f49f0c2 (plain)
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
#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 "config.h"
#ifdef HAVE_SYSTEMD
# include <systemd/sd-bus.h>
#endif

#include "logind.h"
#include "kbd.h"

#ifdef HAVE_SYSTEMD

/* ---------------------------------------------------------------------- */

static sd_bus *logind_dbus = NULL;

int logind_init(void)
{
    int r;

    r = sd_bus_open_system(&logind_dbus);
    if (r < 0) {
        fprintf(stderr, "Failed to connect to system bus: %s\n", strerror(-r));
        return -1;
    }

    r = logind_take_control();
    if (r < 0) {
        sd_bus_unref(logind_dbus);
        logind_dbus = NULL;
        return -1;
    }

    fprintf(stderr, "Opening input devices via logind.\n");
    return 0;
}

int logind_dbus_fd(void)
{
    if (!logind_dbus)
        return -1;
    return sd_bus_get_fd(logind_dbus);
}

void logind_dbus_input(void)
{
    sd_bus_message *m = NULL;
    int ret;

    if (!logind_dbus)
        return;

    do {
        ret = sd_bus_process(logind_dbus, &m);
        fprintf(stderr, "%s: path %s\n", __func__,
                sd_bus_message_get_path(m));
        sd_bus_message_unref(m);
    } while (ret > 0);
}

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;

    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, "TakeControl failed: %s\n", error.message);
        sd_bus_error_free(&error);
    }
    sd_bus_message_unref(m);

    return r;
}

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;

    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, "ReleaseControl failed: %s\n", error.message);
        sd_bus_error_free(&error);
    }
    sd_bus_message_unref(m);

    return r;
}

int logind_open(const char *path, int flags, void *user_data)
{
    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;

    r = stat(path, &st);
    if (r < 0) {
        fprintf(stderr, "stat %s failed: %s\n", path, strerror(errno));
        libinput_deverror++;
        return -1;
    }

    maj = major(st.st_rdev);
    min = minor(st.st_rdev);
    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, "TakeDevice failed: %s\n", error.message);
        sd_bus_error_free(&error);
        libinput_deverror++;
        return -1;
    }

    handle = -1;
    inactive = -1;
    r = sd_bus_message_read(m, "hb", &handle, &inactive);
    if (r < 0) {
        fd = -1;
        fprintf(stderr, "Parsing TakeDevice reply failed: %s\n", strerror(-r));
        libinput_deverror++;
    } else {
        fd = fcntl(handle, F_DUPFD_CLOEXEC, 0);
        fprintf(stderr, "open %s: got fd %d via logind.\n",
                path, fd);
        libinput_devcount++;
    }
    sd_bus_message_unref(m);

    return fd;
}

void logind_close(int fd, void *user_data)
{
    sd_bus_error error = SD_BUS_ERROR_NULL;
    sd_bus_message *m = NULL;
    struct stat st;
    unsigned int maj, min;
    int r;

    r = fstat(fd, &st);
    if (r < 0) {
        fprintf(stderr, "fstat failed: %s\n", strerror(errno));
        return;
    }
    close(fd);

    maj = major(st.st_rdev);
    min = minor(st.st_rdev);
    r = sd_bus_call_method(logind_dbus,
                           "org.freedesktop.login1",
                           "/org/freedesktop/login1/session/self",
                           "org.freedesktop.login1.Session",
                           "ReleaseDevice",
                           &error,
                           &m,
                           "uu",
                           maj,
                           min);
    if (r < 0) {
        fprintf(stderr, "ReleaseDevice failed: %s\n", error.message);
        sd_bus_error_free(&error);
    }
    sd_bus_message_unref(m);
    libinput_devcount--;
    return;
}

#else

/* ---------------------------------------------------------------------- */

int logind_init(void)
{
    fprintf(stderr, "warning: compiled without logind support.\n");
    return -1;
}

int logind_dbus_fd(void)
{
    return -1;
}

void logind_dbus_input(void)
{
}

int logind_take_control(void)
{
    return -1;
}

int logind_open(const char *path, int flags, void *user_data)
{
    fprintf(stderr, "error: compiled without logind support.\n");
    libinput_deverror++;
    return -1;
}

void logind_close(int fd, void *user_data)
{
}

#endif

/* ---------------------------------------------------------------------- */

const struct libinput_interface libinput_if_logind = {
    .open_restricted  = logind_open,
    .close_restricted = logind_close,
};