From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2178) id 550073856B65; Mon, 12 Dec 2022 15:21:33 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 550073856B65 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1670858493; bh=7dkQ43KskIUkgCKPoFO2qTS+n+cnnWNdQvrprS9hQIs=; h=From:To:Subject:Date:From; b=fknuiSKT/M+9el+OHrpUryfCLqBDPxENCxaiYkjFiNBjF9Oxl9f/LUQkNUQ3w2BwU htfHbcqA2ZTS68YXEjHnFiPjOl4Knav/pTWDVreBG9onhEiDCClKRZno4n4WqyxThx ROEVRhCRjw5vPgGuSmfiyLOCrvtgwajMc21bbJbY= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Florian Weimer To: glibc-cvs@sourceware.org Subject: [glibc/fw/vfprintf-2] libio: Convert __vdprintf_internal to buffers X-Act-Checkin: glibc X-Git-Author: Florian Weimer X-Git-Refname: refs/heads/fw/vfprintf-2 X-Git-Oldrev: 214908c0def333495c359630d7822e7ccb6b8154 X-Git-Newrev: 2deb7d2cffb76dbcaf7ba051e616ccb7e275ed54 Message-Id: <20221212152133.550073856B65@sourceware.org> Date: Mon, 12 Dec 2022 15:21:33 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=2deb7d2cffb76dbcaf7ba051e616ccb7e275ed54 commit 2deb7d2cffb76dbcaf7ba051e616ccb7e275ed54 Author: Florian Weimer Date: Thu Aug 18 08:49:54 2022 +0200 libio: Convert __vdprintf_internal to buffers The internal buffer size is set to 2048 bytes. This is less than the original BUFSIZ value used by buffered_vfprintf before the conversion, but it hopefully covers all cases where write boundaries matter. Diff: --- include/printf_buffer.h | 4 +++ libio/iovdprintf.c | 69 ++++++++++++++++++++++---------------- stdio-common/printf_buffer_flush.c | 4 +++ 3 files changed, 49 insertions(+), 28 deletions(-) diff --git a/include/printf_buffer.h b/include/printf_buffer.h index 47e252b52e..3d4ef1d06c 100644 --- a/include/printf_buffer.h +++ b/include/printf_buffer.h @@ -49,6 +49,7 @@ enum __printf_buffer_mode __printf_buffer_mode_sprintf_chk, __printf_buffer_mode_to_file, __printf_buffer_mode_asprintf, + __printf_buffer_mode_dprintf, __printf_buffer_mode_strfmon, __printf_buffer_mode_fp, /* For __printf_fp_l_buffer. */ __printf_buffer_mode_fp_to_wide, /* For __wprintf_fp_l_buffer. */ @@ -305,6 +306,9 @@ void __printf_buffer_flush_to_file (struct __printf_buffer_to_file *) struct __printf_buffer_asprintf; void __printf_buffer_flush_asprintf (struct __printf_buffer_asprintf *) attribute_hidden; +struct __printf_buffer_dprintf; +void __printf_buffer_flush_dprintf (struct __printf_buffer_dprintf *) + attribute_hidden; struct __printf_buffer_fp; void __printf_buffer_flush_fp (struct __printf_buffer_fp *) attribute_hidden; diff --git a/libio/iovdprintf.c b/libio/iovdprintf.c index 1e483868c9..f4cdd6b537 100644 --- a/libio/iovdprintf.c +++ b/libio/iovdprintf.c @@ -24,41 +24,54 @@ This exception applies to code released by its copyright holders in files containing the exception. */ -#include +#include +#include +#include #include +#include +#include -int -__vdprintf_internal (int d, const char *format, va_list arg, - unsigned int mode_flags) +struct __printf_buffer_dprintf { - struct _IO_FILE_plus tmpfil; - struct _IO_wide_data wd; - int done; + struct __printf_buffer base; + int fd; + + /* This should cover most of the packet-oriented file descriptors, + where boundaries between writes could be visible to readers. */ + char buf[2048]; +}; -#ifdef _IO_MTSAFE_IO - tmpfil.file._lock = NULL; -#endif - _IO_no_init (&tmpfil.file, _IO_USER_LOCK, 0, &wd, &_IO_wfile_jumps); - _IO_JUMPS (&tmpfil) = &_IO_file_jumps; - _IO_new_file_init_internal (&tmpfil); - if (_IO_file_attach (&tmpfil.file, d) == NULL) +void +__printf_buffer_flush_dprintf (struct __printf_buffer_dprintf *buf) +{ + char *p = buf->buf; + char *end = buf->base.write_ptr; + while (p < end) { - _IO_un_link (&tmpfil); - return EOF; + ssize_t ret = TEMP_FAILURE_RETRY (write (buf->fd, p, end - p)); + if (ret < 0) + { + __printf_buffer_mark_failed (&buf->base); + return; + } + p += ret; } - tmpfil.file._flags |= _IO_DELETE_DONT_CLOSE; - - _IO_mask_flags (&tmpfil.file, _IO_NO_READS, - _IO_NO_READS+_IO_NO_WRITES+_IO_IS_APPENDING); - - done = __vfprintf_internal (&tmpfil.file, format, arg, mode_flags); - - if (done != EOF && _IO_do_flush (&tmpfil.file) == EOF) - done = EOF; - - _IO_FINISH (&tmpfil.file); + buf->base.write_ptr = buf->buf; +} - return done; +int +__vdprintf_internal (int d, const char *format, va_list arg, + unsigned int mode_flags) +{ + struct __printf_buffer_dprintf buf; + __printf_buffer_init (&buf.base, buf.buf, array_length (buf.buf), + __printf_buffer_mode_dprintf); + buf.fd = d; + __printf_buffer (&buf.base, format, arg, mode_flags); + if (__printf_buffer_has_failed (&buf.base)) + return -1; + __printf_buffer_flush_dprintf (&buf); + return __printf_buffer_done (&buf.base); } int diff --git a/stdio-common/printf_buffer_flush.c b/stdio-common/printf_buffer_flush.c index 14fe1b2df4..922340cc54 100644 --- a/stdio-common/printf_buffer_flush.c +++ b/stdio-common/printf_buffer_flush.c @@ -28,6 +28,7 @@ # pragma weak __printf_buffer_flush_snprintf # pragma weak __printf_buffer_flush_to_file # pragma weak __printf_buffer_flush_asprintf +# pragma weak __printf_buffer_flush_dprintf # pragma weak __printf_buffer_flush_fp # pragma weak __printf_buffer_flush_fp_to_wide # pragma weak __printf_buffer_flush_fphex_to_wide @@ -53,6 +54,9 @@ __printf_buffer_do_flush (struct __printf_buffer *buf) case __printf_buffer_mode_asprintf: __printf_buffer_flush_asprintf ((struct __printf_buffer_asprintf *) buf); return; + case __printf_buffer_mode_dprintf: + __printf_buffer_flush_dprintf ((struct __printf_buffer_dprintf *) buf); + return; case __printf_buffer_mode_strfmon: __set_errno (E2BIG); __printf_buffer_mark_failed (buf);