diff --git a/src/perm_permissions_table.c b/src/perm_permissions_table.c index 063b923..0759319 100644 --- a/src/perm_permissions_table.c +++ b/src/perm_permissions_table.c @@ -272,7 +272,7 @@ access_t check_perm_access(const char *filename, struct process_info pi) { } current_pi.name = NULL; while (current_pi.name == NULL) { - current_pi.PID = get_parent_pid(current_pi.PID); + current_pi.PID = get_main_thread_pid(get_parent_pid(current_pi.PID)); if (current_pi.PID != 0) { current_pi.name = get_process_name_by_pid(current_pi.PID); } else { diff --git a/src/proc_operations.c b/src/proc_operations.c index f6308d9..b440c85 100644 --- a/src/proc_operations.c +++ b/src/proc_operations.c @@ -7,9 +7,49 @@ */ #include "proc_operations.h" +#include #include #include +/** + * @brief Returns the PID of the main thread (i.e., the process ID) of the + * process that the given thread ID (tid) belongs to. + * + * @param tid The thread ID (TID) of any thread in the process. + * @return pid_t The process ID (main thread's PID), or -1 on error. + */ +pid_t get_main_thread_pid(pid_t tid) { + // Validate input + if (tid <= 0) { + // errno = EINVAL; + return 0; + } + + char path[PATH_MAX]; + snprintf(path, sizeof(path), "/proc/%d/status", tid); + + FILE *fp = fopen(path, "r"); + if (!fp) { + return 0; // Could not open the file + } + + pid_t tgid = 0; + char line[256]; + + while (fgets(line, sizeof(line), fp)) { + if (sscanf(line, "Tgid: %d", &tgid) == 1) { + break; + } + } + + fclose(fp); + if (tgid != tid) { + fprintf(stderr, "The tid and and pid wasn't equal. tid:%d, pid:%d.\n", tid, + tgid); + } + return tgid; +} + char *get_process_name_by_pid(const int pid) { char path[1024]; sprintf(path, "/proc/%d/exe", pid); diff --git a/src/proc_operations.h b/src/proc_operations.h index 9b0a5de..152dc9e 100644 --- a/src/proc_operations.h +++ b/src/proc_operations.h @@ -22,4 +22,13 @@ char *get_process_name_by_pid(const int pid); */ pid_t get_parent_pid(pid_t pid); +/** + * @brief Returns the PID of the main thread (i.e., the process ID) of the + * process that the given thread ID (tid) belongs to. + * + * @param tid The thread ID (TID) of any thread in the process. + * @return pid_t The process ID (main thread's PID), or -1 on error. + */ +pid_t get_main_thread_pid(pid_t tid); + #endif // !PROC_OPERATIONS diff --git a/src/process_info.h b/src/process_info.h index cb63881..993ab23 100644 --- a/src/process_info.h +++ b/src/process_info.h @@ -10,7 +10,11 @@ #define PROCESS_INFO_H #include "proc_operations.h" +#include +#include +#include #include +#include struct process_info { pid_t PID; char *name; @@ -18,7 +22,7 @@ struct process_info { static inline struct process_info get_process_info(pid_t pid) { struct process_info pi; - pi.PID = pid; + pi.PID = get_main_thread_pid(pid); pi.name = get_process_name_by_pid(pi.PID); if (pi.name == NULL) { pi.PID = 0; diff --git a/src/temp_permissions_table.c b/src/temp_permissions_table.c index cbfdad5..13f471e 100644 --- a/src/temp_permissions_table.c +++ b/src/temp_permissions_table.c @@ -237,7 +237,7 @@ access_t check_temp_access(const char *filename, struct process_info pi) { if (access != NDEF) { return access; } - current_pid = get_parent_pid(current_pid); + current_pid = get_main_thread_pid(get_parent_pid(current_pid)); } return NDEF;