@matheusbsbs / matheus-bagliano / commits / 5e635e3960
Fork 0

lockfile: record the primary process.

The usual process flow is the main process opens and holds the lock to the index, does its thing, perhaps spawning children during the course, and then writes the resulting index out by releaseing the lock. However, the lockfile interface uses atexit(3) to clean it up, without regard to who actually created the lock. This typically leads to a confusing behaviour of lock being released too early when the child exits, and then the parent process when it calls commit_lockfile() finds that it cannot unlock it. This fixes the problem by recording who created and holds the lock, and upon atexit(3) handler, child simply ignores the lockfile the parent created. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Apr 21, 2007 at 03:11 UTC 5e635e396020cc08bc21a3e67c20c5294d6d13fd
2 files changed +6 -1
cache.h
+1
@@ -209,6 +209,7 @@ extern int refresh_cache(unsigned int flags);
209
210 struct lock_file {
211 struct lock_file *next;
212 + pid_t owner;
213 char on_list;
214 char filename[PATH_MAX];
215 };
lockfile.c
+5 -1
@@ -8,8 +8,11 @@ static const char *alternate_index_output;
8
9 static void remove_lock_file(void)
10 {
11 + pid_t me = getpid();
12 +
13 while (lock_file_list) {
12 - if (lock_file_list->filename[0])
14 + if (lock_file_list->owner == me &&
15 + lock_file_list->filename[0])
16 unlink(lock_file_list->filename);
17 lock_file_list = lock_file_list->next;
18 }
@@ -28,6 +31,7 @@ static int lock_file(struct lock_file *lk, const char *path)
31 sprintf(lk->filename, "%s.lock", path);
32 fd = open(lk->filename, O_RDWR | O_CREAT | O_EXCL, 0666);
33 if (0 <= fd) {
34 + lk->owner = getpid();
35 if (!lk->on_list) {
36 lk->next = lock_file_list;
37 lock_file_list = lk;