summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaron Ball <nullspoon@oper.io>2022-08-18 18:08:45 -0600
committerAaron Ball <nullspoon@oper.io>2022-08-18 18:08:45 -0600
commit68322f9bf918f40d97ffbdeb12979f83597ed2f2 (patch)
treec00a31f4538c6fbf630a8c29d98f08bc79e3d5d2
parenta8058f8aa87ac470894459245146cb15300bde5d (diff)
downloadfd-enum-68322f9bf918f40d97ffbdeb12979f83597ed2f2.tar.gz
fd-enum-68322f9bf918f40d97ffbdeb12979f83597ed2f2.tar.xz
Support truncating dead file descriptors
It is a common needed functionality for leaky services to be able to easily zero out their dead file descriptors to prolong service lifetime before a reboot is needed. This adds the switch `--truncate-dead` which will cause fd-enum to truncate all dead file descriptors to zero length. This saves effort and runtime where previously the list of dead descriptors would need to be parsed and piped to another program for zeroing. This is much faster.
-rw-r--r--main.c37
1 files changed, 22 insertions, 15 deletions
diff --git a/main.c b/main.c
index 1921e14..f3b7459 100644
--- a/main.c
+++ b/main.c
@@ -30,6 +30,7 @@ struct config {
char inodes;
char pipes;
char showstats;
+ char truncate_dead;
};
int fexists(char* path) {
@@ -42,31 +43,35 @@ int fexists(char* path) {
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\
+ -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\
- -h, --help Print this help text\n\
+ -n, --no-stats Hide file descriptor statistics\n\
+ --truncate-dead Truncate dead file descriptors\n\
+ -h, --help Print this help text\n\
");
}
int parse_args(int argc, char* argv[], struct config* c) {
// Initialize config struct
- c->pid = -1;
- c->dead = 0;
- c->live = 0;
- c->sockets = 0;
- c->pipes = 0;
- c->inodes = 0;
- c->showstats = 1;
+ c->pid = -1;
+ c->dead = 0;
+ c->truncate_dead = 0;
+ c->live = 0;
+ c->sockets = 0;
+ c->pipes = 0;
+ c->inodes = 0;
+ c->showstats = 1;
int i = 1;
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) {
@@ -102,7 +107,7 @@ int main(int argc, char* argv[]) {
char lnpath[512] = {'\0'};
int retval = 0;
int len = 0;
- unsigned long unknown = 0;
+ unsigned long unknown = 0;
unsigned long dead = 0;
unsigned long live = 0;
unsigned long sockets = 0;
@@ -163,6 +168,8 @@ int main(int argc, char* argv[]) {
dead++;
if(c.dead)
printf("%s -> %s\n", fdpath, lnpath);
+ if(c.truncate_dead)
+ truncate(fdpath, 0);
}
// Inodes

Generated by cgit