openat: reduce syscalls in first probe of /proc
authorEric Blake <eblake@redhat.com>
Mon, 9 May 2011 22:27:35 +0000 (16:27 -0600)
committerEric Blake <eblake@redhat.com>
Mon, 9 May 2011 22:45:09 +0000 (16:45 -0600)
open/access/close is cheaper than open/stat/stat/close.

Use O_DIRECTORY for safety.

* lib/openat-proc.c (openat_proc_name): Require that /proc/self/fd
be a directory.  Simplify the probe for .. bugs.
* modules/openat (Depends-on): Drop same-inode.
Reported by Bastien ROUCARIES.

Signed-off-by: Eric Blake <eblake@redhat.com>
ChangeLog
lib/openat-proc.c
modules/openat

index be6516f..36fb1fe 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2011-05-09  Eric Blake  <eblake@redhat.com>
+
+       openat: reduce syscalls in first probe of /proc
+       * lib/openat-proc.c (openat_proc_name): Require that /proc/self/fd
+       be a directory.  Simplify the probe for .. bugs.
+       * modules/openat (Depends-on): Drop same-inode.
+       Reported by Bastien ROUCARIES.
+
 2011-05-09  Jim Meyering  <meyering@redhat.com>
 
        maint.mk: change semantics/name of tight_scope variables
index 4a470c5..5711896 100644 (file)
@@ -31,7 +31,6 @@
 #include <unistd.h>
 
 #include "intprops.h"
-#include "same-inode.h"
 
 /* The results of open() in this file are not used with fchdir,
    and we do not leak fds to any single-threaded code that could use stdio,
@@ -75,20 +74,15 @@ openat_proc_name (char buf[OPENAT_BUFFER_SIZE], int fd, char const *file)
          problem is exhibited on code that built on Solaris 8 and
          running on Solaris 10.  */
 
-      int proc_self_fd = open ("/proc/self/fd", O_SEARCH);
+      int proc_self_fd = open ("/proc/self/fd",
+                               O_SEARCH | O_DIRECTORY | O_NOCTTY | O_NONBLOCK);
       if (proc_self_fd < 0)
         proc_status = -1;
       else
         {
-          struct stat proc_self_fd_dotdot_st;
-          struct stat proc_self_st;
-          char dotdot_buf[PROC_SELF_FD_NAME_SIZE_BOUND (sizeof ".." - 1)];
-          sprintf (dotdot_buf, PROC_SELF_FD_FORMAT, proc_self_fd, "..");
-          proc_status =
-            ((stat (dotdot_buf, &proc_self_fd_dotdot_st) == 0
-              && stat ("/proc/self", &proc_self_st) == 0
-              && SAME_INODE (proc_self_fd_dotdot_st, proc_self_st))
-             ? 1 : -1);
+          char dotdot_buf[PROC_SELF_FD_NAME_SIZE_BOUND (sizeof "../fd" - 1)];
+          sprintf (dotdot_buf, PROC_SELF_FD_FORMAT, proc_self_fd, "../fd");
+          proc_status = access (dotdot_buf, F_OK) ? -1 : 1;
           close (proc_self_fd);
         }
     }
index abd3b63..c0cb834 100644 (file)
@@ -30,7 +30,6 @@ mkdir           [test $HAVE_MKDIRAT = 0]
 open
 openat-die
 rmdir           [test $REPLACE_UNLINKAT = 1]
-same-inode
 save-cwd
 stdbool
 sys_stat