diff --git a/include/process.h b/include/process.h index 55713cb..8e663a4 100644 --- a/include/process.h +++ b/include/process.h @@ -46,4 +46,10 @@ int determine_parent_process(process_status_t* list, size_t count, process_statu // get all process threads. for list and count same rules applies as for processes_by_name int process_get_threads(pid_t pid, process_status_t** list, size_t* count); +// returns 1 if state considered active for a process/thread +int is_considered_active(process_state_t state); + +// find any active (running) thread and returns 0 and success, otherwise non zero +int find_active_thread(process_status_t* list, size_t count, process_status_t** thread); + #endif \ No newline at end of file diff --git a/src/dummy_target.c b/src/dummy_target.c index 3ab814c..cfa9a3e 100644 --- a/src/dummy_target.c +++ b/src/dummy_target.c @@ -42,7 +42,9 @@ __attribute__((noreturn)) void* slave3_job(void*) puts("[slave3] will do something each second but in a thread"); while (1) { - __asm__("nop"); + unsigned a = 3; + for (unsigned i = 0; i < 100000; i++) + a = a * 3 - a; sleep(1); } } diff --git a/src/main.c b/src/main.c index 2a854b4..2fd8706 100644 --- a/src/main.c +++ b/src/main.c @@ -1,3 +1,4 @@ +#define _DEFAULT_SOURCE #include #include #include @@ -36,21 +37,39 @@ int main(int argc, char** argv) print_process(parent); - // get threads + // find active thread + puts("Looking for active thread.."); + process_status_t* threads = NULL; size_t thread_count = 0; - if (process_get_threads(parent->pid, &threads, &thread_count)) + + process_status_t* active; + while (1) { - fputs("failed to obtain process threads\n", stderr); - free(list); - return 1; + if (process_get_threads(parent->pid, &threads, &thread_count)) + { + fputs("failed to obtain process threads\n", stderr); + free(list); + return 1; + } + + if (find_active_thread(threads, thread_count, &active)) + { + // no active threads - free list and continue + free(threads); + usleep(500*1000); + } + else + { + // we got active thread! + break; + } } - puts("Threads:"); - for (size_t i = 0; i < thread_count; i++) - print_process(&threads[i]); - - free(list); + puts("Active thread:"); + print_process(active); + free(threads); + free(list); return 0; } \ No newline at end of file diff --git a/src/process.c b/src/process.c index 53931fa..d6ba9f9 100644 --- a/src/process.c +++ b/src/process.c @@ -67,6 +67,9 @@ static int is_numeric(const char* str) int processes_by_name(const char* name, process_status_t** list, size_t* count) { + *list = NULL; + *count = 0; + DIR* proc = opendir("/proc"); if (!proc) return 1; @@ -140,6 +143,9 @@ int determine_parent_process(process_status_t* list, size_t count, process_statu int process_get_threads(pid_t pid, process_status_t** list, size_t* count) { + *list = NULL; + *count = 0; + char taskPath[256] = {0}; snprintf(taskPath, sizeof(taskPath), "/proc/%d/task", pid); @@ -186,4 +192,23 @@ int process_get_threads(pid_t pid, process_status_t** list, size_t* count) closedir(taskDir); return 0; +} + +int is_considered_active(process_state_t state) +{ + return state == INTERRUPTIBLE_SLEEP || state == RUNNING; +} + +int find_active_thread(process_status_t* list, size_t count, process_status_t** thread) +{ + for (size_t i = 0; i < count; i++) + { + TRACE("task %d state %d\n", list[i].pid, list[i].state); + if (is_considered_active(list[i].state)) + { + *thread = &list[i]; + return 0; + } + } + return 1; } \ No newline at end of file