int Process::checkFileMaps(int pid, const char *mountPoint, char *openFilename, size_t max) { FILE *file; char buffer[PATH_MAX + 100]; sprintf(buffer, "/proc/%d/maps", pid); file = fopen(buffer, "r"); if (!file) return 0; while (fgets(buffer, sizeof(buffer), file)) { // skip to the path const char* path = strchr(buffer, '/'); if (path && pathMatchesMountPoint(path, mountPoint)) { if (openFilename) { memset(openFilename, 0, max); strncpy(openFilename, path, max-1); } fclose(file); return 1; } } fclose(file); return 0; }
int Process::checkSymLink(int pid, const char *mountPoint, const char *name) { char path[PATH_MAX]; char link[PATH_MAX]; sprintf(path, "/proc/%d/%s", pid, name); if (readSymLink(path, link, sizeof(link)) && pathMatchesMountPoint(link, mountPoint)) return 1; return 0; }
int checkfileDescriptorSymLinks(int pid, const char *mountPoint, char *openFilename, size_t max) { // compute path to process's directory of open files char path[PATH_MAX]; sprintf(path, "/proc/%d/fd", pid); DIR *dir = opendir(path); if (!dir) return 0; // remember length of the path int parent_length = strlen(path); // append a trailing '/' path[parent_length++] = '/'; struct dirent *de; while ((de = readdir(dir))) { if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..") || strlen(de->d_name) + parent_length + 1 >= PATH_MAX) continue; // append the file name, after truncating to parent directory path[parent_length] = 0; strcat(path, de->d_name); char link[PATH_MAX]; if (readSymLink(path, link, sizeof(link)) && pathMatchesMountPoint(link, mountPoint)) { if (openFilename) { memset(openFilename, 0, max); strncpy(openFilename, link, max - 1); } closedir(dir); return 1; } } closedir(dir); return 0; }