convert: treat an empty string for clean/smudge filters as "cat"
[gitweb.git] / daemon.c
index 385934d46e8bc8c76f0eb86824f0f879bf0a0d77..be70cd4da09ccab8850be41e19b0fc4b9a10f502 100644 (file)
--- a/daemon.c
+++ b/daemon.c
@@ -812,8 +812,6 @@ static char **cld_argv;
 static void handle(int incoming, struct sockaddr *addr, socklen_t addrlen)
 {
        struct child_process cld = CHILD_PROCESS_INIT;
-       char addrbuf[300] = "REMOTE_ADDR=", portbuf[300];
-       char *env[] = { addrbuf, portbuf, NULL };
 
        if (max_connections && live_children >= max_connections) {
                kill_some_child();
@@ -827,27 +825,23 @@ static void handle(int incoming, struct sockaddr *addr, socklen_t addrlen)
        }
 
        if (addr->sa_family == AF_INET) {
+               char buf[128] = "";
                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));
+               inet_ntop(addr->sa_family, &sin_addr->sin_addr, buf, sizeof(buf));
+               argv_array_pushf(&cld.env_array, "REMOTE_ADDR=%s", buf);
+               argv_array_pushf(&cld.env_array, "REMOTE_PORT=%d",
+                                ntohs(sin_addr->sin_port));
 #ifndef NO_IPV6
        } else if (addr->sa_family == AF_INET6) {
+               char buf[128] = "";
                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));
+               inet_ntop(AF_INET6, &sin6_addr->sin6_addr, buf, sizeof(buf));
+               argv_array_pushf(&cld.env_array, "REMOTE_ADDR=[%s]", buf);
+               argv_array_pushf(&cld.env_array, "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,7 +896,7 @@ static const char *ip2str(int family, struct sockaddr *sin, socklen_t len)
                inet_ntop(family, &((struct sockaddr_in*)sin)->sin_addr, ip, len);
                break;
        default:
-               strcpy(ip, "<unknown>");
+               xsnprintf(ip, sizeof(ip), "<unknown>");
        }
        return ip;
 }
@@ -917,7 +911,7 @@ static int setup_named_sock(char *listen_addr, int listen_port, struct socketlis
        int gai;
        long flags;
 
-       sprintf(pbuf, "%d", listen_port);
+       xsnprintf(pbuf, sizeof(pbuf), "%d", listen_port);
        memset(&hints, 0, sizeof(hints));
        hints.ai_family = AF_UNSPEC;
        hints.ai_socktype = SOCK_STREAM;
@@ -1167,15 +1161,6 @@ static struct credentials *prepare_credentials(const char *user_name,
 }
 #endif
 
-static void store_pid(const char *path)
-{
-       FILE *f = fopen(path, "w");
-       if (!f)
-               die_errno("cannot open pid file '%s'", path);
-       if (fprintf(f, "%"PRIuMAX"\n", (uintmax_t) getpid()) < 0 || fclose(f) != 0)
-               die_errno("failed to write pid file '%s'", path);
-}
-
 static int serve(struct string_list *listen_addr, int listen_port,
     struct credentials *cred)
 {
@@ -1386,7 +1371,7 @@ int main(int argc, char **argv)
                sanitize_stdfds();
 
        if (pid_file)
-               store_pid(pid_file);
+               write_file(pid_file, "%"PRIuMAX, (uintmax_t) getpid());
 
        /* prepare argv for serving-processes */
        cld_argv = xmalloc(sizeof (char *) * (argc + 2));