{"id":13738,"date":"2024-06-13T09:42:44","date_gmt":"2024-06-13T01:42:44","guid":{"rendered":"http:\/\/xinyiworld.top\/wordpress_it\/?p=13738"},"modified":"2024-08-06T11:04:08","modified_gmt":"2024-08-06T03:04:08","slug":"android10-init%e7%a8%8b%e5%ba%8f","status":"publish","type":"post","link":"http:\/\/xinyiworld.top\/wordpress_it\/?p=13738","title":{"rendered":"android10 init\u7a0b\u5e8f"},"content":{"rendered":"<p><a href=\"https:\/\/www.cnblogs.com\/littleboy123\/p\/13208179.html\">https:\/\/www.cnblogs.com\/littleboy123\/p\/13208179.html<\/a><br \/>\n<a href=\"https:\/\/www.jianshu.com\/p\/0a8bfc073090\">https:\/\/www.jianshu.com\/p\/0a8bfc073090<\/a><\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_16 counter-hierarchy counter-decimal ez-toc-grey\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\">\u76ee\u5f55<\/p>\n<span class=\"ez-toc-title-toggle\"><a class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" style=\"display: none;\"><i class=\"ez-toc-glyphicon ez-toc-icon-toggle\"><\/i><\/a><\/span><\/div>\n<nav><ul class=\"ez-toc-list ez-toc-list-level-1\"><li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-1\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=13738\/#%E4%B8%80%E3%80%81init%E7%A8%8B%E5%BA%8F%E5%9F%BA%E6%9C%AC%E8%AF%B4%E6%98%8E\" title=\"\u4e00\u3001init\u7a0b\u5e8f\u57fa\u672c\u8bf4\u660e\">\u4e00\u3001init\u7a0b\u5e8f\u57fa\u672c\u8bf4\u660e<\/a><ul class=\"ez-toc-list-level-3\"><li class=\"ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-2\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=13738\/#1first_init\" title=\"1.first init\">1.first init<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-3\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=13738\/#2second_init\" title=\"2.second init\">2.second init<\/a><ul class=\"ez-toc-list-level-4\"><li class=\"ez-toc-heading-level-4\"><a class=\"ez-toc-link ez-toc-heading-4\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=13738\/#2-1SetupSelinux\" title=\"2-1)SetupSelinux\">2-1)SetupSelinux<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-4\"><a class=\"ez-toc-link ez-toc-heading-5\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=13738\/#2-2SecondStageMain\" title=\"2-2)SecondStageMain\">2-2)SecondStageMain<\/a><ul class=\"ez-toc-list-level-5\"><li class=\"ez-toc-heading-level-5\"><a class=\"ez-toc-link ez-toc-heading-6\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=13738\/#2-2-1%EF%BC%89InstallSignalFdHandler\" title=\"2-2-1\uff09InstallSignalFdHandler\">2-2-1\uff09InstallSignalFdHandler<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-5\"><a class=\"ez-toc-link ez-toc-heading-7\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=13738\/#2-2-2%EF%BC%89property_load_boot_defaults\" title=\"2-2-2\uff09property_load_boot_defaults()\">2-2-2\uff09property_load_boot_defaults()<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-5\"><a class=\"ez-toc-link ez-toc-heading-8\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=13738\/#2-2-3%EF%BC%89LoadBootScripts\" title=\"2-2-3\uff09LoadBootScripts\">2-2-3\uff09LoadBootScripts<\/a><\/li><\/ul><\/li><\/ul><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"%E4%B8%80%E3%80%81init%E7%A8%8B%E5%BA%8F%E5%9F%BA%E6%9C%AC%E8%AF%B4%E6%98%8E\"><\/span>\u4e00\u3001init\u7a0b\u5e8f\u57fa\u672c\u8bf4\u660e<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u6240\u8c13\u7684init\u7a0b\u5e8f\u67092\u4e2a\uff0c\u4e00\u4e2a\u662f\u6839\u6587\u4ef6\u7cfb\u7edf\u6839\u76ee\u5f55\u4e0b\u7684init\uff0c\u4e00\u4e2a\u662f\/system\/bin\/init\uff0c\u6211\u4eec\u4e60\u60ef\u628a\u8fd9\u4e24\u4e2ainit\u5206\u522b\u79f0\u4e3a<code>first init<\/code>\u4e0e<code>second init<\/code>.\u5185\u6838\u9ed8\u8ba4\u6267\u884c<code>first init<\/code>\uff0c<code>first init<\/code>\u6700\u7ec8\u4f1a\u6267\u884c<code>second init<\/code>.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"1first_init\"><\/span>1.first init<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>\u5176main\u51fd\u6570\u7c7b\u4e3afirst_stage_main.cpp\uff0c\u7f16\u8bd1\u76ee\u6807\u4f4d\u4e8eAndroid.mk\u3002<\/p>\n<pre><code class=\"language-bash\">LOCAL_MODULE := init_first_stage\nLOCAL_MODULE_STEM := init\n\nLOCAL_FORCE_STATIC_EXECUTABLE := true<\/code><\/pre>\n<p>\u4e3a\u9759\u6001\u94fe\u63a5\u6267\u884c\u6587\u4ef6<br \/>\nfirst_stage_main.cpp\u7684\u6e90\u7801\u975e\u5e38\u7b80\u5355\uff0c\u4f1a\u8c03\u7528first_stage_init.cpp\u91cc\u7684FirstStageMain\u65b9\u6cd5\u3002<\/p>\n<p><strong>FirstStageMain\u65b9\u6cd5\u4e3b\u8981\u5de5\u4f5c<\/strong>\uff1a<br \/>\n1\uff09\u6302\u8f7d\u4e00\u4e9b\u6587\u4ef6\u7cfb\u7edf<br \/>\n2\uff09\u521b\u5efa\u4e00\u4e9b\u8bbe\u5907\u8282\u70b9<\/p>\n<p>FirstStageMain\u6700\u7ec8\u4f1a\u6267\u884csecond first\uff0c\u4f1a\u643a\u5e26selinux_setup\u53c2\u6570\u3002<\/p>\n<pre><code class=\"language-bash\">    const char* path = &quot;\/system\/bin\/init&quot;;\n    const char* args[] = {path, &quot;selinux_setup&quot;, nullptr};\n    execv(path, const_cast&lt;char**&gt;(args));<\/code><\/pre>\n<p>first init \u6700\u7ec8\u4f1a\u751f\u6210\u5728<code>out\/target\/product\/kona\/ramdisk\/<\/code>\u4e2d\uff0c\u5e76\u6253\u5305\u5230ramdisk.img\u4e2d\u3002<\/p>\n<p>\u5728\u7f16\u8bd1boog.img\u65f6\uff0c\u5982\u679cfirst init\u7684\u6e90\u7801\u6709\u6539\u52a8\uff0c\u4f1a\u81ea\u52a8\u7f16\u8bd1\u3002<\/p>\n<h3><span class=\"ez-toc-section\" id=\"2second_init\"><\/span>2.second init<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre><code class=\"language-c\">int main(int argc, char** argv) {\n    #if __has_feature(address_sanitizer)\n        __asan_set_error_report_callback(AsanReportCallback);\n    #endif\n\n    if (!strcmp(basename(argv[0]), &quot;ueventd&quot;)) {\n        return ueventd_main(argc, argv);\n    }\n\n    if (argc &gt; 1) {\n        if (!strcmp(argv[1], &quot;subcontext&quot;)) {\n            android::base::InitLogging(argv, &amp;android::base::KernelLogger);\n            const BuiltinFunctionMap function_map;\n\n            return SubcontextMain(argc, argv, &amp;function_map);\n        }\n\n        if (!strcmp(argv[1], &quot;selinux_setup&quot;)) {\n            return SetupSelinux(argv);\n        }\n\n        if (!strcmp(argv[1], &quot;second_stage&quot;)) {\n            return SecondStageMain(argc, argv);\n        }\n    }\n\n    return FirstStageMain(argc, argv);\n}<\/code><\/pre>\n<h4><span class=\"ez-toc-section\" id=\"2-1SetupSelinux\"><\/span>2-1)SetupSelinux<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>\u5b9e\u73b0\u7c7b\u4e3a<code>system\/core\/init\/selinux.cpp<\/code><\/p>\n<pre><code class=\"language-c\">\/\/ This function initializes SELinux then execs init to run in the init SELinux context.\nint SetupSelinux(char** argv) {\n    InitKernelLogging(argv);\n\n    if (REBOOT_BOOTLOADER_ON_PANIC) {\n        InstallRebootSignalHandlers();\n    }\n\n    \/\/ Set up SELinux, loading the SELinux policy.\n    SelinuxSetupKernelLogging();\n    SelinuxInitialize();\n\n    \/\/ We&#039;re in the kernel domain and want to transition to the init domain.  File systems that\n    \/\/ store SELabels in their xattrs, such as ext4 do not need an explicit restorecon here,\n    \/\/ but other file systems do.  In particular, this is needed for ramdisks such as the\n    \/\/ recovery image for A\/B devices.\n    if (selinux_android_restorecon(&quot;\/system\/bin\/init&quot;, 0) == -1) {\n        PLOG(FATAL) &lt;&lt; &quot;restorecon failed of \/system\/bin\/init failed&quot;;\n    }\n\n    const char* path = &quot;\/system\/bin\/init&quot;;\n    const char* args[] = {path, &quot;second_stage&quot;, nullptr};\n    execv(path, const_cast&lt;char**&gt;(args));\n\n    \/\/ execv() only returns if an error happened, in which case we\n    \/\/ panic and never return from this function.\n    PLOG(FATAL) &lt;&lt; &quot;execv(\\&quot;&quot; &lt;&lt; path &lt;&lt; &quot;\\&quot;) failed&quot;;\n\n    return 1;\n}<\/code><\/pre>\n<p>\u6839\u636e\u6e90\u7801\u53ef\u77e5SetupSelinux\u65b9\u6cd5\u4f1a\u518d\u6b21\u8c03\u7528second init\u5e76\u643a\u5e26\u53c2\u6570<br \/>\nsecond_stage<\/p>\n<h4><span class=\"ez-toc-section\" id=\"2-2SecondStageMain\"><\/span>2-2)SecondStageMain<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>\u5b9e\u73b0\u7c7b\u4e3a<code>system\/core\/init\/init.cpp<\/code><br \/>\nSecondStageMain\u7684\u6e90\u7801\u8ffd\u8e2a\uff1a<\/p>\n<h5><span class=\"ez-toc-section\" id=\"2-2-1%EF%BC%89InstallSignalFdHandler\"><\/span>2-2-1\uff09InstallSignalFdHandler<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<p>\u76d1\u542c\u5b50\u8fdb\u7a0b,\u8fdb\u884c\u5904\u7406\uff08\u56de\u6536\u3001\u91cd\u542f\uff09\u3002<br \/>\n<a href=\"https:\/\/blog.csdn.net\/weixin_49274713\/article\/details\/130485247\">https:\/\/blog.csdn.net\/weixin_49274713\/article\/details\/130485247<\/a><\/p>\n<p><a href=\"https:\/\/blog.csdn.net\/qq_28261343\/article\/details\/128280652\">https:\/\/blog.csdn.net\/qq_28261343\/article\/details\/128280652<\/a><\/p>\n<h5><span class=\"ez-toc-section\" id=\"2-2-2%EF%BC%89property_load_boot_defaults\"><\/span>2-2-2\uff09property_load_boot_defaults()<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<p>\u4ece\u5404\u79cdbuild.prop\u6587\u4ef6\u4e2d\u8bfb\u53d6\u5c5e\u6027<\/p>\n<h5><span class=\"ez-toc-section\" id=\"2-2-3%EF%BC%89LoadBootScripts\"><\/span>2-2-3\uff09LoadBootScripts<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<p>\u89e3\u6790\u5404\u79cd\u548c\u5f00\u673a\u542f\u52a8\u76f8\u5173\u7684.rc\u6587\u4ef6<\/p>\n<button class=\"simplefavorite-button\" data-postid=\"13738\" data-siteid=\"1\" data-groupid=\"1\" data-favoritecount=\"0\" style=\"\">\u6536\u85cf <i class=\"sf-icon-star-empty\"><\/i><\/button>","protected":false},"excerpt":{"rendered":"<p>https:\/\/www.cnblogs.com\/littleboy123\/p\/13208179.ht [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1046],"tags":[],"_links":{"self":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/13738"}],"collection":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=13738"}],"version-history":[{"count":24,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/13738\/revisions"}],"predecessor-version":[{"id":14246,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/13738\/revisions\/14246"}],"wp:attachment":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=13738"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=13738"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=13738"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}