Commit | Line | Data |
---|---|---|
c55a77db PH |
1 | /* A program to check on open file descriptors. There are some weird options |
2 | for running it in Exim testing. If -q is given, make output suitable for | |
3 | queryprogram. If -f is given, copy the input as for a transport filter. If -s | |
4 | is given, add extra output from stat(). */ | |
5 | ||
6 | #include <stdio.h> | |
7 | #include <stdlib.h> | |
8 | #include <string.h> | |
9 | #include <sys/types.h> | |
10 | #include <fcntl.h> | |
11 | #include <limits.h> | |
12 | #include <errno.h> | |
13 | #include <sys/stat.h> | |
14 | #include <unistd.h> | |
15 | ||
16 | ||
17 | /* The way of finding out the maximum file descriptor various between OS. | |
18 | Most have sysconf(), but a few don't. */ | |
19 | ||
20 | #ifdef _SC_OPEN_MAX | |
21 | #define mac_maxfd (sysconf(_SC_OPEN_MAX) - 1) | |
22 | #elif defined OPEN_MAX | |
23 | #define mac_maxfd (OPEN_MAX - 1) | |
24 | #elif defined NOFILE | |
25 | #define mac_maxfd (NOFILE - 1) | |
26 | #else | |
27 | #define mac_maxfd 255; /* just in case */ | |
28 | #endif | |
29 | ||
30 | ||
31 | int main(int argc, char **argv) | |
32 | { | |
33 | int fd; | |
34 | int qpgm = 0; | |
35 | int filter = 0; | |
36 | int use_stat = 0; | |
37 | struct stat statbuf; | |
38 | char buffer[8192]; | |
39 | char *p = buffer; | |
40 | ||
41 | while (argc > 1) | |
42 | { | |
43 | char *arg = argv[--argc]; | |
44 | if (strcmp(arg, "-q") == 0) qpgm = 1; | |
45 | if (strcmp(arg, "-f") == 0) filter = 1; | |
46 | if (strcmp(arg, "-s") == 0) use_stat = 1; | |
47 | } | |
48 | ||
49 | if (filter) | |
50 | { | |
51 | int len; | |
52 | while ((len = read(0, buffer, sizeof(buffer))) > 0) | |
53 | write(1, buffer, len); | |
54 | } | |
55 | ||
56 | p += sprintf(p, "max fd = %d\n", (int)mac_maxfd); | |
57 | ||
58 | for (fd = 0; fd <= mac_maxfd; fd++) | |
59 | { | |
60 | int options = fcntl(fd, F_GETFD); | |
61 | if (options >= 0) | |
62 | { | |
63 | int status = fcntl(fd, F_GETFL); | |
64 | p += sprintf(p, "%3d opt=%d status=%X ", fd, options, status); | |
65 | switch(status & 3) | |
66 | { | |
67 | case 0: p += sprintf(p, "RDONLY"); | |
68 | break; | |
69 | case 1: p += sprintf(p, "WRONLY"); | |
70 | break; | |
71 | case 2: p += sprintf(p, "RDWR"); | |
72 | break; | |
73 | } | |
74 | if (isatty(fd)) p += sprintf(p, " TTY"); | |
75 | if ((status & 8) != 0) p += sprintf(p, " APPEND"); | |
76 | ||
77 | if (use_stat && fstat(fd, &statbuf) >= 0) | |
78 | { | |
79 | p += sprintf(p, " mode=%o uid=%d size=%d", (int)statbuf.st_mode, | |
80 | (int)statbuf.st_uid, (int)statbuf.st_size); | |
81 | } | |
82 | ||
83 | p += sprintf(p, "\n"); | |
84 | } | |
85 | else if (errno != EBADF) | |
86 | { | |
87 | p += sprintf(p, "%3d errno=%d %s\n", fd, errno, strerror(errno)); | |
88 | } | |
89 | } | |
90 | ||
91 | if (qpgm) | |
92 | { | |
93 | for (p = buffer; *p != 0; p++) | |
94 | if (*p == '\n') *p = ' '; | |
95 | printf("ACCEPT DATA=\"%s\"\n", buffer); | |
96 | } | |
97 | else printf("%s", buffer); | |
98 | ||
99 | exit(0); | |
100 | } | |
101 | ||
102 | /* End */ |