summaryrefslogtreecommitdiffstats
path: root/debian/patches/upstream/lsfd-add-LSFD_DEBUG-env-var-for-debugging.patch
blob: 32a16870baed4e3d09092759e70468591d853bdd (plain)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
From: Masatake YAMATO <yamato@redhat.com>
Date: Sat, 30 Mar 2024 18:47:03 +0900
Subject: lsfd: add LSFD_DEBUG env var for debugging

Signed-off-by: Masatake YAMATO <yamato@redhat.com>
---
 misc-utils/lsfd-sock-xinfo.c | 51 ++++++++++++++++++++++++++++++++++++++------
 misc-utils/lsfd.c            | 10 +++++++++
 misc-utils/lsfd.h            | 15 +++++++++++++
 3 files changed, 70 insertions(+), 6 deletions(-)

diff --git a/misc-utils/lsfd-sock-xinfo.c b/misc-utils/lsfd-sock-xinfo.c
index a293806..71be41a 100644
--- a/misc-utils/lsfd-sock-xinfo.c
+++ b/misc-utils/lsfd-sock-xinfo.c
@@ -179,9 +179,12 @@ static void load_sock_xinfo_no_nsswitch(struct netns *nsobj)
 	load_xinfo_from_proc_packet(netns);
 
 	diagsd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_SOCK_DIAG);
+	DBG(ENDPOINTS, ul_debug("made a diagnose socket [fd=%d; %s]", diagsd,
+				(diagsd >= 0)? "successful": strerror(errno)));
 	if (diagsd >= 0) {
 		load_xinfo_from_diag_unix(diagsd, netns);
 		close(diagsd);
+		DBG(ENDPOINTS, ul_debug("close the diagnose socket"));
 	}
 
 	if (nsobj)
@@ -337,6 +340,7 @@ static void send_diag_request(int diagsd, void *req, size_t req_size,
 			      bool (*cb)(ino_t, size_t, void *),
 			      ino_t netns)
 {
+	int r;
 	struct sockaddr_nl nladdr = {
 		.nl_family = AF_NETLINK,
 	};
@@ -361,30 +365,45 @@ static void send_diag_request(int diagsd, void *req, size_t req_size,
 
 	__attribute__((aligned(sizeof(void *)))) uint8_t buf[8192];
 
-	if (sendmsg(diagsd, &mhd, 0) < 0)
+	r = sendmsg(diagsd, &mhd, 0);
+	DBG(ENDPOINTS, ul_debug("sendmsg [rc=%d; %s]",
+				r, (r >= 0)? "successful": strerror(errno)));
+	if (r < 0)
 		return;
 
 	for (;;) {
 		const struct nlmsghdr *h;
-		int r = recvfrom(diagsd, buf, sizeof(buf), 0, NULL, NULL);
+		r = recvfrom(diagsd, buf, sizeof(buf), 0, NULL, NULL);
+		DBG(ENDPOINTS, ul_debug("recvfrom [rc=%d; %s]",
+					r, (r >= 0)? "successful": strerror(errno)));
 		if (r < 0)
 			return;
 
 		h = (void *) buf;
+		DBG(ENDPOINTS, ul_debug("   OK: %d", NLMSG_OK(h, (size_t)r)));
 		if (!NLMSG_OK(h, (size_t)r))
 			return;
 
 		for (; NLMSG_OK(h, (size_t)r); h = NLMSG_NEXT(h, r)) {
-			if (h->nlmsg_type == NLMSG_DONE)
+			if (h->nlmsg_type == NLMSG_DONE) {
+				DBG(ENDPOINTS, ul_debug("      DONE"));
 				return;
-			if (h->nlmsg_type == NLMSG_ERROR)
+			}
+			if (h->nlmsg_type == NLMSG_ERROR)  {
+				struct nlmsgerr *e = (struct nlmsgerr *)NLMSG_DATA(h);
+				DBG(ENDPOINTS, ul_debug("      ERROR: %s",
+							strerror(- e->error)));
 				return;
+			}
 
 			if (h->nlmsg_type == SOCK_DIAG_BY_FAMILY) {
+				DBG(ENDPOINTS, ul_debug("      FAMILY"));
 				if (!cb(netns, h->nlmsg_len, NLMSG_DATA(h)))
 					return;
 			}
+			DBG(ENDPOINTS, ul_debug("   NEXT"));
 		}
+		DBG(ENDPOINTS, ul_debug("   OK: 0"));
 	}
 }
 
@@ -639,6 +658,8 @@ static void load_xinfo_from_proc_unix(ino_t netns_inode)
 	FILE *unix_fp;
 
 	unix_fp = fopen("/proc/net/unix", "r");
+	DBG(ENDPOINTS, ul_debug("open /proc/net/unix [fp=%p; %s]", unix_fp,
+				unix_fp? "successful": strerror(errno)));
 	if (!unix_fp)
 		return;
 
@@ -655,13 +676,18 @@ static void load_xinfo_from_proc_unix(ino_t netns_inode)
 		unsigned long inode;
 		struct unix_xinfo *ux;
 		char path[UNIX_LINE_LEN + 1] = { 0 };
+		int r;
 
+		DBG(ENDPOINTS, ul_debug("   line: %s", line));
 
-		if (sscanf(line, "%*x: %*x %*x %" SCNx64 " %x %x %lu %"
+		r = sscanf(line, "%*x: %*x %*x %" SCNx64 " %x %x %lu %"
 			   stringify_value(UNIX_LINE_LEN) "[^\n]",
-			   &flags, &type, &st, &inode, path) < 4)
+			   &flags, &type, &st, &inode, path);
+		DBG(ENDPOINTS, ul_debug("   scanf: %d", r));
+		if (r < 4)
 			continue;
 
+		DBG(ENDPOINTS, ul_debug("   inode: %lu", inode));
 		if (inode == 0)
 			continue;
 
@@ -675,10 +701,12 @@ static void load_xinfo_from_proc_unix(ino_t netns_inode)
 		ux->st = st;
 		xstrncpy(ux->path, path, sizeof(ux->path));
 
+		DBG(ENDPOINTS, ul_debug("   path: %s", ux->path));
 		add_sock_info(&ux->sock);
 	}
 
  out:
+	DBG(ENDPOINTS, ul_debug("close /proc/net/unix"));
 	fclose(unix_fp);
 }
 
@@ -712,6 +740,9 @@ static bool handle_diag_unix(ino_t netns __attribute__((__unused__)),
 
 	if (diag->udiag_family != AF_UNIX)
 		return false;
+	DBG(ENDPOINTS, ul_debug("         UNIX"));
+	DBG(ENDPOINTS, ul_debug("         LEN: %zu (>= %zu)", nlmsg_len,
+				(size_t)(NLMSG_LENGTH(sizeof(*diag)))));
 
 	if (nlmsg_len < NLMSG_LENGTH(sizeof(*diag)))
 		return false;
@@ -719,21 +750,29 @@ static bool handle_diag_unix(ino_t netns __attribute__((__unused__)),
 	inode = (ino_t)diag->udiag_ino;
 	xinfo = get_sock_xinfo(inode);
 
+	DBG(ENDPOINTS, ul_debug("         inode: %llu", (unsigned long long)inode));
+	DBG(ENDPOINTS, ul_debug("         xinfo: %p", xinfo));
+
 	if (xinfo == NULL)
 		/* The socket is found in the diag response
 		   but not in the proc fs. */
 		return true;
 
+	DBG(ENDPOINTS, ul_debug("         xinfo->class == &unix_xinfo_class: %d",
+				xinfo->class == &unix_xinfo_class));
 	if (xinfo->class != &unix_xinfo_class)
 		return true;
 	unix_xinfo = (struct unix_xinfo *)xinfo;
 
 	rta_len = nlmsg_len - NLMSG_LENGTH(sizeof(*diag));
+	DBG(ENDPOINTS, ul_debug("         rta_len: %zu", rta_len));
 	for (struct rtattr *attr = (struct rtattr *)(diag + 1);
 	     RTA_OK(attr, rta_len);
 	     attr = RTA_NEXT(attr, rta_len)) {
 		size_t len = RTA_PAYLOAD(attr);
 
+		DBG(ENDPOINTS, ul_debug("            len = %2zu, type: %d",
+					rta_len, attr->rta_type));
 		switch (attr->rta_type) {
 		case UNIX_DIAG_NAME:
 			unix_refill_name(xinfo, RTA_DATA(attr), len);
diff --git a/misc-utils/lsfd.c b/misc-utils/lsfd.c
index 771daef..4786d1d 100644
--- a/misc-utils/lsfd.c
+++ b/misc-utils/lsfd.c
@@ -84,6 +84,14 @@ static int kcmp(pid_t pid1 __attribute__((__unused__)),
 
 #include "lsfd.h"
 
+UL_DEBUG_DEFINE_MASK(lsfd);
+UL_DEBUG_DEFINE_MASKNAMES(lsfd) = UL_DEBUG_EMPTY_MASKNAMES;
+
+static void lsfd_init_debug(void)
+{
+	__UL_INIT_DEBUG_FROM_ENV(lsfd, LSFD_DEBUG_, 0, LSFD_DEBUG);
+}
+
 /*
  * /proc/$pid/mountinfo entries
  */
@@ -2271,6 +2279,8 @@ int main(int argc, char *argv[])
 		{ NULL, 0, NULL, 0 },
 	};
 
+	lsfd_init_debug();
+
 	setlocale(LC_ALL, "");
 	bindtextdomain(PACKAGE, LOCALEDIR);
 	textdomain(PACKAGE);
diff --git a/misc-utils/lsfd.h b/misc-utils/lsfd.h
index 1859dc7..e646758 100644
--- a/misc-utils/lsfd.h
+++ b/misc-utils/lsfd.h
@@ -28,7 +28,11 @@
 #include <sys/stat.h>
 #include <dirent.h>
 #include <inttypes.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
 
+#include "debug.h"
 #include "libsmartcols.h"
 #include "list.h"
 #include "nls.h"
@@ -36,6 +40,17 @@
 #include "strutils.h"
 #include "xalloc.h"
 
+/*
+ * debug
+ */
+UL_DEBUG_DECLARE_MASK(lsfd);
+
+#define LSFD_DEBUG_INIT	     (1 << 1)
+#define LSFD_DEBUG_ENDPOINTS (1 << 2)
+#define LSFD_DEBUG_ALL       0xFFFF
+
+#define DBG(m, x)       __UL_DBG(lsfd, LSFD_DEBUG_, m, x)
+
 /*
  * column IDs
  */