Early program termination only from main()

This commit is contained in:
Volodymyr Vasiutyk 2021-10-06 10:45:07 +03:00 committed by BenBE
parent 2977414d54
commit 2ef70ad7f6
20 changed files with 123 additions and 90 deletions

View File

@ -86,9 +86,9 @@ typedef struct CommandLineSettings_ {
bool readonly;
} CommandLineSettings;
static CommandLineSettings parseArguments(const char* program, int argc, char** argv) {
static CommandLineStatus parseArguments(const char* program, int argc, char** argv, CommandLineSettings* flags) {
CommandLineSettings flags = {
*flags = (CommandLineSettings) {
.pidMatchList = NULL,
.commFilter = NULL,
.userId = (uid_t)-1, // -1 is guaranteed to be an invalid uid_t (see setreuid(2))
@ -131,10 +131,10 @@ static CommandLineSettings parseArguments(const char* program, int argc, char**
switch (opt) {
case 'h':
printHelpFlag(program);
exit(0);
return STATUS_OK_EXIT;
case 'V':
printVersionFlag(program);
exit(0);
return STATUS_OK_EXIT;
case 's':
assert(optarg); /* please clang analyzer, cause optarg can be NULL in the 'u' case */
if (String_eq(optarg, "help")) {
@ -143,29 +143,29 @@ static CommandLineSettings parseArguments(const char* program, int argc, char**
const char* description = Process_fields[j].description;
if (name) printf("%19s %s\n", name, description);
}
exit(0);
return STATUS_OK_EXIT;
}
flags.sortKey = 0;
flags->sortKey = 0;
for (int j = 1; j < LAST_PROCESSFIELD; j++) {
if (Process_fields[j].name == NULL)
continue;
if (String_eq(optarg, Process_fields[j].name)) {
flags.sortKey = j;
flags->sortKey = j;
break;
}
}
if (flags.sortKey == 0) {
if (flags->sortKey == 0) {
fprintf(stderr, "Error: invalid column \"%s\".\n", optarg);
exit(1);
return STATUS_ERROR_EXIT;
}
break;
case 'd':
if (sscanf(optarg, "%16d", &(flags.delay)) == 1) {
if (flags.delay < 1) flags.delay = 1;
if (flags.delay > 100) flags.delay = 100;
if (sscanf(optarg, "%16d", &(flags->delay)) == 1) {
if (flags->delay < 1) flags->delay = 1;
if (flags->delay > 100) flags->delay = 100;
} else {
fprintf(stderr, "Error: invalid delay value \"%s\".\n", optarg);
exit(1);
return STATUS_ERROR_EXIT;
}
break;
case 'u':
@ -177,30 +177,30 @@ static CommandLineSettings parseArguments(const char* program, int argc, char**
}
if (!username) {
flags.userId = geteuid();
} else if (!Action_setUserOnly(username, &(flags.userId))) {
flags->userId = geteuid();
} else if (!Action_setUserOnly(username, &(flags->userId))) {
for (const char *itr = username; *itr; ++itr)
if (!isdigit((unsigned char)*itr)) {
fprintf(stderr, "Error: invalid user \"%s\".\n", username);
exit(1);
return STATUS_ERROR_EXIT;
}
flags.userId = atol(username);
flags->userId = atol(username);
}
break;
}
case 'C':
flags.useColors = false;
flags->useColors = false;
break;
case 'M':
#ifdef HAVE_GETMOUSE
flags.enableMouse = false;
flags->enableMouse = false;
#endif
break;
case 'U':
flags.allowUnicode = false;
flags->allowUnicode = false;
break;
case 't':
flags.treeView = true;
flags->treeView = true;
break;
case 'p': {
assert(optarg); /* please clang analyzer, cause optarg can be NULL in the 'u' case */
@ -208,14 +208,14 @@ static CommandLineSettings parseArguments(const char* program, int argc, char**
char* saveptr;
const char* pid = strtok_r(argCopy, ",", &saveptr);
if (!flags.pidMatchList) {
flags.pidMatchList = Hashtable_new(8, false);
if (!flags->pidMatchList) {
flags->pidMatchList = Hashtable_new(8, false);
}
while(pid) {
unsigned int num_pid = atoi(pid);
// deepcode ignore CastIntegerToAddress: we just want a non-NUll pointer here
Hashtable_put(flags.pidMatchList, num_pid, (void *) 1);
// deepcode ignore CastIntegerToAddress: we just want a non-NULL pointer here
Hashtable_put(flags->pidMatchList, num_pid, (void *) 1);
pid = strtok_r(NULL, ",", &saveptr);
}
free(argCopy);
@ -224,7 +224,7 @@ static CommandLineSettings parseArguments(const char* program, int argc, char**
}
case 'F': {
assert(optarg);
free_and_xStrdup(&flags.commFilter, optarg);
free_and_xStrdup(&flags->commFilter, optarg);
break;
}
case 'H': {
@ -234,28 +234,30 @@ static CommandLineSettings parseArguments(const char* program, int argc, char**
delay = argv[optind++];
}
if (delay) {
if (sscanf(delay, "%16d", &(flags.highlightDelaySecs)) == 1) {
if (flags.highlightDelaySecs < 1)
flags.highlightDelaySecs = 1;
if (sscanf(delay, "%16d", &(flags->highlightDelaySecs)) == 1) {
if (flags->highlightDelaySecs < 1)
flags->highlightDelaySecs = 1;
} else {
fprintf(stderr, "Error: invalid highlight delay value \"%s\".\n", delay);
exit(1);
return STATUS_ERROR_EXIT;
}
}
flags.highlightChanges = true;
flags->highlightChanges = true;
break;
}
case 128:
flags.readonly = true;
flags->readonly = true;
break;
default:
if (Platform_getLongOption(opt, argc, argv) == false)
exit(1);
break;
default: {
CommandLineStatus status;
if ((status = Platform_getLongOption(opt, argc, argv)) != STATUS_OK)
return status;
break;
}
}
}
return flags;
return STATUS_OK;
}
static void CommandLine_delay(ProcessList* pl, unsigned long millisec) {
@ -288,12 +290,17 @@ int CommandLine_run(const char* name, int argc, char** argv) {
else
setlocale(LC_CTYPE, "");
CommandLineSettings flags = parseArguments(name, argc, argv);
CommandLineStatus status = STATUS_OK;
CommandLineSettings flags = { 0 };
if ((status = parseArguments(name, argc, argv, &flags)) != STATUS_OK)
return status != STATUS_OK_EXIT ? 1 : 0;
if (flags.readonly)
Settings_enableReadonly();
Platform_init();
if (!Platform_init())
return 1;
Process_setupColumnWidths();

View File

@ -8,6 +8,11 @@ Released under the GNU GPLv2+, see the COPYING file
in the source distribution for its full text.
*/
typedef enum {
STATUS_OK,
STATUS_ERROR_EXIT,
STATUS_OK_EXIT
} CommandLineStatus;
int CommandLine_run(const char* name, int argc, char** argv);

View File

@ -126,7 +126,7 @@ static double Platform_nanosecondsPerMachTick = 1.0;
static double Platform_nanosecondsPerSchedulerTick = -1;
void Platform_init(void) {
bool Platform_init(void) {
Platform_nanosecondsPerMachTick = Platform_calculateNanosecondsPerMachTick();
// Determine the number of scheduler clock ticks per second
@ -139,6 +139,8 @@ void Platform_init(void) {
const double nanos_per_sec = 1e9;
Platform_nanosecondsPerSchedulerTick = nanos_per_sec / scheduler_ticks_per_sec;
return true;
}
// Converts ticks in the Mach "timebase" to nanoseconds.

View File

@ -19,6 +19,7 @@ in the source distribution for its full text.
#include "NetworkIOMeter.h"
#include "ProcessLocksScreen.h"
#include "SignalsPanel.h"
#include "CommandLine.h"
#include "darwin/DarwinProcess.h"
#include "generic/gettime.h"
#include "generic/hostname.h"
@ -33,7 +34,7 @@ extern const unsigned int Platform_numberOfSignals;
extern const MeterClass* const Platform_meterTypes[];
void Platform_init(void);
bool Platform_init(void);
// Converts ticks in the Mach "timebase" to nanoseconds.
// See `mach_timebase_info`, as used to define the `Platform_nanosecondsPerMachTick` constant.
@ -87,8 +88,8 @@ static inline void Platform_getRelease(char** string) {
static inline void Platform_longOptionsUsage(ATTR_UNUSED const char* name) { }
static inline bool Platform_getLongOption(ATTR_UNUSED int opt, ATTR_UNUSED int argc, ATTR_UNUSED char** argv) {
return false;
static inline CommandLineStatus Platform_getLongOption(ATTR_UNUSED int opt, ATTR_UNUSED int argc, ATTR_UNUSED char** argv) {
return STATUS_ERROR_EXIT;
}
static inline void Platform_gettime_realtime(struct timeval* tv, uint64_t* msec) {

View File

@ -105,8 +105,9 @@ const MeterClass* const Platform_meterTypes[] = {
NULL
};
void Platform_init(void) {
bool Platform_init(void) {
/* no platform-specific setup needed */
return true;
}
void Platform_done(void) {

View File

@ -23,6 +23,7 @@ in the source distribution for its full text.
#include "Process.h"
#include "ProcessLocksScreen.h"
#include "SignalsPanel.h"
#include "CommandLine.h"
#include "generic/gettime.h"
#include "generic/hostname.h"
#include "generic/uname.h"
@ -36,7 +37,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);
@ -78,8 +79,8 @@ static inline void Platform_getRelease(char** string) {
static inline void Platform_longOptionsUsage(ATTR_UNUSED const char* name) { }
static inline bool Platform_getLongOption(ATTR_UNUSED int opt, ATTR_UNUSED int argc, ATTR_UNUSED char** argv) {
return false;
static inline CommandLineStatus Platform_getLongOption(ATTR_UNUSED int opt, ATTR_UNUSED int argc, ATTR_UNUSED char** argv) {
return STATUS_ERROR_EXIT;
}
static inline void Platform_gettime_realtime(struct timeval* tv, uint64_t* msec) {

View File

@ -127,8 +127,9 @@ const MeterClass* const Platform_meterTypes[] = {
NULL
};
void Platform_init(void) {
bool Platform_init(void) {
/* no platform-specific setup needed */
return true;
}
void Platform_done(void) {

View File

@ -19,6 +19,7 @@ in the source distribution for its full text.
#include "Process.h"
#include "ProcessLocksScreen.h"
#include "SignalsPanel.h"
#include "CommandLine.h"
#include "generic/gettime.h"
#include "generic/hostname.h"
#include "generic/uname.h"
@ -32,7 +33,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);
@ -78,8 +79,8 @@ static inline void Platform_getRelease(char** string) {
static inline void Platform_longOptionsUsage(ATTR_UNUSED const char* name) { }
static inline bool Platform_getLongOption(ATTR_UNUSED int opt, ATTR_UNUSED int argc, ATTR_UNUSED char** argv) {
return false;
static inline CommandLineStatus Platform_getLongOption(ATTR_UNUSED int opt, ATTR_UNUSED int argc, ATTR_UNUSED char** argv) {
return STATUS_ERROR_EXIT;
}
static inline void Platform_gettime_realtime(struct timeval* tv, uint64_t* msec) {

View File

@ -835,7 +835,7 @@ void Platform_longOptionsUsage(const char* name)
#endif
}
bool Platform_getLongOption(int opt, int argc, char** argv) {
CommandLineStatus Platform_getLongOption(int opt, int argc, char** argv) {
#ifndef HAVE_LIBCAP
(void) argc;
(void) argv;
@ -858,16 +858,16 @@ bool Platform_getLongOption(int opt, int argc, char** argv) {
Platform_capabilitiesMode = CAP_MODE_STRICT;
} else {
fprintf(stderr, "Error: invalid capabilities mode \"%s\".\n", mode);
exit(1);
return STATUS_ERROR_EXIT;
}
return true;
return STATUS_OK;
}
#endif
default:
break;
}
return false;
return STATUS_ERROR_EXIT;
}
#ifdef HAVE_LIBCAP
@ -956,20 +956,22 @@ static int dropCapabilities(enum CapMode mode) {
}
#endif
void Platform_init(void) {
bool Platform_init(void) {
#ifdef HAVE_LIBCAP
if (dropCapabilities(Platform_capabilitiesMode) < 0)
exit(1);
return false;
#endif
if (access(PROCDIR, R_OK) != 0) {
fprintf(stderr, "Error: could not read procfs (compiled to look in %s).\n", PROCDIR);
exit(1);
return false;
}
#ifdef HAVE_SENSORS_SENSORS_H
LibSensors_init();
#endif
return true;
}
void Platform_done(void) {

View File

@ -27,6 +27,7 @@ in the source distribution for its full text.
#include "ProcessLocksScreen.h"
#include "RichString.h"
#include "SignalsPanel.h"
#include "CommandLine.h"
#include "generic/gettime.h"
#include "generic/hostname.h"
#include "generic/uname.h"
@ -45,8 +46,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);
void Platform_setBindings(Htop_Action* keys);
@ -100,7 +100,7 @@ static inline void Platform_getRelease(char** string) {
void Platform_longOptionsUsage(const char* name);
bool Platform_getLongOption(int opt, int argc, char** argv);
CommandLineStatus Platform_getLongOption(int opt, int argc, char** argv);
static inline void Platform_gettime_realtime(struct timeval* tv, uint64_t* msec) {
Generic_gettime_realtime(tv, msec);

View File

@ -174,8 +174,9 @@ const MeterClass* const Platform_meterTypes[] = {
NULL
};
void Platform_init(void) {
bool Platform_init(void) {
/* no platform-specific setup needed */
return true;
}
void Platform_done(void) {

View File

@ -24,6 +24,7 @@ in the source distribution for its full text.
#include "Process.h"
#include "ProcessLocksScreen.h"
#include "SignalsPanel.h"
#include "CommandLine.h"
#include "generic/gettime.h"
#include "generic/hostname.h"
#include "generic/uname.h"
@ -42,7 +43,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);
@ -82,8 +83,8 @@ static inline void Platform_getRelease(char** string) {
static inline void Platform_longOptionsUsage(ATTR_UNUSED const char* name) { }
static inline bool Platform_getLongOption(ATTR_UNUSED int opt, ATTR_UNUSED int argc, ATTR_UNUSED char** argv) {
return false;
static inline CommandLineStatus Platform_getLongOption(ATTR_UNUSED int opt, ATTR_UNUSED int argc, ATTR_UNUSED char** argv) {
return STATUS_ERROR_EXIT;
}
static inline void Platform_gettime_realtime(struct timeval* tv, uint64_t* msec) {

View File

@ -121,8 +121,9 @@ const MeterClass* const Platform_meterTypes[] = {
NULL
};
void Platform_init(void) {
bool Platform_init(void) {
/* no platform-specific setup needed */
return true;
}
void Platform_done(void) {

View File

@ -20,6 +20,7 @@ in the source distribution for its full text.
#include "Process.h"
#include "ProcessLocksScreen.h"
#include "SignalsPanel.h"
#include "CommandLine.h"
#include "generic/gettime.h"
#include "generic/hostname.h"
#include "generic/uname.h"
@ -34,7 +35,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);
@ -76,8 +77,8 @@ static inline void Platform_getRelease(char** string) {
static inline void Platform_longOptionsUsage(ATTR_UNUSED const char* name) { }
static inline bool Platform_getLongOption(ATTR_UNUSED int opt, ATTR_UNUSED int argc, ATTR_UNUSED char** argv) {
return false;
static inline CommandLineStatus Platform_getLongOption(ATTR_UNUSED int opt, ATTR_UNUSED int argc, ATTR_UNUSED char** argv) {
return STATUS_ERROR_EXIT;
}
static inline void Platform_gettime_realtime(struct timeval* tv, uint64_t* msec) {

View File

@ -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) {

View File

@ -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;

View File

@ -122,8 +122,9 @@ const MeterClass* const Platform_meterTypes[] = {
NULL
};
void Platform_init(void) {
bool Platform_init(void) {
/* no platform-specific setup needed */
return true;
}
void Platform_done(void) {

View File

@ -35,6 +35,7 @@ in the source distribution for its full text.
#include "NetworkIOMeter.h"
#include "ProcessLocksScreen.h"
#include "SignalsPanel.h"
#include "CommandLine.h"
#include "generic/gettime.h"
#include "generic/hostname.h"
#include "generic/uname.h"
@ -59,7 +60,7 @@ extern const ProcessField Platform_defaultFields[];
extern const MeterClass* const Platform_meterTypes[];
void Platform_init(void);
bool Platform_init(void);
void Platform_done(void);
@ -105,8 +106,8 @@ static inline void Platform_getRelease(char** string) {
static inline void Platform_longOptionsUsage(ATTR_UNUSED const char* name) { }
static inline bool Platform_getLongOption(ATTR_UNUSED int opt, ATTR_UNUSED int argc, ATTR_UNUSED char** argv) {
return false;
static inline CommandLineStatus Platform_getLongOption(ATTR_UNUSED int opt, ATTR_UNUSED int argc, ATTR_UNUSED char** argv) {
return STATUS_ERROR_EXIT;
}
static inline void Platform_gettime_realtime(struct timeval* tv, uint64_t* msec) {

View File

@ -68,8 +68,9 @@ const MeterClass* const Platform_meterTypes[] = {
static const char Platform_unsupported[] = "unsupported";
void Platform_init(void) {
bool Platform_init(void) {
/* no platform-specific setup needed */
return true;
}
void Platform_done(void) {

View File

@ -15,6 +15,7 @@ in the source distribution for its full text.
#include "NetworkIOMeter.h"
#include "ProcessLocksScreen.h"
#include "SignalsPanel.h"
#include "CommandLine.h"
#include "generic/gettime.h"
#include "unsupported/UnsupportedProcess.h"
@ -27,7 +28,7 @@ extern const ProcessField Platform_defaultFields[];
extern const MeterClass* const Platform_meterTypes[];
void Platform_init(void);
bool Platform_init(void);
void Platform_done(void);
@ -65,8 +66,8 @@ void Platform_getRelease(char** string);
static inline void Platform_longOptionsUsage(ATTR_UNUSED const char* name) { }
static inline bool Platform_getLongOption(ATTR_UNUSED int opt, ATTR_UNUSED int argc, ATTR_UNUSED char** argv) {
return false;
static inline CommandLineStatus Platform_getLongOption(ATTR_UNUSED int opt, ATTR_UNUSED int argc, ATTR_UNUSED char** argv) {
return STATUS_ERROR_EXIT;
}
static inline void Platform_gettime_realtime(struct timeval* tv, uint64_t* msec) {