107
107
struct filter default_filter = {
109
states: SS_ALL & ~((1<<SS_LISTEN)|(1<<SS_CLOSE)|(1<<SS_TIME_WAIT)|(1<<SS_SYN_RECV)),
110
families: (1<<AF_INET)|(1<<AF_INET6),
109
.states = SS_ALL & ~((1<<SS_LISTEN)|(1<<SS_CLOSE)|(1<<SS_TIME_WAIT)|(1<<SS_SYN_RECV)),
110
.families= (1<<AF_INET)|(1<<AF_INET6),
113
113
struct filter current_filter;
115
int generic_proc_open(char *env, char *name)
115
static FILE *generic_proc_open(const char *env, const char *name)
117
const char *p = getenv(env);
118
char *p = getenv(env);
120
121
p = getenv("PROC_ROOT") ? : "/proc";
121
122
snprintf(store, sizeof(store)-1, "%s/%s", p, name);
124
return open(p, O_RDONLY);
126
return fopen(p, "r");
127
int net_tcp_open(void)
129
static FILE *net_tcp_open(void)
129
131
return generic_proc_open("PROC_NET_TCP", "net/tcp");
132
int net_tcp6_open(void)
134
static FILE *net_tcp6_open(void)
134
136
return generic_proc_open("PROC_NET_TCP6", "net/tcp6");
137
int net_udp_open(void)
139
static FILE *net_udp_open(void)
139
141
return generic_proc_open("PROC_NET_UDP", "net/udp");
142
int net_udp6_open(void)
144
static FILE *net_udp6_open(void)
144
146
return generic_proc_open("PROC_NET_UDP6", "net/udp6");
147
int net_raw_open(void)
149
static FILE *net_raw_open(void)
149
151
return generic_proc_open("PROC_NET_RAW", "net/raw");
152
int net_raw6_open(void)
154
static FILE *net_raw6_open(void)
154
156
return generic_proc_open("PROC_NET_RAW6", "net/raw6");
157
int net_unix_open(void)
159
static FILE *net_unix_open(void)
159
161
return generic_proc_open("PROC_NET_UNIX", "net/unix");
162
int net_packet_open(void)
164
static FILE *net_packet_open(void)
164
166
return generic_proc_open("PROC_NET_PACKET", "net/packet");
167
int net_netlink_open(void)
169
static FILE *net_netlink_open(void)
169
171
return generic_proc_open("PROC_NET_NETLINK", "net/netlink");
172
int slabinfo_open(void)
174
static FILE *slabinfo_open(void)
174
176
return generic_proc_open("PROC_SLABINFO", "slabinfo");
177
int net_sockstat_open(void)
179
static FILE *net_sockstat_open(void)
179
181
return generic_proc_open("PROC_NET_SOCKSTAT", "net/sockstat");
182
int net_sockstat6_open(void)
184
static FILE *net_sockstat6_open(void)
184
186
return generic_proc_open("PROC_NET_SOCKSTAT6", "net/sockstat6");
187
int net_snmp_open(void)
189
static FILE *net_snmp_open(void)
189
191
return generic_proc_open("PROC_NET_SNMP", "net/snmp");
192
int net_netstat_open(void)
194
return generic_proc_open("PROC_NET_NETSTAT", "net/netstat");
197
int ephemeral_ports_open(void)
194
static FILE *ephemeral_ports_open(void)
199
196
return generic_proc_open("PROC_IP_LOCAL_PORT_RANGE", "sys/net/ipv4/ip_local_port_range");
202
int find_users(int ino, char *buf, int buflen)
199
int find_users(unsigned ino, char *buf, int buflen)
204
201
char pattern[64];
1160
n = sscanf(data, "%x %x:%x %x:%x %x %d %d %d %d %llx %d %d %d %d %d %[^\n]\n",
1158
n = sscanf(data, "%x %x:%x %x:%x %x %d %d %u %d %llx %d %d %d %d %d %[^\n]\n",
1161
1159
&s.state, &s.wq, &s.rq,
1162
1160
&s.timer, &s.timeout, &s.retrs, &s.uid, &s.probes, &s.ino,
1163
1161
&s.refcnt, &s.sk, &s.rto, &s.ato, &s.qack,
1228
static int generic_record_read(int fd, char *buf, int bufsize,
1229
int (*worker)(char*, struct filter *, int),
1230
struct filter *f, int fam)
1226
static int generic_record_read(FILE *fp,
1227
int (*worker)(char*, const struct filter *, int),
1228
const struct filter *f, int fam)
1237
/* Load the first chunk and calculate record length from it. */
1238
n = read(fd, buf, bufsize);
1233
if (fgets(line, sizeof(line), fp) == NULL)
1241
/* I _know_ that this is wrong, do not remind. :-)
1242
* But this works nowadays. */
1245
p = memchr(buf, '\n', n);
1246
if (p == NULL || (p-buf) >= n)
1247
goto outwrongformat;
1248
recsize = (p-buf)+1;
1252
while ((p+recsize) - buf <= n) {
1253
if (p[recsize-1] != '\n')
1254
goto outwrongformat;
1256
if (worker(p, f, fam) < 0)
1261
int remains = (buf+bufsize) - p;
1262
memcpy(buf, p, remains);
1264
n = read(fd, p, (buf+bufsize) - p);
1267
if (n < (buf+bufsize) - p) {
1271
goto outwrongformat;
1279
goto outwrongformat;
1236
while (fgets(line, sizeof(line), fp) != NULL) {
1237
int n = strlen(line);
1238
if (n == 0 || line[n-1] != '\n') {
1244
if (worker(line, f, fam) < 0)
1249
return ferror(fp) ? -1 : 0;
1292
1252
static char *sprint_bw(char *buf, double bw)
1683
1644
if (f->families & (1<<AF_INET)) {
1684
if ((fd = net_tcp_open()) < 0)
1686
if (generic_record_read(fd, buf, bufsize, tcp_show_line, f, AF_INET))
1645
if ((fp = net_tcp_open()) < 0)
1648
setbuffer(fp, buf, bufsize);
1649
if (generic_record_read(fp, tcp_show_line, f, AF_INET))
1691
1654
if ((f->families & (1<<AF_INET6)) &&
1692
(fd = net_tcp6_open()) >= 0) {
1693
if (generic_record_read(fd, buf, bufsize, tcp_show_line, f, AF_INET6))
1655
(fp = net_tcp6_open()) >= 0) {
1656
setbuffer(fp, buf, bufsize);
1657
if (generic_record_read(fp, tcp_show_line, f, AF_INET6))
1806
1770
int udp_show(struct filter *f)
1810
int bufsize = sizeof(buf);
1812
1774
dg_proto = UDP_PROTO;
1814
1776
if (f->families&(1<<AF_INET)) {
1815
if ((fd = net_udp_open()) < 0)
1817
if (generic_record_read(fd, buf, bufsize, dgram_show_line, f, AF_INET))
1777
if ((fp = net_udp_open()) < 0)
1779
if (generic_record_read(fp, dgram_show_line, f, AF_INET))
1822
1784
if ((f->families&(1<<AF_INET6)) &&
1823
(fd = net_udp6_open()) >= 0) {
1824
if (generic_record_read(fd, buf, bufsize, dgram_show_line, f, AF_INET6))
1785
(fp = net_udp6_open()) >= 0) {
1786
if (generic_record_read(fp, dgram_show_line, f, AF_INET6))
1832
1794
int saved_errno = errno;
1835
1797
errno = saved_errno;
1840
1802
int raw_show(struct filter *f)
1844
int bufsize = sizeof(buf);
1846
1806
dg_proto = RAW_PROTO;
1848
1808
if (f->families&(1<<AF_INET)) {
1849
if ((fd = net_raw_open()) < 0)
1851
if (generic_record_read(fd, buf, bufsize, dgram_show_line, f, AF_INET))
1809
if ((fp = net_raw_open()) < 0)
1811
if (generic_record_read(fp, dgram_show_line, f, AF_INET))
1856
1816
if ((f->families&(1<<AF_INET6)) &&
1857
(fd = net_raw6_open()) >= 0) {
1858
if (generic_record_read(fd, buf, bufsize, dgram_show_line, f, AF_INET6))
1817
(fp = net_raw6_open()) >= 0) {
1818
if (generic_record_read(fp, dgram_show_line, f, AF_INET6))
1866
1826
int saved_errno = errno;
1869
1829
errno = saved_errno;
2311
2271
memset(s, 0, sizeof(*s));
2313
if ((fp = fdopen(net_sockstat_open(), "r")) == NULL)
2273
if ((fp = net_sockstat_open()) == NULL)
2315
2275
while(fgets(buf, sizeof(buf), fp) != NULL)
2316
2276
get_sockstat_line(buf, s);
2319
if ((fp = fdopen(net_sockstat6_open(), "r")) == NULL)
2279
if ((fp = net_sockstat6_open()) == NULL)
2321
2281
while(fgets(buf, sizeof(buf), fp) != NULL)
2322
2282
get_sockstat_line(buf, s);