Android启动以后第一个执行的是init程序,init是一个守护进程,路径为:/system/core/init/init.c

从代码里面可以看出,它包含设备管理,解析init.rc和init.xxx.rc初始化脚本,执行启动脚本中的基本功能和各种服务。

int main(int argc, char **argv)
{
.......

/* Get the basic filesystem setup we need put
* together in the initramdisk on / and then we'll
* let the rc file figure out the rest.
*/
mkdir("/dev", 0755);
mkdir("/proc", 0755);
mkdir("/sys", 0755);

mount("tmpfs", "/dev", "tmpfs", 0, "mode=0755");
mkdir("/dev/pts", 0755);
mkdir("/dev/socket", 0755);
mount("devpts", "/dev/pts", "devpts", 0, NULL);
mount("proc", "/proc", "proc", 0, NULL);
mount("sysfs", "/sys", "sysfs", 0, NULL);

/* We must have some place other than / to create the
* device nodes for kmsg and null, otherwise we won't
* be able to remount / read-only later on.
* Now that tmpfs is mounted on /dev, we can actually
* talk to the outside world.
*/
open_devnull_stdio();
log_init();

INFO("reading config file/n");
parse_config_file("/init.rc");

/* pull the kernel commandline and ramdisk properties file in */
qemu_init();
import_kernel_cmdline(0);

get_hardware_name();
snprintf(tmp, sizeof(tmp), "/init.%s.rc", hardware);
parse_config_file(tmp);

action_for_each_trigger("early-init", action_add_queue_tail);
drain_action_queue();

INFO("device init/n");
device_fd = device_init();

property_init();

// only listen for keychords if ro.debuggable is true
debuggable = property_get("ro.debuggable");
if (debuggable && !strcmp(debuggable, "1")) {
keychord_fd = open_keychord();
}

if (console[0]) {
snprintf(tmp, sizeof(tmp), "/dev/%s", console);
console_name = strdup(tmp);
}

fd = open(console_name, O_RDWR);
if (fd >= 0)
have_console = 1;
close(fd);


.......

property_set("ro.serialno", serialno[0] ? serialno : "");
property_set("ro.bootmode", bootmode[0] ? bootmode : "unknown");
property_set("ro.baseband", baseband[0] ? baseband : "unknown");
property_set("ro.carrier", carrier[0] ? carrier : "unknown");
property_set("ro.bootloader", bootloader[0] ? bootloader : "unknown");

property_set("ro.hardware", hardware);
snprintf(tmp, PROP_VALUE_MAX, "%d", revision);
property_set("ro.revision", tmp);

/* execute all the boot actions to get us started */
action_for_each_trigger("init", action_add_queue_tail);
drain_action_queue();

/* read any property files on system or data and
* fire up the property service. This must happen
* after the ro.foo properties are set above so
* that /data/local.prop cannot interfere with them.
*/
property_set_fd = start_property_service( );

.......

if (ufds[0].revents == POLLIN)
handle_device_fd (device_fd);

if (ufds[1].revents == POLLIN)
handle_property_set_fd (property_set_fd);
if (ufds[3].revents == POLLIN)
handle_keychord(keychord_fd);
}

return 0;
}

Parser.c:parse_config_file(), init.rc, init.xxx.rc

Device.c:device_init(), handle_device_fd()

Property_Service.c:property_init(), start_property_service(), handle_property_set_fd()

init.rc中启动servicemanager,zygote, mediaserver等服务。

servicemanager是Binder的服务管理守护进程,也是Binder机制的核心,所有Binder都会通过它进行注册,然后客户端再通过其获取服务接口,具体可以参考Service_manager.c

流程:打开Binder驱动->成为服务管理进程->进入binder_loop等待访问请求。

Zygote是Android Java部分的孵化器,也就是Android java框架的基础,它首先孵化system_server,system_server是大多数系统服务的守护进程。Zygote在启动后分列为子进程和父进程,子进程孵化system_server。父进程进入孵化状态。

system_server启动系统服务

SystemServer.java

class ServerThread extends Thread {

.......

@Override
public void run() {
EventLog.writeEvent(LOG_BOOT_PROGRESS_SYSTEM_RUN,
SystemClock.uptimeMillis());

ActivityManagerService.prepareTraceFile(false); // create dir

Looper.prepare();

android.os.Process.setThreadPriority(
android.os.Process.THREAD_PRIORITY_FOREGROUND);

String factoryTestStr = SystemProperties.get("ro.factorytest");
int factoryTest = "".equals(factoryTestStr) ? SystemServer.FACTORY_TEST_OFF
: Integer.parseInt(factoryTestStr);

......

// Critical services...
try {
Log.i(TAG, "Entropy Service");
ServiceManager.addService("entropy", new EntropyService());

Log.i(TAG, "Power Manager");
power = new PowerManagerService();
ServiceManager.addService(Context.POWER_SERVICE, power);

Log.i(TAG, "Activity Manager");
context = ActivityManagerService.main(factoryTest);

Log.i(TAG, "Telephony Registry");
ServiceManager.addService("telephony.registry", new TelephonyRegistry(context));

AttributeCache.init(context);

Log.i(TAG, "Package Manager");
pm = PackageManagerService.main(context,
factoryTest != SystemServer.FACTORY_TEST_OFF);

ActivityManagerService.setSystemProcess();

mContentResolver = context.getContentResolver();

// The AccountManager must come before the ContentService
try {
Log.i(TAG, "Account Manager");
ServiceManager.addService(Context.ACCOUNT_SERVICE,
new AccountManagerService(context));
} catch (Throwable e) {
Log.e(TAG, "Failure starting Account Manager", e);
}

Log.i(TAG, "Content Manager");
ContentService.main(context,
factoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL);

Log.i(TAG, "System Content Providers");
ActivityManagerService.installSystemProviders();

Log.i(TAG, "Battery Service");
battery = new BatteryService(context);
ServiceManager.addService("battery", battery);
.......

try {
Log.i(TAG, "Clipboard Service");
ServiceManager.addService("clipboard", new ClipboardService(context));
} catch (Throwable e) {
Log.e(TAG, "Failure starting Clipboard Service", e);
}

try {
Log.i(TAG, "Input Method Service");
imm = new InputMethodManagerService(context, statusBar);
ServiceManager.addService(Context.INPUT_METHOD_SERVICE, imm);
} catch (Throwable e) {
Log.e(TAG, "Failure starting Input Manager Service", e);
}

try {
Log.i(TAG, "NetStat Service");
ServiceManager.addService("netstat", new NetStatService(context));
} catch (Throwable e) {
Log.e(TAG, "Failure starting NetStat Service", e);
}

try {
Log.i(TAG, "Connectivity Service");
connectivity = ConnectivityService.getInstance(context);
ServiceManager.addService(Context.CONNECTIVITY_SERVICE, connectivity);
} catch (Throwable e) {
Log.e(TAG, "Failure starting Connectivity Service", e);
}

.......

}

更多相关文章

  1. 一款霸榜 GitHub 的开源 Linux 资源监视器!
  2. Application Fundamentals--Processes and lifecycles(进程生命周
  3. 关于android创建快捷方式会启动两个应用的问题(一)
  4. Android(安卓)Studio 3.3配置Butterknife处理
  5. Android(安卓)5个进程等级
  6. android 开发adb server is out of date 解决方案
  7. android用webview加载H5页面出现点击事件失效的问题解决
  8. 怎么解决这个问题“The connection to adb is down, and a sever
  9. android L 的开机动画流程

随机推荐

  1. Android中 备份短信 还原短信
  2. Android(安卓)kotlin上传头像实现
  3. Android设备管理器漏洞
  4. android背景选择器selector用法汇总
  5. 使用 IntelliJ Debug Android(安卓)源码
  6. Android:控件GridView的使用
  7. 【Android(安卓)开发】:Android五种布局的
  8. 进程博客纳入
  9. Android应用的LinearLayout中嵌套Relativ
  10. Android(安卓)AVD启动失败