observer.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright (C) 2009 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. #include <string.h>
  18. #include <jni.h>
  19. #include <jni.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <android/log.h>
  24. #include <unistd.h>
  25. #include <sys/inotify.h>
  26. /* 宏定义begin */
  27. //清0宏
  28. #define MEM_ZERO(pDest, destSize) memset(pDest, 0, destSize)
  29. //LOG宏定义
  30. #define LOG_INFO(tag, msg) __android_log_write(ANDROID_LOG_INFO, tag, msg)
  31. #define LOG_DEBUG(tag, msg) __android_log_write(ANDROID_LOG_DEBUG, tag, msg)
  32. #define LOG_WARN(tag, msg) __android_log_write(ANDROID_LOG_WARN, tag, msg)
  33. #define LOG_ERROR(tag, msg) __android_log_write(ANDROID_LOG_ERROR, tag, msg)
  34. /* 内全局变量begin */
  35. static char c_TAG[] = "onEvent";
  36. static jboolean b_IS_COPY = JNI_TRUE;
  37. jstring Java_com_zgy_catchuninstallself_UninstallObserver_startWork(JNIEnv* env,
  38. jobject thiz, jstring path, jstring url, jint version) {
  39. jstring tag = (*env)->NewStringUTF(env, c_TAG);
  40. //初始化log
  41. LOG_DEBUG((*env)->GetStringUTFChars(env, tag, &b_IS_COPY),
  42. (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env, "init OK"),
  43. &b_IS_COPY));
  44. //fork子进程,以执行轮询任务
  45. pid_t pid = fork();
  46. if (pid < 0) {
  47. //出错log
  48. LOG_ERROR((*env)->GetStringUTFChars(env, tag, &b_IS_COPY),
  49. (*env)->GetStringUTFChars(env,
  50. (*env)->NewStringUTF(env, "fork failed !!!"),
  51. &b_IS_COPY));
  52. } else if (pid == 0) {
  53. //子进程注册目录监听器
  54. int fileDescriptor = inotify_init();
  55. if (fileDescriptor < 0) {
  56. LOG_DEBUG((*env)->GetStringUTFChars(env, tag, &b_IS_COPY),
  57. (*env)->GetStringUTFChars(env,
  58. (*env)->NewStringUTF(env,
  59. "inotify_init failed !!!"), &b_IS_COPY));
  60. exit(1);
  61. }
  62. int watchDescriptor;
  63. watchDescriptor = inotify_add_watch(fileDescriptor,
  64. (*env)->GetStringUTFChars(env, path, NULL), IN_DELETE);
  65. if (watchDescriptor < 0) {
  66. LOG_DEBUG((*env)->GetStringUTFChars(env, tag, &b_IS_COPY),
  67. (*env)->GetStringUTFChars(env,
  68. (*env)->NewStringUTF(env,
  69. "inotify_add_watch failed !!!"),
  70. &b_IS_COPY));
  71. exit(1);
  72. }
  73. //分配缓存,以便读取event,缓存大小=一个struct inotify_event的大小,这样一次处理一个event
  74. void *p_buf = malloc(sizeof(struct inotify_event));
  75. if (p_buf == NULL) {
  76. LOG_DEBUG((*env)->GetStringUTFChars(env, tag, &b_IS_COPY),
  77. (*env)->GetStringUTFChars(env,
  78. (*env)->NewStringUTF(env, "malloc failed !!!"),
  79. &b_IS_COPY));
  80. exit(1);
  81. }
  82. //开始监听
  83. LOG_DEBUG((*env)->GetStringUTFChars(env, tag, &b_IS_COPY),
  84. (*env)->GetStringUTFChars(env,
  85. (*env)->NewStringUTF(env, "start observer"),
  86. &b_IS_COPY));
  87. //read会阻塞进程,
  88. size_t readBytes = read(fileDescriptor, p_buf,
  89. sizeof(struct inotify_event));
  90. //走到这里说明收到目录被删除的事件,注销监听器
  91. free(p_buf);
  92. inotify_rm_watch(fileDescriptor, IN_DELETE);
  93. //目录不存在log
  94. LOG_DEBUG((*env)->GetStringUTFChars(env, tag, &b_IS_COPY),
  95. (*env)->GetStringUTFChars(env,
  96. (*env)->NewStringUTF(env, "uninstalled"), &b_IS_COPY));
  97. if (version >= 17) {
  98. //4.2以上的系统由于用户权限管理更严格,需要加上 --user 0
  99. execlp("am", "am", "start", "--user", "0", "-a",
  100. "android.intent.action.VIEW", "-d",
  101. (*env)->GetStringUTFChars(env, url, NULL), (char *) NULL);
  102. } else {
  103. execlp("am", "am", "start", "-a", "android.intent.action.VIEW",
  104. "-d", (*env)->GetStringUTFChars(env, url, NULL),
  105. (char *) NULL);
  106. }
  107. //扩展:可以执行其他shell命令,am(即activity manager),可以打开某程序、服务,broadcast intent,等等
  108. } else {
  109. //父进程直接退出,使子进程被init进程领养,以避免子进程僵死
  110. }
  111. return (*env)->NewStringUTF(env, "Hello from JNI !");
  112. }