2 * dexopt invocation test.
4 * You must have BOOTCLASSPATH defined. On the simulator, you will also
11 #include <sys/types.h>
17 //using namespace android;
20 * Privilege reduction function.
22 * Returns 0 on success, nonzero on failure.
24 static int privFunc(void)
26 printf("--- would reduce privs here\n");
31 * We're in the child process. exec dexopt.
33 static void runDexopt(int zipFd, int odexFd, const char* inputFileName)
35 static const char* kDexOptBin = "/bin/dexopt";
36 static const int kMaxIntLen = 12; // '-'+10dig+'\0' -OR- 0x+8dig
37 char zipNum[kMaxIntLen];
38 char odexNum[kMaxIntLen];
39 const char* androidRoot;
42 /* find dexopt executable; this exists for simulator compatibility */
43 androidRoot = getenv("ANDROID_ROOT");
44 if (androidRoot == NULL)
45 androidRoot = "/system";
46 execFile = (char*) malloc(strlen(androidRoot) + strlen(kDexOptBin) +1);
47 sprintf(execFile, "%s%s", androidRoot, kDexOptBin);
49 sprintf(zipNum, "%d", zipFd);
50 sprintf(odexNum, "%d", odexFd);
52 execl(execFile, execFile, "--zip", zipNum, odexNum, inputFileName,
54 fprintf(stderr, "execl(%s) failed: %s\n", kDexOptBin, strerror(errno));
58 * Run dexopt on the specified Jar/APK.
60 * This uses fork() and exec() to mimic the way this would work in an
61 * installer; in practice for something this simple you could just exec()
62 * unless you really wanted the status messages.
64 * Returns 0 on success.
66 int doStuff(const char* zipName, const char* odexName)
71 * Open the zip archive and the odex file, creating the latter (and
72 * failing if it already exists). This must be done while we still
73 * have sufficient privileges to read the source file and create a file
74 * in the target directory. The "classes.dex" file will be extracted.
76 zipFd = open(zipName, O_RDONLY, 0);
78 fprintf(stderr, "Unable to open '%s': %s\n", zipName, strerror(errno));
82 odexFd = open(odexName, O_RDWR | O_CREAT | O_EXCL, 0644);
84 fprintf(stderr, "Unable to create '%s': %s\n",
85 odexName, strerror(errno));
90 printf("--- BEGIN '%s' (bootstrap=%d) ---\n", zipName, 0);
93 * Fork a child process.
97 /* child -- drop privs */
101 /* lock the input file */
102 if (flock(odexFd, LOCK_EX | LOCK_NB) != 0) {
103 fprintf(stderr, "Unable to lock '%s': %s\n",
104 odexName, strerror(errno));
108 runDexopt(zipFd, odexFd, zipName); /* does not return */
109 exit(67); /* usually */
111 /* parent -- wait for child to finish */
112 printf("waiting for verify+opt, pid=%d\n", (int) pid);
113 int status, oldStatus;
120 * Wait for the optimization process to finish.
123 gotPid = waitpid(pid, &status, 0);
124 if (gotPid == -1 && errno == EINTR) {
125 printf("waitpid interrupted, retrying\n");
131 fprintf(stderr, "waitpid failed: wanted %d, got %d: %s\n",
132 (int) pid, (int) gotPid, strerror(errno));
136 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
137 printf("--- END '%s' (success) ---\n", zipName);
140 printf("--- END '%s' --- status=0x%04x, process failed\n",
150 * Parse args, do stuff.
152 int main(int argc, char** argv)
154 if (argc < 3 || argc > 4) {
155 fprintf(stderr, "Usage: %s <input jar/apk> <output odex> "
156 "[<bootclasspath>]\n\n", argv[0]);
157 fprintf(stderr, "Example: dexopttest "
158 "/system/app/NotePad.apk /system/app/NotePad.odex\n");
163 setenv("BOOTCLASSPATH", argv[3], 1);
166 return (doStuff(argv[1], argv[2]) != 0);