Skip to content

Commit 3a3a29c

Browse files
AlexanderSgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2caa321 commit 3a3a29c

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

Documentation/git-daemon.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ OPTIONS
8585
be either an IPv4 address or an IPv6 address if supported. If IPv6
8686
is not supported, then --listen=hostname is also not supported and
8787
--listen must be given an IPv4 address.
88+
Can be given more than once.
8889
Incompatible with '--inetd' option.
8990

9091
--port=n::

daemon.c

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "exec_cmd.h"
44
#include "run-command.h"
55
#include "strbuf.h"
6+
#include "string-list.h"
67

78
#include <syslog.h>
89

@@ -866,9 +867,21 @@ static int setup_named_sock(char *listen_addr, int listen_port, struct socketlis
866867

867868
#endif
868869

869-
static void socksetup(char *listen_addr, int listen_port, struct socketlist *socklist)
870+
static void socksetup(struct string_list *listen_addr, int listen_port, struct socketlist *socklist)
870871
{
871-
setup_named_sock(listen_addr, listen_port, socklist);
872+
if (!listen_addr->nr)
873+
setup_named_sock(NULL, listen_port, socklist);
874+
else {
875+
int i, socknum;
876+
for (i = 0; i < listen_addr->nr; i++) {
877+
socknum = setup_named_sock(listen_addr->items[i].string,
878+
listen_port, socklist);
879+
880+
if (socknum == 0)
881+
logerror("unable to allocate any listen sockets for host %s on port %u",
882+
listen_addr->items[i].string, listen_port);
883+
}
884+
}
872885
}
873886

874887
static int service_loop(struct socketlist *socklist)
@@ -959,14 +972,14 @@ static void store_pid(const char *path)
959972
die_errno("failed to write pid file '%s'", path);
960973
}
961974

962-
static int serve(char *listen_addr, int listen_port, struct passwd *pass, gid_t gid)
975+
static int serve(struct string_list *listen_addr, int listen_port, struct passwd *pass, gid_t gid)
963976
{
964977
struct socketlist socklist = { NULL, 0, 0 };
965978

966979
socksetup(listen_addr, listen_port, &socklist);
967980
if (socklist.nr == 0)
968-
die("unable to allocate any listen sockets on host %s port %u",
969-
listen_addr, listen_port);
981+
die("unable to allocate any listen sockets on port %u",
982+
listen_port);
970983

971984
if (pass && gid &&
972985
(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
979992
int main(int argc, char **argv)
980993
{
981994
int listen_port = 0;
982-
char *listen_addr = NULL;
995+
struct string_list listen_addr = STRING_LIST_INIT_NODUP;
983996
int inetd_mode = 0;
984997
const char *pid_file = NULL, *user_name = NULL, *group_name = NULL;
985998
int detach = 0;
@@ -994,7 +1007,7 @@ int main(int argc, char **argv)
9941007
char *arg = argv[i];
9951008

9961009
if (!prefixcmp(arg, "--listen=")) {
997-
listen_addr = xstrdup_tolower(arg + 9);
1010+
string_list_append(&listen_addr, xstrdup_tolower(arg + 9));
9981011
continue;
9991012
}
10001013
if (!prefixcmp(arg, "--port=")) {
@@ -1119,7 +1132,7 @@ int main(int argc, char **argv)
11191132
if (inetd_mode && (group_name || user_name))
11201133
die("--user and --group are incompatible with --inetd");
11211134

1122-
if (inetd_mode && (listen_port || listen_addr))
1135+
if (inetd_mode && (listen_port || (listen_addr.nr > 0)))
11231136
die("--listen= and --port= are incompatible with --inetd");
11241137
else if (listen_port == 0)
11251138
listen_port = DEFAULT_GIT_PORT;
@@ -1174,5 +1187,5 @@ int main(int argc, char **argv)
11741187
if (pid_file)
11751188
store_pid(pid_file);
11761189

1177-
return serve(listen_addr, listen_port, pass, gid);
1190+
return serve(&listen_addr, listen_port, pass, gid);
11781191
}

0 commit comments

Comments
 (0)