File: nl-list-sockets.c

package info (click to toggle)
libnl3 3.4.0-1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 6,120 kB
  • sloc: ansic: 57,494; sh: 4,447; python: 3,702; makefile: 888; yacc: 478; cpp: 454; lex: 210
file content (49 lines) | stat: -rw-r--r-- 1,145 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
 * nl-list-sockets.c	Pretty-print /proc/net/netlink
 *
 *	This library is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU Lesser General Public
 *	License as published by the Free Software Foundation version 2.1
 *	of the License.
 *
 * Copyright (c) 2003-2009 Thomas Graf <tgraf@suug.ch>
 */

#include <netlink/cli/utils.h>

#define PROC_NETLINK "/proc/net/netlink"

int main(int argc, char *argv[])
{
	FILE *fd;
	char buf[2048], p[64];

	fd = fopen(PROC_NETLINK, "re");
	if (fd == NULL) {
		perror("fopen");
		return -1;
	}

	printf("Address            Family           PID    Groups   rmem   "
	       "wmem   CB         refcnt\n");

	while (fgets(buf, sizeof(buf), fd)) {
		unsigned long sk, cb;
		int ret, proto, pid, rmem, wmem, refcnt;
		unsigned int groups;
		
		ret = sscanf(buf, "%lx %d %d %08x %d %d %lx %d\n",
			     &sk, &proto, &pid, &groups, &rmem, &wmem,
			     &cb, &refcnt);
		if (ret != 8)
			continue;
		
		printf("0x%016lx %-16s %-6d %08x %-6d %-6d 0x%08lx %d\n",
			sk, nl_nlfamily2str(proto, p, sizeof(p)), pid,
			groups, rmem, wmem, cb, refcnt);
	}

	fclose(fd);

	return 0;
}