From: Erik Faye-Lund <kusmabite@gmail.com>
To: git@vger.kernel.org
Cc: gitster@pobox.com
Subject: [PATCH v7 11/16] daemon: get remote host address from root-process
Date: Thu, 4 Nov 2010 02:35:19 +0100 [thread overview]
Message-ID: <1288834524-2400-12-git-send-email-kusmabite@gmail.com> (raw)
In-Reply-To: <1288834524-2400-1-git-send-email-kusmabite@gmail.com>
Get remote host in the process that accept() and pass it through
the REMOTE_ADDR environment variable to the handler-process.
Introduce the REMOTE_PORT environmen variable for the port.
Use these variables for reporting instead of doing
getpeername(0, ...), which doesn't work on Windows.
Signed-off-by: Erik Faye-Lund <kusmabite@gmail.com>
---
daemon.c | 79 ++++++++++++++++++++++++++++++-------------------------------
1 files changed, 39 insertions(+), 40 deletions(-)
diff --git a/daemon.c b/daemon.c
index 941c095..8162f10 100644
--- a/daemon.c
+++ b/daemon.c
@@ -516,37 +516,14 @@ static void parse_host_arg(char *extra_args, int buflen)
}
-static int execute(struct sockaddr *addr)
+static int execute(void)
{
static char line[1000];
int pktlen, len, i;
+ char *addr = getenv("REMOTE_ADDR"), *port = getenv("REMOTE_PORT");
- if (addr) {
- char addrbuf[256] = "";
- int port = -1;
-
- if (addr->sa_family == AF_INET) {
- struct sockaddr_in *sin_addr = (void *) addr;
- inet_ntop(addr->sa_family, &sin_addr->sin_addr, addrbuf, sizeof(addrbuf));
- port = ntohs(sin_addr->sin_port);
-#ifndef NO_IPV6
- } else if (addr && addr->sa_family == AF_INET6) {
- struct sockaddr_in6 *sin6_addr = (void *) addr;
-
- char *buf = addrbuf;
- *buf++ = '['; *buf = '\0'; /* stpcpy() is cool */
- inet_ntop(AF_INET6, &sin6_addr->sin6_addr, buf, sizeof(addrbuf) - 1);
- strcat(buf, "]");
-
- port = ntohs(sin6_addr->sin6_port);
-#endif
- }
- loginfo("Connection from %s:%d", addrbuf, port);
- setenv("REMOTE_ADDR", addrbuf, 1);
- }
- else {
- unsetenv("REMOTE_ADDR");
- }
+ if (addr)
+ loginfo("Connection from %s:%s", addr, port);
alarm(init_timeout ? init_timeout : timeout);
pktlen = packet_read_line(0, line, sizeof(line));
@@ -680,6 +657,8 @@ static char **cld_argv;
static void handle(int incoming, struct sockaddr *addr, int addrlen)
{
struct child_process cld = { 0 };
+ char addrbuf[300] = "REMOTE_ADDR=", portbuf[300];
+ char *env[] = { addrbuf, portbuf, NULL };
if (max_connections && live_children >= max_connections) {
kill_some_child();
@@ -692,6 +671,28 @@ static void handle(int incoming, struct sockaddr *addr, int addrlen)
}
}
+ if (addr->sa_family == AF_INET) {
+ struct sockaddr_in *sin_addr = (void *) addr;
+ inet_ntop(addr->sa_family, &sin_addr->sin_addr, addrbuf + 12,
+ sizeof(addrbuf) - 12);
+ snprintf(portbuf, sizeof(portbuf), "REMOTE_PORT=%d",
+ ntohs(sin_addr->sin_port));
+#ifndef NO_IPV6
+ } else if (addr && addr->sa_family == AF_INET6) {
+ struct sockaddr_in6 *sin6_addr = (void *) addr;
+
+ char *buf = addrbuf + 12;
+ *buf++ = '['; *buf = '\0'; /* stpcpy() is cool */
+ inet_ntop(AF_INET6, &sin6_addr->sin6_addr, buf,
+ sizeof(addrbuf) - 13);
+ strcat(buf, "]");
+
+ snprintf(portbuf, sizeof(portbuf), "REMOTE_PORT=%d",
+ ntohs(sin6_addr->sin6_port));
+#endif
+ }
+
+ cld.env = (const char **)env;
cld.argv = (const char **)cld_argv;
cld.in = incoming;
cld.out = dup(incoming);
@@ -902,9 +903,15 @@ static int service_loop(struct socketlist *socklist)
for (i = 0; i < socklist->nr; i++) {
if (pfd[i].revents & POLLIN) {
- struct sockaddr_storage ss;
+ union {
+ struct sockaddr sa;
+ struct sockaddr_in sai;
+#ifndef NO_IPV6
+ struct sockaddr_in6 sai6;
+#endif
+ } ss;
unsigned int sslen = sizeof(ss);
- int incoming = accept(pfd[i].fd, (struct sockaddr *)&ss, &sslen);
+ int incoming = accept(pfd[i].fd, &ss.sa, &sslen);
if (incoming < 0) {
switch (errno) {
case EAGAIN:
@@ -915,7 +922,7 @@ static int service_loop(struct socketlist *socklist)
die_errno("accept returned");
}
}
- handle(incoming, (struct sockaddr *)&ss, sslen);
+ handle(incoming, &ss.sa, sslen);
}
}
}
@@ -1160,16 +1167,8 @@ int main(int argc, char **argv)
die_errno("failed to redirect stderr to /dev/null");
}
- if (inetd_mode || serve_mode) {
- struct sockaddr_storage ss;
- struct sockaddr *peer = (struct sockaddr *)&ss;
- socklen_t slen = sizeof(ss);
-
- if (getpeername(0, peer, &slen))
- return execute(NULL);
- else
- return execute(peer);
- }
+ if (inetd_mode || serve_mode)
+ return execute();
if (detach) {
daemonize();
--
1.7.3.2.162.g09d37
next prev parent reply other threads:[~2010-11-04 1:36 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-11-04 1:35 [PATCH v7 00/16] daemon-win32 Erik Faye-Lund
2010-11-04 1:35 ` [PATCH v7 01/16] mingw: add network-wrappers for daemon Erik Faye-Lund
2010-11-04 1:35 ` [PATCH v7 02/16] mingw: implement syslog Erik Faye-Lund
2010-11-04 1:35 ` [PATCH v7 03/16] compat: add inet_pton and inet_ntop prototypes Erik Faye-Lund
2010-11-04 1:35 ` [PATCH v7 04/16] inet_ntop: fix a couple of old-style decls Erik Faye-Lund
2010-11-04 1:35 ` [PATCH v7 05/16] mingw: use real pid Erik Faye-Lund
2010-11-04 1:35 ` [PATCH v7 06/16] mingw: support waitpid with pid > 0 and WNOHANG Erik Faye-Lund
2010-11-04 1:35 ` [PATCH v7 07/16] mingw: add kill emulation Erik Faye-Lund
2010-11-04 1:35 ` [PATCH v7 08/16] daemon: use run-command api for async serving Erik Faye-Lund
2011-01-04 4:04 ` [PATCH] daemon: support <directory> arguments again Jonathan Nieder
2011-01-04 12:42 ` Erik Faye-Lund
2011-01-04 19:18 ` Junio C Hamano
2010-11-04 1:35 ` [PATCH v7 09/16] daemon: use full buffered mode for stderr Erik Faye-Lund
2010-11-04 1:35 ` [PATCH v7 10/16] Improve the mingw getaddrinfo stub to handle more use cases Erik Faye-Lund
2010-11-04 1:35 ` Erik Faye-Lund [this message]
2010-11-04 1:35 ` [PATCH v7 12/16] mingw: import poll-emulation from gnulib Erik Faye-Lund
2011-07-06 9:06 ` Erik Faye-Lund
2010-11-04 1:35 ` [PATCH v7 13/16] mingw: use " Erik Faye-Lund
2010-11-04 1:35 ` [PATCH v7 14/16] daemon: use socklen_t Erik Faye-Lund
2010-11-04 1:35 ` [PATCH v7 15/16] daemon: make --inetd and --detach incompatible Erik Faye-Lund
2010-11-04 1:35 ` [PATCH v7 16/16] daemon: opt-out on features that require posix Erik Faye-Lund
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1288834524-2400-12-git-send-email-kusmabite@gmail.com \
--to=kusmabite@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).