/* ICFS: Interactively Controlled File System Copyright (C) 2024-2025 Fedir Kovalov This program can be distributed under the terms of the GNU GPLv2. See the file LICENSE. */ #include "proc_operations.h" #include #include #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); size_t size = 128; char *name = malloc(size); if (name == NULL) { fprintf(stderr, "Could not get process name by pid %d", pid); perror(""); return NULL; } while (1) { ssize_t len = readlink(path, name, size); if (len == -1) { fprintf(stderr, "Could not get process name by pid %d", pid); perror(""); free(name); return NULL; } if ((size_t)len >= size) { size *= 2; char *new_name = realloc(name, size); if (!new_name) { free(name); return NULL; } name = new_name; } else { // readlink does not set the null character name[len] = 0; break; } } return name; } /** * Finds the parent process ID of a given process. * * @param pid: The process ID of the process to find the parent of * @return: The parent process ID, or 0 if the parent process ID could not be * found */ pid_t get_parent_pid(pid_t pid) { pid_t ppid = 0; char path[256]; snprintf(path, sizeof(path), "/proc/%u/status", pid); FILE *file = fopen(path, "r"); if (file == NULL) { perror("Failed to open /proc//status"); return 0; } char line[256]; while (fgets(line, sizeof(line), file)) { if (sscanf(line, "PPid:\t%d", &ppid) == 1) { fclose(file); return ppid; } } fclose(file); return 0; // Parent PID not found }