Fixed wrong pid bug
The issue was that the thread ID wasn't factored in. A presumption was, that FUSE already returned the PID, not TID. The issue was fixed by implementing a function that translates the TID to PID.
This commit is contained in:
		@@ -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 {
 | 
			
		||||
 
 | 
			
		||||
@@ -7,9 +7,49 @@
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
#include "proc_operations.h"
 | 
			
		||||
#include <linux/limits.h>
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @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);
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
@@ -10,7 +10,11 @@
 | 
			
		||||
#define PROCESS_INFO_H
 | 
			
		||||
 | 
			
		||||
#include "proc_operations.h"
 | 
			
		||||
#include <errno.h>
 | 
			
		||||
#include <limits.h>
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#include <sys/types.h>
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
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;
 | 
			
		||||
 
 | 
			
		||||
@@ -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;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user