1 // Fork.h -*- C++ -*- socket library
2 // Copyright (C) 1992-1996 Gnanasekaran Swaminathan <gs4t@virginia.edu>
3 //
4 // Permission is granted to use at your own risk and distribute this software
5 // in source and  binary forms provided  the above copyright notice and  this
6 // paragraph are  preserved on all copies.  This software is provided "as is"
7 // with no express or implied warranty.
8 //
9 // Version: 12Jan97 1.11
10 
11 #ifndef FORK_H
12 #define FORK_H
13 
14 #ifndef WIN32
15 #include <unistd.h>
16 #include <sys/types.h>
17 #include <signal.h>
18 
19 class Fork {
20  public:
21   class KillForks {
22   public:
23     KillForks () = default;
24     ~KillForks ();
25   };
26 
27   class ForkProcess {
28     friend Fork::KillForks::~KillForks ();
29 
30     static void infanticide_reason (pid_t pid, int status);
31     static void reaper_nohang (int);
32 
33     static ForkProcess* list;
34 
35   public:
36     pid_t          pid;
37     const bool     kill_child;
38     const bool     reason;
39     ForkProcess*   next;
40 
41     ForkProcess (bool kill, bool give_reason);
42     ~ForkProcess ();
43 
44     void           kill_process () const;
45     void           reap_child () const;
46 
47     static void    commit_suicide (int);
48   };
49  private:
50   static KillForks killall;
51 
52   ForkProcess* process;
53 
54  public:
55   Fork (Fork&) = delete;
56   Fork& operator = (Fork&) = delete;
57   Fork (bool kill = false, bool reason = false)
process(new ForkProcess (kill,reason))58     : process (new ForkProcess (kill, reason)) {}
59   ~Fork ();
60 
is_child()61   int  is_child () const { return process->pid == 0; }
is_parent()62   int  is_parent () const { return process->pid > 0; }
process_id()63   int  process_id () const { return process->pid; }
64 
65   static void suicide_signal (int signo = SIGTERM);
66 };
67 
68 #endif//windows does not define fork, and never will.
69 #endif // FORK_H
70