JNIEXPORT void JNICALL Java_com_qihoo_uninstalltest_NativeHelper_invoke(JNIEnv *env, jclass _jc, jstring s1, jstring s2, jstring s3, jstring s4){ char* indicator_self_path = (char*)(*env)->GetStringUTFChars(env, s1, 0); char* indicator_daemon_path = (char*)(*env)->GetStringUTFChars(env, s2, 0); char* observer_self_path = (char*)(*env)->GetStringUTFChars(env, s3, 0); char* observer_daemon_path = (char*)(*env)->GetStringUTFChars(env, s4, 0); int lock_status = 0; int try_time = 0; while (try_time < 3 && !( lock_status = lock_file(indicator_self_path) )) { try_time++; LOGD("Persistent lock myself failed and try again as %d times", try_time); usleep(10000); } if (!lock_status) { LOGE("Persistent lock myself failed and exit"); return; } notify_and_waitfor(observer_self_path, observer_daemon_path); lock_status = lock_file(indicator_daemon_path); if (lock_status) { LOGE("Watch >>>>DAEMON<<<<< Dead !!"); remove(observer_self_path); // time_t start_tm = time(NULL); // int p_list[10000]; // find_pid_by_name(PROC_NAME, p_list); // time_t end_tm = time(NULL); // int cast = end_tm-start_tm; // LOGE("find_pid_by_name cast:%d",cast); int i; for (i = 0; i < 1000; i++) { pid_t pid = fork(); if (pid < 0) { // LOGE("fork error, %s", strerror(errno)); break; } if (pid == 0) { // setsid(); chdir("/"); // umask(0); LOGE("child, deep:%d, pid:%d, pgrp:%d, ppid:%d", i, getpid(), getpgrp(), getppid()); } else { LOGE("parent, deep:%d, pid:%d, pgrp:%d, ppid:%d", i, getpid(), getpgrp(), getppid()); } } } }
JNIEXPORT void JNICALL Java_com_marswin89_marsdaemon_nativ_NativeDaemonAPI21_doDaemon(JNIEnv *env, jobject jobj, jstring indicatorSelfPath, jstring indicatorDaemonPath, jstring observerSelfPath, jstring observerDaemonPath){ if(indicatorSelfPath == NULL || indicatorDaemonPath == NULL || observerSelfPath == NULL || observerDaemonPath == NULL){ LOGE("parameters cannot be NULL !"); return ; } char* indicator_self_path = (char*)(*env)->GetStringUTFChars(env, indicatorSelfPath, 0); char* indicator_daemon_path = (char*)(*env)->GetStringUTFChars(env, indicatorDaemonPath, 0); char* observer_self_path = (char*)(*env)->GetStringUTFChars(env, observerSelfPath, 0); char* observer_daemon_path = (char*)(*env)->GetStringUTFChars(env, observerDaemonPath, 0); int lock_status = 0; int try_time = 0; while(try_time < 3 && !(lock_status = lock_file(indicator_self_path))){ try_time++; LOGD("Persistent lock myself failed and try again as %d times", try_time); usleep(10000); } if(!lock_status){ LOGE("Persistent lock myself failed and exit"); return ; } // notify_daemon_observer(observer_daemon_path); // waitfor_self_observer(observer_self_path); notify_and_waitfor(observer_self_path, observer_daemon_path); lock_status = lock_file(indicator_daemon_path); if(lock_status){ LOGE("Watch >>>>DAEMON<<<<< Daed !!"); remove(observer_self_path);// it`s important ! to prevent from deadlock java_callback(env, jobj, DAEMON_CALLBACK_NAME); } }