Mercurial > hg > octave-lojdl > gnulib-hg
changeset 14720:2663098d5483
openat: reduce syscalls in first probe of /proc
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>
author | Eric Blake <eblake@redhat.com> |
---|---|
date | Mon, 09 May 2011 16:27:35 -0600 |
parents | 88e9ac67f0a5 |
children | 8230401f6111 |
files | ChangeLog lib/openat-proc.c modules/openat |
diffstat | 3 files changed, 13 insertions(+), 12 deletions(-) [+] |
line wrap: on
line diff
--- 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
--- a/lib/openat-proc.c +++ b/lib/openat-proc.c @@ -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 @@ 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); } }