/** * Fd-enum is a simple tool to list open file descriptors of a pid by type * Copyright (C) 2023 Aaron Ball * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include "fdstats.h" long _fgetsize(char* path) { FILE* fd = NULL; long size = 0; fd = fopen(path, "r"); fseek(fd, 0, SEEK_END); size = ftell(fd); fclose(fd); return size; } int fdstats_read(struct fdstats* fds, char* fdpath, struct config* c) { int retval = 0; int len = 0; char lnpath[512] = {'\0'}; struct stat sb; retval = stat(fdpath, &sb); len = readlink(fdpath, lnpath, 512); if(len <= 0) return -1; lnpath[len] = '\0'; // Sockets if(S_ISSOCK(sb.st_mode)) { fds->sockets++; if(c->sockets) printf("%s -> %s\n", fdpath, lnpath); // Pipes } else if(S_ISFIFO(sb.st_mode)) { fds->pipes++; if(c->pipes) printf("%s -> %s\n", fdpath, lnpath); // Char device links } else if(S_ISCHR(sb.st_mode)) { fds->live++; if(c->live) printf("%s -> %s\n", fdpath, lnpath); // File desccriptors } else if(S_ISREG(sb.st_mode) || S_ISDIR(sb.st_mode)) { // Exists if(retval == 0 && access(lnpath, F_OK) == 0) { fds->live++; if(c->live) printf("%s -> %s\n", fdpath, lnpath); if(c->showsizefd || c->showsizelive) fds->livesize += _fgetsize(fdpath); // Does not exist } else { fds->dead++; if(c->dead) printf("%s -> %s\n", fdpath, lnpath); if(c->showsizefd || c->showsizedead) fds->deadsize += _fgetsize(fdpath); if(c->truncate_dead) truncate(fdpath, 0); } // Inodes } else if(sb.st_mode == 384) { fds->inodes++; if(c->inodes) printf("%s -> %s\n", fdpath, lnpath); } else { fds->unknown++; } return 0; }