app_main.cpp revision 195c73c9b2c5be50ab325099dc2160215ac7562a
1/* 2 * Main entry of app process. 3 * 4 * Starts the interpreted runtime, then starts up the application. 5 * 6 */ 7 8#define LOG_TAG "appproc" 9 10#include <binder/IPCThreadState.h> 11#include <binder/ProcessState.h> 12#include <utils/Log.h> 13#include <cutils/memory.h> 14#include <cutils/process_name.h> 15#include <cutils/properties.h> 16#include <cutils/trace.h> 17#include <android_runtime/AndroidRuntime.h> 18 19#include <stdlib.h> 20#include <stdio.h> 21#include <unistd.h> 22#include <sys/prctl.h> 23 24namespace android { 25 26void app_usage() 27{ 28 fprintf(stderr, 29 "Usage: app_process [java-options] cmd-dir start-class-name [options]\n"); 30} 31 32class AppRuntime : public AndroidRuntime 33{ 34public: 35 AppRuntime(char* argBlockStart, const size_t argBlockLength) 36 : AndroidRuntime(argBlockStart, argBlockLength) 37 , mClass(NULL) 38 { 39 } 40 41 void setClassNameAndArgs(const String8& className, int argc, char * const *argv) { 42 mClassName = className; 43 for (int i = 0; i < argc; ++i) { 44 mArgs.add(String8(argv[i])); 45 } 46 } 47 48 virtual void onVmCreated(JNIEnv* env) 49 { 50 if (mClassName.isEmpty()) { 51 return; // Zygote. Nothing to do here. 52 } 53 54 /* 55 * This is a little awkward because the JNI FindClass call uses the 56 * class loader associated with the native method we're executing in. 57 * If called in onStarted (from RuntimeInit.finishInit because we're 58 * launching "am", for example), FindClass would see that we're calling 59 * from a boot class' native method, and so wouldn't look for the class 60 * we're trying to look up in CLASSPATH. Unfortunately it needs to, 61 * because the "am" classes are not boot classes. 62 * 63 * The easiest fix is to call FindClass here, early on before we start 64 * executing boot class Java code and thereby deny ourselves access to 65 * non-boot classes. 66 */ 67 char* slashClassName = toSlashClassName(mClassName.string()); 68 mClass = env->FindClass(slashClassName); 69 if (mClass == NULL) { 70 ALOGE("ERROR: could not find class '%s'\n", mClassName.string()); 71 } 72 free(slashClassName); 73 74 mClass = reinterpret_cast<jclass>(env->NewGlobalRef(mClass)); 75 } 76 77 virtual void onStarted() 78 { 79 sp<ProcessState> proc = ProcessState::self(); 80 ALOGV("App process: starting thread pool.\n"); 81 proc->startThreadPool(); 82 83 AndroidRuntime* ar = AndroidRuntime::getRuntime(); 84 ar->callMain(mClassName, mClass, mArgs); 85 86 IPCThreadState::self()->stopProcess(); 87 } 88 89 virtual void onZygoteInit() 90 { 91 // Re-enable tracing now that we're no longer in Zygote. 92 atrace_set_tracing_enabled(true); 93 94 sp<ProcessState> proc = ProcessState::self(); 95 ALOGV("App process: starting thread pool.\n"); 96 proc->startThreadPool(); 97 } 98 99 virtual void onExit(int code) 100 { 101 if (mClassName.isEmpty()) { 102 // if zygote 103 IPCThreadState::self()->stopProcess(); 104 } 105 106 AndroidRuntime::onExit(code); 107 } 108 109 110 String8 mClassName; 111 Vector<String8> mArgs; 112 jclass mClass; 113}; 114 115} 116 117using namespace android; 118 119static size_t computeArgBlockSize(int argc, char* const argv[]) { 120 // TODO: This assumes that all arguments are allocated in 121 // contiguous memory. There isn't any documented guarantee 122 // that this is the case, but this is how the kernel does it 123 // (see fs/exec.c). 124 // 125 // Also note that this is a constant for "normal" android apps. 126 // Since they're forked from zygote, the size of their command line 127 // is the size of the zygote command line. 128 // 129 // We change the process name of the process by over-writing 130 // the start of the argument block (argv[0]) with the new name of 131 // the process, so we'd mysteriously start getting truncated process 132 // names if the zygote command line decreases in size. 133 uintptr_t start = reinterpret_cast<uintptr_t>(argv[0]); 134 uintptr_t end = reinterpret_cast<uintptr_t>(argv[argc - 1]); 135 end += strlen(argv[argc - 1]); 136 137 return (end - start); 138} 139 140#if defined(__LP64__) 141static const char ABI_LIST_PROPERTY[] = "ro.product.cpu.abilist64"; 142static const char ZYGOTE_NICE_NAME[] = "zygote64"; 143#else 144static const char ABI_LIST_PROPERTY[] = "ro.product.cpu.abilist32"; 145static const char ZYGOTE_NICE_NAME[] = "zygote"; 146#endif 147 148int main(int argc, char* const argv[]) 149{ 150 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) < 0) { 151 // Older kernels don't understand PR_SET_NO_NEW_PRIVS and return 152 // EINVAL. Don't die on such kernels. 153 if (errno != EINVAL) { 154 LOG_ALWAYS_FATAL("PR_SET_NO_NEW_PRIVS failed: %s", strerror(errno)); 155 return 12; 156 } 157 } 158 159 AppRuntime runtime(argv[0], computeArgBlockSize(argc, argv)); 160 // Process command line arguments 161 // ignore argv[0] 162 argc--; 163 argv++; 164 165 // Everything up to '--' or first non '-' arg goes to the vm. 166 // 167 // The first argument after the VM args is the "parent dir", which 168 // is currently unused. 169 // 170 // After the parent dir, we expect one or more the following internal 171 // arguments : 172 // 173 // --zygote : Start in zygote mode 174 // --start-system-server : Start the system server. 175 // --application : Start in application (stand alone, non zygote) mode. 176 // --nice-name : The nice name for this process. 177 // 178 // For non zygote starts, these arguments will be followed by 179 // the main class name. All remaining arguments are passed to 180 // the main method of this class. 181 // 182 // For zygote starts, all remaining arguments are passed to the zygote. 183 // main function. 184 185 186 int i = runtime.addVmArguments(argc, argv); 187 188 // Parse runtime arguments. Stop at first unrecognized option. 189 bool zygote = false; 190 bool startSystemServer = false; 191 bool application = false; 192 const char* niceName = NULL; 193 String8 className; 194 195 ++i; // Skip unused "parent dir" argument. 196 while (i < argc) { 197 const char* arg = argv[i++]; 198 if (strcmp(arg, "--zygote") == 0) { 199 zygote = true; 200 niceName = ZYGOTE_NICE_NAME; 201 } else if (strcmp(arg, "--start-system-server") == 0) { 202 startSystemServer = true; 203 } else if (strcmp(arg, "--application") == 0) { 204 application = true; 205 } else if (strncmp(arg, "--nice-name=", 12) == 0) { 206 niceName = arg + 12; 207 } else if (strncmp(arg, "--", 2) != 0) { 208 className.setTo(arg); 209 break; 210 } else { 211 --i; 212 break; 213 } 214 } 215 216 Vector<String8> args; 217 if (!className.isEmpty()) { 218 // We're not in zygote mode, the only argument we need to pass 219 // to RuntimeInit is the application argument. 220 // 221 // The Remainder of args get passed to startup class main(). Make 222 // copies of them before we overwrite them with the process name. 223 args.add(application ? String8("application") : String8("tool")); 224 runtime.setClassNameAndArgs(className, argc - i, argv + i); 225 } else { 226 if (startSystemServer) { 227 args.add(String8("start-system-server")); 228 } 229 230 char prop[PROP_VALUE_MAX]; 231 if (property_get(ABI_LIST_PROPERTY, prop, NULL) == 0) { 232 LOG_ALWAYS_FATAL("app_process: Unable to determine ABI list from property %s.", 233 ABI_LIST_PROPERTY); 234 return 11; 235 } 236 237 String8 abiFlag("--abi-list="); 238 abiFlag.append(prop); 239 args.add(abiFlag); 240 241 // In zygote mode, pass all remaining arguments to the zygote 242 // main() method. 243 for (; i < argc; ++i) { 244 args.add(String8(argv[i])); 245 } 246 } 247 248 if (niceName && *niceName) { 249 runtime.setArgv0(niceName); 250 set_process_name(niceName); 251 } 252 253 if (zygote) { 254 runtime.start("com.android.internal.os.ZygoteInit", args); 255 } else if (className) { 256 runtime.start("com.android.internal.os.RuntimeInit", args); 257 } else { 258 fprintf(stderr, "Error: no class name or --zygote supplied.\n"); 259 app_usage(); 260 LOG_ALWAYS_FATAL("app_process: no class name or --zygote supplied."); 261 return 10; 262 } 263} 264