下面是英文原版,如果访问慢,也可以直接查看这里

https://wiki.openwrt.org/doc/techref/init.detail.cc

Init (User space boot) reference for Chaos Calmer: procd

Analysis of how the user space part of the boot sequence is implemented in OpenWrt, Chaos Calmer release.

Procd replaces init

On a fully booted Chaos Calmer system, pid 1 is /sbin/procd:

root@openwrt:~# psPID USER       VSZ STAT COMMAND1 root      1440 S    /sbin/procd...

At boot, Linux kernel starts/sbin/initas the first user process. In Chaos Calmer,/sbin/initdoes the preinit/failsafe steps, those that depend only on the read-only partition in flashed image, then execs (that is: is replaced by)/sbin/procdto continue boot as specified by the configuration in writable flash partition. Procd started as pid 1 assumes several roles: service manager, hotplug events handler; this as of February 2016, when this research was done.Procd techref wiki pageat this point in time is a design document and work in progress, if you are reading here and know/understand procd's semantics andAPI, please update that page.

Procd sources:
http://git.openwrt.org/?p=project/procd.git;a=tree;hb=0da5bf2ff222d1a499172a6e09507388676b5a08
at the commit used to build the procd package in Chaos Calmer release:
PKG_SOURCE_VERSION:=0da5bf2ff222d1a499172a6e09507388676b5a08

/sbin/init source:
http://git.openwrt.org/?p=project/procd.git;a=blob;f=initd/init.c;hb=0da5bf2ff222d1a499172a6e09507388676b5a08#l71

Life and death of a Chaos Calmer system

This is the source code path followed in logical order of execution by the processor in user space while booting Chaos Calmer.

 All links to source repositories should show the code at the commit used in Chaos Calmer release.
 Pathnames evaluated at preinit time when / is read only have "(/rom)" prepended, to signify the path where the file is found on a fully booted system.

  1. main(int argc, char **argv)in /sbin/init, line 71
    User space life begins here. OpenWrt calls this phase "preinit".
    1. early()(definition)
      Mount filesystems:/proc,/sys,/sys/fs/cgroup,/dev(a tmpfs),/dev/pts
      Populate/devwith entries from/sys/dev/{char;block}
      Open/dev/consoleas STDIN/STDOUT/STDERR
      Make directories/tmp(optionally on zram),/tmp/run,tmp/lock,/tmp/state

      This accounts for most of the filesystem layout, observed that/etc/fstabis abroken symlink, line 161, with the following additions:
      -procd_coldplug()invoked at hotplug setup timewill recreate/devfrom scratch.
      -/etc/rc.d/S10bootwill invokemount_rootto setup a writable filesystem based on extroot or jffs2 overlay or a tmpfs backedsnapshot capableoverlay, add some directories and files, and mount debugfs.

    2. cmdline()(definition)
      Check kernel cmdline for boot parameter "init_debug={1,2,3,4}".
    3. Fork/sbin/kmodloader (/rom)/etc/modules-boot.d/kmodloader source
      Wait up to 120 seconds for/sbin/kmodloaderto probe the kernel modules declared in(/rom)/etc/modules-boot.d/
      At this point in the boot sequence, '/etc/modules-boot.d' is the one from the rom image (/rom/etc/…when boot is done). The overlay filesystem is mounted later.

      kmodloader is a multicall binary, invoked as
      kmodloader
      does
      main_loader()
      which reads files in(/rom)/etc/modules-boot.d/, looking for lines starting with the name of a module to load, optionally followed by a space and module parameters. There appear to bespecial treatment for files with names beginning with a number: the modules they list are immediately loaded, then modules from files with name beginning with an ascii char greater than "9" are loaded all together in a final load_modprobe call.

    4. uloop_init()line 116(definition)
      Documentation of libubox/uloop.hsays:
      Uloop is a loop runner for i/o. Gets in charge of polling the different file descriptors you have added to it, gets in charge of running timers, and helps you manage child processes. Supports epoll and kqueue as event running backends.
      uloop.c source in libuboxsays uloop's process management duty is assigned by a call to
      int uloop_process_add(struct uloop_process *p)
      p->pidis the process id of a child process to monitor andp->cba pointer to a callback function.
      When the managed child process will exit, uloop_run, running in parent context to receive SIGCHLD signal, will trigger execution of the callback.
    5. preinit()line 117(definition)
      1. Forks a "plugd instance", line 94
        /sbin/procd -h (/rom)/etc/hotplug-preinit.json
        to listen to kernel uevents for any required firmware or for notification of button pressed, handled by (/rom)/etc/rc.button/failsafe
        as the request to enter failsafe mode. A flag file/tmp/failsafe_buttoncontaining the value of${BUTTON}is created if failsafe has been requested.
      2. Forks,at lines 106-111,
        PREINIT=1 /bin/sh (/rom)/etc/preinit
        a shell to execute(/rom)/etc/preinitwithPREINIT=1in its environment. Submits the child process to uloop management with the callback
        spawn_procd()
        that will exec procd to replace init as pid 1 at completion of(/rom)/etc/preinit.
        1. /etc/preinit
          A shell script, fully documented herepreinit_operation. In short, parse files in(/rom)/lib/preinitto build 5 lists of hooks and an environment, then run the hooks from some of the lists depending on the state of the environment.
          One of the steps in a successful boot sequence is to mount the overlay file system with a hook setup by
          (/rom)/lib/preinit/80_mount_root
          to call
          mount_root
          which if extroot is not configured, mounts the writable data partition "rootfs_data" as overlay over the / partition "rootfs". If the data partition is being prepared, overlays a tmpfs in ram.
          Filesystem snapshots are supported; this is a feature listed in Barrier Breaker announce, shell wrapper is/sbin/snapshotscript. The "SNAPSHOT=magic" environment variable is set inmount_snapshot()line 330.
    6. uloop_run(), line 118
      At exit of the(/rom)/etc/preinitshell script, invokes the callback spawn_procd()
    7. spawn_procd()
      As a callback by uloop_run in pid 1, this is pid 1; execs/sbin/procd
  2. /sbin/procd
    Execed by pid 1/sbin/init,/sbin/procdreplaces it as pid 1.
    1. setsid(), line 67
      The process group ID and session ID of the calling process are set to the PID of the calling process: man 2 setsidSee alsoman 7 credentials.
    2. uloop_init(), line 68
      The uloop instance set up before by/sbin/initis gone. Creates a new one.
    3. procd_signal(), line 69(definition), line 82.
      Setup signal handlers. Reboot on SIGTERM or SIGINT, poweroff on SIGUSR2 or SIGUSR2.
    4. trigger_init(), line 70(definition)
      Procd triggers on config file/network interface changes, seeprocd_triggers_on_config_filenetwork_interface_changes
      Initialise a run queue. Anexampleis the sole documentation. A queued task has an uloop callback invoked when done, here sets the empty queue callback to do nothing.
    5. procd_state_next(), line 74(definition)
      Transitions from NONE to EARLY the state of a state machine implemented instate_enter(void)used to sequence the remaining boot steps.
    6. STATE_EARLYinstate_enter()
      1. Emits "- early -" to syslog,
      2. Initialise the watchdog,
      3. hotplug("/etc/hotplug.json")(definition)
        User space device hotplugging handler setup.
        Static variables in file scope are important. The filename of the script to execute is kept in hotplug.c global scope:static char * rule_file;.
        Opens a netlink socket (man 7 netlink) and handles the file descriptor to uloop, to listen to uevents: kernel messages informinguserspace of kernelevents. Seehttps://www.kernel.org/doc/pending/hotplug.txt
        The uloop instance in pid 1uses epoll_waitto monitor file descriptors, the kernel netlink socket FD is one of them, and is instructed to invoke the callbackhotplug_handler()on uevent arrival.
        Thishotplug_handlercallback stays active after coldplug, and will handle all uevents the kernel will emit.
      4. procd_coldplug()(definition)
        Umounts/dev/ptsand/dev, mounts a tmpfs on/dev, creates directories/dev/shmand/dev/pts, forksudevtriggerto reconstruct kernel uevents went unheard before netlink socket opening ("coldplug").
        1. udevtrigger
          Scans/sys/bus/*/devices,/sys/class; and/sys/blockif it isn't a subdir of/sys/class, writing "add" to the uevent file of all devices. Then the kernel synthesizes an "add" uevent message on netlink. See Injecting events into hotplug via "uevent" inhttps://www.kernel.org/doc/pending/hotplug.txt

          A callback chain,udevtrigger_complete()followed bycoldplug_complete()is attached to completion of the child udevtrigger process, such that the still to be reacheduloop_run()in procdmain()function, after all uevents will have been processed, will advance procd state to STATE_UBUS,line 31.

    7. uloop_run, line 75
      Solicited by udevtrigger in another process, the kernel emits uevents and uloop invokes the user space hotplug handler: the callback
      1. hotplug_handler
        to run/etc/hotplug.json.
        1. The/etc/hotplug.jsonscript
          - creates and removes devices files, assigns them permissions,
          - loads firmware,
          - handles buttons by calling scripts in/etc/rc.button/%BUTTON%if the uevent has the "BUTTON" value,
          - and invokes/sbin/hotplug-call "%SUBSYSTEM%"to handle all other subsystem related actions.
          Subystems are: "platform" "net", "input", "usb", "usbmisc", "ieee1394", "block", "atm", "zaptel", "tty", "button" (without BUTTON value, possible?), "usb-serial". "usb-serial" is aliased to "tty" in hotplug.json.
          Documentation of json script syntax? Offline.Use the source. It is the json representation of the abstract syntax tree of a script in a fairly intuitive scripting language.
          There are 2 levels at which decisions are taken: hotplug.json acts as fast path executor or lightweight dispatcher, the subsystem scripts in /etc/hotplug.d/%SUBSYSTEM%/ do the heavy lifting.
          Uevent messages from the kernel contain key-value pairs passed as environment variables to the scripts. The kernel function
          int add_uevent_var(struct kobj_uevent_env *env, const char *format, ...)
          creates them. This linkhttp://lxr.free-electrons.com/ident?v=3.18;i=add_uevent_varprovides a list of all places in the Linux kernel where it is used. It is an authoritative reference of the upstream defined uevent variables. Button events are generated by the out of tree kernel modulesbutton-hotpluggpio-button-hotplugspecific to OpenWrt.
          1. /sbin/hotplug-call "%SUBSYSTEM%"
            is a shell script that scans/etc/hotlug.d/%SUBSYSTEM%/*and sources all scripts assigned to a subsystem. "button" subsystem is handled here if the uevent lacks the "BUTTON" value, unlikely or impossible?.
      2. STATE_UBUS
        At end of coldplug uevents processing, the callbackcoldplug_complete callsprocd_state_nextwhich results in advancing procd to STATE_UBUS.
        "- ubus -" is logged to console, the services infrastructure is initialised, then procd schedules connect to after 1" (line 67) and starts/sbin/ubusas the systemubusservice.
        Transition to next state is triggered by the callbackubus_connect_cbthat at the end, line 118, callsprocd_state_ubus_connect(), line 186, that callsprocd_state_nextto transition to
      3. STATE_INIT
        "- init -" is logged,/etc/inittabis parsed and entries
        ::askconsole:/bin/ash –login
        ::sysinit:/etc/init.d/rcS S boot
        executed. inittab format is the same as the one from busybox (Busybox example inittab).
        The "sysinitaction" handler
        1. runrc
          instantiates a queue, whose empty handlerrcdonewill advance procd state.
          runrcignoresthe process specification "/etc/init.d/rcS" (there is no such a script!), and runs
          1. rcS(pattern="S" , param="boot", rcdone)(line 159)
            that invokes the equivalent of
            _rc(&q, *path="/etc/rc.d", *file="S", *pattern="*", *param="boot")
            to enqueue in glob sort order the scripts
            /etc/rc.d/S* boot
            with "boot" as the action./etc/rc.d/S*aresymlinks made by rc.common enableto files in/etc/init.d, that are shell scripts with the shebang#!/bin/sh /etc/rc.common.
            Invoking a/etc/rc.d/S*script runsrc.commonthat sources the /etc/rc.d/S* script to set up a context, then invokes the function named as the action parameter ("boot()"), in that context.
      4. STATE_RUNNING
        Execution arrives here after rcS scripts are done.
        "- init complete -" is logged.
        This is a stable state, keeping uloop_run in procd.c main() running, mostly waiting on epoll_wait.Upon receipt of a signalin SIGTERM, SIGINT (reboot), or SIGUSR2, SIGUSR2 (poweroff), procd transitions to
      5. STATE_SHUTDOWN
        "- shutdown -" is logged, /etc/inittab shutdown entry is executed, and procd sleepsat line 169while the kernel does poweroff or reboot.
    8. uloop_done
      return 0
      lines 75 & 76 are never reached by pid 1, kernel would panic if init exited.

Openwrt学习笔记(四)——系统开机启动 (续)相关推荐

  1. OpenWRT 学习笔记 -- 用户自定义添加app的开机自启动

    参考自:OpenWRT编译 – 用户自定义添加app的开机自启动 OpenWRT 学习笔记-19 OpenWRT开机自启动服务 文章目录 一.概述 二.自启动实现方式一 三.自启动实现方式二 四.自启 ...

  2. IMX6ULL学习笔记(四) —— uboot 启动流程

    IMX6ULL 学习笔记 version : v1.0 「2023.4.27」 author: Y.Z.T. 摘要: 随记, 记录 I.MX6ULL 系列 SOC 的uboot 启动流程 ⭐️ 目录 ...

  3. STM32F103学习笔记四 时钟系统

    STM32F103学习笔记四 时钟系统 本文简述了自己学习时钟系统的一些框架,参照风水月 1. 单片机中时钟系统的理解 1.1 概述 时钟是单片机的脉搏,是单片机的驱动源 用任何一个外设都必须打开相应 ...

  4. ROS学习笔记四:理解ROS节点

    ROS学习笔记四:理解ROS节点 本节主要介绍ROS图形概念,讨论ROS命令行工具roscore.rosnode和rosrun. 要求 要求已经在Linux系统中安装一个学习用的ros软件包例子: s ...

  5. OpenWrt学习笔记之刷机、救砖

    OpenWrt学习笔记之刷机及救砖 完成了固件的编译之后就可以进行刷机了,当然也可以去官网直接下载固件.连接如下:http://wiki.openwrt.org/toh/tp-link/tl-wr70 ...

  6. MySQL高级学习笔记(四)

    文章目录 MySQL高级学习笔记(四) 1. MySql中常用工具 1.1 mysql 1.1.1 连接选项 1.1.2 执行选项 1.2 mysqladmin 1.3 mysqlbinlog 1.4 ...

  7. 【http学习笔记四】安全篇

    [http学习笔记四]安全篇 文章目录 [http学习笔记四]安全篇 一.HTTPS 与 SSL/TLS ① 什么是安全? 机密性 完整性 身份认证 不可否认 ② 什么是HTTPS? ③ SSL/TL ...

  8. mysql新增表字段回滚_MySql学习笔记四

    MySql学习笔记四 5.3.数据类型 数值型 整型 小数 定点数 浮点数 字符型 较短的文本:char, varchar 较长的文本:text, blob(较长的二进制数据) 日期型 原则:所选择类 ...

  9. 服务器win10系统开机慢,win10专业版系统开机启动慢 三种方法帮你敲定

    win10专业版系统开机启动慢 三种方法帮你敲定对于很多游戏玩家来说,电脑运行速度快,使用不卡顿是非常重要的,有些使用win10系统的玩家会觉得win10的启动速度慢,为了让各位能够愉快的玩游戏,今天 ...

最新文章

  1. linux pcre-devel,ubuntu - 我应该为'pcre-devel'安装什么软件包? - Ubuntu问答
  2. Android开发学习笔记:数据存取之SQLite浅析
  3. PyCharm设置自动换行
  4. SQL Server 数据库管理常用的SQL和T-SQL语句
  5. 360产品演示代码 css3 html5,HTML5 CSS3代码的三维展示
  6. 从计算机基础知识到机器学习第1堂课,最适合新手的5场直播来了
  7. Yann LeCun 会成为下一个居里夫人吗?
  8. 该学学数据结构了,不会数据结构真是寸步难行啊。。。。。
  9. Python破解验证码,只要15分钟就够了!
  10. Python实现Excel和TXT文件格式的转换
  11. 态势感知平台分析流程
  12. SRF04绿色小屏的作用
  13. Open For Bussiness (HelloWorld)
  14. NRF52832+DW1000通信系统架构设计
  15. 云行 | 让数据奔驰在“云”间,天翼云助力贵州筑牢算力底座
  16. airpods pro是按压还是触摸_Airpods Pro深度测评,给犹豫不决的你一些参考。
  17. Docker 搭建 Jenkins 流水线项目配置
  18. NOI / 2.5基本算法之搜索-6044:鸣人和佐助详解
  19. DDos攻击之DNS Query Flood
  20. 值得推荐的几部日本电影(一)

热门文章

  1. NotImplementedError: Cannot convert a symbolic Tensor (while/add_8:0) to a numpy array.
  2. 计算机毕业过程,计算机毕业论文答辩过程及要求
  3. mybaits-plugs 多数据源 最简单的入门案例
  4. 用 TypeScript 写 React Redux - 完全指南
  5. 2大学英语四级关于计算机的图表作文,大学英语四级图表作文模板
  6. POOLED和UNPOOLED源码分析
  7. Django用户认证管理系统(登录|注册|找回密码(邮箱找回)|注销)
  8. 用ffmpeg向b站斗鱼等推流24小时直播一路踩的坑总结
  9. 我在淘宝做弹窗,2022 年初的回顾与展望
  10. AndroidStudio清除无用的资源