summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--main.c57
1 files changed, 49 insertions, 8 deletions
diff --git a/main.c b/main.c
index f3b7459..7a9f526 100644
--- a/main.c
+++ b/main.c
@@ -30,6 +30,9 @@ struct config {
char inodes;
char pipes;
char showstats;
+ char showsizefd;
+ char showsizelive;
+ char showsizedead;
char truncate_dead;
};
@@ -50,11 +53,26 @@ void get_help() {
-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) {
// Initialize config struct
c->pid = -1;
@@ -65,6 +83,9 @@ int parse_args(int argc, char* argv[], struct config* c) {
c->pipes = 0;
c->inodes = 0;
c->showstats = 1;
+ c->showsizefd = 0;
+ c->showsizedead = 0;
+ c->showsizelive = 0;
int i = 1;
while(i < argc) {
@@ -82,9 +103,18 @@ int parse_args(int argc, char* argv[], struct config* c) {
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);
}
@@ -107,12 +137,14 @@ int main(int argc, char* argv[]) {
char lnpath[512] = {'\0'};
int retval = 0;
int len = 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;
+ 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;
@@ -155,19 +187,22 @@ int main(int argc, char* argv[]) {
printf("%s -> %s\n", fdpath, lnpath);
// File desccriptors
- } else if(S_ISREG(sb.st_mode)
- || S_ISDIR(sb.st_mode)) {
+ } 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);
}
@@ -192,6 +227,12 @@ int main(int argc, char* argv[]) {
printf("pipes: %ld\n", pipes);
printf("unknown: %ld\n", unknown);
}
+ if(c.showsizelive)
+ printf("livesize: %ld KB\n", livesize / 1024);
+ if(c.showsizedead)
+ printf("deadsize: %ld KB\n", deadsize / 1024);
+ if(c.showsizefd)
+ printf("fdsize: %ld KB\n", (livesize + deadsize) / 1024);
return 0;
}

Generated by cgit