标签:
/* \system\core\rootdir\init.rc*/
on boot
# basic network init
ifup lo
hostname localhost
domainname localdomain
# set RLIMIT_NICE to allow priorities from 19 to -20
setrlimit 13 40 40
# Memory management. Basic kernel parameters, and allow the high
# level system server to be able to adjust the kernel OOM driver
# parameters to match how it is managing things.
write /proc/sys/vm/overcommit_memory 1
write /proc/sys/vm/min_free_order_shift 4
chown root system /sys/module/lowmemorykiller/parameters/adj
chmod 0220 /sys/module/lowmemorykiller/parameters/adj
chown root system /sys/module/lowmemorykiller/parameters/minfree
chmod 0220 /sys/module/lowmemorykiller/parameters/minfree
....
/* \system\core\rootdir\init.rc*/
//组件 进程名 程序文件
service servicemanager /system/bin/servicemanager //service 表明SM是以服务的形式启动的
class core
user system //表明进程是以系统用户system身份运行的
group system
critical /* 表明SM是系统中的一个关键服务,
* 在系统运行过程中,关键服务是不允许退出的;一旦退出,就会被系统重启;
* 而如果一个关键服务在4分钟内退出次数大于4,则系统会重启*/
onrestart restart healthd /* 表明一旦SM被重启,这些进程zygote等也会被重启*/
onrestart restart zygote
onrestart restart media
onrestart restart surfaceflinger
onrestart restart drm
zygote本身是一个应用层的程序,和驱动,内核模块之类的没点关系。zygote的启动由linux的祖先init启动。zygote,其最初的名字是app_process,通过直接调用pctrl把名字给改成了”zygote”。
/* \system\core\rootdir\init.zygote64.rc */
service zygote /system/bin/app_process64 -Xzygote /system/bin --zygote --start-system-server
class main
socket zygote stream 660 root system
onrestart write /sys/android_power/request_state wake
onrestart write /sys/power/state on
onrestart restart media
onrestart restart netd 可以看到Zygote的class是main(ManagerService的class是core),其路径在/system/bin/app_process64(类似的还有32,32_64,64_32情况);后面跟的都是Zygote的参数。

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:= app_main.cpp
LOCAL_LDFLAGS := -Wl,--version-script,art/sigchainlib/version-script.txt -Wl,--export-dynamic
LOCAL_SHARED_LIBRARIES := libdl libcutils libutils liblog libbinder libandroid_runtime
LOCAL_WHOLE_STATIC_LIBRARIES := libsigchain
LOCAL_MODULE:= app_process
LOCAL_MULTILIB := both
LOCAL_MODULE_STEM_32 := app_process32
LOCAL_MODULE_STEM_64 := app_process64
include $(BUILD_EXECUTABLE)
# Create a symlink from app_process to app_process32 or 64
# depending on the target configuration.
include $(BUILD_SYSTEM)/executable_prefer_symlink.mk
# Build a variant of app_process binary linked with ASan runtime.
# ARM-only at the moment.
ifeq ($(TARGET_ARCH),arm)其涉及的主要文件是app_main.cpp;
/** \frameworks\base\cmds\app_process\app_main.cpp **/
int main(int argc, char* const argv[])
{
......
// 注意runtime是AppRuntime对象
AppRuntime runtime(argv[0], computeArgBlockSize(argc, argv));
/** 中间一系列代码用以解析传入参数argv **/
++i;
/*** 这些参数正是在脚本中传入的Arguments**/
while (i < argc) {
const char* arg = argv[i++];
if (strcmp(arg, "--zygote") == 0) {
zygote = true;
/* 注意这里对名字进行了修改,原来的名称为app_process,这里更改为zygote
* @value static const char ZYGOTE_NICE_NAME[] = "zygote";*/
niceName = ZYGOTE_NICE_NAME;
} else if (strcmp(arg, "--start-system-server") == 0) {
startSystemServer = true;
} else if (strcmp(arg, "--application") == 0) {
application = true;
} else if (strncmp(arg, "--nice-name=", 12) == 0) {
niceName.setTo(arg + 12);
} else if (strncmp(arg, "--", 2) != 0) {
className.setTo(arg);
break;
} else {
--i;
break;
}
}
......
/** 传进来的参数 --zygote,则zygote值为true **/
if (zygote) {
// 下面接下来分析AppRuntime类的start方法时如何启动
runtime.start("com.android.internal.os.ZygoteInit", args);
} elseif (className) {
runtime.start("com.android.internal.os.RuntimeInit", args);
} else {
....
return 10;
}
}
/*
* 这里启动Android Runtime.它实现了启动虚拟机,同时通过"className"来调用
* 该类中的main函数("static void main(String[] args)");
*/
/* \frameworks\base\core\jni\AndroidRuntime.cpp */
void AndroidRuntime::start(const char* className, const Vector<String8>& options)
{
static const String8 startSystemServer("start-system-server");
/*
* 'startSystemServer == true' means runtime is obsolete and not run from
* init.rc anymore, so we print out the boot start event here.
*/
for (size_t i = 0; i < options.size(); ++i) {
if (options[i] == startSystemServer) {
const int LOG_BOOT_PROGRESS_START = 3000;
LOG_EVENT_LONG(LOG_BOOT_PROGRESS_START, ns2ms(systemTime(SYSTEM_TIME_MONOTONIC)));
}
}
// 设置环境变量ANDROID_ROOT为/system
const char* rootDir = getenv("ANDROID_ROOT");
if (rootDir == NULL) {
rootDir = "/system";
if (!hasDir("/system")) {
LOG_FATAL("No root directory specified, and /android does not exist.");
return;
}
setenv("ANDROID_ROOT", rootDir, 1);
}
/*******开启虚拟机 **********/
JniInvocation jni_invocation;
jni_invocation.Init(NULL);
JNIEnv* env;
if (startVm(&mJavaVM, &env) != 0) {
return;
}
onVmCreated(env);
/*
* Register android functions.
*/
if (startReg(env) < 0) {
ALOGE("Unable to register all android natives\n");
return;
}
/*
* 调用main函数
* We want to call main() with a String array with arguments in it.
* At present we have two arguments, the class name and an option string.
* Create an array to hold them.
*/
jclass stringClass;
jobjectArray strArray;
jstring classNameStr;
stringClass = env->FindClass("java/lang/String");
assert(stringClass != NULL);
strArray = env->NewObjectArray(options.size() + 1, stringClass, NULL);
assert(strArray != NULL);
classNameStr = env->NewStringUTF(className);
assert(classNameStr != NULL);
env->SetObjectArrayElement(strArray, 0, classNameStr);
for (size_t i = 0; i < options.size(); ++i) {
jstring optionsStr = env->NewStringUTF(options.itemAt(i).string());
assert(optionsStr != NULL);
env->SetObjectArrayElement(strArray, i + 1, optionsStr);
}
/*
* 启动VM虚拟机,而且这个线程也会变成VM的主线程,当VM退出前该线程都不会退出
*/
char* slashClassName = toSlashClassName(className);
jclass startClass = env->FindClass(slashClassName);
if (startClass == NULL) {
ALOGE("JavaVM unable to locate class '%s'\n", slashClassName);
/* keep going */
} else {
jmethodID startMeth = env->GetStaticMethodID(startClass, "main",
"([Ljava/lang/String;)V");
if (startMeth == NULL) {
ALOGE("JavaVM unable to find main() in '%s'\n", className);
/* keep going */
} else {
env->CallStaticVoidMethod(startClass, startMeth, strArray);
#if 0
if (env->ExceptionCheck())
threadExitUncaughtException(env);
#endif
}
}
free(slashClassName);
ALOGD("Shutting down VM\n");
if (mJavaVM->DetachCurrentThread() != JNI_OK)
ALOGW("Warning: unable to detach main thread\n");
if (mJavaVM->DestroyJavaVM() != 0)
ALOGW("Warning: VM did not shut down cleanly\n");
} 可以看到这里开启了VM虚拟机,使得Zygote进程最终也运行在虚拟机上,进而去预装载各种系统类,启动SystemServer(这是大部分Android系统服务的所在地),而逐步建立起各种SystemServer的运行环境。
/**
* @path: \frameworks\base\core\java\com\android\internal\os\ZygoteInit.java
* Prepare the arguments and fork for the system server process.
*/
private static boolean startSystemServer(String abiList, String socketName)
throws MethodAndArgsCaller, RuntimeException {
......
/* 相关参数 */
String args[] = {
"--setuid=1000",
"--setgid=1000",
"--setgroups=1001,1002,1003,1004,1005,1006,1007,1008,1009,1010,1018,1032,3001,3002,3003,3006,3007",
"--capabilities=" + capabilities + "," + capabilities,
"--runtime-init",
"--nice-name=system_server",
"com.android.server.SystemServer",// 这里是SystemServer所在的包的完整名称
};
ZygoteConnection.Arguments parsedArgs = null;
int pid;
try {
parsedArgs = new ZygoteConnection.Arguments(args);
ZygoteConnection.applyDebuggerSystemProperty(parsedArgs);
ZygoteConnection.applyInvokeWithSystemProperty(parsedArgs);
/* 通过此函数来生成一个新的进程,用以承载各种系统服务,
* 该函数最终实现调用底层的fork来产生一个新的进程 */
pid = Zygote.forkSystemServer(
parsedArgs.uid, parsedArgs.gid,
parsedArgs.gids,
parsedArgs.debugFlags,
null,
parsedArgs.permittedCapabilities,
parsedArgs.effectiveCapabilities);
} catch (IllegalArgumentException ex) {
thrownew RuntimeException(ex);
}
/* pid=0 表示为子进程 */
if (pid == 0) {
if (hasSecondZygote(abiList)) {
waitForSecondaryZygote(socketName);
}
// 子进程即是SystemServer所在进程,通过调用该函数来实现启动各种系统服务
handleSystemServerProcess(parsedArgs);
}
return true;
} 在代码中通过调用forkSystemServer来fork一个新的子进程,用来承载各种服务,进而调用handleSystemServerProcess函数来启动各种系统服务。
版权声明:本文为博主原创文章,未经博主允许不得转载。
Android启动过程——init.rc,Zygote,SystemServer
标签:
原文地址:http://blog.csdn.net/woliuyunyicai/article/details/47749009