From b0f78b272c97b7cc3ff1fbe6c48fd2151d039613 Mon Sep 17 00:00:00 2001 From: John Denker Date: Sun, 15 Jul 2012 05:35:46 -0700 Subject: more complete implementation, including test fixtures --- tools/hi-test.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 84 insertions(+), 1 deletion(-) (limited to 'tools/hi-test.c') diff --git a/tools/hi-test.c b/tools/hi-test.c index d74b3c9..50e4a76 100644 --- a/tools/hi-test.c +++ b/tools/hi-test.c @@ -1,11 +1,94 @@ #include #include +#include +#include + +#include /* perror() */ using namespace std; -int main(){ + +// exit codes, compatible with spamassassin (not with qmail-queue) +const int sa_good(0); +const int sa_spam(1); +const int sa_usage(64); + +//////////////// +// little utility to help with argument parsing: +// +int prefix(const string shorter, const string longer){ + return shorter == longer.substr(0, shorter.length()); +} + +void exeunt(const int sts){ + if (sts == sa_good) exit(sts); + + const char* foo = getenv("HI_Q_GROUP"); + if (!foo) exit(sts); + +// No point in signalling ourself: + sighandler_t rslt = signal(SIGUSR1, SIG_IGN); + if (rslt == SIG_ERR) { + cerr << "error setting signal" << endl; + } + int k = kill(-atoi(foo), SIGUSR1); + if (k) { + cerr << "kill failed on group " << atoi(foo) << " ... "; + perror(0); + } + exit(sts); +} + +using namespace std; + +int main(int _argc, const char** _argv){ + int snooze(0); + int status(0); + int argc(_argc); + const char **argv(_argv); + string progname(*argv); argv++; argc--; + + while (argc) { + string arg(*argv); argv++; argc--; + if (arg.substr(0,2) == "--") arg = arg.substr(1); + if (prefix(arg, "-help")) { +// usage(0); + } + if (prefix(arg, "-snooze")) { + if (!argc) { + cerr << "Option -snooze requires an argument" << endl; + exit(sa_usage); + } + snooze = atoi(*argv); argv++; argc--; + continue; + } + if (prefix(arg, "-kill")) { + if (!argc) { + cerr << "Option -kill requires an argument" << endl; + exit(sa_usage); + } + status = atoi(*argv); argv++; argc--; + continue; + } + if (arg.substr(0,1) == "x") { + continue; + } + if (arg.substr(0,1) == "-") { + cerr << "Unrecognized option '" << arg << "'" << endl; + cerr << "For help, try: " << progname << " -help" << endl; + exit(sa_usage); + } else { + cerr << "Extraneous verbiage '" << arg << "'" << endl; + cerr << "For help, try: " << progname << " -help" << endl; + exit(sa_usage); + } + } + + cerr << "++++ hi-test pid: " << getpid() << " group: " << getpgid(0); char* foo = getenv("HI_Q_GROUP"); if (foo) cerr << " HI_Q_GROUP: " << foo; cerr << endl; + sleep(snooze); + exeunt(status); } -- cgit v1.2.3