android - 有没有办法优雅地停止 init.rc 中的 android 服务(没有 SIGKILL)?

标签 android linux service signals init

我有一个 native Android 服务,它由通过 GUI 设置的属性启动和停止。 init.rc中启动和停止服务的命令如下:

service myservice /system/bin/myservice
    class late_start
    user my_service
    disabled

on property:persist.myapp.myservice.enable=1
    start myservice

on property:persist.myapp.myservice.enable=0
    stop myservice

有JNI内存资源需要在服务停止时清理,但是貌似服务是通过SIGKILL停止的:

6944  6974 V myservice: IMyProcessA::onTransact()
 712   712 I AP_KERNEL: init: Service 'myservice' is being killed...
 510   510 I ServiceManager: service 'ProcessA.MyService' died
 656   656 V ProcessA: ProcessA::NotificationClient::binderDied()
 656   656 V ProcessA: removeNotificationClient() 0xb6bc60a8, pid 6944
 656   656 V ProcessA: ProcessA::NotificationClient::~NotificationClient()
 712   712 I AP_KERNEL: init: Service 'myservice' (pid 6944) killed by signal 9
 712   712 I AP_KERNEL: init: Service 'myservice' (pid 6944) killing any children in process group 

因为没有办法拦截 SIGKILL 来清理,init.rc 'stop myservice' 是否有任何选项可以将终止信号更改为我可以拦截的信号,例如 SIGTERM?

最佳答案

你的“无内存泄漏”是在内核端,但是很多工作需要在一个进程结束前在用户空间完成,比如下面的,

  • 关闭文件并将缓存数据刷新到内核层。
  • 删除一些临时文件。
  • 通知其他进程我快要死了。
  • 将RAM中缓存的一些数据写入磁盘,否则磁盘上的数据会不一致。

…… 对于以上,发送SIGKILL是非常粗暴的。

关于android - 有没有办法优雅地停止 init.rc 中的 android 服务(没有 SIGKILL)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38838044/

相关文章:

java - 查看和查看组android

Android 保持秒表运行

java - Android上的HttpURLConnection什么时候真正调用请求

android - 第二次单击带有图像的 float 按钮(例如 : Add to favorite)

c - 尝试编译 spdif-loop 时出现 "undefined reference"错误消息

c - 在用户空间启用写入组合 IO 访问

Android 让 Service 与 Activity 进行通信?

java - 无法实例化服务 - Java.NullPointerException

linux - 在线 Linux Bash 演示

android - startForegroundService() 在 Oreo 中抛出 IllegalStateException on app BOOT_COMPLETED