Imported Upstream version 3.2.22
[platform/upstream/libnl3.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-2009 Thomas Graf <tgraf@suug.ch>
10  */
11
12 #include <netlink/cli/utils.h>
13
14 #define PROC_NETLINK "/proc/net/netlink"
15
16 int main(int argc, char *argv[])
17 {
18         FILE *fd;
19         char buf[2048], p[64];
20
21         fd = fopen(PROC_NETLINK, "r");
22         if (fd == NULL) {
23                 perror("fopen");
24                 return -1;
25         }
26
27         printf("Address            Family           PID    Groups   rmem   "
28                "wmem   CB         refcnt\n");
29
30         while (fgets(buf, sizeof(buf), fd)) {
31                 unsigned long sk, cb;
32                 int ret, proto, pid, rmem, wmem, refcnt;
33                 unsigned int groups;
34                 
35                 ret = sscanf(buf, "%lx %d %d %08x %d %d %lx %d\n",
36                              &sk, &proto, &pid, &groups, &rmem, &wmem,
37                              &cb, &refcnt);
38                 if (ret != 8)
39                         continue;
40                 
41                 printf("0x%016lx %-16s %-6d %08x %-6d %-6d 0x%08lx %d\n",
42                         sk, nl_nlfamily2str(proto, p, sizeof(p)), pid,
43                         groups, rmem, wmem, cb, refcnt);
44         }
45
46         fclose(fd);
47
48         return 0;
49 }