commit 54e90826a931e30cb9468a8a9968155989a99f74
Author: Samuel Thibault <samuel.thibault@ens-lyon.org>
Date:   Tue Aug 23 12:34:02 2022 +0200

    file_exec: Homogeneize adding the gratuitous send right

diff --git a/libdiskfs/file-exec.c b/libdiskfs/file-exec.c
index b1cb33c3..ec9bc424 100644
--- a/libdiskfs/file-exec.c
+++ b/libdiskfs/file-exec.c
@@ -195,6 +195,10 @@ diskfs_S_file_exec_paths (struct protid *cred,
       do
 	{
 	  right = ports_get_send_right (newpi);
+
+	  /* Add a gratuitous send right on the protid to avoid a no-sender, and thus
+	     interrupt the exec, just because we are precisely replacing the calling
+	     process!  */
 	  cred_right = ports_get_send_right (cred);
 #ifdef HAVE_EXEC_EXEC_PATHS
 	  err = exec_exec_paths (execserver,
diff --git a/libnetfs/file-exec.c b/libnetfs/file-exec.c
index f5d25099..da49ac36 100644
--- a/libnetfs/file-exec.c
+++ b/libnetfs/file-exec.c
@@ -169,6 +169,10 @@ netfs_S_file_exec_paths (struct protid *cred,
 	  if (newpi)
 	    {
 	      right = ports_get_send_right (newpi);
+
+	      /* Add a gratuitous send right on the protid to avoid a no-sender, and thus
+		 interrupt the exec, just because we are precisely replacing the calling
+		 process!  */
 	      cred_right = ports_get_send_right (cred);
 #ifdef HAVE_EXEC_EXEC_PATHS
 	      err = exec_exec_paths (_netfs_exec,
diff --git a/trans/fakeroot.c b/trans/fakeroot.c
index e70cb280..df8d27d5 100644
--- a/trans/fakeroot.c
+++ b/trans/fakeroot.c
@@ -939,13 +939,13 @@ netfs_S_file_exec_paths (struct protid *user,
 			      file, MACH_PORT_RIGHT_SEND, 1);
   pthread_mutex_unlock (&user->po->np->lock);
 
-  /* Add a gratuitous send right on the protid to avoid a no-sender, and thus
-     interrupt the exec, just because we are precisely replacing the calling
-     process!  */
-  mach_port_t gratuitous = ports_get_send_right (user);
-
   if (!err)
     {
+      /* Add a gratuitous send right on the protid to avoid a no-sender, and thus
+	 interrupt the exec, just because we are precisely replacing the calling
+	 process!  */
+      mach_port_t gratuitous = ports_get_send_right (user);
+
 #ifdef HAVE_FILE_EXEC_PATHS
       /* We cannot use MACH_MSG_TYPE_MOVE_SEND because we might need to
 	 retry an interrupted call that would have consumed the rights.  */
@@ -971,6 +971,7 @@ netfs_S_file_exec_paths (struct protid *user,
 			 destroynames, destroynameslen);
 
       mach_port_deallocate (mach_task_self (), file);
+      mach_port_deallocate (mach_task_self (), gratuitous);
     }
 
   if (err == 0)
@@ -983,8 +984,6 @@ netfs_S_file_exec_paths (struct protid *user,
 	mach_port_deallocate (mach_task_self (), portarray[i]);
     }
 
-  mach_port_deallocate (mach_task_self (), gratuitous);
-
   return err;
 }
 
