Android 9,2024最新Android笔经

if (!DoFirstStageMount()) {

LOG(FATAL) << “Failed to mount required partitions early …”;

}

SetInitAvbVersionInRecovery();

// Enable seccomp if global boot option was passed (otherwise it is enabled in zygote).

global_seccomp();

// 优先加载selinux log系统, 紧接着初始化selinux

SelinuxSetupKernelLogging();

SelinuxInitialize();

// 添加 selinux 是否启动成功的log

if (selinux_android_restorecon(“/init”, 0) == -1) {

PLOG(FATAL) << “restorecon failed of /init failed”;

}

setenv(“INIT_SECOND_STAGE”, “true”, 1);

static constexpr uint32_t kNanosecondsPerMillisecond = 1e6;

uint64_t start_ms = start_time.time_since_epoch().count() / kNanosecondsPerMillisecond;

setenv(“INIT_STARTED_AT”, std::to_string(start_ms).c_str(), 1);

char* path = argv[0];

char* args[] = { path, nullptr };

execv(path, args);

// execv() only returns if an error happened, in which case we

// panic and never fall through this conditional.

PLOG(FATAL) << “execv(”" << path << “”) failed";

}

//启动第二阶段

InitKernelLogging(argv);

LOG(INFO) << “init second stage started!”;

// Set up a session keyring that all processes will have access to. It

// will hold things like FBE encryption keys. No process should override

// its session keyring.

keyctl_get_keyring_ID(KEY_SPEC_SESSION_KEYRING, 1);

// Indicate that booting is in progress to background fw loaders, etc.

close(open(“/dev/.booting”, O_WRONLY | O_CREAT | O_CLOEXEC, 0000));

//初始化属性

property_init();

// If arguments are passed both on the command line and in DT,

// properties set in DT always have priority over the command-line ones.

process_kernel_dt();

process_kernel_cmdline();

// Propagate the kernel variables to internal variables

// used by init as well as the current required properties.

export_kernel_boot_props();

// Make the time that init started available for bootstat to log.

property_set(“ro.boottime.init”, getenv(“INIT_STARTED_AT”));

property_set(“ro.boottime.init.selinux”, getenv(“INIT_SELINUX_TOOK”));

// Set libavb version for Framework-only OTA match in Treble build.

const char* avb_version = getenv(“INIT_AVB_VERSION”);

if (avb_version) property_set(“ro.boot.avb_version”, avb_version);

// 清空设置的环境变量

unsetenv(“INIT_SECOND_STAGE”);

unsetenv(“INIT_STARTED_AT”);

unsetenv(“INIT_SELINUX_TOOK”);

unsetenv(“INIT_AVB_VERSION”);

// 设置第二阶段的selinux

SelinuxSetupKernelLogging();

SelabelInitialize();

SelinuxRestoreContext();

//创建 epoll 句柄

epoll_fd = epoll_create1(EPOLL_CLOEXEC);

if (epoll_fd == -1) {

PLOG(FATAL) << “epoll_create1 failed”;

}

//设置 子进程处理函数

sigchld_handler_init();

if (!IsRebootCapable()) {

// If init does not have the CAP_SYS_BOOT capability, it is running in a container.

// In that case, receiving SIGTERM will cause the system to shut down.

InstallSigtermHandler();

}

LoadRscRoProps();

property_load_boot_defaults();

export_oem_lock_status();

//启动属性服务

start_property_service();

//为USB存储设置udc Contorller, sys/class/udc

set_usb_controller();

const BuiltinFunctionMap function_map;

Action::set_function_map(&function_map);

subcontexts = InitializeSubcontexts();

ActionManager& am = ActionManager::GetInstance();

ServiceList& sm = ServiceList::GetInstance();

LoadBootScripts(am, sm);

// Turning this on and letting the INFO logging be discarded adds 0.2s to

// Nexus 9 boot time, so it’s disabled by default.

if (false) DumpState();

am.QueueEventTrigger(“early-init”);

// Queue an action that waits for coldboot done so we know ueventd has set up all of /dev…

am.QueueBuiltinAction(wait_for_coldboot_done_action, “wait_for_coldboot_done”);

// … so that we can start queuing up actions that require stuff from /dev.

am.QueueBuiltinAction(MixHwrngIntoLinuxRngAction, “MixHwrngIntoLinuxRng”);

am.QueueBuiltinAction(SetMmapRndBitsAction, “SetMmapRndBits”);

am.QueueBuiltinAction(SetKptrRestrictAction, “SetKptrRestrict”);

am.QueueBuiltinAction(keychord_init_action, “keychord_init”);

am.QueueBuiltinAction(console_init_action, “console_init”);

// Trigger all the boot actions to get us started.

am.QueueEventTrigger(“init”);

// Repeat mix_hwrng_into_linux_rng in case /dev/hw_random or /dev/random

// wasn’t ready immediately after wait_for_coldboot_done

am.QueueBuiltinAction(MixHwrngIntoLinuxRngAction, “MixHwrngIntoLinuxRng”);

// Don’t mount filesystems or start core system services in charger mode.

std::string bootmode = GetProperty(“ro.bootmode”, “”);

if (bootmode == “charger”) {

am.QueueEventTrigger(“charger”);

} else {

am.QueueEventTrigger(“late-init”);

}

// Run all property triggers based on current state of the properties.

am.QueueBuiltinAction(queue_property_triggers_action, “queue_property_triggers”);

while (true) {

// By default, sleep until something happens.

int epoll_timeout_ms = -1;

if (do_shutdown && !shutting_down) {

do_shutdown = false;

if (HandlePowerctlMessage(shutdown_command)) {

shutting_down = true;

}

}

if (!(waiting_for_prop || Service::is_exec_service_running())) {

am.ExecuteOneCommand();

}

if (!(waiting_for_prop || Service::is_exec_service_running())) {

if (!shutting_down) {

auto next_process_restart_time = RestartProcesses();

// If there’s a process that needs restarting, wake up in time for that.

if (next_process_restart_time) {

epoll_timeout_ms = std::chrono::ceilstd::chrono::milliseconds(

*next_process_restart_time - boot_clock::now())

.count();

if (epoll_timeout_ms < 0) epoll_timeout_ms = 0;

}

}

// If there’s more work to do, wake up again immediately.

if (am.HasMoreCommands()) epoll_timeout_ms = 0;

}

epoll_event ev;

int nr = TEMP_FAILURE_RETRY(epoll_wait(epoll_fd, &ev, 1, epoll_timeout_ms));

if (nr == -1) {

PLOG(ERROR) << “epoll_wait failed”;

} else if (nr == 1) {

((void (*)()) ev.data.ptr)();

}

}

return 0;

}

6.基于MTK 平台 init.cpp 源码分析


640?wx_fmt=other

基于MTK 平台 init.cpp 主要作用

四、 init 启动脚本分析

==============

init.rc 路径 一般在system/core/rootdir下,init脚本是有Android 初始化语言编写。

1.Android Init Language 语句类型


  • 1.Action

  • 2.Command

  • 3.Service

  • 4.Option

  • 5.Import

640?wx_fmt=other

init 进程分析

640?wx_fmt=other

init.rc on

640?wx_fmt=other

init.rc services

640?wx_fmt=other

init.rc import

五、init 进程分析

===========

640?wx_fmt=other

init 进程分析

640?wx_fmt=other

init 解析脚本分析

640?wx_fmt=other

init 事件列表

640?wx_fmt=other

init 事件结构

六 、init 脚本执行

============

640?wx_fmt=other

init 进程解析和执行

640?wx_fmt=other

启动脚本解析结果

640?wx_fmt=other

整理事件列表

640?wx_fmt=other

init 构建事件

640?wx_fmt=other

Service 事件分类

640?wx_fmt=other

init 进程执行命令和启动服务

七、init 进程守护

===========

init进程处理消息事件

  1. 根据Shell或者系统中消息设置系统prop

  2. 守护系统服务,如果服务退出,重启退出的服务。

640?wx_fmt=other

init守护进程

640?wx_fmt=other

init 处理 prop 消息分析

640?wx_fmt=other

init 守护服务分析

八、init rc 脚本启动Zygote

====================

Zygote 的 classname 为main.init.rc文件配置代码如下:

… …

on nonencrypted

class_start main

class_start late_start

on property:sys.init_log_level=*

loglevel ${sys.init_log_level}

… …

九、启动分析小结

========

学习福利

【Android 详细知识点思维脑图(技能树)】

其实Android开发的知识点就那么多,面试问来问去还是那么点东西。所以面试没有其他的诀窍,只看你对这些知识点准备的充分程度。so,出去面试时先看看自己复习到了哪个阶段就好。

虽然 Android 没有前几年火热了,已经过去了会四大组件就能找到高薪职位的时代了。这只能说明 Android 中级以下的岗位饱和了,现在高级工程师还是比较缺少的,很多高级职位给的薪资真的特别高(钱多也不一定能找到合适的),所以努力让自己成为高级工程师才是最重要的。

这里附上上述的面试题相关的几十套字节跳动,京东,小米,腾讯、头条、阿里、美团等公司19年的面试题。把技术点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节。

由于篇幅有限,这里以图片的形式给大家展示一小部分。

详细整理在GitHub可以见;

Android架构视频+BAT面试专题PDF+学习笔记​

roid开发的知识点就那么多,面试问来问去还是那么点东西。所以面试没有其他的诀窍,只看你对这些知识点准备的充分程度。so,出去面试时先看看自己复习到了哪个阶段就好。

虽然 Android 没有前几年火热了,已经过去了会四大组件就能找到高薪职位的时代了。这只能说明 Android 中级以下的岗位饱和了,现在高级工程师还是比较缺少的,很多高级职位给的薪资真的特别高(钱多也不一定能找到合适的),所以努力让自己成为高级工程师才是最重要的。

这里附上上述的面试题相关的几十套字节跳动,京东,小米,腾讯、头条、阿里、美团等公司19年的面试题。把技术点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节。

由于篇幅有限,这里以图片的形式给大家展示一小部分。

[外链图片转存中…(img-foJrHPRt-1727036314700)]

详细整理在GitHub可以见;

Android架构视频+BAT面试专题PDF+学习笔记​

网上学习 Android的资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。希望这份系统化的技术体系对大家有一个方向参考。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值