-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
182 lines (154 loc) · 5.38 KB
/
main.c
File metadata and controls
182 lines (154 loc) · 5.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* Copyright (C) 2017 Marco Almeida <marcoafalmeida@gmail.com> */
/* This file is part of fdstat. */
/* fdstat is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 2 of the License, or */
/* (at your option) any later version. */
/* fdstat is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* You should have received a copy of the GNU General Public License */
/* along with fdstat; if not, write to the Free Software */
/* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <argp.h>
#include "arg_parser.h"
#include "util.h"
#define _POSIX_C_SOURCE >= 200809L
int count_fds(const char *pid)
{
char path[MAX_PATH];
int count = 0;
DIR *dp;
struct dirent *entry;
if (sprintf(path, "/proc/%s/fd", pid) < 0)
suicide("creating /proc/<pid>/fd path");
dp = opendir(path);
/* some processes are very short-lived; just ignore it in case it
* happens to disappear */
if (dp) {
while((entry=readdir(dp))) {
if (strcmp(entry->d_name, ".") && strcmp(entry->d_name, ".."))
count++;
}
}
closedir(dp);
return count;
}
void count_proc_fds(int *procs)
{
DIR *dp;
struct dirent *entry;
struct stat statbuf;
dp = opendir("/proc");
if (!dp)
suicide("reading /proc");
while((entry=readdir(dp))) {
stat(entry->d_name, &statbuf);
if(S_ISDIR(statbuf.st_mode)) {
if (is_integer(entry->d_name)) {
procs[atoi(entry->d_name)] = count_fds(entry->d_name);
}
}
}
closedir(dp);
}
void print(int *proc_fds, int n_procs, int json, int cmdline)
{
int pid;
int i_printed = 0;
char cmdline_buf[MAX_CMDLINE];
char *begin = json ? "{" : "#FDs\tPID\n";
char *end = json ? "}\n" : "\n";
char *pid_fmt = json ? "\"%d\": %d" : "%d\t%d\n";
char *cmdline_fmt = json ? "\"%s\": %d" : "%d\t%s\n";
printf(begin, "");
for (pid = 1; pid < n_procs; pid++) {
/* ignore processes with no open files */
if (proc_fds[pid]) {
/* avoid a trailing comma -- we don't know when the last
* line will be printed, so keep track of the first one
* and prepend commas instead of appending */
if (json && i_printed)
printf(", ");
if (cmdline) {
if (proc_cmdline(pid, cmdline_buf, MAX_CMDLINE)) {
/* if JSON, we want the key to be the cmd line */
if (json)
printf(cmdline_fmt, cmdline_buf, proc_fds[pid]);
else
printf(cmdline_fmt, proc_fds[pid], cmdline_buf);
}
else
fprintf(stderr, "Failed to get cmdline for pid %d\n", pid);
}
else {
/* if JSON, we want the key to be the PID */
if (json)
printf(pid_fmt, pid, proc_fds[pid]);
else
printf(pid_fmt, proc_fds[pid], pid);
}
i_printed = 1;
}
}
printf(end, "");
}
void print_summary(int allocated, int total, int json) {
if (json)
printf("{\"allocated\": %d, \"total\": %d}\n", allocated, total);
else {
printf("Allocated\tMaximum available\n");
printf("%d\t\t%d\n", allocated, total);
}
}
int main(int argc, char *argv[])
{
/* array to store the number of open file descriptors per process
* and total number of running procs */
int *proc_fds, n_procs=max_pid()+1;
/* struct for storing argument's values; also initialize/set
* defaults (conveniently all zero) */
struct arguments arguments = { 0 };
/* parse arguments */
parse_args(argc, argv, &arguments);
/* allocate the array to store the number of open file descriptors
* per process */
if (! (proc_fds=(int *)calloc(n_procs, sizeof(int))))
suicide("allocating process table");
while (1) {
if (arguments.summary) {
print_summary(allocated_fd(), max_fd(), arguments.json);
break;
}
else {
/* reset the process array */
memset(proc_fds, 0, n_procs*sizeof(int));
/* collect/count the number of file descriptors per process */
count_proc_fds(proc_fds);
/* print out the results */
print(proc_fds, n_procs, arguments.json, arguments.cmdline);
}
/* if a COUNT was set, make sure we stop after those many */
if (arguments.n_args == 2) {
if (arguments.args[1]-- == 1)
break;
}
/* if an INTERVAL was set, sleep for that period of time */
if (arguments.n_args > 0)
sleep(arguments.args[0]);
/* if neither COUNT nor INTERVAL are set, run just once */
if (arguments.n_args == 0)
break;
}
/* release memory allocated for */
free(proc_fds);
return 0;
}