File: fork.cpp

package info (click to toggle)
gdcm 2.4.4-3%2Bdeb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 32,912 kB
  • ctags: 52,166
  • sloc: cpp: 188,527; ansic: 124,526; xml: 41,799; sh: 7,162; python: 3,667; cs: 2,128; java: 1,344; lex: 1,290; tcl: 677; php: 128; makefile: 116
file content (204 lines) | stat: -rw-r--r-- 4,785 bytes parent folder | download | duplicates (3)
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Fork.C -*- C++ -*- socket library
// 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.
//
// Version: 12Jan97 1.11

#ifndef WIN32

#include <config.h>

#include <iostream>
#include <stdio.h> // perror in solaris2.3 is declared here
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <sys/wait.h>

#include <socket++/fork.h>

using std::cerr;
using std::endl;

Fork::ForkProcess* Fork::ForkProcess::list = 0;
Fork::KillForks Fork::killall;

Fork::~Fork ()
{
  if (process->pid <= 0)
    delete process;
}

Fork::KillForks::~KillForks ()
  // First, kill all children whose kill_child flag is set.
  // Second, wait for other children to die.
{
  for (ForkProcess* cur = Fork::ForkProcess::list; cur; cur = cur->next)
    if (cur->kill_child)
      delete cur;

  while (Fork::ForkProcess::list && wait (0) > 0) {}
}

Fork::ForkProcess::ForkProcess (bool kill, bool give_reason)
  : kill_child (kill), reason (give_reason), next (0)
{
  if (list == 0) {
    struct sigaction sa;
    sa.sa_handler = (void(*)(int)) sighnd (&Fork::ForkProcess::reaper_nohang);
    sigemptyset (&sa.sa_mask);
    sa.sa_flags = SA_RESTART;
    sigaction (SIGCHLD, &sa, 0);
  }

  pid = fork ();

  if (pid > 0) {
    next = list;
    list = this;
  } else if (pid == 0) {
    // child process. clear list
    ForkProcess* p = list;
    while (p) {
      ForkProcess* nxt = p->next;
      p->pid = 0;
      delete p;
      p = nxt;
    }
    list = 0;

    if (kill_child) {
      struct sigaction sa;
      sa.sa_handler = (void(*)(int)) sighnd (&Fork::ForkProcess::commit_suicide);
      sigemptyset (&sa.sa_mask);
      sa.sa_flags = SA_RESTART;
      sigaction (SIGTERM, &sa, 0);
    }
  }
}

Fork::ForkProcess::~ForkProcess ()
{
  if (pid > 0) {
    if (kill_child)
      kill (pid, SIGTERM);
    reap_child ();

    // I remove myself from list
    if (list == this)
      list = list->next;
    else {
      for (ForkProcess* p = list; p; p = p->next)
	if (p->next == this) {
	  p->next = next;
	  break;
	}
    }
  }
}

void Fork::ForkProcess::kill_process () const
{
  if (pid > 0) {
    kill (pid, SIGKILL);
    reap_child ();
  }
}

void Fork::ForkProcess::reap_child () const
{
  int status;
  if (pid > 0 && waitpid (pid, &status, 0) == pid && reason)
    infanticide_reason (pid, status);
}

void Fork::ForkProcess::infanticide_reason (pid_t pid, int status)
{
  if (pid <= 0)
    return;

#ifdef SOCKETXX_HAVE_STRSIGNAL
  if (WIFSTOPPED (status))
    cerr << "process " << pid << " gets "
      << strsignal(WSTOPSIG (status)) << endl;
  else if (WIFEXITED (status))
    cerr << "process " << pid << " exited with status "
      << WEXITSTATUS (status) << endl;
  else if (WIFSIGNALED (status))
    cerr << "process " << pid << " got "
      << strsignal(WTERMSIG (status)) << endl;
#else
  if (WIFSTOPPED (status))
    cerr << "process " << pid << " gets "
      << SYS_SIGLIST [WSTOPSIG (status)] << endl;
  else if (WIFEXITED (status))
    cerr << "process " << pid << " exited with status "
      << WEXITSTATUS (status) << endl;
  else if (WIFSIGNALED (status))
    cerr << "process " << pid << " got "
      << SYS_SIGLIST [WTERMSIG (status)] << endl;
#endif // SOCKETXX_HAVE_STRSIGNAL
}

void Fork::ForkProcess::reaper_nohang (int signo)
{
  if (signo != SIGCHLD)
    return;

  int status;
  pid_t wpid;
  if ((wpid = waitpid (-1, &status, WNOHANG)) > 0) {
    ForkProcess* prev = 0;
    ForkProcess* cur  = list;
    while (cur) {
      if (cur->pid == wpid) {
	cur->pid = -1;
	if (prev)
	  prev->next = cur->next;
	else
	  list = list->next;
	
	if (cur->reason)
	  infanticide_reason (wpid, status);

	delete cur;
	break;
      }
      prev = cur;
      cur  = cur->next;
    }
  }
}

void Fork::ForkProcess::commit_suicide (int)
{
  // if this process has any children we kill them.

  ForkProcess* p = list;
  while (p) {
    ForkProcess* next = p->next;
    if (!p->kill_child) // otherwise ForkProcess::~ForkProcess will take care
      kill (p->pid, SIGKILL);
    delete p; // ForkProcess::~ForkProcess will call reap_child ().
    p = next;
  }

  exit (0x0f);
}

void Fork::suicide_signal (int signo)
     // commit suicide at the signal signo
{
  struct sigaction sa;
  sa.sa_handler = (void(*)(int)) sighnd (&Fork::ForkProcess::commit_suicide);
  sigemptyset (&sa.sa_mask);
  sa.sa_flags = 0;
  if (sigaction (signo, &sa, 0) == -1)
    perror ("Fork: Cannot commit suicide with the specified signal");
}
#endif //windows does not get fork