Skip to content

Commit

Permalink
Rearranged runsu check and added more logging. fork() seems to get st…
Browse files Browse the repository at this point in the history
…uck some of the time, need to investigate further.
  • Loading branch information
devadvance committed Nov 23, 2015
1 parent 4ab8479 commit 298207e
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions app/src/main/jni/native2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,39 +80,48 @@ extern "C" {

JNIEXPORT jboolean JNICALL Java_com_devadvance_rootinspector_Root_runsu(JNIEnv * env, jobject obj, jstring command)
{
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NATIVE: runsu: starting...\n");
jboolean run_su_success = 1;
jboolean isCopy;
const char * path = env->GetStringUTFChars(command, &isCopy);
int status;
if(fork() == 0){
int status = 1;
pid_t pid = -1;
pid = fork();
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NATIVE: runsu: pid is: [%d]\n", pid);

if(pid == 0) {
// Child process will return 0 from fork()
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NATIVE: Child process here.");
int result = execl("/system/xbin/su", "su", NULL);
int errsv = errno;
if (result != 0) {
char buffer[256];
char * errorMessage = strerror_r( errsv, buffer, 256 ); // get string message from errno
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NATIVE: execl error: [%s]", errorMessage);
char * errorMessage = (char *)strerror_r( errsv, buffer, 256 ); // get string message from errno
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NATIVE: execl error: [%s]\n", errorMessage);
exit(1);
} else {
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NATIVE: execl no error");
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NATIVE: execl no error\n");
exit(0);
}
}else{
} else if (pid > 0){
// Parent process will return a non-zero value from fork()
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NATIVE: Parent process here.");
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NATIVE: Parent process here.\n");
wait(&status);
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NATIVE: Parent done waiting.");
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NATIVE: Parent done waiting.\n");
} else {
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NATIVE: runsu: error with fork()\n");
status = 1;
}

__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NATIVE: status: [%d]", status);
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NATIVE: status: [%d]\n", status);
if (status) {
run_su_success = 0;
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NATIVE: Error in running command.");
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NATIVE: Error in running command.\n");
} else {
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NATIVE: Success in running command.");
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NATIVE: Success in running command.\n");
}

__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NATIVE: runsu: finishing...\n");
return run_su_success;
}

Expand Down

0 comments on commit 298207e

Please sign in to comment.