From 2ef70ad7f692686710091132978129b614f6dd1a Mon Sep 17 00:00:00 2001 From: Volodymyr Vasiutyk Date: Wed, 6 Oct 2021 10:45:07 +0300 Subject: Early program termination only from main() --- pcp/Platform.c | 25 ++++++++++++++----------- pcp/Platform.h | 5 +++-- 2 files changed, 17 insertions(+), 13 deletions(-) (limited to 'pcp') diff --git a/pcp/Platform.c b/pcp/Platform.c index cd7b1f48..150660af 100644 --- a/pcp/Platform.c +++ b/pcp/Platform.c @@ -257,7 +257,7 @@ size_t Platform_addMetric(PCPMetric id, const char* name) { /* global state from the environment and command line arguments */ pmOptions opts; -void Platform_init(void) { +bool Platform_init(void) { const char* source; if (opts.context == PM_CONTEXT_ARCHIVE) { source = opts.archives[0]; @@ -277,12 +277,12 @@ void Platform_init(void) { } if (sts < 0) { fprintf(stderr, "Cannot setup PCP metric source: %s\n", pmErrStr(sts)); - exit(1); + return false; } /* setup timezones and other general startup preparation completion */ if (pmGetContextOptions(sts, &opts) < 0 || opts.errors) { pmflush(); - exit(1); + return false; } pcp = xCalloc(1, sizeof(Platform)); @@ -309,7 +309,8 @@ void Platform_init(void) { sts = pmLookupName(pcp->totalMetrics, pcp->names, pcp->pmids); if (sts < 0) { fprintf(stderr, "Error: cannot lookup metric names: %s\n", pmErrStr(sts)); - exit(1); + Platform_done(); + return false; } for (size_t i = 0; i < pcp->totalMetrics; i++) { @@ -361,6 +362,8 @@ void Platform_init(void) { Platform_getRelease(0); Platform_getMaxCPU(); Platform_getMaxPid(); + + return true; } void Platform_dynamicColumnsDone(Hashtable* columns) { @@ -697,16 +700,16 @@ void Platform_longOptionsUsage(ATTR_UNUSED const char* name) { " --timezone=TZ set reporting timezone\n"); } -bool Platform_getLongOption(int opt, ATTR_UNUSED int argc, char** argv) { +CommandLineStatus Platform_getLongOption(int opt, ATTR_UNUSED int argc, char** argv) { /* libpcp export without a header definition */ extern void __pmAddOptHost(pmOptions*, char*); switch (opt) { case PLATFORM_LONGOPT_HOST: /* --host=HOSTSPEC */ if (argv[optind][0] == '\0') - return false; + return STATUS_ERROR_EXIT; __pmAddOptHost(&opts, optarg); - return true; + return STATUS_OK; case PLATFORM_LONGOPT_HOSTZONE: /* --hostzone */ if (opts.timezone) { @@ -715,23 +718,23 @@ bool Platform_getLongOption(int opt, ATTR_UNUSED int argc, char** argv) { } else { opts.tzflag = 1; } - return true; + return STATUS_OK; case PLATFORM_LONGOPT_TIMEZONE: /* --timezone=TZ */ if (argv[optind][0] == '\0') - return false; + return STATUS_ERROR_EXIT; if (opts.tzflag) { pmprintf("%s: at most one of -Z and -z allowed\n", pmGetProgname()); opts.errors++; } else { opts.timezone = optarg; } - return true; + return STATUS_OK; default: break; } - return false; + return STATUS_ERROR_EXIT; } void Platform_gettime_realtime(struct timeval* tv, uint64_t* msec) { diff --git a/pcp/Platform.h b/pcp/Platform.h index 37a87996..ad38cbbd 100644 --- a/pcp/Platform.h +++ b/pcp/Platform.h @@ -34,6 +34,7 @@ in the source distribution for its full text. #include "ProcessLocksScreen.h" #include "RichString.h" #include "SignalsPanel.h" +#include "CommandLine.h" #include "pcp/PCPDynamicColumn.h" #include "pcp/PCPDynamicMeter.h" @@ -67,7 +68,7 @@ extern const unsigned int Platform_numberOfSignals; extern const MeterClass* const Platform_meterTypes[]; -void Platform_init(void); +bool Platform_init(void); void Platform_done(void); @@ -126,7 +127,7 @@ enum { void Platform_longOptionsUsage(const char* name); -bool Platform_getLongOption(int opt, int argc, char** argv); +CommandLineStatus Platform_getLongOption(int opt, int argc, char** argv); extern pmOptions opts; -- cgit v1.2.3