forked from pcercuei/libopk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopkrun.c
502 lines (409 loc) · 10.5 KB
/
opkrun.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
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
#define _BSD_SOURCE 1
#define _XOPEN_SOURCE 1
#include "opk.h"
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <libgen.h>
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <linux/loop.h>
#ifndef MY_NAME
#define MY_NAME "opkrun"
#endif
#ifndef VTCON_FILE
#define VTCON_FILE "/sys/devices/virtual/vtconsole/vtcon1/bind"
#endif
#ifndef LINKDEV_ALT_MAP_FILE
#define LINKDEV_ALT_MAP_FILE "/sys/devices/platform/linkdev/alt_key_map"
#endif
#ifndef JZ4770FB_ENABLE_DOWNSCALING_FILE
#define JZ4770FB_ENABLE_DOWNSCALING_FILE "/sys/devices/platform/jz-lcd.0/allow_downscaling"
#endif
#ifndef OPK_MOUNTPOINT
#define OPK_MOUNTPOINT "/opk"
#endif
#define NB_PARAMS_MAX 256
struct params {
char *exec[NB_PARAMS_MAX];
char *keymap;
bool needs_terminal, needs_joystick, needs_gsensor, needs_downscaling;
};
static const struct option options[] = {
{"help", no_argument, 0, 'h'},
{"metadata", required_argument, 0, 'm'},
{0, 0, 0, 0},
};
static const char *options_descriptions[] = {
"Show this help and quit.",
"Metadata file to use (default: first one found)",
};
static void usage(void)
{
printf("Usage:\n\t" MY_NAME " [OPTIONS] OPK_FILE [ARGS ...]\n\nOptions:\n");
for (size_t i = 0; options[i].name; i++)
printf("\t-%c, --%s\n\t\t\t%s\n",
options[i].val, options[i].name,
options_descriptions[i]);
}
static struct OPK * open_opk(const char *filename, const char *metadata)
{
struct OPK *opk = opk_open(filename);
if (!opk) {
fprintf(stderr, "Unable to open OPK\n");
return NULL;
}
for (;;) {
const char *meta_file;
int ret = opk_open_metadata(opk, &meta_file);
if (ret < 0) {
fprintf(stderr, "Unable to open metadata file within OPK\n");
goto close_opk;
}
if (!ret) {
fprintf(stderr, "Metadata file not found in OPK\n");
goto close_opk;
}
if (!metadata || !strcmp(metadata, meta_file))
break;
}
return opk;
close_opk:
opk_close(opk);
return NULL;
}
static int read_params(struct OPK *opk, struct params *params)
{
memset(params, 0, sizeof(*params));
const char *exec_name = NULL;
size_t exec_name_len = 0;
params->keymap = NULL;
for (;;) {
const char *key, *val;
size_t skey, sval;
int ret = opk_read_pair(opk, &key, &skey, &val, &sval);
if (ret < 0) {
fprintf(stderr, "Unable to read key/value pair from metadata\n");
return ret;
}
if (!ret)
break;
if (!strncmp(key, "Exec", skey)) {
exec_name_len = sval;
exec_name = val;
continue;
}
if (!strncmp(key, "Terminal", skey)) {
params->needs_terminal = !strncmp(val, "true", sval);
continue;
}
if (!strncmp(key, "X-OD-NeedsJoystick", skey)) {
params->needs_joystick = !strncmp(val, "true", sval);
continue;
}
if (!strncmp(key, "X-OD-NeedsGSensor", skey)) {
params->needs_gsensor = !strncmp(val, "true", sval);
continue;
}
if (!strncmp(key, "X-OD-NeedsDownscaling", skey)) {
params->needs_downscaling = !strncmp(val, "true", sval);
continue;
}
if (!strncmp(key, "FK-Keymap", skey)) {
params->keymap = strndup(val, sval);
continue;
}
}
if (!exec_name) {
fprintf(stderr, "Unable to find the executable name\n");
return -1;
}
char *exec = malloc(exec_name_len + 1);
memcpy(exec, exec_name, exec_name_len);
exec[exec_name_len] = '\0';
/* Split the Exec command into an array of parameters */
char *ptr;
unsigned int arg;
for (ptr = exec, arg = 0; ptr && arg < NB_PARAMS_MAX - 1; arg++) {
params->exec[arg] = ptr;
ptr = strchr(ptr, ' ');
if (ptr) {
*ptr++ = '\0';
while (*ptr == ' ') /* Skip eventual additional spaces */
ptr++;
}
}
params->exec[arg] = NULL;
return 0;
}
static void enable_vtcon(void)
{
FILE *f = fopen(VTCON_FILE, "w");
if (!f) {
perror("Unable to open vtcon file");
return;
}
char one = '1';
fwrite(&one, 1, 1, f);
fclose(f);
}
static void enable_in_sysfs(const char *fn)
{
FILE *f = fopen(fn, "w");
if (!f)
return;
char yes = 'Y';
fwrite(&yes, 1, 1, f);
fclose(f);
}
static void enable_alt_key_map(void)
{
enable_in_sysfs(LINKDEV_ALT_MAP_FILE);
}
static void enable_downscaling(void)
{
enable_in_sysfs(JZ4770FB_ENABLE_DOWNSCALING_FILE);
}
static void enable_gsensor(void)
{
system("/usr/sbin/gsensor --start");
/* Fix for SDL apps to recognize the g-sensor */
putenv("SDL_JOYSTICK_DEVICE=/dev/input/gsensor");
}
static char *get_url(const char *file)
{
char *url = realpath(file, NULL);
if (!url)
return strdup(file);
char *tmp = malloc(strlen(url) + sizeof "file://");
sprintf(tmp, "file://%s", url);
free(url);
return tmp;
}
static int logetfree(void)
{
int fd = open("/dev/loop-control", O_RDWR);
if (fd < 0) {
fprintf(stderr, "Failed to open '/dev/loop-control': %d\n", fd);
return -1;
}
int devnr = ioctl(fd, LOOP_CTL_GET_FREE, NULL);
if (devnr < 0) {
fprintf(stderr, "Failed to acquire free loop device: %d\n", devnr);
}
close(fd);
return devnr;
}
static int losetup(const char *loop, const char *file)
{
unsigned int i;
int filefd, loopfd, ret;
filefd = open(file, O_RDONLY);
if (filefd < 0) {
fprintf(stderr, "losetup: cannot open '%s': %d\n", file, filefd);
return -1;
}
/* We try to open the loop device just a bit after it was created.
* Give some time to udev so that it can set the proper rights. */
for (i = 0; i < 100; i++) {
loopfd = open(loop, O_RDONLY);
if (loopfd < 0 && errno == EACCES)
usleep(10000);
else
break;
}
if (loopfd < 0) {
fprintf(stderr, "losetup: cannot open '%s': %d\n", loop, loopfd);
close(filefd);
return loopfd;
}
ret = ioctl(loopfd, LOOP_SET_FD, (void *)(intptr_t)filefd);
if (ret < 0) {
fprintf(stderr, "Cannot setup loop device '%s': %d\n", loop, ret);
close(loopfd);
close(filefd);
return ret;
}
close(filefd);
return loopfd;
}
int main(int argc, char **argv)
{
if (argc < 2) {
fprintf(stderr, "Error: Too few arguments given.\n\n");
usage();
return EXIT_SUCCESS;
}
int c, option_index = 0, arg_index = 1;
const char *metadata = NULL;
while ((c = getopt_long(argc, argv, "+hm:",
options, &option_index)) != -1) {
switch (c) {
case 'h':
usage();
return EXIT_SUCCESS;
case 'm':
metadata = optarg;
arg_index += 2;
break;
case '?':
return EXIT_FAILURE;
}
}
if (arg_index >= argc) {
fprintf(stderr, "Incorrect number of arguments.\n\n");
usage();
return EXIT_FAILURE;
}
const char *opk_name = argv[arg_index];
struct OPK *opk = open_opk(opk_name, metadata);
if (!opk)
return EXIT_FAILURE;
struct params params;
int ret = read_params(opk, ¶ms);
opk_close(opk);
if (ret < 0)
return EXIT_FAILURE;
char **opk_argv = argv + arg_index + 1;
int opk_argc = argc - arg_index - 1;
if (opk_argc > NB_PARAMS_MAX - 2)
opk_argc = NB_PARAMS_MAX - 2;
char *args[NB_PARAMS_MAX];
memset(args, 0, sizeof(args));
/* This loop is used to replace the [%f %F %u %U] tokens
* with the filenames passed as parameter of opkrun */
char **ptr;
unsigned int arg;
int rom_arg;
for (arg = 0, rom_arg = -1, ptr = params.exec; *ptr && arg < NB_PARAMS_MAX; ptr++, arg++) {
if (!strcmp("%f", *ptr)) {
if (!opk_argc) {
fprintf(stderr, "WARNING: OPK requires a file as parameter, but none was given\n");
} else {
rom_arg = arg;
args[arg] = realpath(*opk_argv++, NULL);
if (--opk_argc)
fprintf(stderr, "WARNING: OPK requires only one file as parameter\n");
}
} else if (!strcmp("%F", *ptr)) {
rom_arg = arg;
while (opk_argc && arg < NB_PARAMS_MAX) {
args[arg++] = realpath(*opk_argv++, NULL);
opk_argc--;
}
arg--; /* Compensate the arg++ in the 'for' */
} else if (!strcmp("%u", *ptr)) {
if (!opk_argc) {
fprintf(stderr, "WARNING: OPK requires an URL as parameter, but none was given\n");
} else {
rom_arg = arg;
args[arg] = get_url(*opk_argv++);
if (--opk_argc)
fprintf(stderr, "WARNING: OPK requires only one URL as parameter\n");
}
} else if (!strcmp("%U", *ptr)) {
rom_arg = arg;
while (opk_argc && arg < NB_PARAMS_MAX) {
args[arg++] = get_url(*opk_argv++);
opk_argc--;
}
arg--; /* Compensate the arg++ in the 'for' */
} else {
args[arg] = strdup(*ptr);
}
}
free(params.exec[0]);
umount(OPK_MOUNTPOINT);
mkdir(OPK_MOUNTPOINT, 0755);
int devnr = logetfree();
if (devnr < 0)
return devnr;
char loop_dev[9 + 10 + 1];
sprintf(loop_dev, "/dev/loop%i", devnr);
int loopfd = losetup(loop_dev, opk_name);
if (loopfd < 0) {
perror("Failed to losetup");
return loopfd;
}
ret = mount(loop_dev, OPK_MOUNTPOINT, "squashfs", MS_NODEV | MS_NOSUID | MS_RDONLY, 0);
if (ret < 0) {
perror("Unable to mount OPK");
return EXIT_FAILURE;
}
FILE *opk_fp = fopen("/mnt/last_opk", "w");
if (opk_fp != NULL) {
fputs(opk_name, opk_fp);
fclose(opk_fp);
}
/* move back to OPK_MOUNTPOINT folder */
chdir(OPK_MOUNTPOINT);
/* Enable params */
if (params.needs_terminal)
enable_vtcon();
if (params.needs_joystick)
enable_alt_key_map();
if (params.needs_gsensor)
enable_gsensor();
if (params.needs_downscaling)
enable_downscaling();
/* Initialize keymap rom command */
char command[PATH_MAX];
if (rom_arg >= 0) {
snprintf(command, PATH_MAX - 1, "keymap rom '%s'",
args[rom_arg]);
} else {
strcpy(command, "keymap rom");
}
printf("Applied keymap rom command: \"%s\"\n", command);
system(command);
/* Launch executable here */
pid_t son = fork();
if (!son) {
/* Drop privileges */
seteuid(getuid());
if (!access(args[0], X_OK)) /* Not in the root of the OPK */
execv(args[0], args); /* Maybe in the $PATH? */
execvp(args[0], args);
}
/* Record the PID into a file */
sprintf(command, "pid record %d", son);
system(command);
/* Wait for son pid to finish */
int status;
waitpid(son, &status, 0);
system("pid erase");
/* Move back to / folder */
chdir("/");
/* Restore default keymap */
system("keymap default");
/** Multiple trials to unmount OPK_MOUNTPOINT */
#define MAX_UMOUNT_TRIALS 100
int retries = MAX_UMOUNT_TRIALS;
int res = -1;
while (retries && res){
res = umount(OPK_MOUNTPOINT);
/*printf("Res %d umounting %s, trial %d/%d\n",
res, OPK_MOUNTPOINT, MAX_UMOUNT_TRIALS-retries+1, MAX_UMOUNT_TRIALS);*/
if(res)
usleep(50*1000);
retries--;
}
if(!retries){
printf("Error(%d) umounting %s\n", res, OPK_MOUNTPOINT);
}
/** Voluntarily commented */
//rmdir(OPK_MOUNTPOINT);
ioctl(loopfd, LOOP_CLR_FD, (void *)0);
close(loopfd);
for (char **ptr = args; *ptr; ptr++)
free(*ptr);
return status;
}