Merge branch 'rs/daemon-fixes' into maint
[gitweb.git] / setup.c
diff --git a/setup.c b/setup.c
index cffb6d605e7893a3f9ff83f427732f6b7f3443ff..0a22f8bd1d631fe5f0afe1d84162ca4064a00e4f 100644 (file)
--- a/setup.c
+++ b/setup.c
@@ -29,7 +29,7 @@ static int abspath_part_inside_repo(char *path)
                return -1;
        wtlen = strlen(work_tree);
        len = strlen(path);
-       off = 0;
+       off = offset_1st_component(path);
 
        /* check if work tree is already the prefix */
        if (wtlen <= len && !strncmp(path, work_tree, wtlen)) {
@@ -45,7 +45,7 @@ static int abspath_part_inside_repo(char *path)
                off = wtlen;
        }
        path0 = path;
-       path += offset_1st_component(path) + off;
+       path += off;
 
        /* check each '/'-terminated level */
        while (*path) {
@@ -841,3 +841,27 @@ void sanitize_stdfds(void)
        if (fd > 2)
                close(fd);
 }
+
+int daemonize(void)
+{
+#ifdef NO_POSIX_GOODIES
+       errno = ENOSYS;
+       return -1;
+#else
+       switch (fork()) {
+               case 0:
+                       break;
+               case -1:
+                       die_errno("fork failed");
+               default:
+                       exit(0);
+       }
+       if (setsid() == -1)
+               die_errno("setsid failed");
+       close(0);
+       close(1);
+       close(2);
+       sanitize_stdfds();
+       return 0;
+#endif
+}