-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfdinspect.cpp
More file actions
47 lines (32 loc) · 905 Bytes
/
fdinspect.cpp
File metadata and controls
47 lines (32 loc) · 905 Bytes
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
#include <sys/ioctl.h>
#include <fcntl.h>
#include <iostream>
#include <sstream>
#include <cstdlib>
using namespace std;
int report_buffer_size(int fd);
int main(int argc, char* argv[]) {
int PID = 0;
if(argc != 2) {
cout << "Usage: fdinspect PID" << endl; return 1;
} else PID = atoi(argv[1]);
stringstream ss_in, ss_out;
ss_in << "/proc/" << PID << "/fd/0";
ss_out << "/proc/" << PID << "/fd/1";
int fd_in = open(ss_in.str().c_str(), O_RDONLY);
int fd_out = open(ss_out.str().c_str(), O_RDONLY);
const string tab = "\t";
cout << "PID" << tab << "stdin"
<< tab << "stdout"
<< endl;
cout << PID << tab << report_buffer_size(fd_in)
<< tab << report_buffer_size(fd_out)
<< endl;
close(fd_in);
close(fd_out);
}
int report_buffer_size(int fd) {
int bytes = -1;
ioctl(fd, FIONREAD, &bytes);
return bytes;
}