| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Main entry of app process. |
| Elliott Hughes | d195e5a | 2011-04-13 15:39:37 -0700 | [diff] [blame] | 3 | * |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4 | * Starts the interpreted runtime, then starts up the application. |
| Elliott Hughes | d195e5a | 2011-04-13 15:39:37 -0700 | [diff] [blame] | 5 | * |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #define LOG_TAG "appproc" |
| 9 | |
| Mathias Agopian | 0795272 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 10 | #include <binder/IPCThreadState.h> |
| 11 | #include <binder/ProcessState.h> |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12 | #include <utils/Log.h> |
| 13 | #include <cutils/process_name.h> |
| 14 | #include <cutils/memory.h> |
| Jamie Gennis | 6ad0452 | 2013-04-15 18:53:24 -0700 | [diff] [blame] | 15 | #include <cutils/trace.h> |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 16 | #include <android_runtime/AndroidRuntime.h> |
| 17 | |
| Nick Kralevich | 1fe21bd | 2013-03-15 11:38:29 -0700 | [diff] [blame] | 18 | #include <stdlib.h> |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 19 | #include <stdio.h> |
| 20 | #include <unistd.h> |
| 21 | |
| 22 | namespace android { |
| 23 | |
| 24 | void app_usage() |
| 25 | { |
| 26 | fprintf(stderr, |
| 27 | "Usage: app_process [java-options] cmd-dir start-class-name [options]\n"); |
| 28 | } |
| 29 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | class AppRuntime : public AndroidRuntime |
| 31 | { |
| 32 | public: |
| Narayan Kamath | a23fcd7 | 2014-03-28 13:39:21 +0000 | [diff] [blame^] | 33 | AppRuntime(char* argBlockStart, const size_t argBlockLength) |
| 34 | : AndroidRuntime(argBlockStart, argBlockLength) |
| 35 | , mParentDir(NULL) |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | , mClassName(NULL) |
| Elliott Hughes | d195e5a | 2011-04-13 15:39:37 -0700 | [diff] [blame] | 37 | , mClass(NULL) |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | , mArgC(0) |
| 39 | , mArgV(NULL) |
| 40 | { |
| 41 | } |
| 42 | |
| 43 | #if 0 |
| 44 | // this appears to be unused |
| 45 | const char* getParentDir() const |
| 46 | { |
| 47 | return mParentDir; |
| 48 | } |
| 49 | #endif |
| 50 | |
| 51 | const char* getClassName() const |
| 52 | { |
| 53 | return mClassName; |
| 54 | } |
| 55 | |
| Elliott Hughes | d195e5a | 2011-04-13 15:39:37 -0700 | [diff] [blame] | 56 | virtual void onVmCreated(JNIEnv* env) |
| 57 | { |
| 58 | if (mClassName == NULL) { |
| 59 | return; // Zygote. Nothing to do here. |
| 60 | } |
| 61 | |
| 62 | /* |
| 63 | * This is a little awkward because the JNI FindClass call uses the |
| 64 | * class loader associated with the native method we're executing in. |
| 65 | * If called in onStarted (from RuntimeInit.finishInit because we're |
| 66 | * launching "am", for example), FindClass would see that we're calling |
| 67 | * from a boot class' native method, and so wouldn't look for the class |
| 68 | * we're trying to look up in CLASSPATH. Unfortunately it needs to, |
| 69 | * because the "am" classes are not boot classes. |
| 70 | * |
| 71 | * The easiest fix is to call FindClass here, early on before we start |
| 72 | * executing boot class Java code and thereby deny ourselves access to |
| 73 | * non-boot classes. |
| 74 | */ |
| 75 | char* slashClassName = toSlashClassName(mClassName); |
| 76 | mClass = env->FindClass(slashClassName); |
| 77 | if (mClass == NULL) { |
| Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 78 | ALOGE("ERROR: could not find class '%s'\n", mClassName); |
| Elliott Hughes | d195e5a | 2011-04-13 15:39:37 -0700 | [diff] [blame] | 79 | } |
| 80 | free(slashClassName); |
| 81 | |
| 82 | mClass = reinterpret_cast<jclass>(env->NewGlobalRef(mClass)); |
| 83 | } |
| 84 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | virtual void onStarted() |
| 86 | { |
| 87 | sp<ProcessState> proc = ProcessState::self(); |
| Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 88 | ALOGV("App process: starting thread pool.\n"); |
| Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 89 | proc->startThreadPool(); |
| Elliott Hughes | d195e5a | 2011-04-13 15:39:37 -0700 | [diff] [blame] | 90 | |
| 91 | AndroidRuntime* ar = AndroidRuntime::getRuntime(); |
| 92 | ar->callMain(mClassName, mClass, mArgC, mArgV); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | |
| Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 94 | IPCThreadState::self()->stopProcess(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | virtual void onZygoteInit() |
| 98 | { |
| Jamie Gennis | 6ad0452 | 2013-04-15 18:53:24 -0700 | [diff] [blame] | 99 | // Re-enable tracing now that we're no longer in Zygote. |
| 100 | atrace_set_tracing_enabled(true); |
| 101 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | sp<ProcessState> proc = ProcessState::self(); |
| Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 103 | ALOGV("App process: starting thread pool.\n"); |
| Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 104 | proc->startThreadPool(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | virtual void onExit(int code) |
| 108 | { |
| 109 | if (mClassName == NULL) { |
| 110 | // if zygote |
| Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 111 | IPCThreadState::self()->stopProcess(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | AndroidRuntime::onExit(code); |
| 115 | } |
| 116 | |
| Elliott Hughes | d195e5a | 2011-04-13 15:39:37 -0700 | [diff] [blame] | 117 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | const char* mParentDir; |
| 119 | const char* mClassName; |
| Elliott Hughes | d195e5a | 2011-04-13 15:39:37 -0700 | [diff] [blame] | 120 | jclass mClass; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 121 | int mArgC; |
| 122 | const char* const* mArgV; |
| 123 | }; |
| 124 | |
| 125 | } |
| 126 | |
| 127 | using namespace android; |
| 128 | |
| Narayan Kamath | a23fcd7 | 2014-03-28 13:39:21 +0000 | [diff] [blame^] | 129 | static size_t computeArgBlockSize(int argc, char* const argv[]) { |
| 130 | // TODO: This assumes that all arguments are allocated in |
| 131 | // contiguous memory. There isn't any documented guarantee |
| 132 | // that this is the case, but this is how the kernel does it |
| 133 | // (see fs/exec.c). |
| 134 | // |
| 135 | // Also note that this is a constant for "normal" android apps. |
| 136 | // Since they're forked from zygote, the size of their command line |
| 137 | // is the size of the zygote command line. |
| 138 | // |
| 139 | // We change the process name of the process by over-writing |
| 140 | // the start of the argument block (argv[0]) with the new name of |
| 141 | // the process, so we'd mysteriously start getting truncated process |
| 142 | // names if the zygote command line decreases in size. |
| 143 | uintptr_t start = reinterpret_cast<uintptr_t>(argv[0]); |
| 144 | uintptr_t end = reinterpret_cast<uintptr_t>(argv[argc - 1]); |
| 145 | end += strlen(argv[argc - 1]); |
| 146 | |
| 147 | return (end - start); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | } |
| 149 | |
| Nick Kralevich | 8a0a929 | 2013-03-14 13:23:52 -0700 | [diff] [blame] | 150 | int main(int argc, char* const argv[]) |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 151 | { |
| Narayan Kamath | a23fcd7 | 2014-03-28 13:39:21 +0000 | [diff] [blame^] | 152 | AppRuntime runtime(argv[0], computeArgBlockSize(argc, argv)); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | // Process command line arguments |
| 154 | // ignore argv[0] |
| 155 | argc--; |
| 156 | argv++; |
| 157 | |
| 158 | // Everything up to '--' or first non '-' arg goes to the vm |
| Elliott Hughes | d195e5a | 2011-04-13 15:39:37 -0700 | [diff] [blame] | 159 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 160 | int i = runtime.addVmArguments(argc, argv); |
| 161 | |
| Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 162 | // Parse runtime arguments. Stop at first unrecognized option. |
| 163 | bool zygote = false; |
| 164 | bool startSystemServer = false; |
| 165 | bool application = false; |
| 166 | const char* parentDir = NULL; |
| 167 | const char* niceName = NULL; |
| 168 | const char* className = NULL; |
| 169 | while (i < argc) { |
| 170 | const char* arg = argv[i++]; |
| 171 | if (!parentDir) { |
| 172 | parentDir = arg; |
| 173 | } else if (strcmp(arg, "--zygote") == 0) { |
| 174 | zygote = true; |
| 175 | niceName = "zygote"; |
| 176 | } else if (strcmp(arg, "--start-system-server") == 0) { |
| 177 | startSystemServer = true; |
| 178 | } else if (strcmp(arg, "--application") == 0) { |
| 179 | application = true; |
| 180 | } else if (strncmp(arg, "--nice-name=", 12) == 0) { |
| 181 | niceName = arg + 12; |
| 182 | } else { |
| 183 | className = arg; |
| 184 | break; |
| 185 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 186 | } |
| 187 | |
| Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 188 | if (niceName && *niceName) { |
| Narayan Kamath | a23fcd7 | 2014-03-28 13:39:21 +0000 | [diff] [blame^] | 189 | runtime.setArgv0(niceName); |
| Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 190 | set_process_name(niceName); |
| 191 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 192 | |
| Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 193 | runtime.mParentDir = parentDir; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 194 | |
| Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 195 | if (zygote) { |
| 196 | runtime.start("com.android.internal.os.ZygoteInit", |
| 197 | startSystemServer ? "start-system-server" : ""); |
| 198 | } else if (className) { |
| 199 | // Remainder of args get passed to startup class main() |
| 200 | runtime.mClassName = className; |
| 201 | runtime.mArgC = argc - i; |
| 202 | runtime.mArgV = argv + i; |
| 203 | runtime.start("com.android.internal.os.RuntimeInit", |
| 204 | application ? "application" : "tool"); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 205 | } else { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 206 | fprintf(stderr, "Error: no class name or --zygote supplied.\n"); |
| 207 | app_usage(); |
| Brian Carlstrom | de6d1d8 | 2010-10-07 16:02:11 -0700 | [diff] [blame] | 208 | LOG_ALWAYS_FATAL("app_process: no class name or --zygote supplied."); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 209 | return 10; |
| 210 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 211 | } |