NetBSD-Bugs archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: bin/60229: bozohttpd: fails to print actual port number with `-I 0'



"campbell+netbsd%mumble.net@localhost via gnats" <gnats-admin%NetBSD.org@localhost> writes:

>>Number:         60229
>>Category:       bin
>>Synopsis:       bozohttpd: fails to print actual port number with `-I 0'
>>Confidential:   no
>>Severity:       serious
>>Priority:       medium
>>Responsible:    bin-bug-people
>>State:          open
>>Class:          sw-bug
>>Submitter-Id:   net
>>Arrival-Date:   Mon May 04 20:50:00 +0000 2026
>>Originator:     Taylor R Campbell
>>Release:        current, 11, 10, 9, ...
>>Organization:
> The NetBozod Localhost, Inc.
>>Environment:
>>Description:
> $ /usr/libexec/httpd -b -f -s -i localhost -I 0 -X . localhost
> started in daemon mode as `localhost' port `0' root `.'
>
> This is not helpful: I want the port number the OS chose.
>
> (Reliable machine-parseable output would be nice too.)
>>How-To-Repeat:
> as above
>>Fix:
> getsockname

This patch prints the numerical host and port upon listen(2)...
diff --git a/libexec/httpd/daemon-bozo.c b/libexec/httpd/daemon-bozo.c
index 64483d07c986..a1b741af84fa 100644
--- a/libexec/httpd/daemon-bozo.c
+++ b/libexec/httpd/daemon-bozo.c
@@ -125,8 +125,11 @@ create_pidfile(bozohttpd_t *httpd)
 void
 bozo_daemon_init(bozohttpd_t *httpd)
 {
+	struct sockaddr_storage ss;
 	struct addrinfo h, *r, *r0;
+	char host[NI_MAXHOST], port[NI_MAXSERV];
 	const char	*portnum;
+	socklen_t slen;
 	int e, i, on = 1;
 
 	if (!httpd->background && !httpd->foreground)
@@ -137,6 +140,7 @@ bozo_daemon_init(bozohttpd_t *httpd)
 
 	portnum = httpd->bindport ? httpd->bindport : httpd->defbindport;
 	
+	slen = sizeof(ss);
 	memset(&h, 0, sizeof(h));
 	h.ai_family = PF_UNSPEC;
 	h.ai_socktype = SOCK_STREAM;
@@ -162,6 +166,14 @@ bozo_daemon_init(bozohttpd_t *httpd)
 			continue;
 		if (listen(httpd->sock[i], SOMAXCONN) == -1)
 			continue;
+		memset(&ss, 0, slen);
+		if (getsockname(httpd->sock[i], (struct sockaddr *)(void *)&ss,
+		    &slen) == 0 &&
+		    getnameinfo((struct sockaddr *)&ss, slen,
+		        host, sizeof(host), port, sizeof(port),
+			NI_NUMERICHOST | NI_NUMERICSERV) == 0)
+			bozowarn(httpd, "listening on host: %s port: %s",
+			    host, port);
 		httpd->fds[i].events = POLLIN | POLLPRI | POLLRDNORM |
 				POLLRDBAND | POLLERR;
 		httpd->fds[i].fd = httpd->sock[i];


Home | Main Index | Thread Index | Old Index