/*
* sa_conv.c: Convert an old format sa file to the up-to-date format.
- * (C) 1999-2017 by Sebastien GODARD (sysstat <at> orange.fr)
+ * (C) 1999-2021 by Sebastien GODARD (sysstat <at> orange.fr)
*
***************************************************************************
* This program is free software; you can redistribute it and/or modify it *
#endif
extern int endian_mismatch;
+extern unsigned int user_hz;
extern unsigned int act_types_nr[];
extern unsigned int rec_types_nr[];
extern unsigned int hdr_types_nr[];
}
/* 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;
}
file_hdr->sa_day = f_hdr_2171->sa_day;
file_hdr->sa_month = f_hdr_2171->sa_month;
file_hdr->sa_sizeof_long = f_hdr_2171->sa_sizeof_long;
- strncpy(file_hdr->sa_sysname, f_hdr_2171->sa_sysname, UTSNAME_LEN);
- file_hdr->sa_sysname[UTSNAME_LEN - 1] = '\0';
- strncpy(file_hdr->sa_nodename, f_hdr_2171->sa_nodename, UTSNAME_LEN);
- file_hdr->sa_nodename[UTSNAME_LEN - 1] = '\0';
- strncpy(file_hdr->sa_release, f_hdr_2171->sa_release, UTSNAME_LEN);
- file_hdr->sa_release[UTSNAME_LEN - 1] = '\0';
- strncpy(file_hdr->sa_machine, f_hdr_2171->sa_machine, UTSNAME_LEN);
- file_hdr->sa_machine[UTSNAME_LEN - 1] = '\0';
+ strncpy(file_hdr->sa_sysname, f_hdr_2171->sa_sysname, sizeof(file_hdr->sa_sysname));
+ file_hdr->sa_sysname[sizeof(file_hdr->sa_sysname) - 1] = '\0';
+ strncpy(file_hdr->sa_nodename, f_hdr_2171->sa_nodename, sizeof(file_hdr->sa_nodename));
+ file_hdr->sa_nodename[sizeof(file_hdr->sa_nodename) - 1] = '\0';
+ strncpy(file_hdr->sa_release, f_hdr_2171->sa_release, sizeof(file_hdr->sa_release));
+ file_hdr->sa_release[sizeof(file_hdr->sa_release) - 1] = '\0';
+ strncpy(file_hdr->sa_machine, f_hdr_2171->sa_machine, sizeof(file_hdr->sa_machine));
+ file_hdr->sa_machine[sizeof(file_hdr->sa_machine) - 1] = '\0';
}
else if (previous_format == FORMAT_MAGIC_2173) {
}
file_hdr->act_size = FILE_ACTIVITY_SIZE;
file_hdr->rec_size = RECORD_HEADER_SIZE;
+
+ /*
+ * Note: @extra_next and @sa_tzname[] members are set to zero
+ * because file_hdr has been memset'd to zero.
+ */
}
/*
n = (previous_format == FORMAT_MAGIC_2171 ? FILE_HEADER_SIZE_2171
: hdr_size);
SREALLOC(buffer, char, n);
- sa_fread(fd, buffer, (size_t) n, HARD_SIZE);
+ sa_fread(fd, buffer, (size_t) n, HARD_SIZE, UEOF_STOP);
/* Upgrade file_header structure */
upgrade_file_header(buffer, file_hdr, previous_format,
for (i = 0; i < file_hdr->sa_act_nr; i++, ofal++) {
- sa_fread(fd, ofal, OLD_FILE_ACTIVITY_SIZE, HARD_SIZE);
+ sa_fread(fd, ofal, OLD_FILE_ACTIVITY_SIZE, HARD_SIZE, UEOF_STOP);
/* Normalize endianness for file_activity structures */
if (endian_mismatch) {
a_cpu = TRUE;
}
- /* Size of an activity cannot be zero */
- if (!ofal->size)
+ /* Sanity checks */
+ if (!ofal->size || (ofal->size > MAX_ITEM_STRUCT_SIZE))
goto invalid_header;
/* Size of activity in file is larger than up-to-date activity size */
}
/* 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;
}
* Note: Consider the value 0x01020304 read on a 32-bit machine.
* Big-endian, saved as: 01 02 03 04
* Lille-endian, saved as: 04 03 02 01
- * The value should be saved as an 64-bit value and endianness should be
+ * The value should be saved as a 64-bit value and endianness should be
* preserved:
* Big-endian: 00 00 00 00 01 02 03 04
* Little-endian: 04 03 02 01 00 00 00 00
* disk.
***************************************************************************
*/
-int upgrade_stats_serial(struct activity *act[], int p, int st_size, int endian_mismatch)
+int upgrade_stats_serial(struct activity *act[], int p, size_t st_size, int endian_mismatch)
{
int i;
unsigned int line;
struct stats_serial *ssc;
/* Copy TTY stats to target structure */
- memcpy(act[p]->buf[1], act[p]->buf[0], act[p]->nr_ini * st_size);
+ memcpy(act[p]->buf[1], act[p]->buf[0], (size_t) act[p]->nr_ini * st_size);
for (i = 0; i < act[p]->nr_ini; i++) {
ssc = (struct stats_serial *) ((char *) act[p]->buf[1] + i * act[p]->fsize);
sndc->tx_compressed = moveto_long_long(&sndp->tx_compressed, endian_mismatch, arch_64);
sndc->multicast = moveto_long_long(&sndp->multicast, endian_mismatch, arch_64);
sndc->speed = 0; /* New field */
- strncpy(sndc->interface, sndp->interface, MAX_IFACE_LEN);
- sndc->interface[MAX_IFACE_LEN - 1] = '\0';
+ strncpy(sndc->interface, sndp->interface, sizeof(sndc->interface));
+ sndc->interface[sizeof(sndc->interface) - 1] = '\0';
sndc->duplex = '\0'; /* New field */
}
}
sndc->tx_compressed = sndp->tx_compressed;
sndc->multicast = sndp->multicast;
sndc->speed = 0; /* New field */
- strncpy(sndc->interface, sndp->interface, MAX_IFACE_LEN);
- sndc->interface[MAX_IFACE_LEN - 1] = '\0';
+ strncpy(sndc->interface, sndp->interface, sizeof(sndc->interface));
+ sndc->interface[sizeof(sndc->interface) - 1] = '\0';
sndc->duplex = '\0'; /* New field */
}
}
sndc->tx_compressed = sndp->tx_compressed;
sndc->multicast = sndp->multicast;
sndc->speed = sndp->speed;
- strncpy(sndc->interface, sndp->interface, MAX_IFACE_LEN);
- sndc->interface[MAX_IFACE_LEN - 1] = '\0';
+ strncpy(sndc->interface, sndp->interface, sizeof(sndc->interface));
+ sndc->interface[sizeof(sndc->interface) - 1] = '\0';
sndc->duplex = sndp->duplex;
}
}
snedc->tx_fifo_errors = moveto_long_long(&snedp->tx_fifo_errors, endian_mismatch, arch_64);
snedc->rx_frame_errors = moveto_long_long(&snedp->rx_frame_errors, endian_mismatch, arch_64);
snedc->tx_carrier_errors = moveto_long_long(&snedp->tx_carrier_errors, endian_mismatch, arch_64);
- strncpy(snedc->interface, snedp->interface, MAX_IFACE_LEN);
- snedc->interface[MAX_IFACE_LEN - 1] = '\0';
+ strncpy(snedc->interface, snedp->interface, sizeof(snedc->interface));
+ snedc->interface[sizeof(snedc->interface) - 1] = '\0';
}
}
else {
snedc->tx_fifo_errors = snedp->tx_fifo_errors;
snedc->rx_frame_errors = snedp->rx_frame_errors;
snedc->tx_carrier_errors = snedp->tx_carrier_errors;
- strncpy(snedc->interface, snedp->interface, MAX_IFACE_LEN);
- snedc->interface[MAX_IFACE_LEN - 1] = '\0';
+ strncpy(snedc->interface, snedp->interface, sizeof(snedc->interface));
+ snedc->interface[sizeof(snedc->interface) - 1] = '\0';
}
}
}
sfc->f_bavail = sfp->f_bavail;
sfc->f_files = sfp->f_files;
sfc->f_ffree = sfp->f_ffree;
- strncpy(sfc->fs_name, sfp->fs_name, MAX_FS_LEN);
- sfc->fs_name[MAX_FS_LEN - 1] = '\0';
+ strncpy(sfc->fs_name, sfp->fs_name, sizeof(sfc->fs_name));
+ sfc->fs_name[sizeof(sfc->fs_name) - 1] = '\0';
if (st_size <= STATS_FILESYSTEM_8A_1_SIZE) {
/* mountp didn't exist with older versions */
sfc->mountp[0] = '\0';
}
else {
- strncpy(sfc->mountp, sfp->mountp, MAX_FS_LEN);
- sfc->mountp[MAX_FS_LEN - 1] = '\0';
+ strncpy(sfc->mountp, sfp->mountp, sizeof(sfc->mountp));
+ sfc->mountp[sizeof(sfc->mountp) - 1] = '\0';
}
}
}
* 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;
}
{
struct record_header rec_hdr;
+ memset(&rec_hdr, 0, sizeof(struct record_header));
+
/* Convert current record header */
rec_hdr.uptime_cs = orec_hdr->uptime0 * 100 / HZ; /* Uptime in cs, not jiffies */
rec_hdr.ust_time = (unsigned long long) orec_hdr->ust_time;
}
/* 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;
}
char file_comment[MAX_COMMENT_LEN];
/* Read the COMMENT record */
- sa_fread(fd, file_comment, MAX_COMMENT_LEN, HARD_SIZE);
- file_comment[MAX_COMMENT_LEN - 1] = '\0';
+ sa_fread(fd, file_comment, sizeof(file_comment), HARD_SIZE, UEOF_STOP);
+ file_comment[sizeof(file_comment) - 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, sizeof(file_comment)) != sizeof(file_comment)) {
fprintf(stderr, "\nwrite: %s\n", strerror(errno));
return -1;
}
* of volatile activity structures. Among them is A_CPU activity.
*/
for (i = 0; i < vol_act_nr; i++) {
- sa_fread(fd, &ofile_act, OLD_FILE_ACTIVITY_SIZE, HARD_SIZE);
+ sa_fread(fd, &ofile_act, OLD_FILE_ACTIVITY_SIZE, HARD_SIZE, UEOF_STOP);
/* Normalize endianness for file_activity structures */
if (endian_mismatch) {
}
/* 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;
}
for (k = 0; k < act[p]->nr2; k++) {
sa_fread(fd,
(char *) act[p]->buf[0] + (j * act[p]->nr2 + k) * act[p]->msize,
- (size_t) ofal->size, HARD_SIZE);
+ (size_t) ofal->size, HARD_SIZE, UEOF_STOP);
}
}
}
else if (act[p]->nr_ini > 0) {
sa_fread(fd, act[p]->buf[0],
(size_t) ofal->size * (size_t) act[p]->nr_ini * (size_t) act[p]->nr2,
- HARD_SIZE);
+ HARD_SIZE, UEOF_STOP);
}
nr_struct = act[p]->nr_ini;
}
/* 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");
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;
}
}
fprintf(stderr, _("Statistics:\n"));
do {
- eosaf = sa_fread(fd, &orec_hdr, OLD_RECORD_HEADER_SIZE, SOFT_SIZE);
+ eosaf = sa_fread(fd, &orec_hdr, OLD_RECORD_HEADER_SIZE, SOFT_SIZE, UEOF_STOP);
/* Normalize endianness */
if (endian_mismatch) {
goto success;
}
- /* Get HZ */
- get_HZ();
+ if (!user_hz) {
+ /* Get HZ */
+ get_HZ();
+ }
+ else {
+ /* HZ set on the command line with option -O */
+ hz = user_hz;
+ }
fprintf(stderr, _("HZ: Using current value: %lu\n"), HZ);
/* Upgrade file's header section */