Topo-sort before --simplify-merges
[gitweb.git] / daemon.c
index 66f8d6f03d91d7dfa4a164137ee0310ca26dc1aa..4540e8df5ab8bc8ff66549144d7db2928e12199b 100644 (file)
--- a/daemon.c
+++ b/daemon.c
@@ -9,14 +9,20 @@
 #define HOST_NAME_MAX 256
 #endif
 
+#ifndef NI_MAXSERV
+#define NI_MAXSERV 32
+#endif
+
 static int log_syslog;
 static int verbose;
 static int reuseaddr;
+static int child_handler_pipe[2];
 
 static const char daemon_usage[] =
-"git-daemon [--verbose] [--syslog] [--export-all]\n"
+"git daemon [--verbose] [--syslog] [--export-all]\n"
 "           [--timeout=n] [--init-timeout=n] [--strict-paths]\n"
-"           [--base-path=path] [--user-path | --user-path=path]\n"
+"           [--base-path=path] [--base-path-relaxed]\n"
+"           [--user-path | --user-path=path]\n"
 "           [--interpolated-path=path]\n"
 "           [--reuseaddr] [--detach] [--pid-file=file]\n"
 "           [--[enable|disable|allow-override|forbid-override]=service]\n"
@@ -34,6 +40,7 @@ static int export_all_trees;
 /* Take all paths relative to this one if non-NULL */
 static char *base_path;
 static char *interpolated_path;
+static int base_path_relaxed;
 
 /* Flag indicating client sent extra args. */
 static int saw_extended_args;
@@ -133,7 +140,7 @@ static int avoid_alias(char *p)
 {
        int sl, ndot;
 
-       /* 
+       /*
         * This resurrects the belts and suspenders paranoia check by HPA
         * done in <435560F7.4080006@zytor.com> thread, now enter_repo()
         * does not do getcwd() based path canonicalizations.
@@ -180,6 +187,7 @@ static char *path_ok(struct interp *itable)
 {
        static char rpath[PATH_MAX];
        static char interp_path[PATH_MAX];
+       int retried_path = 0;
        char *path;
        char *dir;
 
@@ -235,7 +243,22 @@ static char *path_ok(struct interp *itable)
                dir = rpath;
        }
 
-       path = enter_repo(dir, strict_paths);
+       do {
+               path = enter_repo(dir, strict_paths);
+               if (path)
+                       break;
+
+               /*
+                * if we fail and base_path_relaxed is enabled, try without
+                * prefixing the base path
+                */
+               if (base_path && base_path_relaxed && !retried_path) {
+                       dir = itable[INTERP_SLOT_DIR].value;
+                       retried_path = 1;
+                       continue;
+               }
+               break;
+       } while (1);
 
        if (!path) {
                logerror("'%s': unable to chdir or not a git archive", dir);
@@ -247,7 +270,7 @@ static char *path_ok(struct interp *itable)
                int pathlen = strlen(path);
 
                /* The validation is done on the paths after enter_repo
-                * appends optional {.git,.git/.git} and friends, but 
+                * appends optional {.git,.git/.git} and friends, but
                 * it does not use getcwd().  So if your /pub is
                 * a symlink to /mnt/pub, you can whitelist /pub and
                 * do not have to say /mnt/pub.
@@ -284,9 +307,9 @@ struct daemon_service {
 static struct daemon_service *service_looking_at;
 static int service_enabled;
 
-static int git_daemon_config(const char *var, const char *value)
+static int git_daemon_config(const char *var, const char *value, void *cb)
 {
-       if (!strncmp(var, "daemon.", 7) &&
+       if (!prefixcmp(var, "daemon.") &&
            !strcmp(var + 7, service_looking_at->config_name)) {
                service_enabled = git_config_bool(var, value);
                return 0;
@@ -334,7 +357,7 @@ static int run_service(struct interp *itable, struct daemon_service *service)
        if (service->overridable) {
                service_looking_at = service;
                service_enabled = -1;
-               git_config(git_daemon_config);
+               git_config(git_daemon_config, NULL);
                if (0 <= service_enabled)
                        enabled = service_enabled;
        }
@@ -384,7 +407,8 @@ static struct daemon_service daemon_service[] = {
        { "receive-pack", "receivepack", receive_pack, 0, 1 },
 };
 
-static void enable_service(const char *name, int ena) {
+static void enable_service(const char *name, int ena)
+{
        int i;
        for (i = 0; i < ARRAY_SIZE(daemon_service); i++) {
                if (!strcmp(daemon_service[i].name, name)) {
@@ -395,7 +419,8 @@ static void enable_service(const char *name, int ena) {
        die("No such service %s", name);
 }
 
-static void make_service_overridable(const char *name, int ena) {
+static void make_service_overridable(const char *name, int ena)
+{
        int i;
        for (i = 0; i < ARRAY_SIZE(daemon_service); i++) {
                if (!strcmp(daemon_service[i].name, name)) {
@@ -439,7 +464,7 @@ static void parse_extra_args(struct interp *table, char *extra_args, int buflen)
        }
 }
 
-void fill_in_extra_table_entries(struct interp *itable)
+static void fill_in_extra_table_entries(struct interp *itable)
 {
        char *hp;
 
@@ -518,7 +543,7 @@ static int execute(struct sockaddr *addr)
                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 = sin_addr->sin_port;
+                       port = ntohs(sin_addr->sin_port);
 #ifndef NO_IPV6
                } else if (addr && addr->sa_family == AF_INET6) {
                        struct sockaddr_in6 *sin6_addr = (void *) addr;
@@ -528,7 +553,7 @@ static int execute(struct sockaddr *addr)
                        inet_ntop(AF_INET6, &sin6_addr->sin6_addr, buf, sizeof(addrbuf) - 1);
                        strcat(buf, "]");
 
-                       port = sin6_addr->sin6_port;
+                       port = ntohs(sin6_addr->sin6_port);
 #endif
                }
                loginfo("Connection from %s:%d", addrbuf, port);
@@ -562,7 +587,7 @@ static int execute(struct sockaddr *addr)
        for (i = 0; i < ARRAY_SIZE(daemon_service); i++) {
                struct daemon_service *s = &(daemon_service[i]);
                int namelen = strlen(s->name);
-               if (!strncmp("git-", line, 4) &&
+               if (!prefixcmp(line, "git-") &&
                    !strncmp(s->name, line + 4, namelen) &&
                    line[namelen + 4] == ' ') {
                        /*
@@ -670,23 +695,47 @@ static void kill_some_children(int signo, unsigned start, unsigned stop)
        }
 }
 
+static void check_dead_children(void)
+{
+       unsigned spawned, reaped, deleted;
+
+       spawned = children_spawned;
+       reaped = children_reaped;
+       deleted = children_deleted;
+
+       while (deleted < reaped) {
+               pid_t pid = dead_child[deleted % MAX_CHILDREN];
+               const char *dead = pid < 0 ? " (with error)" : "";
+
+               if (pid < 0)
+                       pid = -pid;
+
+               /* XXX: Custom logging, since we don't wanna getpid() */
+               if (verbose) {
+                       if (log_syslog)
+                               syslog(LOG_INFO, "[%d] Disconnected%s",
+                                               pid, dead);
+                       else
+                               fprintf(stderr, "[%d] Disconnected%s\n",
+                                               pid, dead);
+               }
+               remove_child(pid, deleted, spawned);
+               deleted++;
+       }
+       children_deleted = deleted;
+}
+
 static void check_max_connections(void)
 {
        for (;;) {
                int active;
-               unsigned spawned, reaped, deleted;
+               unsigned spawned, deleted;
+
+               check_dead_children();
 
                spawned = children_spawned;
-               reaped = children_reaped;
                deleted = children_deleted;
 
-               while (deleted < reaped) {
-                       pid_t pid = dead_child[deleted % MAX_CHILDREN];
-                       remove_child(pid, deleted, spawned);
-                       deleted++;
-               }
-               children_deleted = deleted;
-
                active = spawned - deleted;
                if (active <= max_connections)
                        break;
@@ -736,18 +785,11 @@ static void child_handler(int signo)
 
                if (pid > 0) {
                        unsigned reaped = children_reaped;
+                       if (!WIFEXITED(status) || WEXITSTATUS(status) > 0)
+                               pid = -pid;
                        dead_child[reaped % MAX_CHILDREN] = pid;
                        children_reaped = reaped + 1;
-                       /* XXX: Custom logging, since we don't wanna getpid() */
-                       if (verbose) {
-                               const char *dead = "";
-                               if (!WIFEXITED(status) || WEXITSTATUS(status) > 0)
-                                       dead = " (with error)";
-                               if (log_syslog)
-                                       syslog(LOG_INFO, "[%d] Disconnected%s", pid, dead);
-                               else
-                                       fprintf(stderr, "[%d] Disconnected%s\n", pid, dead);
-                       }
+                       write(child_handler_pipe[1], &status, 1);
                        continue;
                }
                break;
@@ -893,19 +935,24 @@ static int service_loop(int socknum, int *socklist)
        struct pollfd *pfd;
        int i;
 
-       pfd = xcalloc(socknum, sizeof(struct pollfd));
+       if (pipe(child_handler_pipe) < 0)
+               die ("Could not set up pipe for child handler");
+
+       pfd = xcalloc(socknum + 1, sizeof(struct pollfd));
 
        for (i = 0; i < socknum; i++) {
                pfd[i].fd = socklist[i];
                pfd[i].events = POLLIN;
        }
+       pfd[socknum].fd = child_handler_pipe[0];
+       pfd[socknum].events = POLLIN;
 
        signal(SIGCHLD, child_handler);
 
        for (;;) {
                int i;
 
-               if (poll(pfd, socknum, -1) < 0) {
+               if (poll(pfd, socknum + 1, -1) < 0) {
                        if (errno != EINTR) {
                                error("poll failed, resuming: %s",
                                      strerror(errno));
@@ -913,6 +960,10 @@ static int service_loop(int socknum, int *socklist)
                        }
                        continue;
                }
+               if (pfd[socknum].revents & POLLIN) {
+                       read(child_handler_pipe[0], &i, 1);
+                       check_dead_children();
+               }
 
                for (i = 0; i < socknum; i++) {
                        if (pfd[i].revents & POLLIN) {
@@ -970,8 +1021,8 @@ static void store_pid(const char *path)
        FILE *f = fopen(path, "w");
        if (!f)
                die("cannot open pid file %s: %s", path, strerror(errno));
-       fprintf(f, "%d\n", getpid());
-       fclose(f);
+       if (fprintf(f, "%d\n", getpid()) < 0 || fclose(f) != 0)
+               die("failed to write pid file %s: %s", path, strerror(errno));
 }
 
 static int serve(char *listen_addr, int listen_port, struct passwd *pass, gid_t gid)
@@ -1011,7 +1062,7 @@ int main(int argc, char **argv)
        for (i = 1; i < argc; i++) {
                char *arg = argv[i];
 
-               if (!strncmp(arg, "--listen=", 9)) {
+               if (!prefixcmp(arg, "--listen=")) {
                    char *p = arg + 9;
                    char *ph = listen_addr = xmalloc(strlen(arg + 9) + 1);
                    while (*p)
@@ -1019,7 +1070,7 @@ int main(int argc, char **argv)
                    *ph = 0;
                    continue;
                }
-               if (!strncmp(arg, "--port=", 7)) {
+               if (!prefixcmp(arg, "--port=")) {
                        char *end;
                        unsigned long n;
                        n = strtoul(arg+7, &end, 0);
@@ -1045,11 +1096,11 @@ int main(int argc, char **argv)
                        export_all_trees = 1;
                        continue;
                }
-               if (!strncmp(arg, "--timeout=", 10)) {
+               if (!prefixcmp(arg, "--timeout=")) {
                        timeout = atoi(arg+10);
                        continue;
                }
-               if (!strncmp(arg, "--init-timeout=", 15)) {
+               if (!prefixcmp(arg, "--init-timeout=")) {
                        init_timeout = atoi(arg+15);
                        continue;
                }
@@ -1057,11 +1108,15 @@ int main(int argc, char **argv)
                        strict_paths = 1;
                        continue;
                }
-               if (!strncmp(arg, "--base-path=", 12)) {
+               if (!prefixcmp(arg, "--base-path=")) {
                        base_path = arg+12;
                        continue;
                }
-               if (!strncmp(arg, "--interpolated-path=", 20)) {
+               if (!strcmp(arg, "--base-path-relaxed")) {
+                       base_path_relaxed = 1;
+                       continue;
+               }
+               if (!prefixcmp(arg, "--interpolated-path=")) {
                        interpolated_path = arg+20;
                        continue;
                }
@@ -1073,11 +1128,11 @@ int main(int argc, char **argv)
                        user_path = "";
                        continue;
                }
-               if (!strncmp(arg, "--user-path=", 12)) {
+               if (!prefixcmp(arg, "--user-path=")) {
                        user_path = arg + 12;
                        continue;
                }
-               if (!strncmp(arg, "--pid-file=", 11)) {
+               if (!prefixcmp(arg, "--pid-file=")) {
                        pid_file = arg + 11;
                        continue;
                }
@@ -1086,27 +1141,27 @@ int main(int argc, char **argv)
                        log_syslog = 1;
                        continue;
                }
-               if (!strncmp(arg, "--user=", 7)) {
+               if (!prefixcmp(arg, "--user=")) {
                        user_name = arg + 7;
                        continue;
                }
-               if (!strncmp(arg, "--group=", 8)) {
+               if (!prefixcmp(arg, "--group=")) {
                        group_name = arg + 8;
                        continue;
                }
-               if (!strncmp(arg, "--enable=", 9)) {
+               if (!prefixcmp(arg, "--enable=")) {
                        enable_service(arg + 9, 1);
                        continue;
                }
-               if (!strncmp(arg, "--disable=", 10)) {
+               if (!prefixcmp(arg, "--disable=")) {
                        enable_service(arg + 10, 0);
                        continue;
                }
-               if (!strncmp(arg, "--allow-override=", 17)) {
+               if (!prefixcmp(arg, "--allow-override=")) {
                        make_service_overridable(arg + 17, 1);
                        continue;
                }
-               if (!strncmp(arg, "--forbid-override=", 18)) {
+               if (!prefixcmp(arg, "--forbid-override=")) {
                        make_service_overridable(arg + 18, 0);
                        continue;
                }
@@ -1121,6 +1176,11 @@ int main(int argc, char **argv)
                usage(daemon_usage);
        }
 
+       if (log_syslog) {
+               openlog("git-daemon", 0, LOG_DAEMON);
+               set_die_routine(daemon_die);
+       }
+
        if (inetd_mode && (group_name || user_name))
                die("--user and --group are incompatible with --inetd");
 
@@ -1148,14 +1208,17 @@ int main(int argc, char **argv)
                }
        }
 
-       if (log_syslog) {
-               openlog("git-daemon", 0, LOG_DAEMON);
-               set_die_routine(daemon_die);
-       }
-
        if (strict_paths && (!ok_paths || !*ok_paths))
                die("option --strict-paths requires a whitelist");
 
+       if (base_path) {
+               struct stat st;
+
+               if (stat(base_path, &st) || !S_ISDIR(st.st_mode))
+                       die("base-path '%s' does not exist or "
+                           "is not a directory", base_path);
+       }
+
        if (inetd_mode) {
                struct sockaddr_storage ss;
                struct sockaddr *peer = (struct sockaddr *)&ss;