Package: webdis / 0.1.2+dfsg-2

print-listen-port-number.patch Patch series | download
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
Description: Print HTTP port number in the logs during startup
 Current test suite allows dynamic HTTP port allocation for test purpose.
 Once webdis is started it is possible to discover webdis' HTTP port using
 netstat. However resent builds have shown that netstat has different options
 for hurd and kfreebsd and thus tests are failed and the overall package build
 is marked as failed as well. In order to overcome this I have tried to use
 sockstat instead of netstat but bug #679876 renders sockstat unusable.
 .
 This patch removes the need to use netstat or sockstat to discover the port
 number webdis is listening to and instead allows to simply grep the necessary
 info out of webdis logs.
Author: Andriy Senkovych <jolly_roger@itblog.org.ua>
Last-Update: 2016-11-17
Index: webdis/server.c
===================================================================
--- webdis.orig/server.c
+++ webdis/server.c
@@ -25,6 +25,7 @@ socket_setup(struct server *s, const cha
 
 	int reuse = 1;
 	struct sockaddr_in addr;
+    socklen_t len = sizeof(addr);
 	int fd, ret;
 
 	memset(&addr, 0, sizeof(addr));
@@ -60,7 +61,7 @@ socket_setup(struct server *s, const cha
 	}
 
 	/* bind */
-	ret = bind(fd, (struct sockaddr*)&addr, sizeof(addr));
+	ret = bind(fd, (struct sockaddr*)&addr, len);
 	if (0 != ret) {
 		slog(s, WEBDIS_ERROR, strerror(errno), 0);
 		return -1;
@@ -73,6 +74,18 @@ socket_setup(struct server *s, const cha
 		return -1;
 	}
 
+    if (getsockname(fd, (struct sockaddr *)&addr, &len) != -1) {
+        const char* comment = "Webdis listening on port %d";
+        int port_num = ntohs(addr.sin_port);
+
+        char* buffer = malloc(strlen(comment) -2 + strlen("65535") + 1);
+        sprintf(buffer, comment, port_num);
+
+		slog(s, WEBDIS_INFO, buffer , 0);
+
+        free(buffer);
+    }
+        
 	/* there you go, ready to accept! */
 	return fd;
 }