-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathandroid-main.c
500 lines (414 loc) · 14.5 KB
/
android-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
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
/*
Copyright (c) 2014 Hans-Werner Hilse <software@haveyouseenthiscat.de>
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <android_native_app_glue.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dlfcn.h>
#include <linux/elf.h>
#include <android/log.h>
#include <android/asset_manager.h>
#include "luajit-2.0/lua.h"
#include "luajit-2.0/lauxlib.h"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,"luajit-launcher",__VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,"luajit-launcher",__VA_ARGS__)
#define LOADER_ASSET "android.lua"
static char* getApplicationDirectory(struct android_app* state);
static const char *library_locations[] = {"/lib", "/system/lib", NULL, NULL, NULL};
void android_main(struct android_app* state) {
lua_State *L;
AAsset* luaCode;
const void *buf;
off_t bufsize;
int status;
char *libs_dir;
// Make sure glue isn't stripped.
app_dummy();
char *base_dir = getApplicationDirectory(state);
libs_dir = malloc(strlen(base_dir) + 6);
strcpy(libs_dir, base_dir);
strcat(libs_dir, "/libs");
library_locations[2] = base_dir;
library_locations[3] = libs_dir;
luaCode = AAssetManager_open(state->activity->assetManager, LOADER_ASSET, AASSET_MODE_BUFFER);
if (luaCode == NULL) {
LOGE("error loading loader asset");
return;
}
bufsize = AAsset_getLength(luaCode);
buf = AAsset_getBuffer(luaCode);
if (buf == NULL) {
LOGE("error getting loader asset buffer");
return;
}
// Load initial Lua loader from our asset store:
L = luaL_newstate();
luaL_openlibs(L);
status = luaL_loadbuffer(L, (const char*) buf, (size_t) bufsize, LOADER_ASSET);
AAsset_close(luaCode);
if (status) {
LOGE("error loading file: %s", lua_tostring(L, -1));
return;
}
// pass the android_app state to Lua land:
lua_pushlightuserdata(L, state);
status = lua_pcall(L, 1, LUA_MULTRET, 0);
if (status) {
LOGE("Failed to run script: %s", lua_tostring(L, -1));
return;
}
lua_close(L);
}
char *getApplicationDirectory(struct android_app* state) {
// PackageManager packetManager = getPackageManager();
// String packageName = getPackageName();
// PackageInfo packageInfo = packageManager.getPackageInfo(packageName, 0);
// String dataDir = packageInfo.applicationInfo.dataDir;
// Connect to JVM.
//
ANativeActivity* activity = state->activity;
JavaVM* jvm = state->activity->vm;
JNIEnv* env = NULL;
(*jvm)->GetEnv(jvm, (void **)&env, JNI_VERSION_1_6);
jint result = (*jvm)->AttachCurrentThread(jvm, &env, NULL);
if (result == JNI_ERR)
{
LOGE("Failied to attach to the Java VM's current thread.");
exit(EXIT_FAILURE);
}
// Perform Java calls.
//
jclass clazz = (*env)->GetObjectClass(env, activity->clazz);
jmethodID getPackageManagerID = (*env)->GetMethodID(env, clazz, "getPackageManager", "()Landroid/content/pm/PackageManager;");
jmethodID getPackageNameID = (*env)->GetMethodID(env, clazz, "getPackageName", "()Ljava/lang/String;");
jobject packageManager = (*env)->CallObjectMethod(env, activity->clazz, getPackageManagerID);
jobject packageName = (*env)->CallObjectMethod(env, activity->clazz, getPackageNameID);
clazz = (*env)->GetObjectClass(env, packageManager);
jmethodID getPackageInfoID = (*env)->GetMethodID(env, clazz, "getPackageInfo", "(Ljava/lang/String;I)Landroid/content/pm/PackageInfo;");
jobject packageInfo = (*env)->CallObjectMethod(env, packageManager, getPackageInfoID, packageName, 0);
clazz = (*env)->GetObjectClass(env, packageInfo);
jfieldID applicationInfoID = (*env)->GetFieldID(env, clazz, "applicationInfo", "Landroid/content/pm/ApplicationInfo;");
jobject applicationInfo = (*env)->GetObjectField(env, packageInfo, applicationInfoID);
clazz = (*env)->GetObjectClass(env, applicationInfo);
jfieldID dataDirID = (*env)->GetFieldID(env, clazz, "dataDir", "Ljava/lang/String;");
if (dataDirID == NULL) {
LOGE("dataDir field not found.");
return;
}
jstring dataDir = (*env)->GetObjectField(env, applicationInfo, dataDirID);
// Copy string in a custom buffer.
//
int strlen = (*env)->GetStringUTFLength(env, dataDir);
char *buffer = malloc(strlen + 1);
const char *str = (*env)->GetStringUTFChars(env, dataDir, NULL);
strcpy(buffer, str);
buffer[strlen] = '\0';
(*env)->ReleaseStringUTFChars(env, dataDir, str);
// Detach from JVM.
//
(*jvm)->DetachCurrentThread(jvm);
return buffer;
}
/*
* lo_dlopen from libreoffice/core
* MPL 1.1 / GPLv3+ / LGPLv3+
*/
/* Zip data structures */
/* compression methods */
#define STORE 0
#define DEFLATE 8
#define LZMA 14
struct local_file_header {
uint32_t signature;
uint16_t min_version;
uint16_t general_flag;
uint16_t compression;
uint16_t lastmod_time;
uint16_t lastmod_date;
uint32_t crc32;
uint32_t compressed_size;
uint32_t uncompressed_size;
uint16_t filename_size;
uint16_t extra_field_size;
char data[0];
} __attribute__((__packed__));
struct cdir_entry {
uint32_t signature;
uint16_t creator_version;
uint16_t min_version;
uint16_t general_flag;
uint16_t compression;
uint16_t lastmod_time;
uint16_t lastmod_date;
uint32_t crc32;
uint32_t compressed_size;
uint32_t uncompressed_size;
uint16_t filename_size;
uint16_t extra_field_size;
uint16_t file_comment_size;
uint16_t disk_num;
uint16_t internal_attr;
uint32_t external_attr;
uint32_t offset;
char data[0];
} __attribute__((__packed__));
#define CDIR_END_SIG 0x06054b50
struct cdir_end {
uint32_t signature;
uint16_t disk_num;
uint16_t cdir_disk;
uint16_t disk_entries;
uint16_t cdir_entries;
uint32_t cdir_size;
uint32_t cdir_offset;
uint16_t comment_size;
char comment[0];
} __attribute__((__packed__));
/* End of Zip data structures */
static char * read_section(int fd, Elf32_Shdr *shdr) {
char *result;
result = malloc(shdr->sh_size);
if (lseek(fd, shdr->sh_offset, SEEK_SET) < 0) {
close(fd);
free(result);
return NULL;
}
if (read(fd, result, shdr->sh_size) < shdr->sh_size) {
close(fd);
free(result);
return NULL;
}
return result;
}
static void free_ptrarray(void **pa) {
void **rover = pa;
while (*rover != NULL)
free(*rover++);
free(pa);
}
char ** lo_dlneeds(const char *library) {
int i, fd;
int n_needed;
char **result;
char *shstrtab, *dynstr;
Elf32_Ehdr hdr;
Elf32_Shdr shdr;
Elf32_Dyn dyn;
/* Open library and read ELF header */
fd = open(library, O_RDONLY);
if (fd == -1) {
LOGE("lo_dlneeds: Could not open library %s: %s", library, strerror(errno));
return NULL;
}
if (read(fd, &hdr, sizeof(hdr)) < sizeof(hdr)) {
LOGE("lo_dlneeds: Could not read ELF header of %s", library);
close(fd);
return NULL;
}
/* Read in .shstrtab */
if (lseek(fd, hdr.e_shoff + hdr.e_shstrndx * sizeof(shdr), SEEK_SET) < 0) {
LOGE("lo_dlneeds: Could not seek to .shstrtab section header of %s", library);
close(fd);
return NULL;
}
if (read(fd, &shdr, sizeof(shdr)) < sizeof(shdr)) {
LOGE("lo_dlneeds: Could not read section header of %s", library);
close(fd);
return NULL;
}
shstrtab = read_section(fd, &shdr);
if (shstrtab == NULL)
return NULL;
/* Read section headers, looking for .dynstr section */
if (lseek(fd, hdr.e_shoff, SEEK_SET) < 0) {
LOGE("lo_dlneeds: Could not seek to section headers of %s", library);
close(fd);
return NULL;
}
for (i = 0; i < hdr.e_shnum; i++) {
if (read(fd, &shdr, sizeof(shdr)) < sizeof(shdr)) {
LOGE("lo_dlneeds: Could not read section header of %s", library);
close(fd);
return NULL;
}
if (shdr.sh_type == SHT_STRTAB &&
strcmp(shstrtab + shdr.sh_name, ".dynstr") == 0) {
dynstr = read_section(fd, &shdr);
if (dynstr == NULL) {
free(shstrtab);
return NULL;
}
break;
}
}
if (i == hdr.e_shnum) {
LOGE("lo_dlneeds: No .dynstr section in %s", library);
close(fd);
return NULL;
}
/* Read section headers, looking for .dynamic section */
if (lseek(fd, hdr.e_shoff, SEEK_SET) < 0) {
LOGE("lo_dlneeds: Could not seek to section headers of %s", library);
close(fd);
return NULL;
}
for (i = 0; i < hdr.e_shnum; i++) {
if (read(fd, &shdr, sizeof(shdr)) < sizeof(shdr)) {
LOGE("lo_dlneeds: Could not read section header of %s", library);
close(fd);
return NULL;
}
if (shdr.sh_type == SHT_DYNAMIC) {
int dynoff;
int *libnames;
/* Count number of DT_NEEDED entries */
n_needed = 0;
if (lseek(fd, shdr.sh_offset, SEEK_SET) < 0) {
LOGE("lo_dlneeds: Could not seek to .dynamic section of %s", library);
close(fd);
return NULL;
}
for (dynoff = 0; dynoff < shdr.sh_size; dynoff += sizeof(dyn)) {
if (read(fd, &dyn, sizeof(dyn)) < sizeof(dyn)) {
LOGE("lo_dlneeds: Could not read .dynamic entry of %s", library);
close(fd);
return NULL;
}
if (dyn.d_tag == DT_NEEDED)
n_needed++;
}
/* LOGI("Found %d DT_NEEDED libs", n_needed); */
result = malloc((n_needed+1) * sizeof(char *));
n_needed = 0;
if (lseek(fd, shdr.sh_offset, SEEK_SET) < 0) {
LOGE("lo_dlneeds: Could not seek to .dynamic section of %s", library);
close(fd);
free(result);
return NULL;
}
for (dynoff = 0; dynoff < shdr.sh_size; dynoff += sizeof(dyn)) {
if (read(fd, &dyn, sizeof(dyn)) < sizeof(dyn)) {
LOGE("lo_dlneeds: Could not read .dynamic entry in %s", library);
close(fd);
free(result);
return NULL;
}
if (dyn.d_tag == DT_NEEDED) {
/* LOGI("needs: %s\n", dynstr + dyn.d_un.d_val); */
result[n_needed] = strdup(dynstr + dyn.d_un.d_val);
n_needed++;
}
}
close(fd);
free(dynstr);
free(shstrtab);
result[n_needed] = NULL;
return result;
}
}
LOGE("lo_dlneeds: Could not find .dynamic section in %s", library);
close(fd);
return NULL;
}
void * lo_dlopen(const char *library) {
/*
* We should *not* try to just dlopen() the bare library name
* first, as the stupid dynamic linker remembers for each library
* basename if loading it has failed. Thus if you try loading it
* once, and it fails because of missing needed libraries, and
* your load those, and then try again, it fails with an
* infuriating message "failed to load previously" in the log.
*
* We *must* first dlopen() all needed libraries, recursively. It
* shouldn't matter if we dlopen() a library that already is
* loaded, dlopen() just returns the same value then.
*/
typedef struct loadedLib {
const char *name;
void *handle;
struct loadedLib *next;
} *loadedLib;
static loadedLib loaded_libraries = NULL;
loadedLib rover;
loadedLib new_loaded_lib;
struct stat st;
void *p;
char *full_name;
char **needed;
int i;
int found;
rover = loaded_libraries;
while (rover != NULL &&
strcmp(rover->name, library) != 0)
rover = rover->next;
if (rover != NULL)
return rover->handle;
/* LOGI("lo_dlopen(%s)", library); */
found = 0;
if (library[0] == '/') {
full_name = strdup(library);
if (stat(full_name, &st) == 0 &&
S_ISREG(st.st_mode))
found = 1;
else
free(full_name);
} else {
for (i = 0; !found && library_locations[i] != NULL; i++) {
full_name = malloc(strlen(library_locations[i]) + 1 + strlen(library) + 1);
strcpy(full_name, library_locations[i]);
strcat(full_name, "/");
strcat(full_name, library);
if (stat(full_name, &st) == 0 &&
S_ISREG(st.st_mode))
found = 1;
else
free(full_name);
}
}
if (!found) {
LOGE("lo_dlopen: Library %s not found", library);
return NULL;
}
needed = lo_dlneeds(full_name);
if (needed == NULL) {
free(full_name);
return NULL;
}
for (i = 0; needed[i] != NULL; i++) {
if (lo_dlopen(needed[i]) == NULL) {
free_ptrarray((void **) needed);
free(full_name);
return NULL;
}
}
free_ptrarray((void **) needed);
p = dlopen(full_name, RTLD_LOCAL);
LOGI("dlopen(%s) = %p", full_name, p);
free(full_name);
if (p == NULL)
LOGE("lo_dlopen: Error from dlopen(%s): %s", library, dlerror());
new_loaded_lib = malloc(sizeof(*new_loaded_lib));
new_loaded_lib->name = strdup(library);
new_loaded_lib->handle = p;
new_loaded_lib->next = loaded_libraries;
loaded_libraries = new_loaded_lib;
return p;
}