From patchwork Fri Feb 26 17:33:48 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 589209 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 0B989140325 for ; Sat, 27 Feb 2016 04:34:01 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.b=Pxem+qyC; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:to:from:subject:message-id:date:mime-version :content-type; q=dns; s=default; b=J8VqVYqPRm3e6IpBO+hrNo2dHRxYE JVWG2X+htxHdCbeIVLPY8vzUTX2bU0JhB4qpg9HAhKOP3B4ypIk+0z7Rs37jr1Br VhICi7hpbZZQYfoy2ugDqqz2gNhkkKUdV31n5ScRsaM/TbJJj8qiUdHExwoDUIv5 FbJswMRc7SdOGo= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:to:from:subject:message-id:date:mime-version :content-type; s=default; bh=SL5iMNWkXvlrIRkSuoFg26rJgKM=; b=Pxe m+qyCBg/SuU5U8DgvvpIyflCtAWu9QcKKV2b65saiBpgLEmN7z0qfT12jXxfpOwD DoOIJNCV1w31oquzW2OVwRP66qSLTwQuMt37ziZjE8Nt/IWLygEwUe8BLwtVoP1L P4TqwJCAEwipyhKn8siwW/rtvG6sYWMAIMPtlUbc= Received: (qmail 106289 invoked by alias); 26 Feb 2016 17:33:55 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 106261 invoked by uid 89); 26 Feb 2016 17:33:54 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=BAYES_00, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=slaves, narrow, 2001-2016, 20012016 X-HELO: mx1.redhat.com To: GNU C Library From: Florian Weimer Subject: [PATCH] libio: Always use 8192 bytes as the default buffer size [BZ #4099] Message-ID: <56D08C7C.9080806@redhat.com> Date: Fri, 26 Feb 2016 18:33:48 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.5.0 MIME-Version: 1.0 This avoids overly large buffers with network file systems which report very large block sizes. Remove DEV_TTY_P and related macros, which are now unused. Florian 2016-02-26 Florian Weimer * libio/filedoalloc.c (isatty): Remove. (local_isatty): Add comment. Call __isatty directly. (_IO_file_doallocate): Update comment. Always use _IO_BUFSIZ as the buffer size. * libio/wfiledoalloc.c (isatty): Remove. (_IO_wfile_doallocate): Update comment. Always follow the narrow buffer size. * sysdeps/mach/hurd/device-nrs.h: Remove file. * sysdeps/generic/device-nrs.h (DEV_TTY_P): Remove. * sysdeps/unix/sysv/linux/device-nrs.h (DEV_TTY_LOW_MAJOR) (DEV_TTY_HIGH_MAJOR, DEV_TTY_P): Remove. diff --git a/NEWS b/NEWS index a06a42f..5a4ac4e 100644 --- a/NEWS +++ b/NEWS @@ -21,6 +21,15 @@ Version 2.24 * The readdir_r and readdir64_r functions have been deprecated. It is recommended to use readdir and readdir64 instead. +* Byte-oriented stdio streams use a default buffer size of 8192 bytes. + Previously, on Linux, the default buffer size on most file systems was + 4096 bytes, except on network file systems, where the buffer size was + unpredictable and could be as large as several megabytes. The default + buffer size for wide-oriented streams now follows the byte-oriented buffer + size. Previously, it could be four times as large as that. + +The default buffer size + Security related changes: [Add security related changes here] diff --git a/libio/filedoalloc.c b/libio/filedoalloc.c index b51e910..ce704ff 100644 --- a/libio/filedoalloc.c +++ b/libio/filedoalloc.c @@ -55,48 +55,25 @@ /* Modified for GNU iostream by Per Bothner 1991, 1992. */ -#ifndef _POSIX_SOURCE -# define _POSIX_SOURCE -#endif #include "libioP.h" -#include -#include #include #include -#ifdef _LIBC -# undef isatty -# define isatty(Fd) __isatty (Fd) - -# include -#endif - - +/* Return the result of isatty, without changing errno. */ static int local_isatty (int fd) { int save_errno = errno; - int res = isatty (fd); + int res = __isatty (fd); __set_errno (save_errno); return res; } - -/* - * Allocate a file buffer, or switch to unbuffered I/O. - * Per the ANSI C standard, ALL tty devices default to line buffered. - * - * As a side effect, we set __SOPT or __SNPT (en/dis-able fseek - * optimisation) right after the _fstat() that finds the buffer size. - */ - +/* Allocate a file buffer, or switch to unbuffered I/O. Streams for + * TTY devices default to line buffered. */ int _IO_file_doallocate (_IO_FILE *fp) { - _IO_size_t size; - char *p; - struct stat64 st; - #ifndef _LIBC /* If _IO_cleanup_registration_needed is non-zero, we should call the function it points to. This is to make sure _IO_cleanup gets called @@ -106,28 +83,13 @@ _IO_file_doallocate (_IO_FILE *fp) (*_IO_cleanup_registration_needed) (); #endif - size = _IO_BUFSIZ; - if (fp->_fileno >= 0 && __builtin_expect (_IO_SYSSTAT (fp, &st), 0) >= 0) - { - if (S_ISCHR (st.st_mode)) - { - /* Possibly a tty. */ - if ( -#ifdef DEV_TTY_P - DEV_TTY_P (&st) || -#endif - local_isatty (fp->_fileno)) - fp->_flags |= _IO_LINE_BUF; - } -#if _IO_HAVE_ST_BLKSIZE - if (st.st_blksize > 0) - size = st.st_blksize; -#endif - } - p = malloc (size); + /* Switch to line buffering for TTYs. */ + if (fp->_fileno >= 0 && local_isatty (fp->_fileno)) + fp->_flags |= _IO_LINE_BUF; + char *p = malloc (_IO_BUFSIZ); if (__glibc_unlikely (p == NULL)) return EOF; - _IO_setb (fp, p, p + size, 1); + _IO_setb (fp, p, p + _IO_BUFSIZ, 1); return 1; } libc_hidden_def (_IO_file_doallocate) diff --git a/libio/wfiledoalloc.c b/libio/wfiledoalloc.c index 28c10b6..55bab6c 100644 --- a/libio/wfiledoalloc.c +++ b/libio/wfiledoalloc.c @@ -55,26 +55,11 @@ /* Modified for GNU iostream by Per Bothner 1991, 1992. */ -#ifndef _POSIX_SOURCE -# define _POSIX_SOURCE -#endif #include "libioP.h" -#include -#include #include -#include - -#ifdef _LIBC -# undef isatty -# define isatty(Fd) __isatty (Fd) -#endif /* * Allocate a file buffer, or switch to unbuffered I/O. - * Per the ANSI C standard, ALL tty devices default to line buffered. - * - * As a side effect, we set __SOPT or __SNPT (en/dis-able fseek - * optimisation) right after the _fstat() that finds the buffer size. */ int @@ -87,13 +72,11 @@ _IO_wfile_doallocate (_IO_FILE *fp) if (fp->_IO_buf_base == NULL) _IO_file_doallocate (fp); - /* If narrow buffer is user allocated (set by setvbuf etc.), - use that size as the size of the wide buffer, when it is - allocated by _IO_file_doallocate, multiply that by size - of the wide character. */ + /* Use the size of the narrow buffer as the size of the wide buffer + (both measured in bytes). Round up the wide buffer size to the + next multiple of sizeof (wchar_t) if necessary. */ size = fp->_IO_buf_end - fp->_IO_buf_base; - if ((fp->_flags & _IO_USER_BUF)) - size = (size + sizeof (wchar_t) - 1) / sizeof (wchar_t); + size = (size + sizeof (wchar_t) - 1) / sizeof (wchar_t); p = malloc (size * sizeof (wchar_t)); if (__glibc_unlikely (p == NULL)) return EOF; diff --git a/sysdeps/generic/device-nrs.h b/sysdeps/generic/device-nrs.h index 236d821..9d77318 100644 --- a/sysdeps/generic/device-nrs.h +++ b/sysdeps/generic/device-nrs.h @@ -21,7 +21,4 @@ /* By default we know no device numbers. */ -/* We cannot check whether a given device is a tty. */ -#define DEV_TTY_P(statp) (0) - #endif /* device-nrs.h */ diff --git a/sysdeps/mach/hurd/device-nrs.h b/sysdeps/mach/hurd/device-nrs.h deleted file mode 100644 index 6a56af1..0000000 --- a/sysdeps/mach/hurd/device-nrs.h +++ /dev/null @@ -1,27 +0,0 @@ -/* Device numbers of devices used in the implementation. Hurd version. - Copyright (C) 2001-2016 Free Software Foundation, Inc. - This file is part of the GNU C Library. - - The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - The GNU C Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with the GNU C Library; if not, see - . */ - -#ifndef _DEVICE_NRS_H -#define _DEVICE_NRS_H 1 - -#include - -/* Check whether a given device is a tty. */ -#define DEV_TTY_P(statp) ((statp)->st_fstype == FSTYPE_TERM) - -#endif /* device-nrs.h */ diff --git a/sysdeps/unix/sysv/linux/device-nrs.h b/sysdeps/unix/sysv/linux/device-nrs.h index 78fc79c..2f13147 100644 --- a/sysdeps/unix/sysv/linux/device-nrs.h +++ b/sysdeps/unix/sysv/linux/device-nrs.h @@ -19,8 +19,6 @@ #ifndef _DEVICE_NRS_H #define _DEVICE_NRS_H 1 -#include - /* /dev/null is (1,3). */ #define DEV_NULL_MAJOR 1 #define DEV_NULL_MINOR 3 @@ -29,17 +27,4 @@ #define DEV_FULL_MAJOR 1 #define DEV_FULL_MINOR 7 -/* Pseudo tty slaves. For Linux we use the Unix98 ttys. We could - also include the old BSD-style tty buts they should not be used and - the extra test would only slow down correctly set up systems. If a - system still uses those device the slower tests performed (using - isatty) will catch it. */ -#define DEV_TTY_LOW_MAJOR 136 -#define DEV_TTY_HIGH_MAJOR 143 - -/* Test whether given device is a tty. */ -#define DEV_TTY_P(statp) \ - ({ int __dev_major = major ((statp)->st_rdev); \ - __dev_major >= DEV_TTY_LOW_MAJOR && __dev_major <= DEV_TTY_HIGH_MAJOR; }) - #endif /* device-nrs.h */