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>
Alexander Sulfrian committed
Aug 30, 2010 at 13:30 UTC
3a3a29c1da97b36dda2761c2e6b61c1a82e20a54
2 files changed
+23
-9
Documentation/git-daemon.txt
+1
@@ -85,6 +85,7 @@ OPTIONS
85
be either an IPv4 address or an IPv6 address if supported. If IPv6
86
is not supported, then --listen=hostname is also not supported and
87
--listen must be given an IPv4 address.
88
+ Can be given more than once.
89
Incompatible with '--inetd' option.
90
91
--port=n::
daemon.c
+22
-9
@@ -3,6 +3,7 @@
3
#include "exec_cmd.h"
4
#include "run-command.h"
5
#include "strbuf.h"
6
+#include "string-list.h"
7
8
#include <syslog.h>
9
@@ -866,9 +867,21 @@ static int setup_named_sock(char *listen_addr, int listen_port, struct socketlis
867
868
#endif
869
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)
871
{
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
+ }
885
}
886
887
static int service_loop(struct socketlist *socklist)
@@ -959,14 +972,14 @@ static void store_pid(const char *path)
972
die_errno("failed to write pid file '%s'", path);
973
}
974
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)
976
{
977
struct socketlist socklist = { NULL, 0, 0 };
978
979
socksetup(listen_addr, listen_port, &socklist);
980
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);
983
984
if (pass && gid &&
985
(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
992
int main(int argc, char **argv)
993
{
994
int listen_port = 0;
982
- char *listen_addr = NULL;
995
+ struct string_list listen_addr = STRING_LIST_INIT_NODUP;
996
int inetd_mode = 0;
997
const char *pid_file = NULL, *user_name = NULL, *group_name = NULL;
998
int detach = 0;
@@ -994,7 +1007,7 @@ int main(int argc, char **argv)
1007
char *arg = argv[i];
1008
1009
if (!prefixcmp(arg, "--listen=")) {
997
- listen_addr = xstrdup_tolower(arg + 9);
1010
+ string_list_append(&listen_addr, xstrdup_tolower(arg + 9));
1011
continue;
1012
}
1013
if (!prefixcmp(arg, "--port=")) {
@@ -1119,7 +1132,7 @@ int main(int argc, char **argv)
1132
if (inetd_mode && (group_name || user_name))
1133
die("--user and --group are incompatible with --inetd");
1134
1122
- if (inetd_mode && (listen_port || listen_addr))
1135
+ if (inetd_mode && (listen_port || (listen_addr.nr > 0)))
1136
die("--listen= and --port= are incompatible with --inetd");
1137
else if (listen_port == 0)
1138
listen_port = DEFAULT_GIT_PORT;
@@ -1174,5 +1187,5 @@ int main(int argc, char **argv)
1187
if (pid_file)
1188
store_pid(pid_file);
1189
1177
- return serve(listen_addr, listen_port, pass, gid);
1190
+ return serve(&listen_addr, listen_port, pass, gid);
1191
}