From da67040826e271d4b6be72a9932a01ed7186a18a Mon Sep 17 00:00:00 2001 From: Sebastien GODARD Date: Wed, 27 Dec 2017 14:18:05 +0100 Subject: [PATCH] sadc.c: Stop if no activities are collected Don't collect CPU stats by default if no other activities are collected: sadc now stops in this case. Signed-off-by: Sebastien GODARD --- sa.h | 2 ++ sa_common.c | 11 +++++++++++ sadc.c | 11 ++++++----- sar.c | 4 ++-- 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/sa.h b/sa.h index 8677e20..b9eb411 100644 --- a/sa.h +++ b/sa.h @@ -1257,6 +1257,8 @@ int parse_sar_n_opt (char * [], int *, struct activity * []); int parse_timestamp (char * [], int *, struct tstamp *, const char *); +void print_collect_error + (void); void print_report_hdr (unsigned int, struct tm *, struct file_header *); void print_sar_comment diff --git a/sa_common.c b/sa_common.c index 18f57af..4ae504a 100644 --- a/sa_common.c +++ b/sa_common.c @@ -1289,6 +1289,17 @@ void handle_invalid_sa_file(int *fd, struct file_magic *file_magic, char *file, exit(3); } +/* + *************************************************************************** + * Display an error message then exit. + *************************************************************************** + */ +void print_collect_error(void) +{ + fprintf(stderr, _("Requested activities not available\n")); + exit(1); +} + /* *************************************************************************** * Move structures data. diff --git a/sadc.c b/sadc.c index 0e59a26..5a32e72 100644 --- a/sadc.c +++ b/sadc.c @@ -1239,11 +1239,6 @@ int main(int argc, char **argv) } } - /* At least one activity must be collected (default is A_CPU) */ - if (!get_activity_nr(act, AO_COLLECTED, COUNT_ACTIVITIES)) { - COLLECT_ACTIVITY(A_CPU); - } - /* Process file entered on the command line */ if (WANT_SA_ROTAT(flags)) { /* File name set to '-' */ @@ -1288,6 +1283,12 @@ int main(int argc, char **argv) /* Init structures according to machine architecture */ sa_sys_init(); + /* At least one activity must be collected */ + if (!get_activity_nr(act, AO_COLLECTED, COUNT_ACTIVITIES)) { + /* Requested activities not available: Exit */ + print_collect_error(); + } + if (!interval && !comment[0]) { /* * Interval (and count) not set, and no comment given diff --git a/sar.c b/sar.c index f161b44..2d6e4ed 100644 --- a/sar.c +++ b/sar.c @@ -1101,8 +1101,8 @@ void read_stats(void) read_header_data(); if (!get_activity_nr(act, AO_SELECTED, COUNT_ACTIVITIES)) { - fprintf(stderr, _("Requested activities not available\n")); - exit(1); + /* Requested activities not available: Exit */ + print_collect_error(); } /* Determine if a stat line header has to be displayed */ -- 2.40.0