daemon: allow more than one host address given via --listen

When the host has more than one interfaces, daemon can listen to all
of them by not giving any --listen option, or listen to only one.
Teach it to accept more than one --listen options.

Remove the hostname information form the die, if no socket could be
created. It would only trigger when no interface out of either all
interface or the ones specified on the command line with --listen
options, can be listened to and so the user does know which "host" was
asked.

Signed-off-by: Alexander Sulfrian <alexander@sulfrian.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Alexander Sulfrian 2010-08-30 13:30:51 +02:00 committed by Junio C Hamano
parent 2caa321503
commit 3a3a29c1da
2 changed files with 23 additions and 9 deletions

View File

@ -85,6 +85,7 @@ OPTIONS
be either an IPv4 address or an IPv6 address if supported. If IPv6 be either an IPv4 address or an IPv6 address if supported. If IPv6
is not supported, then --listen=hostname is also not supported and is not supported, then --listen=hostname is also not supported and
--listen must be given an IPv4 address. --listen must be given an IPv4 address.
Can be given more than once.
Incompatible with '--inetd' option. Incompatible with '--inetd' option.
--port=n:: --port=n::

View File

@ -3,6 +3,7 @@
#include "exec_cmd.h" #include "exec_cmd.h"
#include "run-command.h" #include "run-command.h"
#include "strbuf.h" #include "strbuf.h"
#include "string-list.h"
#include <syslog.h> #include <syslog.h>
@ -866,9 +867,21 @@ static int setup_named_sock(char *listen_addr, int listen_port, struct socketlis
#endif #endif
static void socksetup(char *listen_addr, int listen_port, struct socketlist *socklist) static void socksetup(struct string_list *listen_addr, int listen_port, struct socketlist *socklist)
{ {
setup_named_sock(listen_addr, listen_port, socklist); if (!listen_addr->nr)
setup_named_sock(NULL, listen_port, socklist);
else {
int i, socknum;
for (i = 0; i < listen_addr->nr; i++) {
socknum = setup_named_sock(listen_addr->items[i].string,
listen_port, socklist);
if (socknum == 0)
logerror("unable to allocate any listen sockets for host %s on port %u",
listen_addr->items[i].string, listen_port);
}
}
} }
static int service_loop(struct socketlist *socklist) static int service_loop(struct socketlist *socklist)
@ -959,14 +972,14 @@ static void store_pid(const char *path)
die_errno("failed to write pid file '%s'", path); die_errno("failed to write pid file '%s'", path);
} }
static int serve(char *listen_addr, int listen_port, struct passwd *pass, gid_t gid) static int serve(struct string_list *listen_addr, int listen_port, struct passwd *pass, gid_t gid)
{ {
struct socketlist socklist = { NULL, 0, 0 }; struct socketlist socklist = { NULL, 0, 0 };
socksetup(listen_addr, listen_port, &socklist); socksetup(listen_addr, listen_port, &socklist);
if (socklist.nr == 0) if (socklist.nr == 0)
die("unable to allocate any listen sockets on host %s port %u", die("unable to allocate any listen sockets on port %u",
listen_addr, listen_port); listen_port);
if (pass && gid && if (pass && gid &&
(initgroups(pass->pw_name, gid) || setgid (gid) || (initgroups(pass->pw_name, gid) || setgid (gid) ||
@ -979,7 +992,7 @@ static int serve(char *listen_addr, int listen_port, struct passwd *pass, gid_t
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int listen_port = 0; int listen_port = 0;
char *listen_addr = NULL; struct string_list listen_addr = STRING_LIST_INIT_NODUP;
int inetd_mode = 0; int inetd_mode = 0;
const char *pid_file = NULL, *user_name = NULL, *group_name = NULL; const char *pid_file = NULL, *user_name = NULL, *group_name = NULL;
int detach = 0; int detach = 0;
@ -994,7 +1007,7 @@ int main(int argc, char **argv)
char *arg = argv[i]; char *arg = argv[i];
if (!prefixcmp(arg, "--listen=")) { if (!prefixcmp(arg, "--listen=")) {
listen_addr = xstrdup_tolower(arg + 9); string_list_append(&listen_addr, xstrdup_tolower(arg + 9));
continue; continue;
} }
if (!prefixcmp(arg, "--port=")) { if (!prefixcmp(arg, "--port=")) {
@ -1119,7 +1132,7 @@ int main(int argc, char **argv)
if (inetd_mode && (group_name || user_name)) if (inetd_mode && (group_name || user_name))
die("--user and --group are incompatible with --inetd"); die("--user and --group are incompatible with --inetd");
if (inetd_mode && (listen_port || listen_addr)) if (inetd_mode && (listen_port || (listen_addr.nr > 0)))
die("--listen= and --port= are incompatible with --inetd"); die("--listen= and --port= are incompatible with --inetd");
else if (listen_port == 0) else if (listen_port == 0)
listen_port = DEFAULT_GIT_PORT; listen_port = DEFAULT_GIT_PORT;
@ -1174,5 +1187,5 @@ int main(int argc, char **argv)
if (pid_file) if (pid_file)
store_pid(pid_file); store_pid(pid_file);
return serve(listen_addr, listen_port, pass, gid); return serve(&listen_addr, listen_port, pass, gid);
} }