All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3 00/25] Lockfile correctness and refactoring
@ 2014-04-14 13:54 Michael Haggerty
  2014-04-14 13:54 ` [PATCH v3 01/25] unable_to_lock_die(): rename function from unable_to_lock_index_die() Michael Haggerty
                   ` (25 more replies)
  0 siblings, 26 replies; 32+ messages in thread
From: Michael Haggerty @ 2014-04-14 13:54 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: git, Johannes Sixt, Jeff King, Torsten Bögershausen,
	Eric Sunshine, Michael Haggerty

Round v3.  Thanks to Johannes Sixt and Peff for feedback on v2.  This
version addresses all issues raised for v1 [1] and v2 [2].

Changes since v2:

* Instead of keeping track of whether a lock_file object is active via
  a new bit in a flags bitmask, store it in a separate volatile
  sig_atomic_t struct member.  This makes it a little less undefined
  when this field is accessed by the signal handler.

* Add some other "volatile" qualifiers to values used by the signal
  handler.

* Define constants LOCK_SUFFIX and LOCK_SUFFIX_LEN in cache.h and use
  them both inside and outside lockfile.c.  (In v2, only
  LOCK_SUFFIX_LEN was defined and it was only used within lockfile.c,
  because the other potential users in refs.c were rewritten anyway.
  But that rewriting is no longer included in the patch series, so it
  makes sense to define these constants as part of the public lockfile
  API.)

* Swap order of first two patches because the documentation lists
  unable_to_lock_die() under its new name.

* die() (instead of NOP) if commit_lock_file() is called for an
  unlocked lock_file object.

* Rebase to current master (there were no conflicts).

[1] http://thread.gmane.org/gmane.comp.version-control.git/245609
[2] http://thread.gmane.org/gmane.comp.version-control.git/245801

Michael Haggerty (25):
  unable_to_lock_die(): rename function from unable_to_lock_index_die()
  api-lockfile: expand the documentation
  rollback_lock_file(): do not clear filename redundantly
  rollback_lock_file(): set fd to -1
  lockfile: unlock file if lockfile permissions cannot be adjusted
  hold_lock_file_for_append(): release lock on errors
  lock_file(): always add lock_file object to lock_file_list
  lockfile.c: document the various states of lock_file objects
  cache.h: define constants LOCK_SUFFIX and LOCK_SUFFIX_LEN
  delete_ref_loose(): don't muck around in the lock_file's filename
  prepare_index(): declare return value to be (const char *)
  write_packed_entry_fn(): convert cb_data into a (const int *)
  lock_file(): exit early if lockfile cannot be opened
  remove_lock_file(): call rollback_lock_file()
  commit_lock_file(): inline temporary variable
  commit_lock_file(): die() if called for unlocked lockfile object
  lockfile: avoid transitory invalid states
  struct lock_file: declare some fields volatile
  try_merge_strategy(): remove redundant lock_file allocation
  try_merge_strategy(): use a statically-allocated lock_file object
  commit_lock_file(): use a strbuf to manage temporary space
  Change lock_file::filename into a strbuf
  resolve_symlink(): use a strbuf for internal scratch space
  resolve_symlink(): take a strbuf parameter
  trim_last_path_elm(): replace last_path_elm()

 Documentation/technical/api-lockfile.txt |  40 ++++-
 builtin/commit.c                         |  16 +-
 builtin/merge.c                          |  15 +-
 builtin/reflog.c                         |   2 +-
 builtin/update-index.c                   |   2 +-
 cache.h                                  |  15 +-
 config.c                                 |   6 +-
 lockfile.c                               | 275 ++++++++++++++++++-------------
 refs.c                                   |  29 ++--
 shallow.c                                |   6 +-
 10 files changed, 247 insertions(+), 159 deletions(-)

-- 
1.9.1

^ permalink raw reply	[flat|nested] 32+ messages in thread

* [PATCH v3 01/25] unable_to_lock_die(): rename function from unable_to_lock_index_die()
  2014-04-14 13:54 [PATCH v3 00/25] Lockfile correctness and refactoring Michael Haggerty
@ 2014-04-14 13:54 ` Michael Haggerty
  2014-04-14 13:54 ` [PATCH v3 02/25] api-lockfile: expand the documentation Michael Haggerty
                   ` (24 subsequent siblings)
  25 siblings, 0 replies; 32+ messages in thread
From: Michael Haggerty @ 2014-04-14 13:54 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: git, Johannes Sixt, Jeff King, Torsten Bögershausen,
	Eric Sunshine, Michael Haggerty

This function is used for other things besides the index, so rename it
accordingly.

Suggested-by: Jeff King <peff@peff.net>
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
 builtin/update-index.c | 2 +-
 cache.h                | 2 +-
 lockfile.c             | 6 +++---
 refs.c                 | 2 +-
 4 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/builtin/update-index.c b/builtin/update-index.c
index ba54e19..0bab053 100644
--- a/builtin/update-index.c
+++ b/builtin/update-index.c
@@ -919,7 +919,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 		if (newfd < 0) {
 			if (refresh_args.flags & REFRESH_QUIET)
 				exit(128);
-			unable_to_lock_index_die(get_index_file(), lock_error);
+			unable_to_lock_die(get_index_file(), lock_error);
 		}
 		if (write_cache(newfd, active_cache, active_nr) ||
 		    commit_locked_index(lock_file))
diff --git a/cache.h b/cache.h
index 107ac61..3a873a4 100644
--- a/cache.h
+++ b/cache.h
@@ -543,7 +543,7 @@ struct lock_file {
 #define LOCK_DIE_ON_ERROR 1
 #define LOCK_NODEREF 2
 extern int unable_to_lock_error(const char *path, int err);
-extern NORETURN void unable_to_lock_index_die(const char *path, int err);
+extern NORETURN void unable_to_lock_die(const char *path, int err);
 extern int hold_lock_file_for_update(struct lock_file *, const char *path, int);
 extern int hold_lock_file_for_append(struct lock_file *, const char *path, int);
 extern int commit_lock_file(struct lock_file *);
diff --git a/lockfile.c b/lockfile.c
index 8fbcb6a..d4bfb3f 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -181,7 +181,7 @@ int unable_to_lock_error(const char *path, int err)
 	return -1;
 }
 
-NORETURN void unable_to_lock_index_die(const char *path, int err)
+NORETURN void unable_to_lock_die(const char *path, int err)
 {
 	die("%s", unable_to_lock_message(path, err));
 }
@@ -190,7 +190,7 @@ int hold_lock_file_for_update(struct lock_file *lk, const char *path, int flags)
 {
 	int fd = lock_file(lk, path, flags);
 	if (fd < 0 && (flags & LOCK_DIE_ON_ERROR))
-		unable_to_lock_index_die(path, errno);
+		unable_to_lock_die(path, errno);
 	return fd;
 }
 
@@ -201,7 +201,7 @@ int hold_lock_file_for_append(struct lock_file *lk, const char *path, int flags)
 	fd = lock_file(lk, path, flags);
 	if (fd < 0) {
 		if (flags & LOCK_DIE_ON_ERROR)
-			unable_to_lock_index_die(path, errno);
+			unable_to_lock_die(path, errno);
 		return fd;
 	}
 
diff --git a/refs.c b/refs.c
index 28d5eca..67fe8b7 100644
--- a/refs.c
+++ b/refs.c
@@ -2118,7 +2118,7 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
 			 */
 			goto retry;
 		else
-			unable_to_lock_index_die(ref_file, errno);
+			unable_to_lock_die(ref_file, errno);
 	}
 	return old_sha1 ? verify_lock(lock, old_sha1, mustexist) : lock;
 
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v3 02/25] api-lockfile: expand the documentation
  2014-04-14 13:54 [PATCH v3 00/25] Lockfile correctness and refactoring Michael Haggerty
  2014-04-14 13:54 ` [PATCH v3 01/25] unable_to_lock_die(): rename function from unable_to_lock_index_die() Michael Haggerty
@ 2014-04-14 13:54 ` Michael Haggerty
  2014-04-14 13:54 ` [PATCH v3 03/25] rollback_lock_file(): do not clear filename redundantly Michael Haggerty
                   ` (23 subsequent siblings)
  25 siblings, 0 replies; 32+ messages in thread
From: Michael Haggerty @ 2014-04-14 13:54 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: git, Johannes Sixt, Jeff King, Torsten Bögershausen,
	Eric Sunshine, Michael Haggerty

Document a couple more functions and the flags argument as used by
hold_lock_file_for_update() and hold_lock_file_for_append().

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
 Documentation/technical/api-lockfile.txt | 36 +++++++++++++++++++++++++++++---
 1 file changed, 33 insertions(+), 3 deletions(-)

diff --git a/Documentation/technical/api-lockfile.txt b/Documentation/technical/api-lockfile.txt
index dd89404..b53e300 100644
--- a/Documentation/technical/api-lockfile.txt
+++ b/Documentation/technical/api-lockfile.txt
@@ -28,9 +28,39 @@ hold_lock_file_for_update::
 	the final destination (e.g. `$GIT_DIR/index`) and a flag
 	`die_on_error`.  Attempt to create a lockfile for the
 	destination and return the file descriptor for writing
-	to the file.  If `die_on_error` flag is true, it dies if
-	a lock is already taken for the file; otherwise it
-	returns a negative integer to the caller on failure.
+	to the file.  The flags parameter is a combination of
++
+--
+LOCK_NODEREF::
+
+	Usually symbolic links in path are resolved in path and the
+	lockfile is created by adding ".lock" to the resolved path;
+	however, if `LOCK_NODEREF` is set, then the lockfile is
+	created by adding ".lock" to the path argument itself.
+
+LOCK_DIE_ON_ERROR::
+
+	If a lock is already taken for the file, `die()` with an error
+	message.  If this option is not specified, return a negative
+	integer to the caller on failure.
+--
+
+hold_lock_file_for_append::
+
+	Like `hold_lock_file_for_update()`, except that additionally
+	the existing contents of the file (if any) are copied to the
+	lockfile and its write pointer is positioned at the end of the
+	file before returning.
+
+unable_to_lock_error::
+
+	Emit an error describing that there was an error locking the
+	specified path.  The err parameter should be the errno of the
+	problem that caused the failure.
+
+unable_to_lock_die::
+
+	Like `unable_to_lock_error()`, but also `die()`.
 
 commit_lock_file::
 
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v3 03/25] rollback_lock_file(): do not clear filename redundantly
  2014-04-14 13:54 [PATCH v3 00/25] Lockfile correctness and refactoring Michael Haggerty
  2014-04-14 13:54 ` [PATCH v3 01/25] unable_to_lock_die(): rename function from unable_to_lock_index_die() Michael Haggerty
  2014-04-14 13:54 ` [PATCH v3 02/25] api-lockfile: expand the documentation Michael Haggerty
@ 2014-04-14 13:54 ` Michael Haggerty
  2014-04-14 13:54 ` [PATCH v3 04/25] rollback_lock_file(): set fd to -1 Michael Haggerty
                   ` (22 subsequent siblings)
  25 siblings, 0 replies; 32+ messages in thread
From: Michael Haggerty @ 2014-04-14 13:54 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: git, Johannes Sixt, Jeff King, Torsten Bögershausen,
	Eric Sunshine, Michael Haggerty

It is only necessary to clear the lock_file's filename field if it was
not already clear.

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
 lockfile.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lockfile.c b/lockfile.c
index d4bfb3f..7701267 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -277,6 +277,6 @@ void rollback_lock_file(struct lock_file *lk)
 		if (lk->fd >= 0)
 			close(lk->fd);
 		unlink_or_warn(lk->filename);
+		lk->filename[0] = 0;
 	}
-	lk->filename[0] = 0;
 }
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v3 04/25] rollback_lock_file(): set fd to -1
  2014-04-14 13:54 [PATCH v3 00/25] Lockfile correctness and refactoring Michael Haggerty
                   ` (2 preceding siblings ...)
  2014-04-14 13:54 ` [PATCH v3 03/25] rollback_lock_file(): do not clear filename redundantly Michael Haggerty
@ 2014-04-14 13:54 ` Michael Haggerty
  2014-04-14 13:54 ` [PATCH v3 05/25] lockfile: unlock file if lockfile permissions cannot be adjusted Michael Haggerty
                   ` (21 subsequent siblings)
  25 siblings, 0 replies; 32+ messages in thread
From: Michael Haggerty @ 2014-04-14 13:54 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: git, Johannes Sixt, Jeff King, Torsten Bögershausen,
	Eric Sunshine, Michael Haggerty

When rolling back the lockfile, call close_lock_file() so that the
lock_file's fd field gets set back to -1.  This keeps the lock_file
object in a valid state, which is important because these objects are
allowed to be reused.

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
 lockfile.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lockfile.c b/lockfile.c
index 7701267..1122542 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -275,7 +275,7 @@ void rollback_lock_file(struct lock_file *lk)
 {
 	if (lk->filename[0]) {
 		if (lk->fd >= 0)
-			close(lk->fd);
+			close_lock_file(lk);
 		unlink_or_warn(lk->filename);
 		lk->filename[0] = 0;
 	}
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v3 05/25] lockfile: unlock file if lockfile permissions cannot be adjusted
  2014-04-14 13:54 [PATCH v3 00/25] Lockfile correctness and refactoring Michael Haggerty
                   ` (3 preceding siblings ...)
  2014-04-14 13:54 ` [PATCH v3 04/25] rollback_lock_file(): set fd to -1 Michael Haggerty
@ 2014-04-14 13:54 ` Michael Haggerty
  2014-04-14 13:54 ` [PATCH v3 06/25] hold_lock_file_for_append(): release lock on errors Michael Haggerty
                   ` (20 subsequent siblings)
  25 siblings, 0 replies; 32+ messages in thread
From: Michael Haggerty @ 2014-04-14 13:54 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: git, Johannes Sixt, Jeff King, Torsten Bögershausen,
	Eric Sunshine, Michael Haggerty

If the call to adjust_shared_perm() fails, lock_file returns -1, which
to the caller looks like any other failure to lock the file.  So in
this case, roll back the lockfile before returning so that the lock
file is deleted immediately and the lockfile object is left in a
predictable state (namely, unlocked).  Previously, the lockfile was
retained until process cleanup in this situation.

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
 lockfile.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/lockfile.c b/lockfile.c
index 1122542..b101f77 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -148,9 +148,11 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)
 			lock_file_list = lk;
 			lk->on_list = 1;
 		}
-		if (adjust_shared_perm(lk->filename))
-			return error("cannot fix permission bits on %s",
-				     lk->filename);
+		if (adjust_shared_perm(lk->filename)) {
+			error("cannot fix permission bits on %s", lk->filename);
+			rollback_lock_file(lk);
+			return -1;
+		}
 	}
 	else
 		lk->filename[0] = 0;
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v3 06/25] hold_lock_file_for_append(): release lock on errors
  2014-04-14 13:54 [PATCH v3 00/25] Lockfile correctness and refactoring Michael Haggerty
                   ` (4 preceding siblings ...)
  2014-04-14 13:54 ` [PATCH v3 05/25] lockfile: unlock file if lockfile permissions cannot be adjusted Michael Haggerty
@ 2014-04-14 13:54 ` Michael Haggerty
  2014-04-14 13:54 ` [PATCH v3 07/25] lock_file(): always add lock_file object to lock_file_list Michael Haggerty
                   ` (19 subsequent siblings)
  25 siblings, 0 replies; 32+ messages in thread
From: Michael Haggerty @ 2014-04-14 13:54 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: git, Johannes Sixt, Jeff King, Torsten Bögershausen,
	Eric Sunshine, Michael Haggerty

If there is an error copying the old contents to the lockfile, roll
back the lockfile before exiting so that the lockfile is not held
until process cleanup.

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
 lockfile.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/lockfile.c b/lockfile.c
index b101f77..e78a35f 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -212,13 +212,13 @@ int hold_lock_file_for_append(struct lock_file *lk, const char *path, int flags)
 		if (errno != ENOENT) {
 			if (flags & LOCK_DIE_ON_ERROR)
 				die("cannot open '%s' for copying", path);
-			close(fd);
+			rollback_lock_file(lk);
 			return error("cannot open '%s' for copying", path);
 		}
 	} else if (copy_fd(orig_fd, fd)) {
 		if (flags & LOCK_DIE_ON_ERROR)
 			exit(128);
-		close(fd);
+		rollback_lock_file(lk);
 		return -1;
 	}
 	return fd;
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v3 07/25] lock_file(): always add lock_file object to lock_file_list
  2014-04-14 13:54 [PATCH v3 00/25] Lockfile correctness and refactoring Michael Haggerty
                   ` (5 preceding siblings ...)
  2014-04-14 13:54 ` [PATCH v3 06/25] hold_lock_file_for_append(): release lock on errors Michael Haggerty
@ 2014-04-14 13:54 ` Michael Haggerty
  2014-04-14 13:54 ` [PATCH v3 08/25] lockfile.c: document the various states of lock_file objects Michael Haggerty
                   ` (18 subsequent siblings)
  25 siblings, 0 replies; 32+ messages in thread
From: Michael Haggerty @ 2014-04-14 13:54 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: git, Johannes Sixt, Jeff King, Torsten Bögershausen,
	Eric Sunshine, Michael Haggerty

It used to be that if locking failed, lock_file() usually did not
register the lock_file object in lock_file_list but sometimes it did.
This confusion was compounded if lock_file() was called via
hold_lock_file_for_append(), which has its own failure modes.

The ambiguity didn't have any ill effects, because lock_file objects
cannot be removed from the lock_file_list anyway.  But it is
unnecessary to leave this behavior inconsistent.

So change lock_file() to *always* ensure that the lock_file object is
registered in lock_file_list regardless of whether an error occurs.

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
 lockfile.c | 25 ++++++++++++++++---------
 1 file changed, 16 insertions(+), 9 deletions(-)

diff --git a/lockfile.c b/lockfile.c
index e78a35f..120a6d6 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -130,6 +130,22 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)
 	 */
 	static const size_t max_path_len = sizeof(lk->filename) - 5;
 
+	if (!lock_file_list) {
+		/* One-time initialization */
+		sigchain_push_common(remove_lock_file_on_signal);
+		atexit(remove_lock_file);
+	}
+
+	if (!lk->on_list) {
+		/* Initialize *lk and add it to lock_file_list: */
+		lk->fd = -1;
+		lk->owner = 0;
+		lk->on_list = 1;
+		lk->filename[0] = 0;
+		lk->next = lock_file_list;
+		lock_file_list = lk;
+	}
+
 	if (strlen(path) >= max_path_len)
 		return -1;
 	strcpy(lk->filename, path);
@@ -138,16 +154,7 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)
 	strcat(lk->filename, ".lock");
 	lk->fd = open(lk->filename, O_RDWR | O_CREAT | O_EXCL, 0666);
 	if (0 <= lk->fd) {
-		if (!lock_file_list) {
-			sigchain_push_common(remove_lock_file_on_signal);
-			atexit(remove_lock_file);
-		}
 		lk->owner = getpid();
-		if (!lk->on_list) {
-			lk->next = lock_file_list;
-			lock_file_list = lk;
-			lk->on_list = 1;
-		}
 		if (adjust_shared_perm(lk->filename)) {
 			error("cannot fix permission bits on %s", lk->filename);
 			rollback_lock_file(lk);
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v3 08/25] lockfile.c: document the various states of lock_file objects
  2014-04-14 13:54 [PATCH v3 00/25] Lockfile correctness and refactoring Michael Haggerty
                   ` (6 preceding siblings ...)
  2014-04-14 13:54 ` [PATCH v3 07/25] lock_file(): always add lock_file object to lock_file_list Michael Haggerty
@ 2014-04-14 13:54 ` Michael Haggerty
  2014-04-14 13:54 ` [PATCH v3 09/25] cache.h: define constants LOCK_SUFFIX and LOCK_SUFFIX_LEN Michael Haggerty
                   ` (17 subsequent siblings)
  25 siblings, 0 replies; 32+ messages in thread
From: Michael Haggerty @ 2014-04-14 13:54 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: git, Johannes Sixt, Jeff King, Torsten Bögershausen,
	Eric Sunshine, Michael Haggerty

Document the valid states of lock_file objects, how they get into each
state, and how the state is encoded in the object's fields.

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
 lockfile.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 51 insertions(+)

diff --git a/lockfile.c b/lockfile.c
index 120a6d6..8f6652c 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -4,6 +4,57 @@
 #include "cache.h"
 #include "sigchain.h"
 
+/*
+ * File write-locks as used by Git.
+ *
+ * When a file at $FILENAME needs to be written, it is done as
+ * follows:
+ *
+ * 1. Obtain a lock on the file by creating a lockfile at path
+ *    $FILENAME.lock.  The file is opened for read/write using O_CREAT
+ *    and O_EXCL mode to ensure that it doesn't already exist.  Such a
+ *    lock file is respected by writers *but not by readers*.
+ *
+ *    Usually, if $FILENAME is a symlink, then it is resolved, and the
+ *    file ultimately pointed to is the one that is locked and later
+ *    replaced.  However, if LOCK_NODEREF is used, then $FILENAME
+ *    itself is locked and later replaced, even if it is a symlink.
+ *
+ * 2. Write the new file contents to the lockfile.
+ *
+ * 3. Move the lockfile to its final destination using rename(2).
+ *
+ * Instead of (3), the change can be rolled back by deleting lockfile.
+ *
+ * This module keeps track of all locked files in lock_file_list.
+ * When the first file is locked, it registers an atexit(3) handler;
+ * when the program exits, the handler rolls back any files that have
+ * been locked but were never committed or rolled back.
+ *
+ * A lock_file object can be in several states:
+ *
+ * - Uninitialized.  In this state the object's on_list field must be
+ *   zero but the rest of its contents need not be initialized.  As
+ *   soon as the object is used in any way, it is irrevocably
+ *   registered in the lock_file_list, and on_list is set.
+ *
+ * - Locked, lockfile open (after hold_lock_file_for_update() or
+ *   hold_lock_file_for_append()).  In this state, the lockfile
+ *   exists, filename holds the filename of the lockfile, fd holds a
+ *   file descriptor open for writing to the lockfile, and owner holds
+ *   the PID of the process that locked the file.
+ *
+ * - Locked, lockfile closed (after close_lock_file()).  Same as the
+ *   previous state, except that the lockfile is closed and fd is -1.
+ *
+ * - Unlocked (after commit_lock_file(), rollback_lock_file(), or a
+ *   failed attempt to lock).  In this state, filename[0] == '\0' and
+ *   fd is -1.  The object is left registered in the lock_file_list,
+ *   and on_list is set.
+ *
+ * See Documentation/api-lockfile.txt for more information.
+ */
+
 static struct lock_file *lock_file_list;
 static const char *alternate_index_output;
 
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v3 09/25] cache.h: define constants LOCK_SUFFIX and LOCK_SUFFIX_LEN
  2014-04-14 13:54 [PATCH v3 00/25] Lockfile correctness and refactoring Michael Haggerty
                   ` (7 preceding siblings ...)
  2014-04-14 13:54 ` [PATCH v3 08/25] lockfile.c: document the various states of lock_file objects Michael Haggerty
@ 2014-04-14 13:54 ` Michael Haggerty
  2014-04-14 13:54 ` [PATCH v3 10/25] delete_ref_loose(): don't muck around in the lock_file's filename Michael Haggerty
                   ` (16 subsequent siblings)
  25 siblings, 0 replies; 32+ messages in thread
From: Michael Haggerty @ 2014-04-14 13:54 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: git, Johannes Sixt, Jeff King, Torsten Bögershausen,
	Eric Sunshine, Michael Haggerty

There are a few places that use these values, so define constants for
them.

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
If I were confident that it would always be inlined, I would have
used

    #define LOCK_SUFFIX_LEN strlen(LOCK_SUFFIX)

Or maybe even have omitted this constant and write strlen(LOCK_SUFFIX)
inline where it is needed instead.  But I'm not so I didn't.

It's possible I missed other code that uses ".lock" or the magic
number 5, but these are the only ones I found.

 cache.h    |  4 ++++
 lockfile.c | 12 ++++++------
 refs.c     |  7 ++++---
 3 files changed, 14 insertions(+), 9 deletions(-)

diff --git a/cache.h b/cache.h
index 3a873a4..825cd0a 100644
--- a/cache.h
+++ b/cache.h
@@ -533,6 +533,10 @@ extern void fill_stat_cache_info(struct cache_entry *ce, struct stat *st);
 #define REFRESH_IN_PORCELAIN	0x0020	/* user friendly output, not "needs update" */
 extern int refresh_index(struct index_state *, unsigned int flags, const struct pathspec *pathspec, char *seen, const char *header_msg);
 
+/* String appended to a filename to derive the lockfile name: */
+#define LOCK_SUFFIX ".lock"
+#define LOCK_SUFFIX_LEN 5
+
 struct lock_file {
 	struct lock_file *next;
 	int fd;
diff --git a/lockfile.c b/lockfile.c
index 8f6652c..1ce0e87 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -172,14 +172,14 @@ static char *resolve_symlink(char *p, size_t s)
 	return p;
 }
 
-
 static int lock_file(struct lock_file *lk, const char *path, int flags)
 {
 	/*
-	 * subtract 5 from size to make sure there's room for adding
-	 * ".lock" for the lock file name
+	 * subtract LOCK_SUFFIX_LEN from size to make sure there's
+	 * room for adding ".lock" for the lock file name:
 	 */
-	static const size_t max_path_len = sizeof(lk->filename) - 5;
+	static const size_t max_path_len = sizeof(lk->filename) -
+					   LOCK_SUFFIX_LEN;
 
 	if (!lock_file_list) {
 		/* One-time initialization */
@@ -202,7 +202,7 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)
 	strcpy(lk->filename, path);
 	if (!(flags & LOCK_NODEREF))
 		resolve_symlink(lk->filename, max_path_len);
-	strcat(lk->filename, ".lock");
+	strcat(lk->filename, LOCK_SUFFIX);
 	lk->fd = open(lk->filename, O_RDWR | O_CREAT | O_EXCL, 0666);
 	if (0 <= lk->fd) {
 		lk->owner = getpid();
@@ -296,7 +296,7 @@ int commit_lock_file(struct lock_file *lk)
 	if (lk->fd >= 0 && close_lock_file(lk))
 		return -1;
 	strcpy(result_file, lk->filename);
-	i = strlen(result_file) - 5; /* .lock */
+	i = strlen(result_file) - LOCK_SUFFIX_LEN; /* .lock */
 	result_file[i] = 0;
 	if (rename(lk->filename, result_file))
 		return -1;
diff --git a/refs.c b/refs.c
index 67fe8b7..2805b31 100644
--- a/refs.c
+++ b/refs.c
@@ -63,7 +63,8 @@ static int check_refname_component(const char *refname, int flags)
 		if (refname[1] == '\0')
 			return -1; /* Component equals ".". */
 	}
-	if (cp - refname >= 5 && !memcmp(cp - 5, ".lock", 5))
+	if (cp - refname >= LOCK_SUFFIX_LEN &&
+	    !memcmp(cp - LOCK_SUFFIX_LEN, LOCK_SUFFIX, LOCK_SUFFIX_LEN))
 		return -1; /* Refname ends with ".lock". */
 	return cp - refname;
 }
@@ -2486,11 +2487,11 @@ static int delete_ref_loose(struct ref_lock *lock, int flag)
 {
 	if (!(flag & REF_ISPACKED) || flag & REF_ISSYMREF) {
 		/* loose */
-		int err, i = strlen(lock->lk->filename) - 5; /* .lock */
+		int err, i = strlen(lock->lk->filename) - LOCK_SUFFIX_LEN;
 
 		lock->lk->filename[i] = 0;
 		err = unlink_or_warn(lock->lk->filename);
-		lock->lk->filename[i] = '.';
+		lock->lk->filename[i] = LOCK_SUFFIX[0];
 		if (err && errno != ENOENT)
 			return 1;
 	}
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v3 10/25] delete_ref_loose(): don't muck around in the lock_file's filename
  2014-04-14 13:54 [PATCH v3 00/25] Lockfile correctness and refactoring Michael Haggerty
                   ` (8 preceding siblings ...)
  2014-04-14 13:54 ` [PATCH v3 09/25] cache.h: define constants LOCK_SUFFIX and LOCK_SUFFIX_LEN Michael Haggerty
@ 2014-04-14 13:54 ` Michael Haggerty
  2014-04-14 13:54 ` [PATCH v3 11/25] prepare_index(): declare return value to be (const char *) Michael Haggerty
                   ` (15 subsequent siblings)
  25 siblings, 0 replies; 32+ messages in thread
From: Michael Haggerty @ 2014-04-14 13:54 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: git, Johannes Sixt, Jeff King, Torsten Bögershausen,
	Eric Sunshine, Michael Haggerty

It's bad manners.  Especially since, if unlink_or_warn() failed, the
memory wasn't restored to its original contents.

So make our own copy to work with.

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
 refs.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/refs.c b/refs.c
index 2805b31..20c483b 100644
--- a/refs.c
+++ b/refs.c
@@ -2486,12 +2486,15 @@ static int repack_without_ref(const char *refname)
 static int delete_ref_loose(struct ref_lock *lock, int flag)
 {
 	if (!(flag & REF_ISPACKED) || flag & REF_ISSYMREF) {
-		/* loose */
-		int err, i = strlen(lock->lk->filename) - LOCK_SUFFIX_LEN;
-
-		lock->lk->filename[i] = 0;
-		err = unlink_or_warn(lock->lk->filename);
-		lock->lk->filename[i] = LOCK_SUFFIX[0];
+		/*
+		 * loose.  The loose file name is the same as the
+		 * lockfile name, minus ".lock":
+		 */
+		char *loose_filename = xmemdupz(
+				lock->lk->filename,
+				strlen(lock->lk->filename) - LOCK_SUFFIX_LEN);
+		int err = unlink_or_warn(loose_filename);
+		free(loose_filename);
 		if (err && errno != ENOENT)
 			return 1;
 	}
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v3 11/25] prepare_index(): declare return value to be (const char *)
  2014-04-14 13:54 [PATCH v3 00/25] Lockfile correctness and refactoring Michael Haggerty
                   ` (9 preceding siblings ...)
  2014-04-14 13:54 ` [PATCH v3 10/25] delete_ref_loose(): don't muck around in the lock_file's filename Michael Haggerty
@ 2014-04-14 13:54 ` Michael Haggerty
  2014-04-14 13:54 ` [PATCH v3 12/25] write_packed_entry_fn(): convert cb_data into a (const int *) Michael Haggerty
                   ` (14 subsequent siblings)
  25 siblings, 0 replies; 32+ messages in thread
From: Michael Haggerty @ 2014-04-14 13:54 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: git, Johannes Sixt, Jeff King, Torsten Bögershausen,
	Eric Sunshine, Michael Haggerty

Declare the return value to be const to make it clear that we aren't
giving callers permission to write over the string that it points at.
(The return value is the filename field of a struct lock_file, which
can be used by a signal handler at any time and therefore shouldn't be
tampered with.)

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
 builtin/commit.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/builtin/commit.c b/builtin/commit.c
index 9cfef6c..8ffb3ef 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -302,8 +302,8 @@ static void refresh_cache_or_die(int refresh_flags)
 		die_resolve_conflict("commit");
 }
 
-static char *prepare_index(int argc, const char **argv, const char *prefix,
-			   const struct commit *current_head, int is_status)
+static const char *prepare_index(int argc, const char **argv, const char *prefix,
+				 const struct commit *current_head, int is_status)
 {
 	int fd;
 	struct string_list partial;
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v3 12/25] write_packed_entry_fn(): convert cb_data into a (const int *)
  2014-04-14 13:54 [PATCH v3 00/25] Lockfile correctness and refactoring Michael Haggerty
                   ` (10 preceding siblings ...)
  2014-04-14 13:54 ` [PATCH v3 11/25] prepare_index(): declare return value to be (const char *) Michael Haggerty
@ 2014-04-14 13:54 ` Michael Haggerty
  2014-04-14 13:54 ` [PATCH v3 13/25] lock_file(): exit early if lockfile cannot be opened Michael Haggerty
                   ` (13 subsequent siblings)
  25 siblings, 0 replies; 32+ messages in thread
From: Michael Haggerty @ 2014-04-14 13:54 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: git, Johannes Sixt, Jeff King, Torsten Bögershausen,
	Eric Sunshine, Michael Haggerty

This makes it obvious that we have no plans to change the integer
pointed to, which is actually the fd field from a struct lock_file.

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
 refs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/refs.c b/refs.c
index 20c483b..cb2f825 100644
--- a/refs.c
+++ b/refs.c
@@ -2177,7 +2177,7 @@ static void write_packed_entry(int fd, char *refname, unsigned char *sha1,
  */
 static int write_packed_entry_fn(struct ref_entry *entry, void *cb_data)
 {
-	int *fd = cb_data;
+	const int *fd = cb_data;
 	enum peel_status peel_status = peel_entry(entry, 0);
 
 	if (peel_status != PEEL_PEELED && peel_status != PEEL_NON_TAG)
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v3 13/25] lock_file(): exit early if lockfile cannot be opened
  2014-04-14 13:54 [PATCH v3 00/25] Lockfile correctness and refactoring Michael Haggerty
                   ` (11 preceding siblings ...)
  2014-04-14 13:54 ` [PATCH v3 12/25] write_packed_entry_fn(): convert cb_data into a (const int *) Michael Haggerty
@ 2014-04-14 13:54 ` Michael Haggerty
  2014-04-14 13:54 ` [PATCH v3 14/25] remove_lock_file(): call rollback_lock_file() Michael Haggerty
                   ` (12 subsequent siblings)
  25 siblings, 0 replies; 32+ messages in thread
From: Michael Haggerty @ 2014-04-14 13:54 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: git, Johannes Sixt, Jeff King, Torsten Bögershausen,
	Eric Sunshine, Michael Haggerty

This is a bit easier to read than the old version, which nested part
of the non-error code in an "if" block.

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
 lockfile.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/lockfile.c b/lockfile.c
index 1ce0e87..ba791d5 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -204,16 +204,16 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)
 		resolve_symlink(lk->filename, max_path_len);
 	strcat(lk->filename, LOCK_SUFFIX);
 	lk->fd = open(lk->filename, O_RDWR | O_CREAT | O_EXCL, 0666);
-	if (0 <= lk->fd) {
-		lk->owner = getpid();
-		if (adjust_shared_perm(lk->filename)) {
-			error("cannot fix permission bits on %s", lk->filename);
-			rollback_lock_file(lk);
-			return -1;
-		}
-	}
-	else
+	if (lk->fd < 0) {
 		lk->filename[0] = 0;
+		return -1;
+	}
+	lk->owner = getpid();
+	if (adjust_shared_perm(lk->filename)) {
+		error("cannot fix permission bits on %s", lk->filename);
+		rollback_lock_file(lk);
+		return -1;
+	}
 	return lk->fd;
 }
 
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v3 14/25] remove_lock_file(): call rollback_lock_file()
  2014-04-14 13:54 [PATCH v3 00/25] Lockfile correctness and refactoring Michael Haggerty
                   ` (12 preceding siblings ...)
  2014-04-14 13:54 ` [PATCH v3 13/25] lock_file(): exit early if lockfile cannot be opened Michael Haggerty
@ 2014-04-14 13:54 ` Michael Haggerty
  2014-04-14 13:54 ` [PATCH v3 15/25] commit_lock_file(): inline temporary variable Michael Haggerty
                   ` (11 subsequent siblings)
  25 siblings, 0 replies; 32+ messages in thread
From: Michael Haggerty @ 2014-04-14 13:54 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: git, Johannes Sixt, Jeff King, Torsten Bögershausen,
	Eric Sunshine, Michael Haggerty

It does just what we need.

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
 lockfile.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)

diff --git a/lockfile.c b/lockfile.c
index ba791d5..477bf4b 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -63,12 +63,8 @@ static void remove_lock_file(void)
 	pid_t me = getpid();
 
 	while (lock_file_list) {
-		if (lock_file_list->owner == me &&
-		    lock_file_list->filename[0]) {
-			if (lock_file_list->fd >= 0)
-				close(lock_file_list->fd);
-			unlink_or_warn(lock_file_list->filename);
-		}
+		if (lock_file_list->owner == me)
+			rollback_lock_file(lock_file_list);
 		lock_file_list = lock_file_list->next;
 	}
 }
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v3 15/25] commit_lock_file(): inline temporary variable
  2014-04-14 13:54 [PATCH v3 00/25] Lockfile correctness and refactoring Michael Haggerty
                   ` (13 preceding siblings ...)
  2014-04-14 13:54 ` [PATCH v3 14/25] remove_lock_file(): call rollback_lock_file() Michael Haggerty
@ 2014-04-14 13:54 ` Michael Haggerty
  2014-04-14 13:54 ` [PATCH v3 16/25] commit_lock_file(): die() if called for unlocked lockfile object Michael Haggerty
                   ` (10 subsequent siblings)
  25 siblings, 0 replies; 32+ messages in thread
From: Michael Haggerty @ 2014-04-14 13:54 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: git, Johannes Sixt, Jeff King, Torsten Bögershausen,
	Eric Sunshine, Michael Haggerty

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
 lockfile.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/lockfile.c b/lockfile.c
index 477bf4b..664b0c3 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -288,12 +288,14 @@ int close_lock_file(struct lock_file *lk)
 int commit_lock_file(struct lock_file *lk)
 {
 	char result_file[PATH_MAX];
-	size_t i;
+
 	if (lk->fd >= 0 && close_lock_file(lk))
 		return -1;
+
 	strcpy(result_file, lk->filename);
-	i = strlen(result_file) - LOCK_SUFFIX_LEN; /* .lock */
-	result_file[i] = 0;
+	/* remove ".lock": */
+	result_file[strlen(result_file) - LOCK_SUFFIX_LEN] = 0;
+
 	if (rename(lk->filename, result_file))
 		return -1;
 	lk->filename[0] = 0;
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v3 16/25] commit_lock_file(): die() if called for unlocked lockfile object
  2014-04-14 13:54 [PATCH v3 00/25] Lockfile correctness and refactoring Michael Haggerty
                   ` (14 preceding siblings ...)
  2014-04-14 13:54 ` [PATCH v3 15/25] commit_lock_file(): inline temporary variable Michael Haggerty
@ 2014-04-14 13:54 ` Michael Haggerty
  2014-04-15  6:49   ` Johannes Sixt
  2014-04-14 13:54 ` [PATCH v3 17/25] lockfile: avoid transitory invalid states Michael Haggerty
                   ` (9 subsequent siblings)
  25 siblings, 1 reply; 32+ messages in thread
From: Michael Haggerty @ 2014-04-14 13:54 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: git, Johannes Sixt, Jeff King, Torsten Bögershausen,
	Eric Sunshine, Michael Haggerty

It was previously a bug to call commit_lock_file() with a lock_file
object that was not active (an illegal access would happen within the
function).  It was presumably never done, but this would be an easy
programming error to overlook.  So before continuing, do a consistency
check that the lock_file object really is locked.

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
 Documentation/technical/api-lockfile.txt | 4 +++-
 lockfile.c                               | 3 +++
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/Documentation/technical/api-lockfile.txt b/Documentation/technical/api-lockfile.txt
index b53e300..9a94ead 100644
--- a/Documentation/technical/api-lockfile.txt
+++ b/Documentation/technical/api-lockfile.txt
@@ -68,7 +68,9 @@ commit_lock_file::
 	with an earlier call to `hold_lock_file_for_update()`,
 	close the file descriptor and rename the lockfile to its
 	final destination.  Returns 0 upon success, a negative
-	value on failure to close(2) or rename(2).
+	value on failure to close(2) or rename(2).  It is a bug to
+	call `commit_lock_file()` for a `lock_file` object that is not
+	currently locked.
 
 rollback_lock_file::
 
diff --git a/lockfile.c b/lockfile.c
index 664b0c3..1453a7a 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -292,6 +292,9 @@ int commit_lock_file(struct lock_file *lk)
 	if (lk->fd >= 0 && close_lock_file(lk))
 		return -1;
 
+	if (!lk->filename[0])
+		die("BUG: attempt to commit unlocked object");
+
 	strcpy(result_file, lk->filename);
 	/* remove ".lock": */
 	result_file[strlen(result_file) - LOCK_SUFFIX_LEN] = 0;
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v3 17/25] lockfile: avoid transitory invalid states
  2014-04-14 13:54 [PATCH v3 00/25] Lockfile correctness and refactoring Michael Haggerty
                   ` (15 preceding siblings ...)
  2014-04-14 13:54 ` [PATCH v3 16/25] commit_lock_file(): die() if called for unlocked lockfile object Michael Haggerty
@ 2014-04-14 13:54 ` Michael Haggerty
  2014-04-14 13:54 ` [PATCH v3 18/25] struct lock_file: declare some fields volatile Michael Haggerty
                   ` (8 subsequent siblings)
  25 siblings, 0 replies; 32+ messages in thread
From: Michael Haggerty @ 2014-04-14 13:54 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: git, Johannes Sixt, Jeff King, Torsten Bögershausen,
	Eric Sunshine, Michael Haggerty

Because remove_lock_file() can be called any time by the signal
handler, it is important that any lock_file objects that are in the
lock_file_list are always in a valid state.  And since lock_file
objects are often reused (but are never removed from lock_file_list),
that means we have to be careful whenever mutating a lock_file object
to always keep it in a well-defined state.

This was formerly not the case, because part of the state was recorded
by whether lk->filename was the empty string or a valid filename.  It
is wrong to assume that this string can be updated atomically; for
example, even

    strcpy(lk->filename, value)

is unsafe.  But the old code was even more reckless; for example,

    strcpy(lk->filename, path);
    if (!(flags & LOCK_NODEREF))
            resolve_symlink(lk->filename, max_path_len);
    strcat(lk->filename, ".lock");

During the call to resolve_symlink(), lk->filename contained the name
of the file that was being locked, not the name of the lockfile.  If a
signal would have been raised during that interval, then the signal
handler would have deleted the valuable file!

We could probably continue to use the filename field to encode the
state by being careful to write characters 1..N-1 of the filename
first, and then overwrite the NUL at filename[0] with the first
character of the filename, but that would be awkward and error-prone.

So, instead of using the filename field to determine whether the
lock_file object is active, add a new field "lock_file::active" for
this purpose.  Be careful to set this field only when filename really
contains the name of a file that should be deleted on cleanup.

Helped-by: Johannes Sixt <j.sixt@viscovery.net>
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
 cache.h    |  1 +
 lockfile.c | 45 +++++++++++++++++++++++++++++++++------------
 2 files changed, 34 insertions(+), 12 deletions(-)

diff --git a/cache.h b/cache.h
index 825cd0a..b7af173 100644
--- a/cache.h
+++ b/cache.h
@@ -539,6 +539,7 @@ extern int refresh_index(struct index_state *, unsigned int flags, const struct
 
 struct lock_file {
 	struct lock_file *next;
+	volatile sig_atomic_t active;
 	int fd;
 	pid_t owner;
 	char on_list;
diff --git a/lockfile.c b/lockfile.c
index 1453a7a..50a0541 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -27,11 +27,14 @@
  * Instead of (3), the change can be rolled back by deleting lockfile.
  *
  * This module keeps track of all locked files in lock_file_list.
- * When the first file is locked, it registers an atexit(3) handler;
- * when the program exits, the handler rolls back any files that have
- * been locked but were never committed or rolled back.
+ * When the first file is locked, it registers an atexit(3) handler
+ * and a signal handler; when the program exits, the handler rolls
+ * back any files that have been locked but were never committed or
+ * rolled back.
  *
- * A lock_file object can be in several states:
+ * Because the signal handler can be called at any time, a lock_file
+ * object must always be in a well-defined state.  The possible states
+ * are as follows:
  *
  * - Uninitialized.  In this state the object's on_list field must be
  *   zero but the rest of its contents need not be initialized.  As
@@ -39,18 +42,29 @@
  *   registered in the lock_file_list, and on_list is set.
  *
  * - Locked, lockfile open (after hold_lock_file_for_update() or
- *   hold_lock_file_for_append()).  In this state, the lockfile
- *   exists, filename holds the filename of the lockfile, fd holds a
- *   file descriptor open for writing to the lockfile, and owner holds
- *   the PID of the process that locked the file.
+ *   hold_lock_file_for_append()).  In this state:
+ *   - the lockfile exists
+ *   - active is set
+ *   - filename holds the filename of the lockfile
+ *   - fd holds a file descriptor open for writing to the lockfile
+ *   - owner holds the PID of the process that locked the file
  *
- * - Locked, lockfile closed (after close_lock_file()).  Same as the
- *   previous state, except that the lockfile is closed and fd is -1.
+ * - Locked, lockfile closed (after close_lock_file() or an
+ *   unsuccessful commit_lock_file()).  Same as the previous state,
+ *   except that the lockfile is closed and fd is -1.
  *
  * - Unlocked (after commit_lock_file(), rollback_lock_file(), or a
  *   failed attempt to lock).  In this state, filename[0] == '\0' and
  *   fd is -1.  The object is left registered in the lock_file_list,
  *   and on_list is set.
+ * - Unlocked (after rollback_lock_file(), a successful
+ *   commit_lock_file(), or a failed attempt to lock).  In this state:
+ *   - active is unset
+ *   - filename[0] == '\0' (usually, though there are transitory states
+ *     in which this condition doesn't hold)
+ *   - fd is -1
+ *   - the object is left registered in the lock_file_list, and
+ *     on_list is set.
  *
  * See Documentation/api-lockfile.txt for more information.
  */
@@ -183,9 +197,12 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)
 		atexit(remove_lock_file);
 	}
 
+	assert(!lk->active);
+
 	if (!lk->on_list) {
 		/* Initialize *lk and add it to lock_file_list: */
 		lk->fd = -1;
+		lk->active = 0;
 		lk->owner = 0;
 		lk->on_list = 1;
 		lk->filename[0] = 0;
@@ -205,6 +222,7 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)
 		return -1;
 	}
 	lk->owner = getpid();
+	lk->active = 1;
 	if (adjust_shared_perm(lk->filename)) {
 		error("cannot fix permission bits on %s", lk->filename);
 		rollback_lock_file(lk);
@@ -292,7 +310,7 @@ int commit_lock_file(struct lock_file *lk)
 	if (lk->fd >= 0 && close_lock_file(lk))
 		return -1;
 
-	if (!lk->filename[0])
+	if (!lk->active)
 		die("BUG: attempt to commit unlocked object");
 
 	strcpy(result_file, lk->filename);
@@ -301,6 +319,7 @@ int commit_lock_file(struct lock_file *lk)
 
 	if (rename(lk->filename, result_file))
 		return -1;
+	lk->active = 0;
 	lk->filename[0] = 0;
 	return 0;
 }
@@ -325,6 +344,7 @@ int commit_locked_index(struct lock_file *lk)
 			return -1;
 		if (rename(lk->filename, alternate_index_output))
 			return -1;
+		lk->active = 0;
 		lk->filename[0] = 0;
 		return 0;
 	}
@@ -334,10 +354,11 @@ int commit_locked_index(struct lock_file *lk)
 
 void rollback_lock_file(struct lock_file *lk)
 {
-	if (lk->filename[0]) {
+	if (lk->active) {
 		if (lk->fd >= 0)
 			close_lock_file(lk);
 		unlink_or_warn(lk->filename);
+		lk->active = 0;
 		lk->filename[0] = 0;
 	}
 }
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v3 18/25] struct lock_file: declare some fields volatile
  2014-04-14 13:54 [PATCH v3 00/25] Lockfile correctness and refactoring Michael Haggerty
                   ` (16 preceding siblings ...)
  2014-04-14 13:54 ` [PATCH v3 17/25] lockfile: avoid transitory invalid states Michael Haggerty
@ 2014-04-14 13:54 ` Michael Haggerty
  2014-04-15  6:55   ` Johannes Sixt
  2014-04-14 13:54 ` [PATCH v3 19/25] try_merge_strategy(): remove redundant lock_file allocation Michael Haggerty
                   ` (7 subsequent siblings)
  25 siblings, 1 reply; 32+ messages in thread
From: Michael Haggerty @ 2014-04-14 13:54 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: git, Johannes Sixt, Jeff King, Torsten Bögershausen,
	Eric Sunshine, Michael Haggerty

The function remove_lock_file_on_signal() is used as a signal handler.
It is not realistic to make the signal handler conform strictly to the
C standard, which is very restrictive about what a signal handler is
allowed to do.  But let's increase the likelihood that it will work:

The lock_file_list global variable and several fields from struct
lock_file are used by the signal handler.  Declare those values
"volatile" to increase the chance that the signal handler will see a
valid object state.

Suggested-by: Johannes Sixt <j.sixt@viscovery.net>
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
 cache.h    | 6 +++---
 lockfile.c | 2 +-
 refs.c     | 5 +++--
 3 files changed, 7 insertions(+), 6 deletions(-)

diff --git a/cache.h b/cache.h
index b7af173..9019c7d 100644
--- a/cache.h
+++ b/cache.h
@@ -538,10 +538,10 @@ extern int refresh_index(struct index_state *, unsigned int flags, const struct
 #define LOCK_SUFFIX_LEN 5
 
 struct lock_file {
-	struct lock_file *next;
+	struct lock_file *volatile next;
 	volatile sig_atomic_t active;
-	int fd;
-	pid_t owner;
+	volatile int fd;
+	volatile pid_t owner;
 	char on_list;
 	char filename[PATH_MAX];
 };
diff --git a/lockfile.c b/lockfile.c
index 50a0541..fce53f1 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -69,7 +69,7 @@
  * See Documentation/api-lockfile.txt for more information.
  */
 
-static struct lock_file *lock_file_list;
+static struct lock_file *volatile lock_file_list;
 static const char *alternate_index_output;
 
 static void remove_lock_file(void)
diff --git a/refs.c b/refs.c
index cb2f825..db8057c 100644
--- a/refs.c
+++ b/refs.c
@@ -2213,15 +2213,16 @@ int commit_packed_refs(void)
 	struct packed_ref_cache *packed_ref_cache =
 		get_packed_ref_cache(&ref_cache);
 	int error = 0;
+	int fd;
 
 	if (!packed_ref_cache->lock)
 		die("internal error: packed-refs not locked");
 	write_or_die(packed_ref_cache->lock->fd,
 		     PACKED_REFS_HEADER, strlen(PACKED_REFS_HEADER));
 
+	fd = packed_ref_cache->lock->fd;
 	do_for_each_entry_in_dir(get_packed_ref_dir(packed_ref_cache),
-				 0, write_packed_entry_fn,
-				 &packed_ref_cache->lock->fd);
+				 0, write_packed_entry_fn, &fd);
 	if (commit_lock_file(packed_ref_cache->lock))
 		error = -1;
 	packed_ref_cache->lock = NULL;
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v3 19/25] try_merge_strategy(): remove redundant lock_file allocation
  2014-04-14 13:54 [PATCH v3 00/25] Lockfile correctness and refactoring Michael Haggerty
                   ` (17 preceding siblings ...)
  2014-04-14 13:54 ` [PATCH v3 18/25] struct lock_file: declare some fields volatile Michael Haggerty
@ 2014-04-14 13:54 ` Michael Haggerty
  2014-04-14 13:54 ` [PATCH v3 20/25] try_merge_strategy(): use a statically-allocated lock_file object Michael Haggerty
                   ` (6 subsequent siblings)
  25 siblings, 0 replies; 32+ messages in thread
From: Michael Haggerty @ 2014-04-14 13:54 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: git, Johannes Sixt, Jeff King, Torsten Bögershausen,
	Eric Sunshine, Michael Haggerty

By the time the "if" block is entered, the lock_file instance from the
main function block is no longer in use, so re-use that one instead of
allocating a second one.

Note that the "lock" variable in the "if" block shadowed the "lock"
variable at function scope, so the only change needed is to remove the
inner definition.

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
 builtin/merge.c | 1 -
 1 file changed, 1 deletion(-)

diff --git a/builtin/merge.c b/builtin/merge.c
index 66d8843..9b7e7cb 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -671,7 +671,6 @@ static int try_merge_strategy(const char *strategy, struct commit_list *common,
 	if (!strcmp(strategy, "recursive") || !strcmp(strategy, "subtree")) {
 		int clean, x;
 		struct commit *result;
-		struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
 		int index_fd;
 		struct commit_list *reversed = NULL;
 		struct merge_options o;
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v3 20/25] try_merge_strategy(): use a statically-allocated lock_file object
  2014-04-14 13:54 [PATCH v3 00/25] Lockfile correctness and refactoring Michael Haggerty
                   ` (18 preceding siblings ...)
  2014-04-14 13:54 ` [PATCH v3 19/25] try_merge_strategy(): remove redundant lock_file allocation Michael Haggerty
@ 2014-04-14 13:54 ` Michael Haggerty
  2014-04-14 13:54 ` [PATCH v3 21/25] commit_lock_file(): use a strbuf to manage temporary space Michael Haggerty
                   ` (5 subsequent siblings)
  25 siblings, 0 replies; 32+ messages in thread
From: Michael Haggerty @ 2014-04-14 13:54 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: git, Johannes Sixt, Jeff King, Torsten Bögershausen,
	Eric Sunshine, Michael Haggerty

Even the one lockfile object needn't be allocated each time the
function is called.  Instead, define one statically-allocated
lock_file object and reuse it for every call.

Suggested-by: Jeff King <peff@peff.net>
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
 builtin/merge.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/builtin/merge.c b/builtin/merge.c
index 9b7e7cb..1efa7b6 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -657,16 +657,16 @@ static int try_merge_strategy(const char *strategy, struct commit_list *common,
 			      struct commit_list *remoteheads,
 			      struct commit *head, const char *head_arg)
 {
+	static struct lock_file lock;
 	int index_fd;
-	struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
 
-	index_fd = hold_locked_index(lock, 1);
+	index_fd = hold_locked_index(&lock, 1);
 	refresh_cache(REFRESH_QUIET);
 	if (active_cache_changed &&
 			(write_cache(index_fd, active_cache, active_nr) ||
-			 commit_locked_index(lock)))
+			 commit_locked_index(&lock)))
 		return error(_("Unable to write index."));
-	rollback_lock_file(lock);
+	rollback_lock_file(&lock);
 
 	if (!strcmp(strategy, "recursive") || !strcmp(strategy, "subtree")) {
 		int clean, x;
@@ -699,14 +699,14 @@ static int try_merge_strategy(const char *strategy, struct commit_list *common,
 		for (j = common; j; j = j->next)
 			commit_list_insert(j->item, &reversed);
 
-		index_fd = hold_locked_index(lock, 1);
+		index_fd = hold_locked_index(&lock, 1);
 		clean = merge_recursive(&o, head,
 				remoteheads->item, reversed, &result);
 		if (active_cache_changed &&
 				(write_cache(index_fd, active_cache, active_nr) ||
-				 commit_locked_index(lock)))
+				 commit_locked_index(&lock)))
 			die (_("unable to write %s"), get_index_file());
-		rollback_lock_file(lock);
+		rollback_lock_file(&lock);
 		return clean ? 0 : 1;
 	} else {
 		return try_merge_command(strategy, xopts_nr, xopts,
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v3 21/25] commit_lock_file(): use a strbuf to manage temporary space
  2014-04-14 13:54 [PATCH v3 00/25] Lockfile correctness and refactoring Michael Haggerty
                   ` (19 preceding siblings ...)
  2014-04-14 13:54 ` [PATCH v3 20/25] try_merge_strategy(): use a statically-allocated lock_file object Michael Haggerty
@ 2014-04-14 13:54 ` Michael Haggerty
  2014-04-14 13:54 ` [PATCH v3 22/25] Change lock_file::filename into a strbuf Michael Haggerty
                   ` (4 subsequent siblings)
  25 siblings, 0 replies; 32+ messages in thread
From: Michael Haggerty @ 2014-04-14 13:54 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: git, Johannes Sixt, Jeff King, Torsten Bögershausen,
	Eric Sunshine, Michael Haggerty

Avoid relying on the filename length restrictions that are currently
checked by lock_file().

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
 lockfile.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/lockfile.c b/lockfile.c
index fce53f1..0aa2998 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -305,7 +305,8 @@ int close_lock_file(struct lock_file *lk)
 
 int commit_lock_file(struct lock_file *lk)
 {
-	char result_file[PATH_MAX];
+	static struct strbuf result_file = STRBUF_INIT;
+	int err;
 
 	if (lk->fd >= 0 && close_lock_file(lk))
 		return -1;
@@ -313,11 +314,12 @@ int commit_lock_file(struct lock_file *lk)
 	if (!lk->active)
 		die("BUG: attempt to commit unlocked object");
 
-	strcpy(result_file, lk->filename);
 	/* remove ".lock": */
-	result_file[strlen(result_file) - LOCK_SUFFIX_LEN] = 0;
-
-	if (rename(lk->filename, result_file))
+	strbuf_add(&result_file, lk->filename,
+		   strlen(lk->filename) - LOCK_SUFFIX_LEN);
+	err = rename(lk->filename, result_file.buf);
+	strbuf_reset(&result_file);
+	if (err)
 		return -1;
 	lk->active = 0;
 	lk->filename[0] = 0;
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v3 22/25] Change lock_file::filename into a strbuf
  2014-04-14 13:54 [PATCH v3 00/25] Lockfile correctness and refactoring Michael Haggerty
                   ` (20 preceding siblings ...)
  2014-04-14 13:54 ` [PATCH v3 21/25] commit_lock_file(): use a strbuf to manage temporary space Michael Haggerty
@ 2014-04-14 13:54 ` Michael Haggerty
  2014-04-14 13:54 ` [PATCH v3 23/25] resolve_symlink(): use a strbuf for internal scratch space Michael Haggerty
                   ` (3 subsequent siblings)
  25 siblings, 0 replies; 32+ messages in thread
From: Michael Haggerty @ 2014-04-14 13:54 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: git, Johannes Sixt, Jeff King, Torsten Bögershausen,
	Eric Sunshine, Michael Haggerty

For now, we still make sure to allocate at least PATH_MAX characters
for the strbuf because resolve_symlink() doesn't know how to expand
the space for its return value.  (That will be fixed in a moment.)

Another alternative would be to just use a strbuf as scratch space in
lock_file() but then store a pointer to the naked string in struct
lock_file.  But lock_file objects are often reused.  By reusing the
same strbuf, we can avoid having to reallocate the string most times
when a lock_file object is reused.

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
 builtin/commit.c | 12 ++++++------
 builtin/reflog.c |  2 +-
 cache.h          |  2 +-
 config.c         |  6 +++---
 lockfile.c       | 45 +++++++++++++++++++--------------------------
 refs.c           |  6 +++---
 shallow.c        |  6 +++---
 7 files changed, 36 insertions(+), 43 deletions(-)

diff --git a/builtin/commit.c b/builtin/commit.c
index 8ffb3ef..38c137f 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -330,7 +330,7 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix
 			die(_("unable to create temporary index"));
 
 		old_index_env = getenv(INDEX_ENVIRONMENT);
-		setenv(INDEX_ENVIRONMENT, index_lock.filename, 1);
+		setenv(INDEX_ENVIRONMENT, index_lock.filename.buf, 1);
 
 		if (interactive_add(argc, argv, prefix, patch_interactive) != 0)
 			die(_("interactive add failed"));
@@ -341,10 +341,10 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix
 			unsetenv(INDEX_ENVIRONMENT);
 
 		discard_cache();
-		read_cache_from(index_lock.filename);
+		read_cache_from(index_lock.filename.buf);
 
 		commit_style = COMMIT_NORMAL;
-		return index_lock.filename;
+		return index_lock.filename.buf;
 	}
 
 	/*
@@ -368,7 +368,7 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix
 		    close_lock_file(&index_lock))
 			die(_("unable to write new_index file"));
 		commit_style = COMMIT_NORMAL;
-		return index_lock.filename;
+		return index_lock.filename.buf;
 	}
 
 	/*
@@ -453,9 +453,9 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix
 		die(_("unable to write temporary index file"));
 
 	discard_cache();
-	read_cache_from(false_lock.filename);
+	read_cache_from(false_lock.filename.buf);
 
-	return false_lock.filename;
+	return false_lock.filename.buf;
 }
 
 static int run_status(FILE *fp, const char *index_file, const char *prefix, int nowarn,
diff --git a/builtin/reflog.c b/builtin/reflog.c
index c12a9784..d7df34e 100644
--- a/builtin/reflog.c
+++ b/builtin/reflog.c
@@ -431,7 +431,7 @@ static int expire_reflog(const char *ref, const unsigned char *sha1, int unused,
 			 write_str_in_full(lock->lock_fd, "\n") != 1 ||
 			 close_ref(lock) < 0)) {
 			status |= error("Couldn't write %s",
-				lock->lk->filename);
+					lock->lk->filename.buf);
 			unlink(newlog_path);
 		} else if (rename(newlog_path, log_file)) {
 			status |= error("cannot rename %s to %s",
diff --git a/cache.h b/cache.h
index 9019c7d..319251b 100644
--- a/cache.h
+++ b/cache.h
@@ -543,7 +543,7 @@ struct lock_file {
 	volatile int fd;
 	volatile pid_t owner;
 	char on_list;
-	char filename[PATH_MAX];
+	struct strbuf filename;
 };
 #define LOCK_DIE_ON_ERROR 1
 #define LOCK_NODEREF 2
diff --git a/config.c b/config.c
index 6821cef..78c3dad 100644
--- a/config.c
+++ b/config.c
@@ -1706,7 +1706,7 @@ out_free:
 	return ret;
 
 write_err_out:
-	ret = write_error(lock->filename);
+	ret = write_error(lock->filename.buf);
 	goto out_free;
 
 }
@@ -1821,7 +1821,7 @@ int git_config_rename_section_in_file(const char *config_filename,
 				}
 				store.baselen = strlen(new_name);
 				if (!store_write_section(out_fd, new_name)) {
-					ret = write_error(lock->filename);
+					ret = write_error(lock->filename.buf);
 					goto out;
 				}
 				/*
@@ -1847,7 +1847,7 @@ int git_config_rename_section_in_file(const char *config_filename,
 			continue;
 		length = strlen(output);
 		if (write_in_full(out_fd, output, length) != length) {
-			ret = write_error(lock->filename);
+			ret = write_error(lock->filename.buf);
 			goto out;
 		}
 	}
diff --git a/lockfile.c b/lockfile.c
index 0aa2998..f552e33 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -184,13 +184,6 @@ static char *resolve_symlink(char *p, size_t s)
 
 static int lock_file(struct lock_file *lk, const char *path, int flags)
 {
-	/*
-	 * subtract LOCK_SUFFIX_LEN from size to make sure there's
-	 * room for adding ".lock" for the lock file name:
-	 */
-	static const size_t max_path_len = sizeof(lk->filename) -
-					   LOCK_SUFFIX_LEN;
-
 	if (!lock_file_list) {
 		/* One-time initialization */
 		sigchain_push_common(remove_lock_file_on_signal);
@@ -205,26 +198,26 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)
 		lk->active = 0;
 		lk->owner = 0;
 		lk->on_list = 1;
-		lk->filename[0] = 0;
+		strbuf_init(&lk->filename, PATH_MAX);
 		lk->next = lock_file_list;
 		lock_file_list = lk;
 	}
 
-	if (strlen(path) >= max_path_len)
-		return -1;
-	strcpy(lk->filename, path);
-	if (!(flags & LOCK_NODEREF))
-		resolve_symlink(lk->filename, max_path_len);
-	strcat(lk->filename, LOCK_SUFFIX);
-	lk->fd = open(lk->filename, O_RDWR | O_CREAT | O_EXCL, 0666);
+	strbuf_addstr(&lk->filename, path);
+	if (!(flags & LOCK_NODEREF)) {
+		resolve_symlink(lk->filename.buf, lk->filename.alloc);
+		strbuf_setlen(&lk->filename, strlen(lk->filename.buf));
+	}
+	strbuf_addstr(&lk->filename, LOCK_SUFFIX);
+	lk->fd = open(lk->filename.buf, O_RDWR | O_CREAT | O_EXCL, 0666);
 	if (lk->fd < 0) {
-		lk->filename[0] = 0;
+		strbuf_reset(&lk->filename);
 		return -1;
 	}
 	lk->owner = getpid();
 	lk->active = 1;
-	if (adjust_shared_perm(lk->filename)) {
-		error("cannot fix permission bits on %s", lk->filename);
+	if (adjust_shared_perm(lk->filename.buf)) {
+		error("cannot fix permission bits on %s", lk->filename.buf);
 		rollback_lock_file(lk);
 		return -1;
 	}
@@ -315,14 +308,14 @@ int commit_lock_file(struct lock_file *lk)
 		die("BUG: attempt to commit unlocked object");
 
 	/* remove ".lock": */
-	strbuf_add(&result_file, lk->filename,
-		   strlen(lk->filename) - LOCK_SUFFIX_LEN);
-	err = rename(lk->filename, result_file.buf);
+	strbuf_add(&result_file, lk->filename.buf,
+		   lk->filename.len - LOCK_SUFFIX_LEN);
+	err = rename(lk->filename.buf, result_file.buf);
 	strbuf_reset(&result_file);
 	if (err)
 		return -1;
 	lk->active = 0;
-	lk->filename[0] = 0;
+	strbuf_reset(&lk->filename);
 	return 0;
 }
 
@@ -344,10 +337,10 @@ int commit_locked_index(struct lock_file *lk)
 	if (alternate_index_output) {
 		if (lk->fd >= 0 && close_lock_file(lk))
 			return -1;
-		if (rename(lk->filename, alternate_index_output))
+		if (rename(lk->filename.buf, alternate_index_output))
 			return -1;
 		lk->active = 0;
-		lk->filename[0] = 0;
+		strbuf_reset(&lk->filename);
 		return 0;
 	}
 	else
@@ -359,8 +352,8 @@ void rollback_lock_file(struct lock_file *lk)
 	if (lk->active) {
 		if (lk->fd >= 0)
 			close_lock_file(lk);
-		unlink_or_warn(lk->filename);
+		unlink_or_warn(lk->filename.buf);
 		lk->active = 0;
-		lk->filename[0] = 0;
+		strbuf_reset(&lk->filename);
 	}
 }
diff --git a/refs.c b/refs.c
index db8057c..4bad084 100644
--- a/refs.c
+++ b/refs.c
@@ -2492,8 +2492,8 @@ static int delete_ref_loose(struct ref_lock *lock, int flag)
 		 * lockfile name, minus ".lock":
 		 */
 		char *loose_filename = xmemdupz(
-				lock->lk->filename,
-				strlen(lock->lk->filename) - LOCK_SUFFIX_LEN);
+				lock->lk->filename.buf,
+				lock->lk->filename.len - LOCK_SUFFIX_LEN);
 		int err = unlink_or_warn(loose_filename);
 		free(loose_filename);
 		if (err && errno != ENOENT)
@@ -2831,7 +2831,7 @@ int write_ref_sha1(struct ref_lock *lock,
 	if (write_in_full(lock->lock_fd, sha1_to_hex(sha1), 40) != 40 ||
 	    write_in_full(lock->lock_fd, &term, 1) != 1
 		|| close_ref(lock) < 0) {
-		error("Couldn't write %s", lock->lk->filename);
+		error("Couldn't write %s", lock->lk->filename.buf);
 		unlock_ref(lock);
 		return -1;
 	}
diff --git a/shallow.c b/shallow.c
index 0b267b6..cb29a66 100644
--- a/shallow.c
+++ b/shallow.c
@@ -269,8 +269,8 @@ void setup_alternate_shallow(struct lock_file *shallow_lock,
 	if (write_shallow_commits(&sb, 0, extra)) {
 		if (write_in_full(fd, sb.buf, sb.len) != sb.len)
 			die_errno("failed to write to %s",
-				  shallow_lock->filename);
-		*alternate_shallow_file = shallow_lock->filename;
+				  shallow_lock->filename.buf);
+		*alternate_shallow_file = shallow_lock->filename.buf;
 	} else
 		/*
 		 * is_repository_shallow() sees empty string as "no
@@ -316,7 +316,7 @@ void prune_shallow(int show_only)
 	if (write_shallow_commits_1(&sb, 0, NULL, SEEN_ONLY)) {
 		if (write_in_full(fd, sb.buf, sb.len) != sb.len)
 			die_errno("failed to write to %s",
-				  shallow_lock.filename);
+				  shallow_lock.filename.buf);
 		commit_lock_file(&shallow_lock);
 	} else {
 		unlink(git_path("shallow"));
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v3 23/25] resolve_symlink(): use a strbuf for internal scratch space
  2014-04-14 13:54 [PATCH v3 00/25] Lockfile correctness and refactoring Michael Haggerty
                   ` (21 preceding siblings ...)
  2014-04-14 13:54 ` [PATCH v3 22/25] Change lock_file::filename into a strbuf Michael Haggerty
@ 2014-04-14 13:54 ` Michael Haggerty
  2014-04-14 13:54 ` [PATCH v3 24/25] resolve_symlink(): take a strbuf parameter Michael Haggerty
                   ` (2 subsequent siblings)
  25 siblings, 0 replies; 32+ messages in thread
From: Michael Haggerty @ 2014-04-14 13:54 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: git, Johannes Sixt, Jeff King, Torsten Bögershausen,
	Eric Sunshine, Michael Haggerty

Aside from shortening and simplifying the code, this removes another
place where the path name length is arbitrarily limited.

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
 lockfile.c | 33 ++++++++++++---------------------
 1 file changed, 12 insertions(+), 21 deletions(-)

diff --git a/lockfile.c b/lockfile.c
index f552e33..59ed758 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -141,44 +141,35 @@ static char *last_path_elm(char *p)
 static char *resolve_symlink(char *p, size_t s)
 {
 	int depth = MAXDEPTH;
+	static struct strbuf link = STRBUF_INIT;
 
 	while (depth--) {
-		char link[PATH_MAX];
-		int link_len = readlink(p, link, sizeof(link));
-		if (link_len < 0) {
-			/* not a symlink anymore */
-			return p;
-		}
-		else if (link_len < sizeof(link))
-			/* readlink() never null-terminates */
-			link[link_len] = '\0';
-		else {
-			warning("%s: symlink too long", p);
-			return p;
-		}
+		if (strbuf_readlink(&link, p, strlen(p)) < 0)
+			break;
 
-		if (is_absolute_path(link)) {
+		if (is_absolute_path(link.buf)) {
 			/* absolute path simply replaces p */
-			if (link_len < s)
-				strcpy(p, link);
+			if (link.len < s)
+				strcpy(p, link.buf);
 			else {
 				warning("%s: symlink too long", p);
-				return p;
+				break;
 			}
 		} else {
 			/*
-			 * link is a relative path, so I must replace the
+			 * link is a relative path, so replace the
 			 * last element of p with it.
 			 */
 			char *r = (char *)last_path_elm(p);
-			if (r - p + link_len < s)
-				strcpy(r, link);
+			if (r - p + link.len < s)
+				strcpy(r, link.buf);
 			else {
 				warning("%s: symlink too long", p);
-				return p;
+				break;
 			}
 		}
 	}
+	strbuf_reset(&link);
 	return p;
 }
 
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v3 24/25] resolve_symlink(): take a strbuf parameter
  2014-04-14 13:54 [PATCH v3 00/25] Lockfile correctness and refactoring Michael Haggerty
                   ` (22 preceding siblings ...)
  2014-04-14 13:54 ` [PATCH v3 23/25] resolve_symlink(): use a strbuf for internal scratch space Michael Haggerty
@ 2014-04-14 13:54 ` Michael Haggerty
  2014-04-14 13:54 ` [PATCH v3 25/25] trim_last_path_elm(): replace last_path_elm() Michael Haggerty
  2014-04-15 18:40 ` [PATCH v3 00/25] Lockfile correctness and refactoring Torsten Bögershausen
  25 siblings, 0 replies; 32+ messages in thread
From: Michael Haggerty @ 2014-04-14 13:54 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: git, Johannes Sixt, Jeff King, Torsten Bögershausen,
	Eric Sunshine, Michael Haggerty

Change resolve_symlink() to take a strbuf rather than a string as
parameter.  This simplifies the code and removes an arbitrary pathname
length restriction.  It also means that lock_file's filename field no
longer needs to be initialized to a large size.

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
 lockfile.c | 53 +++++++++++++++++++----------------------------------
 1 file changed, 19 insertions(+), 34 deletions(-)

diff --git a/lockfile.c b/lockfile.c
index 59ed758..65f56ef 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -124,53 +124,40 @@ static char *last_path_elm(char *p)
 #define MAXDEPTH 5
 
 /*
- * p = path that may be a symlink
- * s = full size of p
+ * path contains a path that might be a symlink.
  *
- * If p is a symlink, attempt to overwrite p with a path to the real
- * file or directory (which may or may not exist), following a chain of
- * symlinks if necessary.  Otherwise, leave p unmodified.
+ * If path is a symlink, attempt to overwrite it with a path to the
+ * real file or directory (which may or may not exist), following a
+ * chain of symlinks if necessary.  Otherwise, leave path unmodified.
  *
- * This is a best-effort routine.  If an error occurs, p will either be
- * left unmodified or will name a different symlink in a symlink chain
- * that started with p's initial contents.
- *
- * Always returns p.
+ * This is a best-effort routine.  If an error occurs, path will
+ * either be left unmodified or will name a different symlink in a
+ * symlink chain that started with the original path.
  */
-
-static char *resolve_symlink(char *p, size_t s)
+static void resolve_symlink(struct strbuf *path)
 {
 	int depth = MAXDEPTH;
 	static struct strbuf link = STRBUF_INIT;
 
 	while (depth--) {
-		if (strbuf_readlink(&link, p, strlen(p)) < 0)
+		if (strbuf_readlink(&link, path->buf, path->len) < 0)
 			break;
 
-		if (is_absolute_path(link.buf)) {
+		if (is_absolute_path(link.buf))
 			/* absolute path simply replaces p */
-			if (link.len < s)
-				strcpy(p, link.buf);
-			else {
-				warning("%s: symlink too long", p);
-				break;
-			}
-		} else {
+			strbuf_reset(path);
+		else {
 			/*
 			 * link is a relative path, so replace the
 			 * last element of p with it.
 			 */
-			char *r = (char *)last_path_elm(p);
-			if (r - p + link.len < s)
-				strcpy(r, link.buf);
-			else {
-				warning("%s: symlink too long", p);
-				break;
-			}
+			char *r = last_path_elm(path->buf);
+			strbuf_setlen(path, r - path->buf);
 		}
+
+		strbuf_addbuf(path, &link);
 	}
 	strbuf_reset(&link);
-	return p;
 }
 
 static int lock_file(struct lock_file *lk, const char *path, int flags)
@@ -189,16 +176,14 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)
 		lk->active = 0;
 		lk->owner = 0;
 		lk->on_list = 1;
-		strbuf_init(&lk->filename, PATH_MAX);
+		strbuf_init(&lk->filename, 0);
 		lk->next = lock_file_list;
 		lock_file_list = lk;
 	}
 
 	strbuf_addstr(&lk->filename, path);
-	if (!(flags & LOCK_NODEREF)) {
-		resolve_symlink(lk->filename.buf, lk->filename.alloc);
-		strbuf_setlen(&lk->filename, strlen(lk->filename.buf));
-	}
+	if (!(flags & LOCK_NODEREF))
+		resolve_symlink(&lk->filename);
 	strbuf_addstr(&lk->filename, LOCK_SUFFIX);
 	lk->fd = open(lk->filename.buf, O_RDWR | O_CREAT | O_EXCL, 0666);
 	if (lk->fd < 0) {
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [PATCH v3 25/25] trim_last_path_elm(): replace last_path_elm()
  2014-04-14 13:54 [PATCH v3 00/25] Lockfile correctness and refactoring Michael Haggerty
                   ` (23 preceding siblings ...)
  2014-04-14 13:54 ` [PATCH v3 24/25] resolve_symlink(): take a strbuf parameter Michael Haggerty
@ 2014-04-14 13:54 ` Michael Haggerty
  2014-04-15 18:40 ` [PATCH v3 00/25] Lockfile correctness and refactoring Torsten Bögershausen
  25 siblings, 0 replies; 32+ messages in thread
From: Michael Haggerty @ 2014-04-14 13:54 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: git, Johannes Sixt, Jeff King, Torsten Bögershausen,
	Eric Sunshine, Michael Haggerty

Rewrite last_path_elm() to take a strbuf parameter and to trim off the
last path name element in place rather than returning a pointer to the
beginning of the last path name element.  This simplifies the function
a bit and makes it integrate better with its caller, which is also
strbuf-based.  Rename the function accordingly.

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
 lockfile.c | 38 ++++++++++++++++----------------------
 1 file changed, 16 insertions(+), 22 deletions(-)

diff --git a/lockfile.c b/lockfile.c
index 65f56ef..2515577 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -91,32 +91,28 @@ static void remove_lock_file_on_signal(int signo)
 }
 
 /*
- * p = absolute or relative path name
+ * path = absolute or relative path name
  *
- * Return a pointer into p showing the beginning of the last path name
- * element.  If p is empty or the root directory ("/"), just return p.
+ * Remove the last path name element from path (leaving the preceding
+ * "/", if any).  If path is empty or the root directory ("/"), set
+ * path to the empty string.
  */
-static char *last_path_elm(char *p)
+static void trim_last_path_elm(struct strbuf *path)
 {
-	/* r starts pointing to null at the end of the string */
-	char *r = strchr(p, '\0');
-
-	if (r == p)
-		return p; /* just return empty string */
-
-	r--; /* back up to last non-null character */
+	int i = path->len;
 
 	/* back up past trailing slashes, if any */
-	while (r > p && *r == '/')
-		r--;
+	while (i && path->buf[i - 1] == '/')
+		i--;
 
 	/*
-	 * then go backwards until I hit a slash, or the beginning of
-	 * the string
+	 * then go backwards until a slash, or the beginning of the
+	 * string
 	 */
-	while (r > p && *(r-1) != '/')
-		r--;
-	return r;
+	while (i && path->buf[i - 1] != '/')
+		i--;
+
+	strbuf_setlen(path, i);
 }
 
 
@@ -146,14 +142,12 @@ static void resolve_symlink(struct strbuf *path)
 		if (is_absolute_path(link.buf))
 			/* absolute path simply replaces p */
 			strbuf_reset(path);
-		else {
+		else
 			/*
 			 * link is a relative path, so replace the
 			 * last element of p with it.
 			 */
-			char *r = last_path_elm(path->buf);
-			strbuf_setlen(path, r - path->buf);
-		}
+			trim_last_path_elm(path);
 
 		strbuf_addbuf(path, &link);
 	}
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 32+ messages in thread

* Re: [PATCH v3 16/25] commit_lock_file(): die() if called for unlocked lockfile object
  2014-04-14 13:54 ` [PATCH v3 16/25] commit_lock_file(): die() if called for unlocked lockfile object Michael Haggerty
@ 2014-04-15  6:49   ` Johannes Sixt
  2014-04-16 15:17     ` Michael Haggerty
  0 siblings, 1 reply; 32+ messages in thread
From: Johannes Sixt @ 2014-04-15  6:49 UTC (permalink / raw)
  To: Michael Haggerty, Junio C Hamano
  Cc: git, Jeff King, Torsten Bögershausen, Eric Sunshine

Am 4/14/2014 15:54, schrieb Michael Haggerty:
> diff --git a/lockfile.c b/lockfile.c
> index 664b0c3..1453a7a 100644
> --- a/lockfile.c
> +++ b/lockfile.c
> @@ -292,6 +292,9 @@ int commit_lock_file(struct lock_file *lk)
>  	if (lk->fd >= 0 && close_lock_file(lk))
>  		return -1;
>  
> +	if (!lk->filename[0])
> +		die("BUG: attempt to commit unlocked object");
> +

Shouldn't this be the first thing to check after entering the function?

>  	strcpy(result_file, lk->filename);
>  	/* remove ".lock": */
>  	result_file[strlen(result_file) - LOCK_SUFFIX_LEN] = 0;
> 

^ permalink raw reply	[flat|nested] 32+ messages in thread

* Re: [PATCH v3 18/25] struct lock_file: declare some fields volatile
  2014-04-14 13:54 ` [PATCH v3 18/25] struct lock_file: declare some fields volatile Michael Haggerty
@ 2014-04-15  6:55   ` Johannes Sixt
  2014-04-16 15:36     ` Michael Haggerty
  0 siblings, 1 reply; 32+ messages in thread
From: Johannes Sixt @ 2014-04-15  6:55 UTC (permalink / raw)
  To: Michael Haggerty, Junio C Hamano
  Cc: git, Jeff King, Torsten Bögershausen, Eric Sunshine

Am 4/14/2014 15:54, schrieb Michael Haggerty:
> The function remove_lock_file_on_signal() is used as a signal handler.
> It is not realistic to make the signal handler conform strictly to the
> C standard, which is very restrictive about what a signal handler is
> allowed to do.  But let's increase the likelihood that it will work:
> 
> The lock_file_list global variable and several fields from struct
> lock_file are used by the signal handler.  Declare those values
> "volatile" to increase the chance that the signal handler will see a
> valid object state.

Yes, it's important that the signal handler sees a valid object state, and
"volatile" can help here. But I think the reason why it helps is not
obvious, and it should be mentioned in the commit message:

It is not so much that "volatile" forces the compiler to lay down each
access of the variable coded in C in the assembly code, but more
importantly, that "volatile" disallows any re-ordering of these accesses.
Then:

- 'lock->active = 1' must be the last assignment during setup

- 'lock->active = 0' must be the first assignment during tear-down.

- Ideally, all members of struct lock_file should be "volatile".

The last point is important because the compiler is allowed to re-order
accesses to non-"volatile" variables across "volatile" accesses. I say
"ideally" because if filename were defined "volatile filename[PATH_MAX]",
strcpy() could not be used anymore. OTOH, it is unlikely that a compiler
re-orders a strcpy() call across other expressions, and we can get away
without "volatile" in the "filename" case in practice.

> Suggested-by: Johannes Sixt <j.sixt@viscovery.net>

Not a big deal, but just in case you re-roll again and you do not forget:

  Johannes Sixt <j6t@kdbg.org>

is preferred.

> Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
> ---
>  cache.h    | 6 +++---
>  lockfile.c | 2 +-
>  refs.c     | 5 +++--
>  3 files changed, 7 insertions(+), 6 deletions(-)
> 
> diff --git a/cache.h b/cache.h
> index b7af173..9019c7d 100644
> --- a/cache.h
> +++ b/cache.h
> @@ -538,10 +538,10 @@ extern int refresh_index(struct index_state *, unsigned int flags, const struct
>  #define LOCK_SUFFIX_LEN 5
>  
>  struct lock_file {
> -	struct lock_file *next;
> +	struct lock_file *volatile next;
>  	volatile sig_atomic_t active;
> -	int fd;
> -	pid_t owner;
> +	volatile int fd;
> +	volatile pid_t owner;
>  	char on_list;
>  	char filename[PATH_MAX];
>  };
> diff --git a/lockfile.c b/lockfile.c
> index 50a0541..fce53f1 100644
> --- a/lockfile.c
> +++ b/lockfile.c
> @@ -69,7 +69,7 @@
>   * See Documentation/api-lockfile.txt for more information.
>   */
>  
> -static struct lock_file *lock_file_list;
> +static struct lock_file *volatile lock_file_list;
>  static const char *alternate_index_output;
>  
>  static void remove_lock_file(void)
> diff --git a/refs.c b/refs.c
> index cb2f825..db8057c 100644
> --- a/refs.c
> +++ b/refs.c
> @@ -2213,15 +2213,16 @@ int commit_packed_refs(void)
>  	struct packed_ref_cache *packed_ref_cache =
>  		get_packed_ref_cache(&ref_cache);
>  	int error = 0;
> +	int fd;
>  
>  	if (!packed_ref_cache->lock)
>  		die("internal error: packed-refs not locked");
>  	write_or_die(packed_ref_cache->lock->fd,
>  		     PACKED_REFS_HEADER, strlen(PACKED_REFS_HEADER));
>  
> +	fd = packed_ref_cache->lock->fd;
>  	do_for_each_entry_in_dir(get_packed_ref_dir(packed_ref_cache),
> -				 0, write_packed_entry_fn,
> -				 &packed_ref_cache->lock->fd);
> +				 0, write_packed_entry_fn, &fd);
>  	if (commit_lock_file(packed_ref_cache->lock))
>  		error = -1;
>  	packed_ref_cache->lock = NULL;
> 

^ permalink raw reply	[flat|nested] 32+ messages in thread

* Re: [PATCH v3 00/25] Lockfile correctness and refactoring
  2014-04-14 13:54 [PATCH v3 00/25] Lockfile correctness and refactoring Michael Haggerty
                   ` (24 preceding siblings ...)
  2014-04-14 13:54 ` [PATCH v3 25/25] trim_last_path_elm(): replace last_path_elm() Michael Haggerty
@ 2014-04-15 18:40 ` Torsten Bögershausen
  2014-04-16 19:50   ` Michael Haggerty
  25 siblings, 1 reply; 32+ messages in thread
From: Torsten Bögershausen @ 2014-04-15 18:40 UTC (permalink / raw)
  To: Michael Haggerty, Junio C Hamano
  Cc: git, Johannes Sixt, Jeff King, Torsten Bögershausen, Eric Sunshine

refs.c:
int close_ref(struct ref_lock *lock)
{
	if (close_lock_file(lock->lk))
		return -1;
	lock->lock_fd = -1;
	return 0;
}

When the close() fails, fd is still >= 0, even if the file is closed.

Could it be written like this ?

int close_ref(struct ref_lock *lock)
{
	return close_lock_file(lock->lk);
}
=====================
lockfile.c, line 49
 *   - filename holds the filename of the lockfile

I think we have a strbuf here? (which is a good thing),
should we write:
 *   - strbuf filename holds the filename of the lockfile
----------
(and at some places filename[0] is mentioned,
should that be filename.buf[0] ?)



=========================
int commit_lock_file(struct lock_file *lk)
{
	static struct strbuf result_file = STRBUF_INIT;
	int err;

	if (lk->fd >= 0 && close_lock_file(lk))
		return -1;
##What happens if close() fails and close_lock_file() returns != 0?
##Is the lk now in a good shaped state?
I think the file which had been open by lk->fd is in an unkown state,
and should not be used for anything.
When I remember it right, an error on close() can mean "the file could not
be written and closed as expected, it can be truncated or corrupted.
This can happen on a network file system like NFS, or probably even other FS.
For me the failing of close() means I smell a need for a rollback.

	if (!lk->active)
		die("BUG: attempt to commit unlocked object");

	/* remove ".lock": */
	strbuf_add(&result_file, lk->filename.buf,
		   lk->filename.len - LOCK_SUFFIX_LEN);
	err = rename(lk->filename.buf, result_file.buf);
	strbuf_reset(&result_file);
	if (err)
		return -1;
	lk->active = 0;
	strbuf_reset(&lk->filename);
	return 0;
}

================
Please treat my comments more than questions rather than answers,
thanks for an interesting reading

^ permalink raw reply	[flat|nested] 32+ messages in thread

* Re: [PATCH v3 16/25] commit_lock_file(): die() if called for unlocked lockfile object
  2014-04-15  6:49   ` Johannes Sixt
@ 2014-04-16 15:17     ` Michael Haggerty
  0 siblings, 0 replies; 32+ messages in thread
From: Michael Haggerty @ 2014-04-16 15:17 UTC (permalink / raw)
  To: Johannes Sixt, Junio C Hamano
  Cc: git, Jeff King, Torsten Bögershausen, Eric Sunshine

On 04/15/2014 08:49 AM, Johannes Sixt wrote:
> Am 4/14/2014 15:54, schrieb Michael Haggerty:
>> diff --git a/lockfile.c b/lockfile.c
>> index 664b0c3..1453a7a 100644
>> --- a/lockfile.c
>> +++ b/lockfile.c
>> @@ -292,6 +292,9 @@ int commit_lock_file(struct lock_file *lk)
>>  	if (lk->fd >= 0 && close_lock_file(lk))
>>  		return -1;
>>  
>> +	if (!lk->filename[0])
>> +		die("BUG: attempt to commit unlocked object");
>> +
> 
> Shouldn't this be the first thing to check after entering the function?

You're right; I will change it.

Michael

-- 
Michael Haggerty
mhagger@alum.mit.edu
http://softwareswirl.blogspot.com/

^ permalink raw reply	[flat|nested] 32+ messages in thread

* Re: [PATCH v3 18/25] struct lock_file: declare some fields volatile
  2014-04-15  6:55   ` Johannes Sixt
@ 2014-04-16 15:36     ` Michael Haggerty
  0 siblings, 0 replies; 32+ messages in thread
From: Michael Haggerty @ 2014-04-16 15:36 UTC (permalink / raw)
  To: Johannes Sixt, Junio C Hamano
  Cc: git, Jeff King, Torsten Bögershausen, Eric Sunshine

On 04/15/2014 08:55 AM, Johannes Sixt wrote:
> Am 4/14/2014 15:54, schrieb Michael Haggerty:
>> The function remove_lock_file_on_signal() is used as a signal handler.
>> It is not realistic to make the signal handler conform strictly to the
>> C standard, which is very restrictive about what a signal handler is
>> allowed to do.  But let's increase the likelihood that it will work:
>>
>> The lock_file_list global variable and several fields from struct
>> lock_file are used by the signal handler.  Declare those values
>> "volatile" to increase the chance that the signal handler will see a
>> valid object state.
> 
> Yes, it's important that the signal handler sees a valid object state, and
> "volatile" can help here. But I think the reason why it helps is not
> obvious, and it should be mentioned in the commit message:
> 
> It is not so much that "volatile" forces the compiler to lay down each
> access of the variable coded in C in the assembly code, but more
> importantly, that "volatile" disallows any re-ordering of these accesses.
> Then:
> 
> - 'lock->active = 1' must be the last assignment during setup
> 
> - 'lock->active = 0' must be the first assignment during tear-down.
> 
> - Ideally, all members of struct lock_file should be "volatile".
> 
> The last point is important because the compiler is allowed to re-order
> accesses to non-"volatile" variables across "volatile" accesses. I say
> "ideally" because if filename were defined "volatile filename[PATH_MAX]",
> strcpy() could not be used anymore. OTOH, it is unlikely that a compiler
> re-orders a strcpy() call across other expressions, and we can get away
> without "volatile" in the "filename" case in practice.

Thanks for the clarification.  I will edit the commit message to better
explain the rationale.

>> Suggested-by: Johannes Sixt <j.sixt@viscovery.net>
> 
> Not a big deal, but just in case you re-roll again and you do not forget:
> 
>   Johannes Sixt <j6t@kdbg.org>
> 
> is preferred.

ACK.

Michael

-- 
Michael Haggerty
mhagger@alum.mit.edu
http://softwareswirl.blogspot.com/

^ permalink raw reply	[flat|nested] 32+ messages in thread

* Re: [PATCH v3 00/25] Lockfile correctness and refactoring
  2014-04-15 18:40 ` [PATCH v3 00/25] Lockfile correctness and refactoring Torsten Bögershausen
@ 2014-04-16 19:50   ` Michael Haggerty
  0 siblings, 0 replies; 32+ messages in thread
From: Michael Haggerty @ 2014-04-16 19:50 UTC (permalink / raw)
  To: Torsten Bögershausen, Junio C Hamano
  Cc: git, Johannes Sixt, Jeff King, Eric Sunshine

On 04/15/2014 08:40 PM, Torsten Bögershausen wrote:
> refs.c:
> int close_ref(struct ref_lock *lock)
> {
> 	if (close_lock_file(lock->lk))
> 		return -1;
> 	lock->lock_fd = -1;
> 	return 0;
> }
> 
> When the close() fails, fd is still >= 0, even if the file is closed.
> 
> Could it be written like this ?
> 
> int close_ref(struct ref_lock *lock)
> {
> 	return close_lock_file(lock->lk);
> }

It seem to me it would be better to set lock->lock_fd to -1 regardless
of whether close_lock_file() succeeds.  Or maybe this field is not even
needed, and instead lock->lk->fd should be used.

This is a bit beyond the scope of this patch series, but I will put it
on my todo list.

> =====================
> lockfile.c, line 49
>  *   - filename holds the filename of the lockfile
> 
> I think we have a strbuf here? (which is a good thing),
> should we write:
>  *   - strbuf filename holds the filename of the lockfile
> ----------
> (and at some places filename[0] is mentioned,
> should that be filename.buf[0] ?)

I think it is OK to speak of a strbuf as "holding" a filename (what else
would that construct mean?

But you are correct that the comments shouldn't speak of filename[0]
anymore.  I will fix it.

> =========================
> int commit_lock_file(struct lock_file *lk)
> {
> 	static struct strbuf result_file = STRBUF_INIT;
> 	int err;
> 
> 	if (lk->fd >= 0 && close_lock_file(lk))
> 		return -1;
> ##What happens if close() fails and close_lock_file() returns != 0?
> ##Is the lk now in a good shaped state?
> I think the file which had been open by lk->fd is in an unkown state,
> and should not be used for anything.
> When I remember it right, an error on close() can mean "the file could not
> be written and closed as expected, it can be truncated or corrupted.
> This can happen on a network file system like NFS, or probably even other FS.
> For me the failing of close() means I smell a need for a rollback.

Yes, this is a good catch.  I think a rollback should definitely be done
in this case.  I will fix it.

In fact, I'm wondering whether it would be appropriate for
close_lock_file() itself to do a rollback if close() fails.  I guess I
will first have to audit callers to make sure that they don't try to use
lock_file::filename after a failed close_lock_file() (e.g., for
generating an error message).

> Please treat my comments more than questions rather than answers,
> thanks for an interesting reading

Thanks for your helpful comments!

Michael

-- 
Michael Haggerty
mhagger@alum.mit.edu
http://softwareswirl.blogspot.com/

^ permalink raw reply	[flat|nested] 32+ messages in thread

end of thread, other threads:[~2014-04-16 19:51 UTC | newest]

Thread overview: 32+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-04-14 13:54 [PATCH v3 00/25] Lockfile correctness and refactoring Michael Haggerty
2014-04-14 13:54 ` [PATCH v3 01/25] unable_to_lock_die(): rename function from unable_to_lock_index_die() Michael Haggerty
2014-04-14 13:54 ` [PATCH v3 02/25] api-lockfile: expand the documentation Michael Haggerty
2014-04-14 13:54 ` [PATCH v3 03/25] rollback_lock_file(): do not clear filename redundantly Michael Haggerty
2014-04-14 13:54 ` [PATCH v3 04/25] rollback_lock_file(): set fd to -1 Michael Haggerty
2014-04-14 13:54 ` [PATCH v3 05/25] lockfile: unlock file if lockfile permissions cannot be adjusted Michael Haggerty
2014-04-14 13:54 ` [PATCH v3 06/25] hold_lock_file_for_append(): release lock on errors Michael Haggerty
2014-04-14 13:54 ` [PATCH v3 07/25] lock_file(): always add lock_file object to lock_file_list Michael Haggerty
2014-04-14 13:54 ` [PATCH v3 08/25] lockfile.c: document the various states of lock_file objects Michael Haggerty
2014-04-14 13:54 ` [PATCH v3 09/25] cache.h: define constants LOCK_SUFFIX and LOCK_SUFFIX_LEN Michael Haggerty
2014-04-14 13:54 ` [PATCH v3 10/25] delete_ref_loose(): don't muck around in the lock_file's filename Michael Haggerty
2014-04-14 13:54 ` [PATCH v3 11/25] prepare_index(): declare return value to be (const char *) Michael Haggerty
2014-04-14 13:54 ` [PATCH v3 12/25] write_packed_entry_fn(): convert cb_data into a (const int *) Michael Haggerty
2014-04-14 13:54 ` [PATCH v3 13/25] lock_file(): exit early if lockfile cannot be opened Michael Haggerty
2014-04-14 13:54 ` [PATCH v3 14/25] remove_lock_file(): call rollback_lock_file() Michael Haggerty
2014-04-14 13:54 ` [PATCH v3 15/25] commit_lock_file(): inline temporary variable Michael Haggerty
2014-04-14 13:54 ` [PATCH v3 16/25] commit_lock_file(): die() if called for unlocked lockfile object Michael Haggerty
2014-04-15  6:49   ` Johannes Sixt
2014-04-16 15:17     ` Michael Haggerty
2014-04-14 13:54 ` [PATCH v3 17/25] lockfile: avoid transitory invalid states Michael Haggerty
2014-04-14 13:54 ` [PATCH v3 18/25] struct lock_file: declare some fields volatile Michael Haggerty
2014-04-15  6:55   ` Johannes Sixt
2014-04-16 15:36     ` Michael Haggerty
2014-04-14 13:54 ` [PATCH v3 19/25] try_merge_strategy(): remove redundant lock_file allocation Michael Haggerty
2014-04-14 13:54 ` [PATCH v3 20/25] try_merge_strategy(): use a statically-allocated lock_file object Michael Haggerty
2014-04-14 13:54 ` [PATCH v3 21/25] commit_lock_file(): use a strbuf to manage temporary space Michael Haggerty
2014-04-14 13:54 ` [PATCH v3 22/25] Change lock_file::filename into a strbuf Michael Haggerty
2014-04-14 13:54 ` [PATCH v3 23/25] resolve_symlink(): use a strbuf for internal scratch space Michael Haggerty
2014-04-14 13:54 ` [PATCH v3 24/25] resolve_symlink(): take a strbuf parameter Michael Haggerty
2014-04-14 13:54 ` [PATCH v3 25/25] trim_last_path_elm(): replace last_path_elm() Michael Haggerty
2014-04-15 18:40 ` [PATCH v3 00/25] Lockfile correctness and refactoring Torsten Bögershausen
2014-04-16 19:50   ` Michael Haggerty

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.