1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1993-2021 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING.  If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if ! defined (octave_child_list_h)
27 #define octave_child_list_h 1
28 
29 #include "octave-config.h"
30 
31 #include <csignal>
32 
33 #include <sys/types.h>
34 
35 #include "base-list.h"
36 
37 namespace octave
38 {
39   class OCTAVE_API child
40   {
41   public:
42 
43     // Do whatever to handle event for child with PID (might not
44     // actually be dead, could just be stopped).  Return true if
45     // the list element corresponding to PID should be removed from
46     // list.  This function should not call any functions that modify
47     // the child_list.
48 
49     typedef bool (*child_event_handler) (pid_t, int);
50 
51     child (pid_t id = -1, child_event_handler f = nullptr)
pid(id)52       : pid (id), handler (f), have_status (0), status (0)
53     { }
54 
55     child (const child&) = default;
56 
57     child& operator = (const child&) = default;
58 
59     ~child (void) = default;
60 
61     // The process id of this child.
62     pid_t pid;
63 
64     // The function we call if an event happens for this child.
65     child_event_handler handler;
66 
67     // Nonzero if this child has stopped or terminated.
68     sig_atomic_t have_status;
69 
70     // The status of this child; 0 if running, otherwise a status value
71     // from waitpid.
72     int status;
73   };
74 
75   class OCTAVE_API child_list
76   {
77   public:
78 
child_list(void)79     child_list (void) { }
80 
81     void insert (pid_t pid, child::child_event_handler f);
82 
83     void remove (pid_t pid);
84 
85     void reap (void);
86 
87     bool wait (void);
88 
89   private:
90 
91     base_list<child> m_list;
92   };
93 }
94 
95 #endif
96