-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
freon.c
342 lines (296 loc) · 9.85 KB
/
freon.c
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
/* Copyright (c) 2016 The crouton Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*
* LD_PRELOAD hack to make Xorg happy in a system without VT-switching.
* gcc -shared -fPIC -ldrm -ldl -I/usr/include/libdrm -Wall -O2 freon.c -o croutonfreon.so
*
* Powered by black magic.
*/
#define _GNU_SOURCE
#include <dlfcn.h>
#include <stdio.h>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
#include <linux/input.h>
#include <linux/vt.h>
#include <xf86drm.h>
#include <xf86drmMode.h>
#define LOCK_FILE_DIR "/tmp/crouton-lock"
#define DISPLAY_LOCK_FILE LOCK_FILE_DIR "/display"
#define LIBCROSSERVICE_METHOD_CALL(function) \
system("host-dbus dbus-send --system --dest=org.chromium.LibCrosService " \
"--type=method_call --print-reply /org/chromium/LibCrosService " \
"org.chromium.LibCrosServiceInterface." #function)
#define DISPLAYSERVICE_METHOD_CALL(function) \
system("host-dbus dbus-send --system --dest=org.chromium.DisplayService " \
"--type=method_call --print-reply /org/chromium/DisplayService " \
"org.chromium.DisplayServiceInterface." #function)
#define TRACE(...) /* fprintf(stderr, __VA_ARGS__) */
#define ERROR(...) fprintf(stderr, __VA_ARGS__)
static int tty0fd = -1;
static int tty7fd = -1;
static int lockfd = -1;
static int (*orig_ioctl)(int d, int request, void* data);
static int (*orig_open)(const char *pathname, int flags, mode_t mode);
static int (*orig_open64)(const char *pathname, int flags, mode_t mode);
static int (*orig_close)(int fd);
static void preload_init() {
orig_ioctl = dlsym(RTLD_NEXT, "ioctl");
orig_open = dlsym(RTLD_NEXT, "open");
orig_open64 = dlsym(RTLD_NEXT, "open64");
orig_close = dlsym(RTLD_NEXT, "close");
}
/* Grabs the system-wide lockfile that arbitrates which chroot is using the GPU.
*
* pid should be either the pid of the process that owns the GPU (eg. getpid()),
* or 0 to indicate that Chromium OS now owns the GPU.
*
* Returns 0 on success, or -1 on error.
*/
static int set_display_lock(unsigned int pid) {
if (lockfd == -1) {
if (pid == 0) {
ERROR("No display lock to release.\n");
return 0;
}
lockfd = orig_open(DISPLAY_LOCK_FILE, O_CREAT | O_WRONLY, 0666);
if (lockfd == -1) {
ERROR("Unable to open display lock file.\n");
return -1;
}
if (flock(lockfd, LOCK_EX) == -1) {
ERROR("Unable to lock display lock file.\n");
return -1;
}
}
if (ftruncate(lockfd, 0) == -1) {
ERROR("Unable to truncate display lock file.\n");
return -1;
}
char buf[11];
int len;
if ((len = snprintf(buf, sizeof(buf), "%u\n", pid)) < 0) {
ERROR("pid sprintf failed.\n");
return -1;
}
if (write(lockfd, buf, len) == -1) {
ERROR("Unable to write to display lock file.\n");
return -1;
}
if (pid == 0) {
int ret = orig_close(lockfd);
lockfd = -1;
if (ret == -1) {
ERROR("Failure when closing display lock file.\n");
}
return ret;
}
return 0;
}
static int32_t crtc_set_prop(int fd, uint32_t crtc_id,
drmModeObjectPropertiesPtr props,
const char *name, uint64_t value)
{
uint32_t u;
int32_t ret;
drmModePropertyPtr prop;
for (u = 0; u < props->count_props; u++) {
prop = drmModeGetProperty(fd, props->props[u]);
if (!prop)
continue;
if (strcmp(prop->name, name)) {
drmModeFreeProperty(prop);
continue;
}
ret = drmModeObjectSetProperty(fd, crtc_id, DRM_MODE_OBJECT_CRTC,
props->props[u], value);
if (ret < 0)
TRACE("setting property %s failed with %d\n", name, ret);
else
ret = 0;
drmModeFreeProperty(prop);
return ret;
}
TRACE("could not find property %s\n", name);
return -1;
}
/* Reset CTM/GAMMA properties to avoid artifacts (#3791). */
static void drm_reset_props()
{
int i, fd;
drmModeRes* resources;
if (!orig_open) preload_init();
fd = orig_open("/dev/dri/card0", O_RDWR, 0);
TRACE("%s %d\n", __func__, fd);
if (fd < 0)
return;
resources = drmModeGetResources(fd);
TRACE("%s res=%p\n", __func__, resources);
if (!resources)
goto closefd;
for (i = 0; i < resources->count_crtcs; i++) {
drmModeObjectPropertiesPtr crtc_props;
uint32_t crtc_id = resources->crtcs[i];
crtc_props = drmModeObjectGetProperties(fd, crtc_id, DRM_MODE_OBJECT_CRTC);
TRACE("%s crtc %d %p\n", __func__, i, crtc_props);
if (!crtc_props)
continue;
/* Reset color matrix to identity and gamma/degamma LUTs to pass through,
* ignore errors in case they are not supported.
* ref: https://chromium.googlesource.com/chromiumos/platform/frecon/+/master/drm.c
*/
crtc_set_prop(fd, crtc_id, crtc_props, "CTM", 0);
crtc_set_prop(fd, crtc_id, crtc_props, "DEGAMMA_LUT", 0);
crtc_set_prop(fd, crtc_id, crtc_props, "GAMMA_LUT", 0);
drmModeFreeObjectProperties(crtc_props);
}
drmModeFreeResources(resources);
closefd:
orig_close(fd);
}
/* Prevents some glitch if Chromium OS keeps cursor enabled (#2878). */
static void drm_disable_cursor()
{
int i, fd;
drmModeRes* resources;
if (!orig_open) preload_init();
fd = orig_open("/dev/dri/card0", O_RDWR, 0);
TRACE("%s %d\n", __func__, fd);
if (fd < 0)
return;
resources = drmModeGetResources(fd);
if (!resources)
goto closefd;
TRACE("%s res=%p\n", __func__, resources);
for (i = 0; i < resources->count_crtcs; i++) {
drmModeCrtc* crtc;
crtc = drmModeGetCrtc(fd, resources->crtcs[i]);
TRACE("%s crtc %d %p\n", __func__, i, crtc);
if (crtc) {
drmModeSetCursor(fd, crtc->crtc_id,
0, 0, 0);
drmModeFreeCrtc(crtc);
}
}
drmModeFreeResources(resources);
closefd:
orig_close(fd);
}
int ioctl(int fd, unsigned long int request, ...) {
if (!orig_ioctl) preload_init();
int ret = 0;
va_list argp;
va_start(argp, request);
void* data = va_arg(argp, void*);
if (fd == tty0fd) {
TRACE("ioctl tty0 %d %lx %p\n", fd, request, data);
if (request == VT_OPENQRY) {
TRACE("OPEN\n");
*(int*)data = 7;
}
ret = 0;
} else if (fd == tty7fd) {
TRACE("ioctl tty7 %d %lx %p\n", fd, request, data);
if (request == VT_GETSTATE) {
TRACE("STATE\n");
struct vt_stat* stat = data;
stat->v_active = 0;
}
if ((request == VT_RELDISP && (uintptr_t)data == 1) ||
(request == VT_ACTIVATE && (uintptr_t)data == 0)) {
if (lockfd != -1) {
drm_reset_props();
TRACE("Telling Chromium OS to regain control\n");
ret = LIBCROSSERVICE_METHOD_CALL(TakeDisplayOwnership);
if (WEXITSTATUS(ret) == 1) {
ret = DISPLAYSERVICE_METHOD_CALL(TakeOwnership);
}
if (set_display_lock(0) < 0) {
ERROR("Failed to release display lock\n");
}
}
} else if ((request == VT_RELDISP && (uintptr_t)data == 2) ||
(request == VT_ACTIVATE && (uintptr_t)data == 7)) {
if (set_display_lock(getpid()) == 0) {
TRACE("Telling Chromium OS to drop control\n");
ret = LIBCROSSERVICE_METHOD_CALL(ReleaseDisplayOwnership);
if (WEXITSTATUS(ret) == 1) {
ret = DISPLAYSERVICE_METHOD_CALL(ReleaseOwnership);
}
} else {
ERROR("Unable to claim display lock\n");
ret = -1;
}
drm_disable_cursor();
} else {
ret = 0;
}
} else {
if (request == EVIOCGRAB) {
TRACE("ioctl GRAB %d %lx %p\n", fd, request, data);
/* Driver requested a grab: assume we have it already and report
* success */
ret = 0;
} else {
ret = orig_ioctl(fd, request, data);
}
}
va_end(argp);
return ret;
}
static int _open(int (*origfunc)(const char *pathname, int flags, mode_t mode),
const char *origname, const char *pathname, int flags, mode_t mode) {
TRACE("%s %s\n", origname, pathname);
if (!strcmp(pathname, "/dev/tty0")) {
tty0fd = origfunc("/dev/null", flags, mode);
return tty0fd;
} else if (!strcmp(pathname, "/dev/tty7")) {
tty7fd = origfunc("/dev/null", flags, mode);
return tty7fd;
} else {
const char* event = "/dev/input/event";
int fd = origfunc(pathname, flags, mode);
TRACE("%s %s %d\n", origname, pathname, fd);
if (!strncmp(pathname, event, strlen(event))) {
TRACE("GRAB\n");
orig_ioctl(fd, EVIOCGRAB, (void *) 1);
}
return fd;
}
}
int open(const char *pathname, int flags, ...) {
if (!orig_open) preload_init();
va_list argp;
va_start(argp, flags);
mode_t mode = va_arg(argp, mode_t);
va_end(argp);
return _open(orig_open, "open", pathname, flags, mode);
}
int open64(const char *pathname, int flags, ...) {
if (!orig_open64) preload_init();
va_list argp;
va_start(argp, flags);
mode_t mode = va_arg(argp, mode_t);
va_end(argp);
return _open(orig_open64, "open64", pathname, flags, mode);
}
int close(int fd) {
if (!orig_close) preload_init();
TRACE("close %d\n", fd);
if (fd == tty0fd) {
tty0fd = -1;
} else if (fd == tty7fd) {
tty7fd = -1;
}
return orig_close(fd);
}
uid_t getuid0(void) {
TRACE("getuid0\n");
return 0;
}