File: tsig-0.C

package info (click to toggle)
socket%2B%2B 1.12.13%2Bgit20131030.5d039ba-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 668 kB
  • sloc: cpp: 3,988; makefile: 116; sh: 22; ansic: 14
file content (86 lines) | stat: -rw-r--r-- 2,185 bytes parent folder | download | duplicates (5)
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
// socket++ library. tsig-0.C
// Copyright (C) 1992-1996 Gnanasekaran Swaminathan <gs4t@virginia.edu>
//
// Permission is granted to use at your own risk and distribute this software
// in source and  binary forms provided  the above copyright notice and  this
// paragraph are  preserved on all copies.  This software is provided "as is"
// with no express or implied warranty.

#include <iostream>
#include <socket++/sig.h>
#include <unistd.h>

using namespace std;

  class hnd: public sig::hnd {
    public:
    int id;

    hnd (int signo): sig::hnd (signo), id (signo) {}
    hnd (int signo, int i): sig::hnd (signo), id (i) {}
    void operator () (int s) {
      cout << "handler for " << id << endl;
      cout << "Sleeping .." << endl;
      sleep (5);
      cout << "..finished" << endl;
	  if (sig::nal.ispending (s)) {
			cout << "Signal pendig" << endl;
		}
	
      // cout << "Pending signals = " << hex << sig::nal.pending () << dec << endl; // commented out by LN
			sigset_t set;
			for (unsigned int i=0; i<_SIGSET_NWORDS; i++) {
				cout << "sigset[" << i << "]: " << set.__val[i] << endl;
			}
			for (unsigned int i=1; i<15; i++) {
				cout << "ispending(" << i << "): " << sig::nal.ispending (i) << endl;
			}
      cout << "called by " << s << endl;
    }
  };

int main (int ac, char** av)
{

  hnd sterm0 (SIGTERM);
  hnd sterm1 (SIGTERM, SIGTERM * 100);
  hnd sint (SIGINT);
  hnd susr1 (SIGUSR1);
  hnd susr2 (SIGUSR2);
  hnd squit (SIGQUIT);

  /*
  sig::nal.set (SIGTERM, &sterm0);
  sig::nal.set (SIGTERM, &sterm1);
  sig::nal.set (SIGINT, &sint);
  sig::nal.set (SIGUSR1, &susr1);
  sig::nal.set (SIGUSR2, &susr2);
  sig::nal.set (SIGQUIT, &squit);
  */

	cout << "SIGSET_NWORDS: " << _SIGSET_NWORDS << endl;
  try {
    hnd illegal (300);
  }
  catch (sigerr e) {
    //cout << "sigerr exception thrown by illegal(300)\n";
		cout << "catched..." << endl;
	}

  sig::nal.mask (SIGTERM, SIGUSR1);
  sig::nal.mask (SIGTERM, SIGUSR2);

  pid_t pid = getpid ();

  cout << "send SIGUSR2\n";
  kill (pid, SIGTERM);

  sleep (20);
  sig::nal.unmask (SIGTERM, SIGUSR2);
  
  cout << "send SIGUSR2 again\n";
  kill (pid, SIGTERM);

  sleep (300);
  return 0;
}