Subject: Re: [PATCH] daemon: restore getpeername(0,...) use

From: "Joachim Schmitz" <[email-masked]>
Date: Sat, 8 Sep 2012 19:57:33 +0200

Jan Engelhardt wrote:
> This reverts f9c87be6b42dd0f8b31a4bb8c6a44326879fdd1a, in a sense,
> because that commit broke logging of "Connection from ..." when
> git-daemon is run under xinetd.
> 
> This patch here computes the text representation of the peer and then
> copies that to environment variables such that the code in execute()
> and subfunctions can stay as-is.
> 
> Signed-off-by: Jan Engelhardt <[email-masked]>
> ---
> daemon.c |   55
> +++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file
> changed, 51 insertions(+), 4 deletions(-) 
> 
> diff --git a/daemon.c b/daemon.c
> index 4602b46..eaf08c2 100644
> --- a/daemon.c
> +++ b/daemon.c
> @@ -1,3 +1,4 @@
> +#include <stdbool.h>
> #include "cache.h"
> #include "pkt-line.h"
> #include "exec_cmd.h"
> @@ -1164,6 +1165,54 @@ static int serve(struct string_list
>  *listen_addr, int listen_port, return service_loop(&socklist);
> }
> 
> +static void inetd_mode_prepare(void)
> +{
> + struct sockaddr_storage ss;
> + struct sockaddr *addr = (void *)&ss;
> + socklen_t slen = sizeof(ss);
> + char addrbuf[256], portbuf[6] = "";
> +
> + if (!freopen("/dev/null", "w", stderr))
> + die_errno("failed to redirect stderr to /dev/null");
> +
> + /*
> + * Windows is said to not be able to handle this, so we will simply
> + * ignore failure here. (It only affects a log message anyway.)
> + */
> + if (getpeername(0, addr, &slen) < 0)
> + return;
> +
> + if (addr->sa_family == AF_INET) {
> + const struct sockaddr_in *sin_addr = (void *)addr;
> +
> + if (inet_ntop(addr->sa_family, &sin_addr->sin_addr,
> +       addrbuf, sizeof(addrbuf)) == NULL)
> + return;
> + snprintf(portbuf, sizeof(portbuf), "%hu",
> + ntohs(sin_addr->sin_port));
> +#ifndef NO_IPV6
> + } else if (addr->sa_family == AF_INET6) {
> + const struct sockaddr_in6 *sin6_addr = (void *)addr;
> +
> + addrbuf[0] = '[';
> + addrbuf[1] = '\0';
> + if (inet_ntop(AF_INET6, &sin6_addr->sin6_addr, addrbuf + 1,
> +       sizeof(addrbuf) - 2) == NULL)
> + return;
> + strcat(addrbuf, "]");
> +
> + snprintf(portbuf, sizeof(portbuf), "%hu",
> + ntohs(sin6_addr->sin6_port));
> +#endif
> + } else {
> + snprintf(addrbuf, sizeof(addrbuf), "<AF %d>",
> + addr->sa_family);
> + }
> + if (setenv("REMOTE_ADDR", addrbuf, true) < 0)
> + return;
> + setenv("REMOTE_PORT", portbuf, true);

setenv() is not a function available on all plattfomrs.

Bye, Jojo