#include /* getpid() */ #include /* read(), getpid() */ #include #include #include #include #include #include /* perror() */ #include "utils.h" using namespace std; // exit codes, compatible with spamassassin (not with qmail-queue) const int sa_good(0); const int sa_spam(1); const int sa_usage(64); int verbosity(0); 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; string progname; string progid; int mypid; void dump(const string var){ char* str = getenv(var.c_str()); cerr << progid << " " << var; if (str) cerr << " is set to '" << str << "'" << endl; else cerr << " is not set." << endl; } void countsome(const int unit){ char buf[3000]; int total(0); for (;;) { int rslt = read(unit, buf, sizeof(buf)); if (verbosity) cerr << "hi-test: count: unit " << unit << " read returns " << rslt << endl; if (rslt <= 0) break; total += rslt; } cerr << progid << " read " << total << " bytes from unit " << unit << endl; } int main(int _argc, const char** _argv){ int snooze(0); int status(0); int killmode(0); int countmode(0); int argc(_argc); const char **argv(_argv); { progname = *argv; mypid = getpid(); stringstream binder; binder << "+++++ " << basename(progname) << "[" << mypid << "]"; progid = binder.str(); } 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, "-exit")) { if (!argc) { cerr << "Option -exit requires an argument" << endl; exit(sa_usage); } status = atoi(*argv); argv++; argc--; continue; } if (prefix(arg, "-kill")) { killmode++; continue; } if (prefix(arg, "-count")) { countmode++; 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 << progid << " group: " << getpgid(0); char* foo = getenv("HI_Q_GROUP"); if (foo) cerr << " HI_Q_GROUP: " << foo; cerr << endl; sleep(snooze); if (countmode) { countsome(0); countsome(1); } if (killmode) exeunt(status); exit(status); }