libnl 3.5.0

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