Skip to contentSkip to conversation

monitor: Support printing processes which caused the event

PATCH New Phil Sutter 1 participant 3 messages May 15, 2017 netfilter

lore
Share & tools
lore
author reviewer
#2 lore raw

PATCH 2/2 monitor: Support printing processes which caused the event

In reply to Phil Sutter

This adds support for printing the process ID and name for changes which 'nft monitor' reports:

| nft -a -p monitor
| add chain ip t2 bla3 # pid 11616 (nft)

If '-n' was given in addition to '-p', parsing the process name from /proc/<pid>/cmdline is suppressed.

---
 include/nftables.h |  1 +
 src/main.c         | 12 ++++++++-
 src/netlink.c      | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++----
 src/rule.c         |  2 --
 4 files changed, 83 insertions(+), 8 deletions(-)
include/nftables.h +1 -0
index 6f541557364ba..9107b56b26151 100644
@@ -29,6 +29,7 @@ extern unsigned int numeric_output;
 extern unsigned int stateless_output;
 extern unsigned int ip2name_output;
 extern unsigned int handle_output;
+extern unsigned int pid_output;
 extern unsigned int debug_level;
 extern const char *include_paths[INCLUDE_PATHS_MAX];
 
src/main.c +11 -1
index 1cc8b39ff4ab9..670e6791e8877 100644
@@ -33,6 +33,7 @@ unsigned int numeric_output;
 unsigned int stateless_output;
 unsigned int ip2name_output;
 unsigned int handle_output;
+unsigned int pid_output;
 #ifdef DEBUG
 unsigned int debug_level;
 #endif
@@ -51,10 +52,11 @@ enum opt_vals {
 	OPT_IP2NAME		= 'N',
 	OPT_DEBUG		= 'd',
 	OPT_HANDLE_OUTPUT	= 'a',
+	OPT_PID_OUTPUT		= 'p',
 	OPT_INVALID		= '?',
 };
 
-#define OPTSTRING	"hvf:iI:vnsNa"
+#define OPTSTRING	"hvf:iI:vnsNap"
 
 static const struct option options[] = {
 	{
@@ -103,6 +105,10 @@ static const struct option options[] = {
 		.val		= OPT_HANDLE_OUTPUT,
 	},
 	{
+		.name		= "pid",
+		.val		= OPT_PID_OUTPUT,
+	},
+	{
 		.name		= NULL
 	}
 };
@@ -125,6 +131,7 @@ static void show_help(const char *name)
 "  -s, --stateless		Omit stateful information of ruleset.\n"
 "  -N				Translate IP addresses to names.\n"
 "  -a, --handle			Output rule handle.\n"
+"  -p, --pid			Output process information in monitor.\n"
 "  -I, --includepath <directory>	Add <directory> to the paths searched for include files.\n"
 #ifdef DEBUG
 "  --debug <level [,level...]>	Specify debugging level (scanner, parser, eval, netlink, mnl, proto-ctx, segtree, all)\n"
@@ -333,6 +340,9 @@ int main(int argc, char * const *argv)
 		case OPT_HANDLE_OUTPUT:
 			handle_output++;
 			break;
+		case OPT_PID_OUTPUT:
+			pid_output++;
+			break;
 		case OPT_INVALID:
 			exit(NFT_EXIT_FAILURE);
 		}
src/netlink.c +71 -5

151 lines not on this page · raw message

src/rule.c +0 -2
index 209cf2d7d9f60..10095320348eb 100644
@@ -691,8 +691,6 @@ void chain_print_plain(const struct chain *chain)
 		       chain->type, chain->hookstr,
 		       chain->priority, chain_policy2str(chain->policy));
 	}
-
-	printf("\n");
 }
 
 struct table *table_alloc(void)
Signature · 1 line
2.11.0

Phil Sutter

author reviewer
#0 lore raw

cover monitor: Support printing processes which caused the event

This is a hopefully correct implementation of what I wanted to achieve in the same named RFC sent before: Instead of relying on the netlink msgid to be equal to the sender's PID, make use of the kernel infrastructure added in a previous patch.

Note that this implementation does not use libnftnl like other parts of the code. I skipped that because handling of NFT_MSG_PROC_INFO type messages is unidirectional only and they contain just two attributes - the amount of code to add to libnftnl to parse them there seemed not feasible just to follow coding-style.

The first patch is probably redundant since it's changes will be imported by a generic kernel header update anyway, but it allows for quickly testing the implementation so left here for reference.

Phil Sutter (2):
include: Add proc_info symbols to nf_tables.h
monitor: Support printing processes which caused the event

 include/linux/netfilter/nf_tables.h | 15 ++++++++
 include/nftables.h                  |  1 +
 src/main.c                          | 12 +++++-
 src/netlink.c                       | 76 ++++++++++++++++++++++++++++++++++---
 src/rule.c                          |  2 -
 5 files changed, 98 insertions(+), 8 deletions(-)
Signature · 1 line
2.11.0
author reviewer
#1 lore raw

PATCH 1/2 include: Add proc_info symbols to nf_tables.h

---
 include/linux/netfilter/nf_tables.h | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
include/linux/netfilter/nf_tables.h +15 -0
index 683f6f88fcace..2a73c80f80f65 100644
@@ -114,6 +114,7 @@ enum nf_tables_msg_types {
 	NFT_MSG_GETOBJ,
 	NFT_MSG_DELOBJ,
 	NFT_MSG_GETOBJ_RESET,
+	NFT_MSG_PROC_INFO,
 	NFT_MSG_MAX,
 };
 
@@ -1375,4 +1376,18 @@ enum nft_ng_types {
 };
 #define NFT_NG_MAX	(__NFT_NG_MAX - 1)
 
+/**
+ * enum nft_proc_info_attributes - nf_tables process information netlink attributes
+ *
+ * @NFTA_PROC_PID: process id (NLA_U32)
+ * @NFTA_PROC_NAME: process name (NLA_STRING)
+ */
+enum nft_proc_info_attributes {
+        NFTA_PROC_UNSPEC,
+        NFTA_PROC_PID,
+        NFTA_PROC_NAME,
+        __NFTA_PROC_MAX
+};
+#define NFTA_PROC_MAX   (__NFTA_PROC_MAX - 1)
+
 #endif /* _LINUX_NF_TABLES_H */
Signature · 1 line
2.11.0