From owner-svn-src-stable-9@FreeBSD.ORG Fri Apr 12 11:37:25 2013 Return-Path: Delivered-To: svn-src-stable-9@freebsd.org Received: from mx1.freebsd.org (mx1.FreeBSD.org [8.8.178.115]) by hub.freebsd.org (Postfix) with ESMTP id 208AC6D3; Fri, 12 Apr 2013 11:37:25 +0000 (UTC) (envelope-from jilles@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:1900:2254:2068::e6a:0]) by mx1.freebsd.org (Postfix) with ESMTP id EBB16103; Fri, 12 Apr 2013 11:37:24 +0000 (UTC) Received: from svn.freebsd.org ([127.0.1.70]) by svn.freebsd.org (8.14.6/8.14.6) with ESMTP id r3CBbOQ1080339; Fri, 12 Apr 2013 11:37:24 GMT (envelope-from jilles@svn.freebsd.org) Received: (from jilles@localhost) by svn.freebsd.org (8.14.6/8.14.5/Submit) id r3CBbOKf080338; Fri, 12 Apr 2013 11:37:24 GMT (envelope-from jilles@svn.freebsd.org) Message-Id: <201304121137.r3CBbOKf080338@svn.freebsd.org> From: Jilles Tjoelker Date: Fri, 12 Apr 2013 11:37:24 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-9@freebsd.org Subject: svn commit: r249401 - stable/9/lib/libc/gen X-SVN-Group: stable-9 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-stable-9@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: SVN commit messages for only the 9-stable src tree List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 12 Apr 2013 11:37:25 -0000 Author: jilles Date: Fri Apr 12 11:37:24 2013 New Revision: 249401 URL: http://svnweb.freebsd.org/changeset/base/249401 Log: MFC r232385 by ru: Remove 3 syscalls from opendir(). Finally removed the stat() and fstat() calls from the opendir() code. They were made excessive in r205424 by opening with O_DIRECTORY. Also eliminated the fcntl() call used to set FD_CLOEXEC by opening with O_CLOEXEC. (fdopendir() still checks that the passed descriptor is a directory, and sets FD_CLOEXEC on it.) The necessary kernel support for O_DIRECTORY and O_CLOEXEC was already in 9.0-RELEASE. Discussed with: ru Modified: stable/9/lib/libc/gen/opendir.c Directory Properties: stable/9/lib/libc/ (props changed) Modified: stable/9/lib/libc/gen/opendir.c ============================================================================== --- stable/9/lib/libc/gen/opendir.c Fri Apr 12 08:52:19 2013 (r249400) +++ stable/9/lib/libc/gen/opendir.c Fri Apr 12 11:37:24 2013 (r249401) @@ -66,7 +66,17 @@ opendir(const char *name) DIR * fdopendir(int fd) { + struct stat statb; + /* Check that fd is associated with a directory. */ + if (_fstat(fd, &statb) != 0) + return (NULL); + if (!S_ISDIR(statb.st_mode)) { + errno = ENOTDIR; + return (NULL); + } + if (_fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) + return (NULL); return (__opendir_common(fd, NULL, DTF_HIDEW|DTF_NODUP)); } @@ -74,19 +84,9 @@ DIR * __opendir2(const char *name, int flags) { int fd; - struct stat statb; - /* - * stat() before _open() because opening of special files may be - * harmful. - */ - if (stat(name, &statb) != 0) - return (NULL); - if (!S_ISDIR(statb.st_mode)) { - errno = ENOTDIR; - return (NULL); - } - if ((fd = _open(name, O_RDONLY | O_NONBLOCK | O_DIRECTORY)) == -1) + if ((fd = _open(name, + O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_CLOEXEC)) == -1) return (NULL); return __opendir_common(fd, name, flags); @@ -110,19 +110,9 @@ __opendir_common(int fd, const char *nam int incr; int saved_errno; int unionstack; - struct stat statb; - dirp = NULL; - /* _fstat() the open handler because the file may have changed. */ - if (_fstat(fd, &statb) != 0) - goto fail; - if (!S_ISDIR(statb.st_mode)) { - errno = ENOTDIR; - goto fail; - } - if (_fcntl(fd, F_SETFD, FD_CLOEXEC) == -1 || - (dirp = malloc(sizeof(DIR) + sizeof(struct _telldir))) == NULL) - goto fail; + if ((dirp = malloc(sizeof(DIR) + sizeof(struct _telldir))) == NULL) + return (NULL); dirp->dd_td = (struct _telldir *)((char *)dirp + sizeof(DIR)); LIST_INIT(&dirp->dd_td->td_locq);