summaryrefslogtreecommitdiff
path: root/fdstats.c
blob: d2652af62f3af1ecece644dbad0646e8eb5a99f0 (plain)
    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 "fdstats.h"
   19 
   20 long _fgetsize(char* path) {
   21   FILE* fd = NULL;
   22   long size = 0;
   23   fd = fopen(path, "r");
   24 
   25   fseek(fd, 0, SEEK_END);
   26   size = ftell(fd);
   27 
   28   fclose(fd);
   29   return size;
   30 }
   31 
   32 int fdstats_read(struct fdstats* fds, char* fdpath, struct config* c) {
   33   int retval       = 0;
   34   int len          = 0;
   35   char lnpath[512] = {'\0'};
   36   struct stat sb;
   37 
   38   retval = stat(fdpath, &sb);
   39   len = readlink(fdpath, lnpath, 512);
   40 
   41   if(len <= 0)
   42     return -1;
   43   lnpath[len] = '\0';
   44 
   45   // Sockets
   46   if(S_ISSOCK(sb.st_mode)) {
   47     fds->sockets++;
   48     if(c->sockets)
   49       printf("%s -> %s\n", fdpath, lnpath);
   50 
   51   // Pipes
   52   } else if(S_ISFIFO(sb.st_mode)) {
   53     fds->pipes++;
   54     if(c->pipes)
   55       printf("%s -> %s\n", fdpath, lnpath);
   56 
   57   // Char device links
   58   } else if(S_ISCHR(sb.st_mode)) {
   59     fds->live++;
   60     if(c->live)
   61       printf("%s -> %s\n", fdpath, lnpath);
   62 
   63   // File desccriptors
   64   } else if(S_ISREG(sb.st_mode) || S_ISDIR(sb.st_mode)) {
   65     // Exists
   66     if(retval == 0 && access(lnpath, F_OK) == 0) {
   67       fds->live++;
   68       if(c->live)
   69         printf("%s -> %s\n", fdpath, lnpath);
   70       if(c->showsizefd || c->showsizelive)
   71         fds->livesize += _fgetsize(fdpath);
   72 
   73     // Does not exist
   74     } else {
   75       fds->dead++;
   76       if(c->dead)
   77         printf("%s -> %s\n", fdpath, lnpath);
   78       if(c->showsizefd || c->showsizedead)
   79         fds->deadsize += _fgetsize(fdpath);
   80       if(c->truncate_dead)
   81         truncate(fdpath, 0);
   82     }
   83 
   84   // Inodes
   85   } else if(sb.st_mode == 384) {
   86     fds->inodes++;
   87     if(c->inodes)
   88       printf("%s -> %s\n", fdpath, lnpath);
   89 
   90   } else {
   91     fds->unknown++;
   92   }
   93   return 0;
   94 }

Generated by cgit