From a23fcd7be8e40078a913b1a99222cdd89229e67b Mon Sep 17 00:00:00 2001 From: Narayan Kamath Date: Fri, 28 Mar 2014 13:39:21 +0000 Subject: [PATCH] Remove ProcessState::mArgc,mArgV,mArgLen These look like historical oddities, and weren't really being used for anything useful. Process:setArgV0 was being called by android.util.Process, but that functionality can be moved directly into the implementation of that class. bug: 13647418 Change-Id: I216c8f8a4c065f0cf3a61f19f9e32decd26f93f6 --- cmds/app_process/app_main.cpp | 46 ++++++++++++------------ core/jni/AndroidRuntime.cpp | 11 ++++-- core/jni/android_util_Process.cpp | 6 ++-- include/android_runtime/AndroidRuntime.h | 6 +++- 4 files changed, 41 insertions(+), 28 deletions(-) diff --git a/cmds/app_process/app_main.cpp b/cmds/app_process/app_main.cpp index 8d2b739fc3011..bdbb08c949670 100644 --- a/cmds/app_process/app_main.cpp +++ b/cmds/app_process/app_main.cpp @@ -30,8 +30,9 @@ void app_usage() class AppRuntime : public AndroidRuntime { public: - AppRuntime() - : mParentDir(NULL) + AppRuntime(char* argBlockStart, const size_t argBlockLength) + : AndroidRuntime(argBlockStart, argBlockLength) + , mParentDir(NULL) , mClassName(NULL) , mClass(NULL) , mArgC(0) @@ -125,29 +126,30 @@ public: using namespace android; -/* - * sets argv0 to as much of newArgv0 as will fit - */ -static void setArgv0(const char *argv0, const char *newArgv0) -{ - strlcpy(const_cast(argv0), newArgv0, strlen(argv0)); +static size_t computeArgBlockSize(int argc, char* const argv[]) { + // TODO: This assumes that all arguments are allocated in + // contiguous memory. There isn't any documented guarantee + // that this is the case, but this is how the kernel does it + // (see fs/exec.c). + // + // Also note that this is a constant for "normal" android apps. + // Since they're forked from zygote, the size of their command line + // is the size of the zygote command line. + // + // We change the process name of the process by over-writing + // the start of the argument block (argv[0]) with the new name of + // the process, so we'd mysteriously start getting truncated process + // names if the zygote command line decreases in size. + uintptr_t start = reinterpret_cast(argv[0]); + uintptr_t end = reinterpret_cast(argv[argc - 1]); + end += strlen(argv[argc - 1]); + + return (end - start); } int main(int argc, char* const argv[]) { - // These are global variables in ProcessState.cpp - mArgC = argc; - mArgV = argv; - - mArgLen = 0; - for (int i=0; i #include -#include #include +#include #include #include #include @@ -402,7 +402,9 @@ void android_os_Process_setArgV0(JNIEnv* env, jobject clazz, jstring name) } if (name8.size() > 0) { - ProcessState::self()->setArgV0(name8.string()); + const char* procName = name8.string(); + set_process_name(procName); + AndroidRuntime::getRuntime()->setArgv0(procName); } } diff --git a/include/android_runtime/AndroidRuntime.h b/include/android_runtime/AndroidRuntime.h index 649f4c344d64c..6f2af908f70d2 100644 --- a/include/android_runtime/AndroidRuntime.h +++ b/include/android_runtime/AndroidRuntime.h @@ -34,7 +34,7 @@ namespace android { class AndroidRuntime { public: - AndroidRuntime(); + AndroidRuntime(char* argBlockStart, size_t argBlockSize); virtual ~AndroidRuntime(); enum StartMode { @@ -44,6 +44,8 @@ public: Tool, }; + void setArgv0(const char* argv0); + /** * Register a set of methods in the specified class. */ @@ -120,6 +122,8 @@ private: Vector mOptions; bool mExitWithoutCleanup; + char* const mArgBlockStart; + const size_t mArgBlockLength; /* JNI JavaVM pointer */ static JavaVM* mJavaVM;