开源鸿蒙北向框架开发:系统服务理论详解

发布于:2025-09-10 ⋅ 阅读:(21) ⋅ 点赞:(0)

系统服务的启动

基本可以认为:OpenHarmony 的系统服务进程都是“由 init 直接或间接拉起”的。

  • 直接方式:
    • init 按 /system/etc/init/.cfg 启动可执行(如 /system/bin/sa_main、/system/bin/samgr 等),这些进程的 PPid=1。
  • 间接方式:
    • 应用进程由 appspawn 拉起,但 appspawn 自身也是 init 启动的;系统能力(SA)一般不走 appspawn,而是走 sa_main 宿主进程。
  • 特殊/例外
    • 开发测试可手动启动进程(不通过 init);但系统正式启动路径都以 init 为根。
    • 同一宿主可承载多个 SA,这些 SA 并非独立进程,但宿主仍由 init 启动。

通过OpenHarmony系统进程pid可证明如上的观点:
输入如下命令:

  • ps -o pid,ppid,comm,args -A | grep -E 'samgr|sa_main'
    输出如下:
# ps -o pid,ppid,comm,args -A | grep -E 'samgr|sa_main'
  275     1 sa_main                     storage_manager
  277     1 sa_main                     khstabilitymgr
  278     1 samgr                       samgr
  279     1 sa_main                     resource_metrics
  280     1 sa_main                     param_watcher
  284     1 sa_main                     khmonitorservice
  286     1 sa_main                     khdeviceauth_service
  287     1 sa_main                     khcontainermanager_service
  290     1 sa_main                     cadaemon
  460     1 sa_main                     accessibility
  461     1 sa_main                     audio_server
  462     1 sa_main                     bluetooth_service
  468     1 sa_main                     concurrent_task_service
  506     1 sa_main                     huks_service
  510     1 sa_main                     kh_dids_agent_service
  521     1 sa_main                     khlink_service
  523     1 sa_main                     kh_sle_service
  542     1 sa_main                     memmgrservice
  546     1 sa_main                     multimodalinput
  578     1 sa_main                     resource_schedule_executor
  579     1 sa_main                     resource_schedule_service
  620     1 sa_main                     netmanager
  637     1 sa_main                     telephony
  638     1 sa_main                     wallpaper_service
  735     1 sa_main                     bgtaskmgr_service
  748     1 sa_main                     msdp
  770     1 sa_main                     time_service
  771     1 sa_main                     uart_service
  803     1 sa_main                     device_manager
  843     1 sa_main                     useriam
  853     1 sa_main                     ui_service
  854     1 sa_main                     thingmodel_service
  856     1 sa_main                     thing_accessmanager_service
  859     1 sa_main                     superdevice_thm
  860     1 sa_main                     superdevice_cfg
  862     1 sa_main                     superdevice_acs
  863     1 sa_main                     streaming_service
  874     1 sa_main                     softbus_server
  875     1 sa_main                     sensors
  876     1 sa_main                     security_guard
  878     1 sa_main                     khremoteops
  879     1 sa_main                     privacy_service
  880     1 sa_main                     powermgr
  882     1 sa_main                     pki_manager_service
  885     1 sa_main                     netsysnative
  887     1 sa_main                     miracast_service
  888     1 sa_main                     media_service
  889     1 sa_main                     media_monitor
  890     1 sa_main                     kloudaccount
  891     1 sa_main                     kh_update_sa
  892     1 sa_main                     khthingaccessmanager_peripherals
  894     1 sa_main                     khability_service
  895     1 sa_main                     khsharing_service
  896     1 sa_main                     khrule_engine_service
  897     1 sa_main                     khqrdetector_service
  898     1 sa_main                     license_sa
  899     1 sa_main                     khdlp_permission_service
  901     1 sa_main                     khcontainercomm_service
  911     1 sa_main                     kh_update
  913     1 sa_main                     kh_dids_manager_service
  916     1 sa_main                     inputmethod_service
  931     1 sa_main                     foundation
  932     1 sa_main                     drm_service
  934     1 sa_main                     distributeddata
  935     1 sa_main                     kh_dscreen
  936     1 sa_main                     deviceinfoservice
  938     1 sa_main                     camera_service
  940     1 sa_main                     av_session
  941     1 sa_main                     av_codec_service
  953     1 sa_main                     audio_magic_service
  956     1 sa_main                     accountmgr
  962     1 sa_main                     accesstoken_service
 1013     1 sa_main                     superdevice_khcomm
 1470     1 sa_main                     usb_service
 1471     1 sa_main                     hdf_ext_devmgr
 1788     1 sa_main                     kloudauth
 1789     1 sa_main                     otpauth
 1791     1 sa_main                     pinauth
 1886     1 sa_main                     wifi_manager_service
 2065     1 sa_main                     khwms_service
 2114     1 sa_main                     asset_service
 2207     1 sa_main                     download_server
 2245     1 sa_main                     cert_manager_service
 2879     1 sa_main                     deviceprofile
 6567  4997 toybox                      grep -E samgr|sa_main

可以看到很多sa_main,和一个samgr ,为什么呢?
分析命令:ps -o pid,ppid,comm,args -A | grep -E 'samgr|sa_main'
这里是使用ps查看当前的全部进程信息,且查看的信息是指定的4个字段

  • pid 当前的进程号
  • ppid 父进程号
  • comm 可执行文件的文件名(原始文件名)
  • args 完整的启动参数

通过上面的信息可知,所有的sa_main和samgr都是由init进程拉起的,因为init进程的进程号是1,且有很多个sa_main
查看init的进程号:

# ps -elf | grep init
root             1     0 0 07:59:59 ?     00:01:02 init --second-stage 2101515
root          7662  4997 1 01:42:09 pts/0 00:00:00 grep init

查看init的配置:
grep -Rni 'sa_main\|samgr' /system/etc/init

/system/etc/init/access_token.cfg:17:            "path" : ["/system/bin/sa_main", "/system/profile/accesstoken_service.json"],
/system/etc/init/accessibility.cfg:13:            "path" : ["/system/bin/sa_main", "/system/profile/accessibility.json"],
/system/etc/init/accountmgr.cfg:18:            "path" : ["/system/bin/sa_main", "/system/profile/accountmgr.json"],
/system/etc/init/app_domain_verify_agent.cfg:13:            "path" : ["/system/bin/sa_main", "/system/profile/app_domain_verify_agent.json"],
/system/etc/init/app_fwk_update_service.cfg:4:            "path" : ["/system/bin/sa_main", "/system/profile/app_fwk_update_service.json"],
/system/etc/init/asset_service.cfg:11:            "path" : ["/system/bin/sa_main", "/system/profile/asset_service.json"],
/system/etc/init/audio_magic_service.cfg:12:            "path" : ["/system/bin/sa_main", "/system/profile/audio_magic_service.json"],
/system/etc/init/audio_server.cfg:44:      "path" : ["/system/bin/sa_main", "/system/profile/audio_server.json"],
/system/etc/init/av_codec_service.cfg:4:        "path" : ["/system/bin/sa_main", "/system/profile/av_codec_service.json"],
/system/etc/init/avsession_service.cfg:12:            "path" : ["/system/bin/sa_main", "/system/profile/av_session.json"],
/system/etc/init/backup.cfg:4:            "path" : ["/system/bin/sa_main", "/system/profile/backup_sa.json"],
/system/etc/init/bgtaskmgr_service.cfg:15:            "path" : ["/system/bin/sa_main", "/system/profile/bgtaskmgr_service.json"],
/system/etc/init/bluetooth_service.cfg:16:            "path" : ["/system/bin/sa_main", "/system/profile/bluetooth_service.json"],
/system/etc/init/cadaemon.cfg:16:            "path" : ["/system/bin/sa_main", "/system/profile/cadaemon.json"],
/system/etc/init/camera_service.cfg:12:            "path" : ["/system/bin/sa_main", "/system/profile/camera_service.json"],
/system/etc/init/cert_manager_service.cfg:20:            "path" : ["/system/bin/sa_main", "/system/profile/cert_manager_service.json"],
/system/etc/init/compiler_service.cfg:4:            "path" : ["/system/bin/sa_main", "/system/profile/compiler_service.json"],
/system/etc/init/concurrent_task_service.cfg:11:            "path" : ["/system/bin/sa_main", "/system/profile/concurrent_task_service.json"],
/system/etc/init/daudio.cfg:4:        "path" : ["/system/bin/sa_main", "/system/profile/daudio.json"],
/system/etc/init/dcamera.cfg:4:        "path" : ["/system/bin/sa_main","/system/profile/dcamera.json"],
/system/etc/init/devattest_service.cfg:11:            "path" : ["/system/bin/sa_main", "/system/profile/devattest_service.json"],
/system/etc/init/device_manager.cfg:12:        "path" : ["/system/bin/sa_main", "/system/profile/device_manager.json"],
/system/etc/init/deviceinfoservice.cfg:4:            "path" : ["/system/bin/sa_main", "/system/profile/deviceinfoservice.json"],
/system/etc/init/deviceprofile.cfg:13:            "path" : ["/system/bin/sa_main", "/system/profile/deviceprofile.json"],
/system/etc/init/dhardware.cfg:13:            "path" : ["/system/bin/sa_main", "/system/profile/dhardware.json"],
/system/etc/init/dhardwarecommon_service.cfg:10:        "path" : ["/system/bin/sa_main", "/system/profile/kh_dscreen.json"],
/system/etc/init/dinput.cfg:4:        "path" : ["/system/bin/sa_main", "/system/profile/dinput.json"],
/system/etc/init/distributed_data.cfg:18:        "path" : ["/system/bin/sa_main","/system/profile/distributeddata.json"],
/system/etc/init/distributedbms.cfg:12:            "path" : ["/system/bin/sa_main", "/system/profile/d-bms.json"],
/system/etc/init/distributedfile.cfg:11:        "path": ["/system/bin/sa_main", "/system/profile/distributedfiledaemon.json"],
/system/etc/init/distributedfile.cfg:29:        "path": ["/system/bin/sa_main", "/system/profile/cloudfiledaemon.json"],
/system/etc/init/distributedfile.cfg:46:        "path": ["/system/bin/sa_main", "/system/profile/cloudfileservice.json"],
/system/etc/init/distributedsched.cfg:12:            "path" : ["/system/bin/sa_main", "/system/profile/distributedsched.json"],
/system/etc/init/dlp_permission_service.cfg:17:            "path" : ["/system/bin/sa_main", "/system/profile/dlp_permission_service.json"],
/system/etc/init/downloadservice.cfg:12:            "path" : ["/system/bin/sa_main", "/system/profile/download_server.json"],
/system/etc/init/drm_service.cfg:4:            "path" : ["/system/bin/sa_main", "/system/profile/drm_service.json"],
/system/etc/init/dscreen.cfg:4:        "path" : ["/system/bin/sa_main", "/system/profile/dscreen.json"],
/system/etc/init/dslm_service.cfg:7:                "/system/bin/sa_main",
/system/etc/init/edm.cfg:13:            "path" : ["/system/bin/sa_main", "/system/profile/edm.json"],
/system/etc/init/el5_filekey_manager.cfg:15:        "/system/bin/sa_main",
/system/etc/init/file_access_service.cfg:4:            "path" : ["/system/bin/sa_main", "/system/profile/file_access_service.json"],
/system/etc/init/foundation.cfg:43:            "path" : ["/system/bin/sa_main", "/system/profile/foundation.json"],
/system/etc/init/hdf_ext_devmgr.cfg:13:            "path" : ["/system/bin/sa_main", "/system/profile/hdf_ext_devmgr.json"],
/system/etc/init/hidumper_service.cfg:11:            "path" : ["/system/bin/sa_main", "/system/profile/hidumper_service.json"],
/system/etc/init/huks_service.cfg:17:            "path" : ["/system/bin/sa_main", "/system/profile/huks_service.json"],
/system/etc/init/i18n_service.cfg:17:            "path": ["/system/bin/sa_main", "/system/profile/i18n_service.json"],
/system/etc/init/inputmethodservice.cfg:11:            "path" : ["/system/bin/sa_main", "/system/profile/inputmethod_service.json"],
/system/etc/init/installs.cfg:12:            "path" : ["/system/bin/sa_main", "/system/profile/installs.json"],
/system/etc/init/intell_voice_service.cfg:16:            "path" : ["/system/bin/sa_main", "/system/profile/intell_voice_service.json"],
/system/etc/init/kh_dids_agent_service.cfg:13:            "path" : ["/system/bin/sa_main", "/system/profile/kh_dids_agent_service.json"],
/system/etc/init/kh_dids_manager_service.cfg:11:            "path" : ["/system/bin/sa_main", "/system/profile/kh_dids_manager_service.json"],
/system/etc/init/kh_dinput.cfg:4:        "path" : ["/system/bin/sa_main", "/system/profile/kh_dinput.json"],
/system/etc/init/kh_update.cfg:4:            "path" : ["/system/bin/sa_main", "/system/profile/kh_update.json"],
/system/etc/init/khcontainercomm_service.cfg:12:            "path" : ["/system/bin/sa_main", "/system/profile/khcontainercomm_service.json"],
/system/etc/init/khcontainermanager_service.cfg:26:            "path" : ["/system/bin/sa_main", "/system/profile/khcontainermanager_service.json"]
/system/etc/init/khdeviceauthservice.cfg:13:      "path": [ "/system/bin/sa_main", "/system/profile/khdeviceauth_service.json" ],
/system/etc/init/khdlp_permission_service.cfg:12:            "path" : ["/system/bin/sa_main", "/system/profile/khdlp_permission_service.json"],
/system/etc/init/khlicense_sa.cfg:5:            "path" : ["/system/bin/sa_main", "/system/profile/license_sa.json"],
/system/etc/init/khlink.cfg:12:            "path" : ["/system/bin/sa_main", "/system/profile/khlink_service.json"],
/system/etc/init/khmonitor.cfg:4:            "path" : ["/system/bin/sa_main", "/system/profile/khmonitorservice.json"],
/system/etc/init/khqrdetector_service.cfg:11:            "path" : ["/system/bin/sa_main", "/system/profile/khqrdetector_service.json"],
/system/etc/init/khrule_engine_service.cfg:4:            "path" : ["/system/bin/sa_main", "/system/profile/khrule_engine_service.json"],
/system/etc/init/khsharing_service.cfg:4:            "path" : ["/system/bin/sa_main", "/system/profile/khsharing_service.json"],
/system/etc/init/khsle.cfg:13:            "path" : ["/system/bin/sa_main", "/system/profile/kh_sle_service.json"],
/system/etc/init/khsysabilityservice_service.cfg:12:            "path" : ["/system/bin/sa_main", "/system/profile/khability_service.json"],
/system/etc/init/khthingaccessmanager_peripherals.cfg:11:        "path" : ["/system/bin/sa_main", "/system/profile/khthingaccessmanager_peripherals.json"],
/system/etc/init/khupdate_sa.cfg:13:            "path" : ["/system/bin/sa_main", "/system/profile/kh_update_sa.json"],
/system/etc/init/khwms_service.cfg:14:            "path" : ["/system/bin/sa_main", "/system/profile/khwms_service.json"],
/system/etc/init/kloud_account.cfg:10:            "path" : ["/system/bin/sa_main", "/system/profile/kloudaccount.json"],
/system/etc/init/kloud_auth.cfg:17:            "path" : ["/system/bin/sa_main", "/system/profile/kloudauth.json"],
/system/etc/init/local_code_sign.cfg:4:            "path" : ["/system/bin/sa_main", "/system/profile/local_code_sign.json"],
/system/etc/init/locationsa.cfg:13:            "path" : ["/system/bin/sa_main", "/system/profile/locationhub.json"],
/system/etc/init/mdnsmanager.cfg:4:            "path" : ["/system/bin/sa_main", "/system/profile/mdnsmanager.json"],
/system/etc/init/media_monitor.cfg:11:            "path" : ["/system/bin/sa_main", "/system/profile/media_monitor.json"],
/system/etc/init/media_service.cfg:4:            "path" : ["/system/bin/sa_main", "/system/profile/media_service.json"],
/system/etc/init/memmgrservice.cfg:29:            "path" : ["/system/bin/sa_main", "/system/profile/memmgrservice.json"],
/system/etc/init/miracast_service.cfg:4:            "path" : ["/system/bin/sa_main", "/system/profile/miracast_service.json"],
/system/etc/init/module_update_sa.cfg:15:            "path" : ["/system/bin/sa_main", "/system/profile/module_update_sa.json"],
/system/etc/init/msdp_musl.cfg:13:            "path" : ["/system/bin/sa_main", "/system/profile/msdp.json"],
/system/etc/init/multimodalinput.cfg:26:            "path" : ["/system/bin/sa_main", "/system/profile/multimodalinput.json"],
/system/etc/init/netmanager_base.cfg:23:            "path" : ["/system/bin/sa_main", "/system/profile/netmanager.json"],
/system/etc/init/netsysnative.cfg:13:            "path" : ["/system/bin/sa_main", "/system/profile/netsysnative.json"],
/system/etc/init/oaidservice.cfg:12:            "path" : ["/system/bin/sa_main", "/system/profile/oaid_service.json"],
/system/etc/init/otp_auth.cfg:17:            "path" : ["/system/bin/sa_main", "/system/profile/otpauth.json"],
/system/etc/init/param_watcher.cfg:4:            "path" : ["/system/bin/sa_main", "/system/profile/param_watcher.json"],
/system/etc/init/pasteboardservice.cfg:12:            "path" : ["/system/bin/sa_main", "/system/profile/pasteboard_service.json"],
/system/etc/init/pinauth_sa_profile.cfg:12:            "path" : ["/system/bin/sa_main", "/system/profile/pinauth.json"],
/system/etc/init/pki_manager_service.cfg:12:            "path" : ["/system/bin/sa_main", "/system/profile/pki_manager_service.json"],
/system/etc/init/powermgr.cfg:20:            "path" : ["/system/bin/sa_main", "/system/profile/powermgr.json"],
/system/etc/init/printservice.cfg:30:            "path" : ["/system/bin/sa_main", "/system/profile/print_service.json"],
/system/etc/init/privacy.cfg:4:            "path" : ["/system/bin/sa_main", "/system/profile/privacy_service.json"],
/system/etc/init/quick_fix.cfg:4:            "path" : ["/system/bin/sa_main", "/system/profile/quick_fix.json"],
/system/etc/init/remote_ops.cfg:18:            "path" : ["/system/bin/sa_main", "/system/profile/khremoteops.json"],
/system/etc/init/resource_metrics_service.cfg:4:            "path" : ["/system/bin/sa_main", "/system/profile/resource_metrics.json"],
/system/etc/init/resource_schedule_executor.cfg:11:            "path" : ["/system/bin/sa_main", "/system/profile/resource_schedule_executor.json"],
/system/etc/init/resource_schedule_service.cfg:119:            "path" : ["/system/bin/sa_main", "/system/profile/resource_schedule_service.json"],
/system/etc/init/samgr_standard.cfg:5:                "mkdir /data/samgr 0740 samgr samgr"
/system/etc/init/samgr_standard.cfg:10:            "name" : "samgr",
/system/etc/init/samgr_standard.cfg:11:            "path" : ["/system/bin/samgr"],
/system/etc/init/samgr_standard.cfg:13:            "uid" : "samgr",
/system/etc/init/samgr_standard.cfg:14:            "gid" : ["samgr", "system", "readproc", "data_reserve"],
/system/etc/init/samgr_standard.cfg:16:            "bootevents":"bootevent.samgr.ready",
/system/etc/init/samgr_standard.cfg:41:            "secon" : "u:r:samgr:s0",
/system/etc/init/sandbox_manager_service.cfg:10:        "path" : ["/system/bin/sa_main", "/system/profile/sandbox_manager_service.json"],
/system/etc/init/scanservice.cfg:4:            "path" : ["/system/bin/sa_main", "/system/profile/scan_service.json"],
/system/etc/init/scanservice.rc:16:service scan_service /system/bin/sa_main /system/profile/scan_service.json
/system/etc/init/security_collector.cfg:6:        "/system/bin/sa_main",
/system/etc/init/security_component_service.cfg:11:            "path" : ["/system/bin/sa_main", "/system/profile/security_component_service.json"],
/system/etc/init/security_guard.cfg:15:            "path" : ["/system/bin/sa_main", "/system/profile/security_guard.json"],
/system/etc/init/sensors_musl.cfg:4:            "path" : ["/system/bin/sa_main", "/system/profile/sensors.json"],
/system/etc/init/softbus_server_musl.cfg:13:            "path" : ["/system/bin/sa_main", "/system/profile/softbus_server.json"],
/system/etc/init/srms.cfg:5:            "path" : ["/system/bin/sa_main", "/system/profile/service_router.json"],
/system/etc/init/stability_mgr_service.cfg:12:            "path" : ["/system/bin/sa_main", "/system/profile/khstabilitymgr.json"],
/system/etc/init/storage_manager.cfg:4:         "path": ["/system/bin/sa_main", "/system/profile/storage_manager.json"],
/system/etc/init/streaming_service.cfg:4:            "path" : ["/system/bin/sa_main", "/system/profile/streaming_service.json"],
/system/etc/init/superdevice_acs.cfg:13:            "path" : ["/system/bin/sa_main", "/system/profile/superdevice_acs.json"],
/system/etc/init/superdevice_cfg.cfg:12:            "path" : ["/system/bin/sa_main", "/system/profile/superdevice_cfg.json"],
/system/etc/init/superdevice_khcloudcomm.cfg:19:            "path" : ["/system/bin/sa_main", "/system/profile/superdevice_khcomm.json"],
/system/etc/init/superdevice_thm.cfg:15:            "path" : ["/system/bin/sa_main", "/system/profile/superdevice_thm.json"],
/system/etc/init/sys_installer_sa.cfg:25:            "path" : ["/system/bin/sa_main", "/system/profile/sys_installer_sa.json"],
/system/etc/init/telephony.cfg:13:            "path" : ["/system/bin/sa_main", "/system/profile/telephony.json"],
/system/etc/init/testserver.cfg:6:        "/system/bin/sa_main",
/system/etc/init/thing_accessmanager.cfg:16:            "path" : ["/system/bin/sa_main", "/system/profile/thing_accessmanager_service.json"],
/system/etc/init/thingmodel_service.cfg:14:            "path" : ["/system/bin/sa_main", "/system/profile/thingmodel_service.json"],
/system/etc/init/timeservice.cfg:14:            "path" : ["/system/bin/sa_main", "/system/profile/time_service.json"],
/system/etc/init/token_sync.cfg:4:            "path" : ["/system/bin/sa_main", "/system/profile/token_sync_service.json"],
/system/etc/init/uart.cfg:15:            "path" : ["/system/bin/sa_main", "/system/profile/uart_service.json"],
/system/etc/init/ui_service.cfg:4:            "path" : ["/system/bin/sa_main", "/system/profile/ui_service.json"],
/system/etc/init/updater_sa.cfg:24:            "path" : ["/system/bin/sa_main", "/system/profile/updater_sa.json"],
/system/etc/init/usb_service.cfg:4:            "path" : ["/system/bin/sa_main", "/system/profile/usb_service.json"],
/system/etc/init/useriam.cfg:4:            "path" : ["/system/bin/sa_main", "/system/profile/useriam.json"],
/system/etc/init/wallpaperservice.cfg:12:            "path" : ["/system/bin/sa_main", "/system/profile/wallpaper_service.json"],
/system/etc/init/wifi_standard.cfg:23:            "path" : ["/system/bin/sa_main", "/system/profile/wifi_manager_service.json"],

可以看到形如:"path": ["/system/bin/sa_main", "/system/profile/xxx.json"] 等配置行
这表示init拉起的程序是sa_main,给sa_main的启动参数是/system/profile/xxx.json文件
至此可以得出如下结论:OpenHarmony 的系统服务进程都是“由 init 直接或间接拉起”的
那么我们自己创建的服务也应该由init进程去拉起,此时就需要配置init的拉起配置文件

系统服务启动的疑点

上面的命令行查出来的都是sa_main,但是实际上查看进程信息的时候压根就没有看到sa_main,比如如下命令:
ps -elf | grep storage_manager

# ps -elf | grep storage_manager
storage_manager 275    1 0 08:00:06 ?     00:00:02 storage_manager
root          8686  4997 0 01:56:53 pts/0 00:00:00 grep storage_manager

如上是我们最常用的查看进程信息的方法,但是查看storage_manager进程的时候没有发现sa_main字段,这是怎么回事呢?上面明明出现了

# ps -o pid,ppid,comm,args -A | grep -E 'samgr|sa_main'
  275     1 sa_main                     storage_manager

这个表示storage_manager只是启动的命令,为什么会变成进程名呢?
现在根据以上的信息我们可知:sa服务是由init拉起,且init拉起的可执行程序是sa_main,只是在sa_main的执行逻辑中把进程名改成了启动参数(storage_manager)
那么现在是不是还需要考虑下sa_main的启动配置文件

由上面两个章节可知:
一个系统服务至少需要两个配置文件:

  1. init启动配置文件
  2. sa_main的载入配置文件

什么是sa_main

sa_main 是 OpenHarmony 的“系统能力(SA)通用宿主可执行程序”,路径通常为 /system/bin/sa_main。

# pwd
/system/bin
# find ./ -name "*main*"
./dnsdomainname
./sa_main

职责:
启动后解析传入的SA Profile(如/system/profile/xxx.json)。
按libpath/depend/run-on-create 在同一进程内dlopen各 SA 动态库,创建实例并调用 OnStart。
将SA注册到系统能力管理器(SAMgr),用于跨进程发现与调用。
启动链路:init 通过 init 配置(.cfg)拉起 /system/bin/sa_main /system/profile/xxx.json → sa_main 装载和启动多个 SA → SA 对外经 SAMgr 提供 IPC。
特点:

  • 一个 sa_main 进程可承载多个 SA(共驻进程,省进程/内存)。
  • sa_main 不负责“创建进程”(这是 init 的事),负责“进程内加载 SA 并管理其生命周期”。

为什么OpenHarmony需要把系统服务做成sa形式而不是像linux那样使用systemd+守护进程

  • 定位与约束不同
    • OpenHarmony面向端侧/嵌入式/多内核(Linux/LiteOS),不依赖systemd生态;需要更轻量、可裁剪、统一于大小系统的机制。
    • 资源敏感与启动确定性要求高,偏好“多服务共驻”与精细依赖编排。
  • SA(System Ability)带来的架构收益
    • 统一宿主与装载:sa_main+profile 在同一进程内 dlopen 多个 SA,减少进程数与内存占用,提升冷启速度。
    • 依赖与生命周期:profile 的 depend/run-on-create/depend-time-out + OnStart/OnStop/OnAddSystemAbility,内置依赖等待与“后补初始化”回调。
    • 统一发现与调用:SAMgr 以 SA_ID 做注册/发现,客户端经 iface_cast 获取 Proxy,天然走统一 IPC(Binder/LiteIPC)模型,类型安全、带死亡通知。
    • 跨版本与多形态一致:同一套 SA/IPC 模型覆盖 AOSP 风格设备与轻量设备(LiteIPC),开发者心智统一。
    • 安全一体化:与 AccessToken 权限、SELinux 域、init 能力位(caps)等打通,易做最小权限与进程级管控。
  • 为什么不走“systemd+守护进程”常规做法
    • systemd 体量与依赖链对端侧并不总适配;OHOS 自带 init 与参数服务、事件/权限体系,更易裁剪和跨内核迁移。
    • 守护进程各自为政:依赖/发现/IPC/权限需自行约定;SA 模型把“发现(SAMgr)+ 调用(Proxy/Stub)+ 生命周期”做了平台级统一,降低系统集成与维护成本。
    • 分布式场景友好:SA_ID 语义、跨设备能力扩展更顺滑(与分布式总线/能力发现结合)。
  • 能否用套接字/自定义守护
    • 可以,但会失去 SAMgr 统一发现、类型化 IPC、依赖编排与权限治理带来的平台收益。通常建议:对内用 SA;对外协议(MQTT/HTTP/TCP)由 SA 内部服务化暴露。
      一句话:SA 是 OHOS 为端侧/多形态场景量身设计的“统一系统服务模型”,在资源、依赖、IPC、安全、分布式等维度提供平台级一体化能力,这是它相对“systemd+daemon”更契合 OHOS 目标的原因。

网站公告

今日签到

点亮在社区的每一天
去签到