-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
407 lines (337 loc) · 10.2 KB
/
main.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
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
/* Copyright (C) 2019-2021 Torge Matthies */
/*
* This file is part of osu-handler-wine.
*
* osu-handler-wine is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* osu-handler-wine is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/*
* Author contact info:
* E-Mail address: openglfreak@googlemail.com
* GPG key fingerprint: 0535 3830 2F11 C888 9032 FAD2 7C95 CD70 C9E8 438D
*/
#define _POSIX_C_SOURCE 200809L /* openat, readlinkat */
#define _DEFAULT_SOURCE /* openat, readlinkat */
#include "attrs.h" /* attr_const */
#include "bool.h" /* bool */
#include "inline.h" /* inline */
#include "procdir.h" /* close_procdir, open_procdir, procdir_dirfd,
procdir_handle, procdir_next_process */
#include "notifications.h" /* show_notification */
#include "static_string.h" /* static_strlen, static_endswith, static_startswith */
#include <assert.h> /* assert */
#include <ctype.h> /* toupper */
#include <dirent.h> /* struct dent */
#include <errno.h> /* ENOTSUP, errno */
#include <fcntl.h> /* O_DIRECTORY, O_SEARCH, O_RDONLY, openat */
#include <stddef.h> /* size_t, ssize_t */
#include <stdlib.h> /* free, malloc, realloc */
#include <string.h> /* memcmp, memchr, strerror, strdup */
#include <sys/stat.h> /* fstat, struct stat */
#include <sys/types.h> /* uid_t */
#include <unistd.h> /* close, execve, execvp, getuid, read, readlinkat */
uid_t our_uid;
static inline bool test_uid(int const dirfd)
{
struct stat buf;
return fstat(dirfd, &buf) != -1 && buf.st_uid == our_uid;
}
static inline bool test_comm(int const dirfd)
{
static char wanted_comm[] = "osu!.exe\n";
int fd;
char buf[static_strlen(wanted_comm)];
bool b;
fd = openat(dirfd, "comm", O_RDONLY);
if (fd == -1)
return false;
b = read(fd, buf, sizeof(buf)) == sizeof(buf);
close(fd);
return b && memcmp(buf, wanted_comm, static_strlen(wanted_comm)) == 0;
}
static inline char* get_exe_path(int const dirfd, size_t* const path_len)
{
size_t bufsize;
char* buffer;
for (bufsize = 128;;bufsize *= 2)
{
ssize_t link_len;
buffer = (char*)malloc(sizeof(char) * bufsize);
if (!buffer)
break;
link_len = readlinkat(dirfd, "exe", buffer, bufsize);
if (link_len == -1)
{
free(buffer);
buffer = 0;
break;
}
if ((size_t)link_len < bufsize)
{
buffer[(size_t)link_len] = '\0';
*path_len = (size_t)link_len;
break;
}
free(buffer);
}
return buffer;
}
static inline bool test_exe(int const dirfd, char** const out_exe_path)
{
char* exe_path;
size_t path_len;
char* exe_path2;
exe_path = get_exe_path(dirfd, &path_len);
if (!exe_path)
return false;
if (!static_endswith((size_t)path_len, exe_path, "/wine-preloader") &&
!static_endswith((size_t)path_len, exe_path, "/wine64-preloader"))
{
free(exe_path);
return false;
}
exe_path2 = (char*)realloc(exe_path, sizeof(char) * (path_len + 1));
*out_exe_path = exe_path2 ? exe_path2 : exe_path;
return true;
}
static inline bool test_dir(int const dirfd, char** const out_exe_path)
{
if (!test_uid(dirfd))
return false;
if (!test_comm(dirfd))
return false;
if (!test_exe(dirfd, out_exe_path))
return false;
return true;
}
static inline bool try_realloc(void** const ptr, size_t const new_size)
{
void* const new_ptr = realloc(*ptr, new_size);
if (new_ptr) *ptr = new_ptr;
return !!new_ptr;
}
#define malloc_overhead 32
static inline bool read_environ_content(int const fd, char** const out_environ,
size_t* const out_environ_size)
{
size_t bufsize;
char* buffer;
size_t pos;
ssize_t n;
bufsize = 8192 - malloc_overhead;
buffer = (char*)malloc(sizeof(char) * bufsize);
if (!buffer)
return false;
pos = 0;
while ((n = read(fd, &buffer[pos], bufsize - pos)) > 0)
{
pos += (size_t)n;
if (pos < bufsize)
continue;
bufsize = (bufsize + malloc_overhead) * 2 - malloc_overhead;
if (!try_realloc((void**)&buffer, sizeof(char) * bufsize))
{
free(buffer);
return false;
}
}
if (n < 0)
{
free(buffer);
return false;
}
try_realloc((void**)&buffer, sizeof(char) * pos);
*out_environ = buffer;
*out_environ_size = pos;
return true;
}
static inline bool read_environ(int const dirfd, char** const out_environ,
size_t* const out_environ_size)
{
int fd;
bool ret;
fd = openat(dirfd, "environ", O_RDONLY);
if (fd == -1)
return false;
ret = read_environ_content(fd, out_environ, out_environ_size);
close(fd);
return ret;
}
static inline size_t count_envvars(char const* environ,
char const* const environ_end)
{
size_t count = 0;
while ((environ = (char const*)memchr(environ, '\0', environ_end - environ)))
{
++environ;
++count;
}
return count;
}
static struct filtered_envvar {
char const* envar;
size_t length;
} const filtered_envvars[] = {
#define FENVAR(x) { (x), static_strlen((x)) }
FENVAR("WINELOADERNOEXEC="),
FENVAR("WINEPRELOADRESERVE="),
FENVAR("WINESERVERSOCKET=")
#undef FENVAR
};
static inline bool test_envar(char const* const envar_start,
char const* const envar_end)
{
size_t i = 0;
for (; i < sizeof(filtered_envvars) / sizeof(filtered_envvars[0]); ++i)
{
char const* const envar = filtered_envvars[i].envar;
size_t const length = filtered_envvars[i].length;
if (envar_end - envar_start - 1 < length)
continue;
if (memcmp(envar_start, envar, length) == 0)
return false;
}
return true;
}
static inline void fill_envp_from_environ(char** const envp,
char*** const out_envp_end, char* environ, char* const environ_end)
{
char** envp_end = envp;
char* envar_start = environ;
char* envar_end = environ;
while ((envar_end = (char*)memchr(envar_end, '\0', environ_end - envar_end)))
{
++envar_end;
envar_start = environ;
environ = envar_end;
if (test_envar(envar_start, envar_end))
*envp_end++ = envar_start;
}
*envp_end++ = 0;
*out_envp_end = envp_end;
}
static inline bool construct_envp_from_environ(char* const environ,
size_t const environ_size, char*** const out_envp)
{
char* const environ_end = &environ[environ_size];
size_t envvar_count;
char** envp;
char** envp_end;
envvar_count = count_envvars(environ, environ_end);
envp = (char**)malloc(sizeof(char*) * envvar_count);
if (!envp)
return false;
fill_envp_from_environ(envp, &envp_end, environ, environ_end);
try_realloc((void**)&envp, sizeof(envp[0]) * (envp_end - envp));
*out_envp = envp;
return true;
}
/* Because POSIX says basename(3) may write to the input string... */
static inline attr_const char const* basename_n(char const* const path,
size_t const length)
{
char const* ptr = path + length;
while (ptr > path && *--ptr != '/');
return ptr;
}
static inline int handle_dir(int const proc_dirfd,
struct dirent const* const dent, char* argv[], bool* const out_error)
{
int dirfd;
char* exe_path;
char* environ;
bool b;
size_t environ_size;
char** envp;
size_t exe_path_length;
#ifdef O_SEARCH
dirfd = openat(proc_dirfd, dent->d_name, O_SEARCH | O_DIRECTORY);
#else
dirfd = openat(proc_dirfd, dent->d_name, O_RDONLY | O_DIRECTORY);
#endif
if (dirfd == -1)
return (errno == ESRCH || errno == ENOENT) ? 0 : errno;
if (!test_dir(dirfd, &exe_path))
{
close(dirfd);
return 0;
}
b = read_environ(dirfd, &environ, &environ_size);
close(dirfd);
b = b && construct_envp_from_environ(environ, environ_size, &envp);
if (!b)
{
*out_error = true;
return 0;
}
exe_path_length = strlen(exe_path);
exe_path[exe_path_length -= static_strlen("-preloader")] = '\0';
argv[0] = (char*)basename_n(exe_path, exe_path_length);
execve(exe_path, argv, envp);
return errno;
}
static inline int run_launcher(char* argv[])
{
argv[0] = (char*)"osu";
execvp("osu", argv);
return errno;
}
static int handle_error(int error)
{
char const* error_message;
char* duplicated_message = 0;
errno = 0;
error_message = strerror(error);
if (errno != 0 || !error_message || !error_message[0])
error_message = "Unknown error";
else if ((duplicated_message = strdup(error_message)))
{
duplicated_message[0] = toupper(duplicated_message[0]);
error_message = duplicated_message;
}
show_notification(error_message);
free(duplicated_message);
error &= 0xFF;
return error ? error : 1;
}
int main(int argc, char* argv[])
{
int error;
procdir_handle pdhandle;
int dirfd;
struct dirent* dent;
bool exit_loop;
(void)argc;
our_uid = getuid();
if ((error = open_procdir(&pdhandle)) != 0)
return handle_error(error);
if ((dirfd = procdir_dirfd(pdhandle)) == -1)
return handle_error(ENOTSUP);
exit_loop = false;
do {
if ((error = procdir_next_process(pdhandle, &dent)) != 0 || !dent)
break;
if ((error = handle_dir(dirfd, dent, argv, &exit_loop)) != 0)
break;
} while (!exit_loop);
close_procdir(pdhandle);
if (error != 0)
return handle_error(error);
if (!exit_loop)
error = run_launcher(argv);
if (error != 0 && error != ENOENT)
return handle_error(error);
if (!exit_loop)
show_notification("Could not find a running osu! instance");
return 0;
}