summaryrefslogtreecommitdiff
path: root/standalone/android/start.c
diff options
context:
space:
mode:
authorGravatar Joey Hess <joey@kitenet.net>2013-02-24 13:25:55 -0400
committerGravatar Joey Hess <joey@kitenet.net>2013-02-24 13:27:06 -0400
commit2b2b7c56397fd5457bd178e9243d392afd2b07f0 (patch)
tree76693e311929a94504974b61836dd5c81ec3d69f /standalone/android/start.c
parent0ef3771e174671ebb9074c96c52063c5db6789b2 (diff)
use C shim to start Android app
This should avoid relying on features of the Android builtin shell, and so hopefully avoid failures like this one http://git-annex.branchable.com/design/assistant/blog/day_197__template_haskell/#comment-07f90830f78f6495dcbdf90eb8636129 The C shim sets up busybox, and uses its builtin shell to run runshell. It's important that busybox be configured with CONFIG_FEATURE_SH_STANDALONE, so that while runshell is running, it does not rely on either system utilities, or busybox being already installed.
Diffstat (limited to 'standalone/android/start.c')
-rw-r--r--standalone/android/start.c52
1 files changed, 52 insertions, 0 deletions
diff --git a/standalone/android/start.c b/standalone/android/start.c
new file mode 100644
index 000000000..1f54ba259
--- /dev/null
+++ b/standalone/android/start.c
@@ -0,0 +1,52 @@
+/* Installed as lib.start.so, this bootstraps a working busybox and uses
+ * it to run lib.runshell.so. */
+
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+
+void chopdir (char *s) {
+ char *p=strrchr(s, '/');
+ if (p == NULL) {
+ fprintf(stderr, "cannot find directory in %s", s);
+ exit(1);
+ }
+ p[0] = '\0';
+}
+
+main () {
+ char buf[1024];
+ char *p;
+ struct stat st_buf;
+
+ /* Get something like /data/data/ga.androidterm/lib/lib.start.so */
+ int n=readlink("/proc/self/exe", buf, 1023);
+ if (n < 1) {
+ fprintf(stderr, "failed to find own name");
+ exit(1);
+ }
+ buf[n] = '\0';
+
+ /* Change directory to something like /data/data/ga.androidterm */
+ chopdir(buf);
+ chopdir(buf);
+ if (chdir(buf) != 0) {
+ perror("chdir");
+ exit(1);
+ }
+
+ /* If this is the first run, set up busybox link. */
+ if (stat("bin/busybox", &st_buf) != 0) {
+ mkdir("bin", 0777);
+ if (link("lib/lib.busybox.so", "bin/busybox") != 0) {
+ perror("link busybox");
+ exit(1);
+ }
+ }
+
+ execl("bin/busybox", "bin/busybox", "sh", "lib/lib.runshell.so", NULL);
+ perror("error running busybox sh");
+}