From fb6042c5cd8a411cfcfcf6c1a1d466eb42a4ff33 Mon Sep 17 00:00:00 2001 From: John Denker Date: Sat, 14 Jul 2012 23:13:38 -0700 Subject: working toward a process-group signalling system --- tools/hi-q.c | 37 +++++++++++++++++++++++----------- tools/hi-test.c | 7 +++++++ tools/hi-test.conf | 3 +++ tools/makefile | 2 +- tools/skrewt.c | 58 +++++++++++++++++++++++++++++++++++++++++++----------- 5 files changed, 83 insertions(+), 24 deletions(-) create mode 100644 tools/hi-test.c create mode 100644 tools/hi-test.conf diff --git a/tools/hi-q.c b/tools/hi-q.c index a065ad0..14ca5ec 100644 --- a/tools/hi-q.c +++ b/tools/hi-q.c @@ -197,17 +197,16 @@ int main(int argc, char** argv, char const * const * env) { } if (job.size()) filter.push_back(job); } - if (verbose) for (vector::const_iterator job = filter.begin(); - job != filter.end(); job++) { - cerr << "Filter: "; - for (VS::const_iterator token = job->begin(); - token != job->end(); token++){ + unsigned int nkids = filter.size(); + if (verbose) for (unsigned int ii = 0; ii < nkids; ii++) { + cerr << "hi-q filter[" << ii << "] :; "; + for (VS::const_iterator token = filter[ii].begin(); + token != filter[ii].end(); token++){ cerr << *token << " "; } cerr << endl; } - int nkids = filter.size(); vector kidpid(nkids); // indexed by kid number // At this point, there are some loop invariants; @@ -218,7 +217,7 @@ int main(int argc, char** argv, char const * const * env) { // doesn't choose it. That allows N-1 of the kids // to close it and dup() something useful onto it. - for (int ii=0; ii < nkids; ii++){ /* loop over all kids */ + for (unsigned int ii=0; ii < nkids; ii++){ /* loop over all kids */ int datapipe[2]; int kid_end; @@ -256,6 +255,16 @@ int main(int argc, char** argv, char const * const * env) { kidpid[ii] = fork(); if (!kidpid[ii]) { /*** child code ***/ + pid_t kidgroup(0); // process group for all kids is + // equal to pid of kid#0 + if (ii) kidgroup = kidpid[0]; + if (setpgid(0, kidgroup) != 0) { + cerr << "setpgid failed! " << errno << " ... "; + perror(0); + } + cerr << "kid [" << ii << "] starts: " << getpid() + << " kidpid[0] " << kidpid[0] + << endl; // Now that we are through creating pipes, we don't // need to continue blocking fd1: close(1); @@ -295,10 +304,14 @@ int main(int argc, char** argv, char const * const * env) { panic(ex_syserr); } close(kid_end); -#ifdef more_testing - fprintf(stderr, "forked kid #%d (%d) piping %d\n", - ii, kidpid[ii], kid_end); -// sleep(1); /* let kid run a while */ +// it is important to let kid#0 run a while; +// otherwise it won't fully exist when the other kids start, +// and depending race conditions, the setpgid could fail + if (1) usleep(0); +#if 1 + cerr << "forked kid #" << ii + << " (" << kidpid[ii] << ") " + << endl; #endif } @@ -307,7 +320,7 @@ int main(int argc, char** argv, char const * const * env) { close(0); // the reading end of stdin was // delegated to the first child - for (int ii=0; ii + +using namespace std; +int main(){ + cerr << getpid() << " : " << getpgid(0) << endl; +} diff --git a/tools/hi-test.conf b/tools/hi-test.conf new file mode 100644 index 0000000..d09ba81 --- /dev/null +++ b/tools/hi-test.conf @@ -0,0 +1,3 @@ +hi-test +hi-test +hi-test diff --git a/tools/makefile b/tools/makefile index 8de2bd4..3803e44 100644 --- a/tools/makefile +++ b/tools/makefile @@ -10,7 +10,7 @@ CC= /usr/bin/g++ -Wall -g -I $(HOME)/lib/include .SECONDARY : # do not remove any intermediate files -progs = pido hi-q skrewt +progs = pido hi-q skrewt hi-test all: $(progs) diff --git a/tools/skrewt.c b/tools/skrewt.c index 8ae4db4..b81f0ea 100644 --- a/tools/skrewt.c +++ b/tools/skrewt.c @@ -21,7 +21,8 @@ void usage(const int sts){ "\n" " Typically used as a filter in a pipeline, along with spamc -E\n" " Options\n" -" -h print this msg (and exit immediately).\n" +" -help print this msg (and exit immediately).\n" +" -maxsize ii msg size in bytes; anything bigger will be rejected.\n" "\n" " Messages containing the string '-please-bounce-this-' will be rejected.\n" " Messages with no date will be rejected.\n" @@ -82,17 +83,53 @@ string toLower(const std::string& a){ return rslt; } - +//////////////// string ltrim(string foo){ size_t where = foo.find_first_not_of(" \t\r\n"); if (where == foo.npos) return foo; return foo.substr(where); } -int main(int argc, char** argv){ - if (argc > 1) { - if (argv[1] == string("-h")) usage(0); - usage(1); +//////////////// +// little utility to help with argument parsing: +// +int prefix(const string shorter, const string longer){ + return shorter == longer.substr(0, shorter.length()); +} + +//////////////////////////////////////////////////////////// +int main(int _argc, const char** _argv){ +//// pid_t pid = getpid(); +//// cout << pid << endl; +//// cout << getpgid(pid) << endl; + int argc(_argc); + const char **argv(_argv); + string progname(*argv); argv++; argc--; + + int maxsize(1000000); + + 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, "-maxsize")) { + if (!argc) { + cerr << "Option -maxsize requires an argument" << endl; + exit(1); + } + maxsize = atoi(*argv); argv++; argc--; + } + if (arg.substr(0,1) == "-") { + cerr << "Unrecognized option '" << arg << "'" << endl; + cerr << "For help, try: " << progname << " -help" << endl; + exit(1); + } else { + cerr << "Extraneous verbiage '" << arg << "'" << endl; + cerr << "For help, try: " << progname << " -help" << endl; + exit(1); + } } int inheads(1); @@ -101,7 +138,6 @@ int main(int argc, char** argv){ int textlines(0); int gotdate(0); int msgsize(0); - int msgmax(1000000); for (;;){ if (cin.eof()) break; if (cin.bad()) return 1; @@ -119,8 +155,8 @@ int main(int argc, char** argv){ string line; if (getline(cin, line).fail()) continue; msgsize += line.length()+1; - if (msgsize > msgmax) { - cerr << "skrewt rejection: bigger than " << msgmax << endl; + if (msgsize > maxsize) { + cerr << "skrewt rejection: bigger than " << maxsize << endl; exit(1); } header += "\n" + line; @@ -185,8 +221,8 @@ int main(int argc, char** argv){ string line; if (!getline(cin, line).fail()) { msgsize += line.length()+1; - if (msgsize > msgmax) { - cerr << "skrewt rejection: bigger than " << msgmax << endl; + if (msgsize > maxsize) { + cerr << "skrewt rejection: bigger than " << maxsize << endl; exit(1); } if (line == "--" + boundary) { -- cgit v1.2.3