/** * 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 #include #include #include #include #include #include "fdstats.h" #include "config.h" int main(int argc, char* argv[]) { char pidpath_fd[32] = {'\0'}; char fdpath[289] = {'\0'}; DIR *dp = NULL; struct dirent *de = NULL; struct config c; struct fdstats fds; //if(parse_args(argc, argv, &c) != 0) memset(&c, 0, sizeof(struct config)); c.pid = -1; // Default to an impossible pid c.showstats = 1; // Default to showing stats if(config_from_argv(&c, argc, argv) != 0) return 1; // Construct the pid path sprintf(pidpath_fd, "/proc/%d/fd", c.pid); dp = opendir(pidpath_fd); // Initialize all fdstats to 0 memset(&fds, 0, sizeof(struct fdstats)); while((de = readdir(dp)) != NULL) { sprintf(fdpath, "%s/%s%c", pidpath_fd, de->d_name, '\0'); fdstats_read(&fds, fdpath, &c); } closedir(dp); if(c.showstats) { printf("dead: %ld\n", fds.dead); printf("live: %ld\n", fds.live); printf("sockets: %ld\n", fds.sockets); printf("anon_inodes: %ld\n", fds.inodes); printf("pipes: %ld\n", fds.pipes); printf("unknown: %ld\n", fds.unknown); } if(c.showsizelive) printf("livesize: %ld KB\n", fds.livesize / 1024); if(c.showsizedead) printf("deadsize: %ld KB\n", fds.deadsize / 1024); if(c.showsizefd) printf("fdsize: %ld KB\n", (fds.livesize + fds.deadsize) / 1024); return 0; }