@@ -79,19 +79,13 @@ static size_t syslog_identifier_len;
7979
8080#define WRITEV_BUFFER_N_IOV 128
8181
82- typedef struct {
83- int iovcnt ;
84- struct iovec iov [WRITEV_BUFFER_N_IOV ];
85- } writev_buffer_t ;
86-
8782static void parse_log_path (char * log_config );
8883static const char * stdpipe_name (stdpipe_t pipe );
8984static int write_journald (int pipe , char * buf , ssize_t num_read );
9085static int write_k8s_log (stdpipe_t pipe , const char * buf , ssize_t buflen );
9186static bool get_line_len (ptrdiff_t * line_len , const char * buf , ssize_t buflen );
92- static ssize_t writev_buffer_append_segment (int fd , writev_buffer_t * buf , const void * data , ssize_t len );
93- static ssize_t writev_buffer_append_segment_no_flush (writev_buffer_t * buf , const void * data , ssize_t len );
94- static ssize_t writev_buffer_flush (int fd , writev_buffer_t * buf );
87+ static ssize_t writev_buffer_append_segment (int fd , writev_iov_t * buf , const void * data , ssize_t len );
88+ static ssize_t writev_buffer_append_segment_no_flush (writev_iov_t * buf , const void * data , ssize_t len );
9589static void set_k8s_timestamp (char * buf , ssize_t buflen , const char * pipename );
9690static void reopen_k8s_file (void );
9791static int parse_priority_prefix (const char * buf , ssize_t buflen , int * priority , const char * * message_start );
@@ -386,7 +380,8 @@ static int write_journald(int pipe, char *buf, ssize_t buflen)
386380 ptrdiff_t line_len = 0 ;
387381
388382 while (buflen > 0 || * partial_buf_len > 0 ) {
389- writev_buffer_t bufv = {0 };
383+ struct iovec vecs [WRITEV_BUFFER_N_IOV ];
384+ writev_iov_t bufv = { 0 , WRITEV_BUFFER_N_IOV , vecs };
390385
391386 bool partial = buflen == 0 || get_line_len (& line_len , buf , buflen );
392387
@@ -489,7 +484,8 @@ static int write_k8s_log(stdpipe_t pipe, const char *buf, ssize_t buflen)
489484 static bool stdout_has_partial = false;
490485 static bool stderr_has_partial = false;
491486
492- writev_buffer_t bufv = {0 };
487+ struct iovec vecs [WRITEV_BUFFER_N_IOV ];
488+ writev_iov_t bufv = { 0 , WRITEV_BUFFER_N_IOV , vecs };
493489 int64_t bytes_to_be_written = 0 ;
494490
495491 bool * has_partial = (pipe == STDOUT_PIPE ) ? & stdout_has_partial : & stderr_has_partial ;
@@ -550,7 +546,7 @@ static int write_k8s_log(stdpipe_t pipe, const char *buf, ssize_t buflen)
550546 * a timestamp.
551547 */
552548 if ((log_size_max > 0 ) && (k8s_bytes_written + bytes_to_be_written ) > log_size_max ) {
553- if (writev_buffer_flush (k8s_log_fd , & bufv ) < 0 ) {
549+ if (writev_all (k8s_log_fd , & bufv ) < 0 ) {
554550 nwarn ("failed to flush buffer to log" );
555551 }
556552 reopen_k8s_file ();
@@ -600,7 +596,7 @@ static int write_k8s_log(stdpipe_t pipe, const char *buf, ssize_t buflen)
600596 buflen -= line_len ;
601597 }
602598
603- if (writev_buffer_flush (k8s_log_fd , & bufv ) < 0 ) {
599+ if (writev_all (k8s_log_fd , & bufv ) < 0 ) {
604600 nwarn ("failed to flush buffer to log" );
605601 }
606602
@@ -622,66 +618,12 @@ static bool get_line_len(ptrdiff_t *line_len, const char *buf, ssize_t buflen)
622618 return partial ;
623619}
624620
625-
626- static ssize_t writev_buffer_flush (int fd , writev_buffer_t * buf )
627- {
628- ssize_t count = 0 ;
629- int iovcnt = buf -> iovcnt ;
630- struct iovec * iov = buf -> iov ;
631-
632- /*
633- * By definition, flushing the buffers will either be entirely successful, or will fail at some point
634- * along the way. There is no facility to attempt to retry a writev() system call outside of an EINTR
635- * errno. Therefore, no matter the outcome, always reset the writev_buffer_t data structure.
636- */
637- buf -> iovcnt = 0 ;
638-
639- while (iovcnt > 0 ) {
640- ssize_t res ;
641- do {
642- res = writev (fd , iov , iovcnt );
643- } while (res == -1 && errno == EINTR );
644-
645- if (res <= 0 ) {
646- /*
647- * Any unflushed data is lost (this would be a good place to add a counter for how many times
648- * this occurs and another count for how much data is lost).
649- *
650- * Note that if writev() returns a 0, this logic considers it an error.
651- */
652- return -1 ;
653- }
654-
655- count += res ;
656-
657- while (res > 0 ) {
658- size_t iov_len = iov -> iov_len ;
659- size_t from_this = MIN ((size_t )res , iov_len );
660- res -= from_this ;
661- iov_len -= from_this ;
662-
663- if (iov_len == 0 ) {
664- iov ++ ;
665- iovcnt -- ;
666- /* continue, res still > 0 */
667- } else {
668- iov -> iov_len = iov_len ;
669- iov -> iov_base += from_this ;
670- /* break, res is 0 */
671- }
672- }
673- }
674-
675- return count ;
676- }
677-
678-
679- ssize_t writev_buffer_append_segment (int fd , writev_buffer_t * buf , const void * data , ssize_t len )
621+ ssize_t writev_buffer_append_segment (int fd , writev_iov_t * buf , const void * data , ssize_t len )
680622{
681623 if (data == NULL )
682624 return 1 ;
683625
684- if (buf -> iovcnt == WRITEV_BUFFER_N_IOV && writev_buffer_flush (fd , buf ) < 0 )
626+ if (buf -> iovcnt == buf -> max_iovcnt && writev_all (fd , buf ) < 0 )
685627 return -1 ;
686628
687629 if (len > 0 ) {
@@ -693,12 +635,12 @@ ssize_t writev_buffer_append_segment(int fd, writev_buffer_t *buf, const void *d
693635 return 1 ;
694636}
695637
696- ssize_t writev_buffer_append_segment_no_flush (writev_buffer_t * buf , const void * data , ssize_t len )
638+ ssize_t writev_buffer_append_segment_no_flush (writev_iov_t * buf , const void * data , ssize_t len )
697639{
698640 if (data == NULL )
699641 return 1 ;
700642
701- if (buf -> iovcnt == WRITEV_BUFFER_N_IOV )
643+ if (buf -> iovcnt == buf -> max_iovcnt )
702644 return -1 ;
703645
704646 if (len > 0 ) {
0 commit comments