]> granicus.if.org Git - sysstat/commitdiff
sa_conv.c: Use write_all() function instead of write()
authorSebastien GODARD <sysstat@users.noreply.github.com>
Fri, 20 Jul 2018 06:41:53 +0000 (08:41 +0200)
committerSebastien GODARD <sysstat@users.noreply.github.com>
Fri, 20 Jul 2018 06:41:53 +0000 (08:41 +0200)
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 <sysstat@users.noreply.github.com>
sa.h
sa_common.c
sa_conv.c
sadc.c

diff --git a/sa.h b/sa.h
index f2609f1b6a7271c1115212a2327927548b0f3347..78c9743f3983e9a5d0e55a41b52e14155395cd58 100644 (file)
--- 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
index 83c6ee9af5281ac6a382331448907d841c08de15..ecb4c677bed774e5af173c8ed70f54e570f0d582 100644 (file)
@@ -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
 /*
  ***************************************************************************
index 2ccc4d53bf8c66cec0309dcb1100175cf752b559..1bf419e98446665dd076cb60c3d981b076cf6e24 100644 (file)
--- 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 9ab1d66b2311fcd2c68d1cabe8c56a62859508db..5f251b4bb0d983650274dddb023965f3ff242b54 100644 (file)
--- 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.