builtin/mv: fix out of bounds write
[gitweb.git] / builtin / gc.c
index 891a2c2ecb046dc29b2d9ce8b9d2982084bf9b19..25f2237c08f3b8fbc8f3554f3e22bf7e59e74b1c 100644 (file)
@@ -14,6 +14,7 @@
 #include "cache.h"
 #include "parse-options.h"
 #include "run-command.h"
+#include "sigchain.h"
 #include "argv-array.h"
 
 #define FAILED_RUN "failed to run %s"
@@ -35,6 +36,21 @@ static struct argv_array repack = ARGV_ARRAY_INIT;
 static struct argv_array prune = ARGV_ARRAY_INIT;
 static struct argv_array rerere = ARGV_ARRAY_INIT;
 
+static char *pidfile;
+
+static void remove_pidfile(void)
+{
+       if (pidfile)
+               unlink(pidfile);
+}
+
+static void remove_pidfile_on_signal(int signo)
+{
+       remove_pidfile();
+       sigchain_pop(signo);
+       raise(signo);
+}
+
 static int gc_config(const char *var, const char *value, void *cb)
 {
        if (!strcmp(var, "gc.packrefs")) {
@@ -179,6 +195,10 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
        FILE *fp;
        int fd, should_exit;
 
+       if (pidfile)
+               /* already locked */
+               return NULL;
+
        if (gethostname(my_host, sizeof(my_host)))
                strcpy(my_host, "unknown");
 
@@ -202,7 +222,7 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
                        time(NULL) - st.st_mtime <= 12 * 3600 &&
                        fscanf(fp, "%"PRIuMAX" %127c", &pid, locking_host) == 2 &&
                        /* be gentle to concurrent "gc" on remote hosts */
-                       (strcmp(locking_host, my_host) || !kill(pid, 0));
+                       (strcmp(locking_host, my_host) || !kill(pid, 0) || errno == EPERM);
                if (fp != NULL)
                        fclose(fp);
                if (should_exit) {
@@ -219,6 +239,10 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
        strbuf_release(&sb);
        commit_lock_file(&lock);
 
+       pidfile = git_pathdup("gc.pid");
+       sigchain_push_common(remove_pidfile_on_signal);
+       atexit(remove_pidfile);
+
        return NULL;
 }