oops
[libnl.git] / src / nl-list-sockets.c
1 /*
2  * nl-list-sockets.c    Pretty-print /proc/net/netlink
3  *
4  *      This library is free software; you can redistribute it and/or
5  *      modify it under the terms of the GNU Lesser General Public
6  *      License as published by the Free Software Foundation version 2.1
7  *      of the License.
8  *
9  * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
10  */
11
12 #include "utils.h"
13
14 #define PROC_NETLINK "/proc/net/netlink"
15
16 static void print_usage(void)
17 {
18         fprintf(stderr, "Usage: nl-list-sockets [<file>]\n");
19         exit(1);
20 }
21
22 int main(int argc, char *argv[])
23 {
24         FILE *fd;
25         char buf[2048], p[64];
26
27         if (argc > 1 && !strcasecmp(argv[1], "-h"))
28                 print_usage();
29
30         fd = fopen(PROC_NETLINK, "r");
31         if (fd == NULL) {
32                 perror("fopen");
33                 return -1;
34         }
35
36         printf("Address    Family           PID    Groups   rmem   wmem   " \
37                "CB         refcnt\n");
38
39         while (fgets(buf, sizeof(buf), fd)) {
40                 unsigned long sk, cb;
41                 int ret, proto, pid, rmem, wmem, refcnt;
42                 uint32_t groups;
43                 
44                 ret = sscanf(buf, "%lx %d %d %08x %d %d %lx %d\n",
45                              &sk, &proto, &pid, &groups, &rmem, &wmem,
46                              &cb, &refcnt);
47                 if (ret != 8)
48                         continue;
49                 
50                 printf("0x%08lx %-16s %-6d %08x %-6d %-6d 0x%08lx %d\n",
51                         sk, nl_nlfamily2str(proto, p, sizeof(p)), pid,
52                         groups, rmem, wmem, cb, refcnt);
53         }
54
55         fclose(fd);
56
57         return 0;
58 }