From baa988a5fed6b38a25d51bd9fab0098e17076d68 Mon Sep 17 00:00:00 2001 From: Sebastien GODARD Date: Fri, 20 Jul 2018 08:41:53 +0200 Subject: [PATCH] sa_conv.c: Use write_all() function instead of write() Use write_all() function in sa_conv.c just like sadc. write_all() function handles signal interruption which the plain write() doesn't. Signed-off-by: Sebastien GODARD --- sa.h | 6 ++++-- sa_common.c | 37 +++++++++++++++++++++++++++++++++++++ sa_conv.c | 20 ++++++++++---------- sadc.c | 37 ------------------------------------- 4 files changed, 51 insertions(+), 49 deletions(-) diff --git a/sa.h b/sa.h index f2609f1..78c9743 100644 --- a/sa.h +++ b/sa.h @@ -1274,12 +1274,14 @@ int get_activity_nr (struct activity * [], unsigned int, int); int get_activity_position (struct activity * [], unsigned int, int); -int set_default_file - (char *, int, int); void handle_invalid_sa_file (int, struct file_magic *, char *, int); void print_collect_error (void); +int set_default_file + (char *, int, int); +int write_all + (int, const void *, int); #ifndef SOURCE_SADC int add_list_item diff --git a/sa_common.c b/sa_common.c index 83c6ee9..ecb4c67 100644 --- a/sa_common.c +++ b/sa_common.c @@ -392,6 +392,43 @@ void enum_version_nr(struct file_magic *fm) fm->sysstat_extraversion = atoi(v) & 0xff; } +/* + *************************************************************************** + * Write data to file. If the write() call was interrupted by a signal, try + * again so that the whole buffer can be written. + * + * IN: + * @fd Output file descriptor. + * @buf Data buffer. + * @nr_bytes Number of bytes to write. + * + * RETURNS: + * Number of bytes written to file, or -1 on error. + *************************************************************************** + */ +int write_all(int fd, const void *buf, int nr_bytes) +{ + int block, offset = 0; + char *buffer = (char *) buf; + + while (nr_bytes > 0) { + block = write(fd, &buffer[offset], nr_bytes); + + if (block < 0) { + if (errno == EINTR) + continue; + return block; + } + if (block == 0) + return offset; + + offset += block; + nr_bytes -= block; + } + + return offset; +} + #ifndef SOURCE_SADC /* *************************************************************************** diff --git a/sa_conv.c b/sa_conv.c index 2ccc4d5..1bf419e 100644 --- a/sa_conv.c +++ b/sa_conv.c @@ -140,7 +140,7 @@ int upgrade_magic_section(char dfile[], int *fd, int stdfd, struct file_magic *f } /* Write file's magic structure */ - if (write(stdfd, &fm, FILE_MAGIC_SIZE) != FILE_MAGIC_SIZE) { + if (write_all(stdfd, &fm, FILE_MAGIC_SIZE) != FILE_MAGIC_SIZE) { fprintf(stderr, "\nwrite: %s\n", strerror(errno)); return -1; } @@ -378,7 +378,7 @@ int upgrade_header_section(char dfile[], int fd, int stdfd, struct activity *act } /* Write file_header structure */ - if ((n = write(stdfd, &fh, FILE_HEADER_SIZE)) != FILE_HEADER_SIZE) { + if ((n = write_all(stdfd, &fh, FILE_HEADER_SIZE)) != FILE_HEADER_SIZE) { fprintf(stderr, "\nwrite: %s\n", strerror(errno)); return -1; } @@ -1424,7 +1424,7 @@ int upgrade_activity_section(int stdfd, struct activity *act[], * Even unknown activities must be written * (they are counted in sa_act_nr). */ - if (write(stdfd, &fa, FILE_ACTIVITY_SIZE) != FILE_ACTIVITY_SIZE) { + if (write_all(stdfd, &fa, FILE_ACTIVITY_SIZE) != FILE_ACTIVITY_SIZE) { fprintf(stderr, "\nwrite: %s\n", strerror(errno)); return -1; } @@ -1472,7 +1472,7 @@ int upgrade_record_header(int fd, int stdfd, struct old_record_header *orec_hdr, } /* Write record header */ - if (write(stdfd, &rec_hdr, RECORD_HEADER_SIZE) != RECORD_HEADER_SIZE) { + if (write_all(stdfd, &rec_hdr, RECORD_HEADER_SIZE) != RECORD_HEADER_SIZE) { fprintf(stderr, "\nwrite: %s\n", strerror(errno)); return -1; } @@ -1503,7 +1503,7 @@ int upgrade_comment_record(int fd, int stdfd) file_comment[MAX_COMMENT_LEN - 1] = '\0'; /* Then write it. No changes at this time */ - if (write(stdfd, file_comment, MAX_COMMENT_LEN) != MAX_COMMENT_LEN) { + if (write_all(stdfd, file_comment, MAX_COMMENT_LEN) != MAX_COMMENT_LEN) { fprintf(stderr, "\nwrite: %s\n", strerror(errno)); return -1; } @@ -1578,7 +1578,7 @@ int upgrade_restart_record(int fd, int stdfd, struct activity *act[], } /* Write new number of CPU following the restart record */ - if (write(stdfd, &cpu_nr, sizeof(__nr_t)) != sizeof(__nr_t)) { + if (write_all(stdfd, &cpu_nr, sizeof(__nr_t)) != sizeof(__nr_t)) { fprintf(stderr, "\nwrite: %s\n", strerror(errno)); return -1; } @@ -1794,7 +1794,7 @@ int upgrade_common_record(int fd, int stdfd, struct activity *act[], struct file } /* Write number of structures for current activity */ - if (write(stdfd, &nr, sizeof(__nr_t)) != sizeof(__nr_t)) + if (write_all(stdfd, &nr, sizeof(__nr_t)) != sizeof(__nr_t)) goto write_error; fprintf(stderr, "n"); @@ -1802,9 +1802,9 @@ int upgrade_common_record(int fd, int stdfd, struct activity *act[], struct file for (j = 0; j < nr_struct; j++) { for (k = 0; k < act[p]->nr2; k++) { - if (write(stdfd, - (char *) act[p]->buf[1] + (j * act[p]->nr2 + k) * act[p]->fsize, - act[p]->fsize) != act[p]->fsize) + if (write_all(stdfd, + (char *) act[p]->buf[1] + (j * act[p]->nr2 + k) * act[p]->fsize, + act[p]->fsize) != act[p]->fsize) goto write_error; } } diff --git a/sadc.c b/sadc.c index 9ab1d66..5f251b4 100644 --- a/sadc.c +++ b/sadc.c @@ -368,43 +368,6 @@ void sa_sys_free(void) } } -/* - *************************************************************************** - * Write data to file. If the write() call was interrupted by a signal, try - * again so that the whole buffer can be written. - * - * IN: - * @fd Output file descriptor. - * @buf Data buffer. - * @nr_bytes Number of bytes to write. - * - * RETURNS: - * Number of bytes written to file, or -1 on error. - *************************************************************************** - */ -int write_all(int fd, const void *buf, int nr_bytes) -{ - int block, offset = 0; - char *buffer = (char *) buf; - - while (nr_bytes > 0) { - block = write(fd, &buffer[offset], nr_bytes); - - if (block < 0) { - if (errno == EINTR) - continue; - return block; - } - if (block == 0) - return offset; - - offset += block; - nr_bytes -= block; - } - - return offset; -} - /* *************************************************************************** * If -L option used, request a non-blocking, exclusive lock on the file. -- 2.40.0