summaryrefslogtreecommitdiff
path: root/tools/hi-test.c
blob: 47128a9473e4c180f5ac5cde3d497b4f4ae6e541 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108

#include <iostream>
#include <stdlib.h>
#include <string>
#include <signal.h>

#include <stdio.h>              /* perror() */

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

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

string progname;

void dump(const string var){
  char* str = getenv(var.c_str());
  cerr << progname << ": " << var;
  if (str) cerr << " is set to '" << str << "'" << endl;
  else  cerr << " is not set." << endl;
}

int main(int _argc, const char** _argv){
  int snooze(0);
  int status(0);
  int killmode(0);
  int argc(_argc);
  const char **argv(_argv);
  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, "-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 (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);
  if (killmode) exeunt(status);
  exit(status);
}