Skip to contentSkip to conversation

Add cloexec information to fdinfo

PATCH In review [email protected] 5 participants 14 messages Jun 10, 2011 – Aug 6, 2011

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Add cloexec information to fdinfo

There is one piece of information about a file descriptor which is currently not visible from the outside: the close-on-exec flag. The /proc/PID/fdinfo/* files have the mode information but this is missing. Is the following patch acceptable?

What I don't know is whether the RCU locking is needed given that real locks are taken. Someone with more knowledge could just remove those two lines.

 base.c |   17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
fs/proc/base.c +14 -3
index 14def99..bda3651 100644
@@ -1924,12 +1924,23 @@ static int proc_fd_info(struct inode *inode, struct path *path, char *info)
 				*path = file->f_path;
 				path_get(&file->f_path);
 			}
-			if (info)
+			if (info) {
+				int cloexec;
+				struct fdtable *fdt;
+
+				rcu_read_lock();
+				fdt = files_fdtable(files);
+				cloexec = FD_ISSET(fd, fdt->close_on_exec);
+				rcu_read_unlock();
+
 				snprintf(info, PROC_FDINFO_MAX,
 					 "pos:\t%lli\n"
-					 "flags:\t0%o\n",
+					 "flags:\t0%o\n"
+					 "cloexec: %d\n",
 					 (long long) file->f_pos,
-					 file->f_flags);
+					 file->f_flags,
+					 cloexec);
+			}
 			spin_unlock(&files->file_lock);
 			put_files_struct(files);
 			return 0;
#1 lore raw
Quoted 36 lines · (2011/06/10 12:55), [email protected] wrote:

There is one piece of information about a file descriptor which is currently not visible from the outside: the close-on-exec flag. The /proc/PID/fdinfo/* files have the mode information but this is missing. Is the following patch acceptable?

What I don't know is whether the RCU locking is needed given that real locks are taken. Someone with more knowledge could just remove those two lines.

 base.c |   17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
fs/proc/base.c +9 -2
index 14def99..bda3651 100644
@@ -1924,12 +1924,23 @@ static int proc_fd_info(struct inode *inode, struct path *path, char *info)
 				*path = file->f_path;
 				path_get(&file->f_path);
 			}
-			if (info)
+			if (info) {
+				int cloexec;
+				struct fdtable *fdt;
+
+				rcu_read_lock();
+				fdt = files_fdtable(files);
+				cloexec = FD_ISSET(fd, fdt->close_on_exec);
+				rcu_read_unlock();
+
 				snprintf(info, PROC_FDINFO_MAX,
 					 "pos:\t%lli\n"
-					 "flags:\t0%o\n",

fs/proc/base.c static int proc_fd_info(struct inode *inode, struct path *path, char *info) · line 1946

Quoted 10 lines
fs/proc/base.c +5 -1
+					 "flags:\t0%o\n"
+					 "cloexec: %d\n",
 					 (long long) file->f_pos,
-					 file->f_flags);
+					 file->f_flags,
+					 cloexec);
+			}
 			spin_unlock(&files->file_lock);
 			put_files_struct(files);
 			return 0;

I think this is correct and useful. However I don't know fdtable life cycle detail. cc to linux-fsdevel.

#2 lore raw
In reply to [email protected]
Quoted 8 lines · On Thu, 9 Jun 2011 23:55:08 -0400 [email protected]…

There is one piece of information about a file descriptor which is currently not visible from the outside: the close-on-exec flag. The /proc/PID/fdinfo/* files have the mode information but this is missing. Is the following patch acceptable?

What I don't know is whether the RCU locking is needed given that real locks are taken. Someone with more knowledge could just remove those two lines.

The locking looks OK to me.

Hopefully /prod/pid/fdinfo is documented somewhere. [email protected] appears to be the email address we use to rub the documentation lamp.

Quoted 6 lines
fs/proc/base.c +0 -0
index 14def99..bda3651 100644
@@ -1924,12 +1924,23 @@ static int proc_fd_info(struct inode *inode, struct path *path, char *info)
 				*path = file->f_path;

fs/proc/base.c static int proc_fd_info(struct inode *inode, struct path *path, char *info) · line 1933

Quoted 10 lines
fs/proc/base.c +7 -1
 				path_get(&file->f_path);
 			}
-			if (info)
+			if (info) {
+				int cloexec;
+				struct fdtable *fdt;
+
+				rcu_read_lock();
+				fdt = files_fdtable(files);
+				cloexec = FD_ISSET(fd, fdt->close_on_exec);

Does FD_ISSET return 0 or 1? Or 0 or non-zero?

For x86 it's the former.

<checks the architectures>

arch/h8300/include/asm/posix_types.h: busted
#define __FD_ISSET(d, set) ((set)->fds_bits[__FDELT(d)] & __FDMASK(d))

arch/m68k/include/asm/posix_types.h: busted
#define __FD_ISSET(d, set) ((set)->fds_bits[__FDELT(d)] & __FDMASK(d))

arch/xtensa/include/asm/posix_types.h: busted
#define __FD_ISSET(d, set) ((set)->fds_bits[__FDELT(d)] & __FDMASK(d))

From: Andrew Morton <akpm-de/[email protected]>

Harmonise these return values with other architectures. In some cases this affects all compilers and in other cases non-gcc compilers only.

Cc Yoshinori Sato <[email protected]>
Cc Geert Uytterhoeven <[email protected]>
Cc Chris Zankel <[email protected]>
Cc Ulrich Drepper <[email protected]>

---

 arch/h8300/include/asm/posix_types.h  |    2 +-
 arch/m68k/include/asm/posix_types.h   |    2 +-
 arch/xtensa/include/asm/posix_types.h |    2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)
arch/h8300/include/asm/posix_types.h renamed from arch/h8300/include/asm/posix_types.h~h8300-m68k-xtensa-__fd_isset-should-return-0-1 +1 -1
diff -puN arch/h8300/include/asm/posix_types.h~h8300-m68k-xtensa-__fd_isset-should-return-0-1 arch/h8300/include/asm/posix_types.h
@@ -50,7 +50,7 @@ typedef struct {
 #define	__FD_CLR(d, set)	((set)->fds_bits[__FDELT(d)] &= ~__FDMASK(d))
 
 #undef	__FD_ISSET
-#define	__FD_ISSET(d, set)	((set)->fds_bits[__FDELT(d)] & __FDMASK(d))
+#define	__FD_ISSET(d, set)	(!!((set)->fds_bits[__FDELT(d)] & __FDMASK(d)))
 
 #undef	__FD_ZERO
 #define __FD_ZERO(fdsetp) (memset (fdsetp, 0, sizeof(*(fd_set *)fdsetp)))
arch/m68k/include/asm/posix_types.h renamed from arch/m68k/include/asm/posix_types.h~h8300-m68k-xtensa-__fd_isset-should-return-0-1 +1 -1
diff -puN arch/m68k/include/asm/posix_types.h~h8300-m68k-xtensa-__fd_isset-should-return-0-1 arch/m68k/include/asm/posix_types.h
@@ -51,7 +51,7 @@ typedef struct {
 #define	__FD_CLR(d, set)	((set)->fds_bits[__FDELT(d)] &= ~__FDMASK(d))
 
 #undef	__FD_ISSET
-#define	__FD_ISSET(d, set)	((set)->fds_bits[__FDELT(d)] & __FDMASK(d))
+#define	__FD_ISSET(d, set)	(!!((set)->fds_bits[__FDELT(d)] & __FDMASK(d)))
 
 #undef	__FD_ZERO
 #define __FD_ZERO(fdsetp) (memset (fdsetp, 0, sizeof(*(fd_set *)fdsetp)))
arch/xtensa/include/asm/posix_types.h renamed from arch/xtensa/include/asm/posix_types.h~h8300-m68k-xtensa-__fd_isset-should-return-0-1 +1 -1
diff -puN arch/xtensa/include/asm/posix_types.h~h8300-m68k-xtensa-__fd_isset-should-return-0-1 arch/xtensa/include/asm/posix_types.h
@@ -58,7 +58,7 @@ typedef struct {
 
 #define	__FD_SET(d, set)	((set)->fds_bits[__FDELT(d)] |= __FDMASK(d))
 #define	__FD_CLR(d, set)	((set)->fds_bits[__FDELT(d)] &= ~__FDMASK(d))
-#define	__FD_ISSET(d, set)	((set)->fds_bits[__FDELT(d)] & __FDMASK(d))
+#define	__FD_ISSET(d, set)	(!!((set)->fds_bits[__FDELT(d)] & __FDMASK(d)))
 #define	__FD_ZERO(set)	\
   ((void) memset ((void *) (set), 0, sizeof (__kernel_fd_set)))
 

_

fs/proc/base.c static int proc_fd_info(struct inode *inode, struct path *path, char *info) · line 1939

Quoted 7 lines
fs/proc/base.c +4 -1
+				rcu_read_unlock();
+
 				snprintf(info, PROC_FDINFO_MAX,
 					 "pos:\t%lli\n"
-					 "flags:\t0%o\n",
+					 "flags:\t0%o\n"
+					 "cloexec: %d\n",

Should use a tab here for consistency.

fs/proc/base.c renamed from fs/proc/base.c~proc-pid-fdinfo-add-cloexec-information-fix +1 -1
@@ -1936,7 +1936,7 @@ static int proc_fd_info(struct inode *in
 				snprintf(info, PROC_FDINFO_MAX,
 					 "pos:\t%lli\n"
 					 "flags:\t0%o\n"
-					 "cloexec: %d\n",
+					 "cloexec:\t%d\n",
 					 (long long) file->f_pos,
 					 file->f_flags,
 					 cloexec);

_

fs/proc/base.c static int proc_fd_info(struct inode *inode, struct path *path, char *info) · line 1946

Quoted 8 lines
fs/proc/base.c +3 -1
 					 (long long) file->f_pos,
-					 file->f_flags);
+					 file->f_flags,
+					 cloexec);
+			}
 			spin_unlock(&files->file_lock);
 			put_files_struct(files);
 			return 0;

--
To unsubscribe from this list: send the line "unsubscribe linux-man" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#3 lore raw
Quoted 6 lines · Andrew Morton

Does FD_ISSET return 0 or 1? Or 0 or non-zero?

For x86 it's the former.

<checks the architectures>

Quoted 8 lines

arch/h8300/include/asm/posix_types.h: busted
#define __FD_ISSET(d, set) ((set)->fds_bits[__FDELT(d)] & __FDMASK(d))

arch/m68k/include/asm/posix_types.h: busted
#define __FD_ISSET(d, set) ((set)->fds_bits[__FDELT(d)] & __FDMASK(d))

arch/xtensa/include/asm/posix_types.h: busted
#define __FD_ISSET(d, set) ((set)->fds_bits[__FDELT(d)] & __FDMASK(d))

I agree, this is an issue. Existing code works around this. We should use the attached patch on top of the existing one.

This doesn't invalidate your patch to harmonize the architectures but the above is what existing code does. I didn't use a tab in the output since the initial string is long enough to force the subsequent text in a new column. Using a single space seemed more visually pleasing.

fs/proc/base.c +1 -1
@@ -1939,7 +1939,7 @@
 					 "cloexec:\t%d\n",
 					 (long long) file->f_pos,
 					 file->f_flags,
-					 cloexec);
+					 cloexec ? FD_CLOEXEC : 0);
 			}
 			spin_unlock(&files->file_lock);
 			put_files_struct(files);
#4 lore raw
In reply to [email protected]
Quoted 4 lines · On Thu, Jun 9, 2011 at 8:55 PM, <[email protected]> wrote:

There is one piece of information about a file descriptor which is currently not visible from the outside: the close-on-exec flag.  The /proc/PID/fdinfo/* files have the mode information but this is missing.  Is the following patch acceptable?

The description makes no mention of why this would be needed?

Also, it's by no means the only thing not visible in /proc. Things like file locking status, leases, file descriptor ownership, signal number associated with the setown etc.

So the kernel has _lots_ of internal file knowledge, and not all of it makes sense to export to user space. What is so special about close-on-exec?

Linus

#5 lore raw
Quoted 1 line · Linus Torvalds

The description makes no mention of why this would be needed?

In this specific case I was trying to re-implement the pfiles program. For normal file descriptors (not sockets) this was the last piece of information which wasn't available. This is all part of my "give Solaris users no reason to not switch" effort. I intend to offer the code to the util-linux-ng maintainers.

Quoted 3 lines

Also, it's by no means the only thing not visible in /proc. Things like file locking status, leases, file descriptor ownership, signal number associated with the setown etc.

True, and maybe that will be useful as well.

I might actually have some more patches to get to socket information but I haven't fully checked out yet what is available.

#6 lore raw
Quoted 8 lines · Ulrich Drepper
Quoted 1 line · Linus Torvalds

The description makes no mention of why this would be needed?

In this specific case I was trying to re-implement the pfiles program. For normal file descriptors (not sockets) this was the last piece of information which wasn't available. This is all part of my "give Solaris users no reason to not switch" effort. I intend to offer the code to the util-linux-ng maintainers.

For the curious (me):

solaris:~ > pfiles $$
8418: -bash
Current rlimit: 256 file descriptors
0: S_IFCHR mode:0620 dev:349,28 ino:130202 uid:11003 gid:7 rdev:24,116

      O_RDWR|O_NOCTTY|O_LARGEFILE
      /dev/pts/116
   1: S_IFCHR mode:0620 dev:349,28 ino:130202 uid:11003 gid:7 rdev:24,116
      O_RDWR|O_NOCTTY|O_LARGEFILE
      /dev/pts/116
   2: S_IFCHR mode:0620 dev:349,28 ino:130202 uid:11003 gid:7 rdev:24,116
      O_RDWR|O_NOCTTY|O_LARGEFILE
      /dev/pts/116
   3: S_IFDOOR mode:0444 dev:347,0 ino:189 uid:0 gid:0 size:0
      O_RDONLY|O_LARGEFILE FD_CLOEXEC  door to nscd[11276]
      /var/run/name_service_door
 255: S_IFCHR mode:0620 dev:349,28 ino:130202 uid:11003 gid:7 rdev:24,116
      O_RDWR|O_NOCTTY|O_LARGEFILE FD_CLOEXEC
      /dev/pts/116

I guess lsof +f g could be suitably embellished as well.

cheers,
Pádraig.

#7 lore raw
In reply to Ulrich Drepper
Quoted 6 lines · Ulrich Drepper

In this specific case I was trying to re-implement the pfiles program. For normal file descriptors (not sockets) this was the last piece of information which wasn't available.  This is all part of my "give Solaris users no reason to not switch" effort.  I intend to offer the code to the util-linux-ng maintainers.

Ok.

So the part I really dislike about the patch is how it makes O_CLOEXEC so magically different from the other O_xyz flags.

Wouldn't it be much nicer to just show it in the "flags:" line?

Also, I don't think you need the rcu locking, since we hold the files->file_lock here anyway.

Quoted 8 lines
Quoted 3 lines

Also, it's by no means the only thing not visible in /proc. Things like file locking status, leases, file descriptor ownership, signal number associated with the setown etc.

True, and maybe that will be useful as well.

I might actually have some more patches to get to socket information but I haven't fully checked out yet what is available.

So at least O_CLOEXEC seems to have a real reason, and fits the existing model. I think a patch like the attached would be ok. Does that work for you? It's entirely untested here.

Linus

 fs/proc/base.c |   10 +++++++++-
 1 files changed, 9 insertions(+), 1 deletions(-)
fs/proc/base.c +9 -1
index fc5bc2767692..e2a016f24d38 100644
@@ -1920,6 +1920,14 @@ static int proc_fd_info(struct inode *inode, struct path *path, char *info)
 		spin_lock(&files->file_lock);
 		file = fcheck_files(files, fd);
 		if (file) {
+			unsigned int f_flags;
+			struct fdtable *fdt;
+
+			fdt = files_fdtable(files);
+			f_flags = file->f_flags & ~O_CLOEXEC;
+			if (FD_ISSET(fd, fdt->close_on_exec))
+				f_flags |= O_CLOEXEC;
+
 			if (path) {
 				*path = file->f_path;
 				path_get(&file->f_path);
@@ -1929,7 +1937,7 @@ static int proc_fd_info(struct inode *inode, struct path *path, char *info)
 					 "pos:\t%lli\n"
 					 "flags:\t0%o\n",
 					 (long long) file->f_pos,
-					 file->f_flags);
+					 f_flags);
 			spin_unlock(&files->file_lock);
 			put_files_struct(files);
 			return 0;
#8 lore raw
Quoted 4 lines · Linus Torvalds

So at least O_CLOEXEC seems to have a real reason, and fits the existing model. I think a patch like the attached would be ok. Does that work for you? It's entirely untested here.

Ok, minimally tested now, it seems to work from the one trivial test-case I wrote for it.

I'm not going to commit it, but if others think this is a reasonable thing to do and remind me during the next merge window, we can do it then.

Linus

#9 lore raw

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Quoted 6 lines · Linus Torvalds

Ok, minimally tested now, it seems to work from the one trivial test-case I wrote for it.

I'm not going to commit it, but if others think this is a reasonable thing to do and remind me during the next merge window, we can do it then.

It provides the information, yes, but it also hides some information. If you do it this way we cannot distinguish code which uses O_CLOEXEC at open-time from uses of fcntl(FD_CLOEXEC).

This is not needed for the specific application I have in mind. But if you, for instance, want to track down code which uses fcntl instead of doing the work at open-time using O_CLOEXEC we would need this information. -----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org/

iEYEARECAAYFAk4L5qQACgkQ2ijCOnn/RHSplwCgrA7RXs9gI7sr4e7SuvYsI04b dmEAnA9eD4k9ZdFoJuQvGoXkBkjolfZn
=6GVj
-----END PGP SIGNATURE-----

#10 lore raw
Quoted 3 lines · Ulrich Drepper

It provides the information, yes, but it also hides some information. If you do it this way we cannot distinguish code which uses O_CLOEXEC at open-time from uses of fcntl(FD_CLOEXEC).

One more reason to at least not use the patch as you have it right now.
If a file descriptor was opened with O_CLOEXEC but subsequently the bit
has been reset using fcntl() the f_flags value would still have the O_CLOEXEC bit set.

If you don't like the separate cloexec line, at least reset the O_CLOEXEC bit in the f_flags output if the FD_ISSET() test returns zero.

#11 lore raw
Quoted 5 lines · Ulrich Drepper

One more reason to at least not use the patch as you have it right now. If a file descriptor was opened with O_CLOEXEC but subsequently the bit has been reset using fcntl() the f_flags value would still have the O_CLOEXEC bit set.

Umm. Look at my patch again. It masks the O_CLOEXEC bit *exactly* because the bit is useless as it is now.

Which is why your previous objection was so silly too and I didn't even bother replying to the blather. O_CLOEXEC at open time has no special magical meaning. The only meaningful value for that bit is the current one.

Which is exactly what my patch did.

Quoted 2 lines

If you don't like the separate cloexec line, at least reset the O_CLOEXEC bit in the f_flags output if the FD_ISSET() test returns zero.

How about you just read the patch again?

This issue is closed as far as I am concerned. I repeat from my previous email:

"I'm not going to commit it, but if others think this is a reasonable
thing to do and remind me during the next merge window, we can do it then."

Gaah.

Linus

#12 lore raw
Quoted 3 lines · Linus Torvalds

"I'm not going to commit it, but if others think this is a reasonable
thing to do and remind me during the next merge window, we can do it then."

Can you please accept this message as a reminder to include your change?

Thanks.

#13 lore raw
Quoted 2 lines · Ulrich Drepper

Can you please accept this message as a reminder to include your change?

Thanks, done,

Linus