summaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
authorAaron Ball <nullspoon@oper.io>2023-06-09 17:49:04 -0600
committerAaron Ball <nullspoon@oper.io>2023-06-09 17:49:04 -0600
commit102f10b0c2bb16cdbc654506cd2fa4000c66de78 (patch)
tree8910cd95061820dda6dd790fb090ef2fbcf479a5 /main.c
parent2fcb8ea3ffa50b4866e1d97f3625129c8257605b (diff)
downloadfd-enum-102f10b0c2bb16cdbc654506cd2fa4000c66de78.tar.gz
fd-enum-102f10b0c2bb16cdbc654506cd2fa4000c66de78.tar.xz
Reorganize code
This moves file descriptor stating code into the new fdstats files, and the config code into the new config files. Relevant updates made to reference structs by pointer.
Diffstat (limited to 'main.c')
-rw-r--r--main.c206
1 files changed, 25 insertions, 181 deletions
diff --git a/main.c b/main.c
index c384896..fd6254e 100644
--- a/main.c
+++ b/main.c
@@ -22,209 +22,53 @@
#include <string.h>
#include <sys/stat.h>
-struct config {
- pid_t pid;
- char dead;
- char live;
- char sockets;
- char inodes;
- char pipes;
- char showstats;
- char showsizefd;
- char showsizelive;
- char showsizedead;
- char truncate_dead;
-};
-
-int fexists(char* path) {
- FILE *fd = fopen(path, "r");
- if(!fd)
- return 0;
- fclose(fd);
- return 1;
-}
-
-void get_help() {
- puts("Usage: \n\
- -d, --dead Print list of dead file descriptors (deleted but still open)\n\
- -i, --inodes Print list of inode file descriptors\n\
- -l, --live Print list of live file descriptors (open and existing)\n\
- -p, --pipes Print list of pipe file descriptors\n\
- -s, --sockets Print list of socket file descriptors\n\
-\n\
- -n, --no-stats Hide file descriptor statistics\n\
- -sf, --sizefd Show total size of dead and live file descriptors\n\
- -sd, --sizedead Show total size of dead file descriptors\n\
- -sl, --sizelive Show total size of live file descriptors\n\
- --truncate-dead Truncate dead file descriptors\n\
- -h, --help Print this help text\n\
-");
-}
-
-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 parse_args(int argc, char* argv[], struct config* c) {
- int i = 1;
- // Initialize config struct to all 0
- memset(c, 0, sizeof(struct config));
- c->pid = -1; // Default to an impossible pid
- c->showstats = 1; // Default to showing stats
-
- while(i < argc) {
- if(strcmp(argv[i], "-d") == 0 || strcmp(argv[i], "--dead") == 0) {
- c->dead = 1;
- } else if(strcmp(argv[i], "--truncate-dead") == 0) {
- c->truncate_dead = 1;
- } else if(strcmp(argv[i], "-l") == 0 || strcmp(argv[i], "--live") == 0) {
- c->live = 1;
- } else if(strcmp(argv[i], "-s") == 0 || strcmp(argv[i], "--sockets") == 0) {
- c->sockets = 1;
- } else if(strcmp(argv[i], "-i") == 0 || strcmp(argv[i], "--inodes") == 0) {
- c->inodes = 1;
- } else if(strcmp(argv[i], "-p") == 0 || strcmp(argv[i], "--pipes") == 0) {
- c->pipes = 1;
- } else if(strcmp(argv[i], "-n") == 0 || strcmp(argv[i], "--no-stats") == 0) {
- c->showstats = 0;
- } else if(strcmp(argv[i], "-sfd") == 0 || strcmp(argv[i], "--sizefd") == 0) {
- c->showsizefd = 1;
- } else if(strcmp(argv[i], "-sd") == 0 || strcmp(argv[i], "--sizedead") == 0) {
- c->showsizedead = 1;
- } else if(strcmp(argv[i], "-sl") == 0 || strcmp(argv[i], "--sizelive") == 0) {
- c->showsizelive = 1;
- } else if(strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
- get_help();
- exit(1);
- } else if(argv[i][0] == '-') {
- fprintf(stderr, "Unknown argument \"%s\"\n", argv[i]);
- exit(1);
- } else {
- c->pid = strtol(argv[i], NULL, 10);
- }
-
- i++;
- }
-
- if(c->pid == -1) {
- fprintf(stderr, "Must specify valid pid\n");
- return 2;
- }
- return 0;
-}
+#include "fdstats.h"
+#include "config.h"
int main(int argc, char* argv[]) {
- struct config c;
-
char pidpath_fd[32] = {'\0'};
char fdpath[289] = {'\0'};
- char lnpath[512] = {'\0'};
- int retval = 0;
- int len = 0;
- unsigned long livesize = 0;
- unsigned long deadsize = 0;
- unsigned long unknown = 0;
- unsigned long dead = 0;
- unsigned long live = 0;
- unsigned long sockets = 0;
- unsigned long inodes = 0;
- unsigned long pipes = 0;
-
- struct stat sb;
+ DIR *dp = NULL;
+ struct dirent *de = NULL;
+ struct config c;
+ struct fdstats fds;
- DIR *dp = NULL;
- struct dirent *de = NULL;
+ //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(parse_args(argc, argv, &c) != 0)
+ 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);
- while((de = readdir(dp)) != NULL) {
- sprintf(fdpath, "%s/%s%c", pidpath_fd, de->d_name, '\0');
-
- retval = stat(fdpath, &sb);
- len = readlink(fdpath, lnpath, 512);
-
- if(len <= 0)
- continue;
- lnpath[len] = '\0';
- // Sockets
- if(S_ISSOCK(sb.st_mode)) {
- sockets++;
- if(c.sockets)
- printf("%s -> %s\n", fdpath, lnpath);
+ // Initialize all fdstats to 0
+ memset(&fds, 0, sizeof(struct fdstats));
- // Pipes
- } else if(S_ISFIFO(sb.st_mode)) {
- pipes++;
- if(c.pipes)
- printf("%s -> %s\n", fdpath, lnpath);
-
- // Char device links
- } else if(S_ISCHR(sb.st_mode)) {
- 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 && fexists(lnpath)) {
- live++;
- if(c.live)
- printf("%s -> %s\n", fdpath, lnpath);
- if(c.showsizefd || c.showsizelive)
- livesize += fgetsize(fdpath);
-
- // Does not exist
- } else {
- dead++;
- if(c.dead)
- printf("%s -> %s\n", fdpath, lnpath);
- if(c.showsizefd || c.showsizedead)
- deadsize += fgetsize(fdpath);
- if(c.truncate_dead)
- truncate(fdpath, 0);
- }
-
- // Inodes
- } else if(sb.st_mode == 384) {
- inodes++;
- if(c.inodes)
- printf("%s -> %s\n", fdpath, lnpath);
-
- } else {
- unknown++;
- }
+ 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", dead);
- printf("live: %ld\n", live);
- printf("sockets: %ld\n", sockets);
- printf("anon_inodes: %ld\n", inodes);
- printf("pipes: %ld\n", pipes);
- printf("unknown: %ld\n", unknown);
+ 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", livesize / 1024);
+ printf("livesize: %ld KB\n", fds.livesize / 1024);
if(c.showsizedead)
- printf("deadsize: %ld KB\n", deadsize / 1024);
+ printf("deadsize: %ld KB\n", fds.deadsize / 1024);
if(c.showsizefd)
- printf("fdsize: %ld KB\n", (livesize + deadsize) / 1024);
+ printf("fdsize: %ld KB\n", (fds.livesize + fds.deadsize) / 1024);
return 0;
}

Generated by cgit