1 /**
2 * Fd-enum is a simple tool to list open file descriptors of a pid by type
3 * Copyright (C) 2023 Aaron Ball <nullspoon@oper.io>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <dirent.h>
21 #include <unistd.h>
22 #include <string.h>
23 #include <sys/stat.h>
24
25 #include "fdstats.h"
26 #include "config.h"
27
28 int main(int argc, char* argv[]) {
29 char pidpath_fd[32] = {'\0'};
30 char fdpath[289] = {'\0'};
31 DIR *dp = NULL;
32 struct dirent *de = NULL;
33 struct config c;
34 struct fdstats fds;
35
36 //if(parse_args(argc, argv, &c) != 0)
37 memset(&c, 0, sizeof(struct config));
38 c.pid = -1; // Default to an impossible pid
39 c.showstats = 1; // Default to showing stats
40
41 if(config_from_argv(&c, argc, argv) != 0)
42 return 1;
43
44 // Construct the pid path
45 sprintf(pidpath_fd, "/proc/%d/fd", c.pid);
46
47 dp = opendir(pidpath_fd);
48
49 // Initialize all fdstats to 0
50 memset(&fds, 0, sizeof(struct fdstats));
51
52 while((de = readdir(dp)) != NULL) {
53 sprintf(fdpath, "%s/%s%c", pidpath_fd, de->d_name, '\0');
54 fdstats_read(&fds, fdpath, &c);
55 }
56 closedir(dp);
57
58 if(c.showstats) {
59 printf("dead: %ld\n", fds.dead);
60 printf("live: %ld\n", fds.live);
61 printf("sockets: %ld\n", fds.sockets);
62 printf("anon_inodes: %ld\n", fds.inodes);
63 printf("pipes: %ld\n", fds.pipes);
64 printf("unknown: %ld\n", fds.unknown);
65 }
66 if(c.showsizelive)
67 printf("livesize: %ld KB\n", fds.livesize / 1024);
68 if(c.showsizedead)
69 printf("deadsize: %ld KB\n", fds.deadsize / 1024);
70 if(c.showsizefd)
71 printf("fdsize: %ld KB\n", (fds.livesize + fds.deadsize) / 1024);
72
73 return 0;
74 }
|