Skip to content

Commit 7397247

Browse files
committed
WIP: Add a process manager "class"
In many places there's a need to keep track of sub-processes spawned by the main process. Such processes need to be looked up by various parameters, interacted with, and, eventually, terminated. Having a common, reliable and fast implementation with high test coverage that holds the various pieces of information about sub-processes together is beneficial. Ticket: CFE-3572 Changelog: None
1 parent 6092009 commit 7397247

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed

libutils/proc_manager.h

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
Copyright 2021 Northern.tech AS
3+
4+
This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
5+
6+
This program is free software; you can redistribute it and/or modify it
7+
under the terms of the GNU General Public License as published by the
8+
Free Software Foundation; version 3.
9+
10+
This program is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
GNU General Public License for more details.
14+
15+
You should have received a copy of the GNU General Public License
16+
along with this program; if not, write to the Free Software
17+
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
18+
19+
To the extent this program is licensed as part of the Enterprise
20+
versions of CFEngine, the applicable Commercial Open Source License
21+
(COSL) may apply to this file if you as a licensee so wish it. See
22+
included file COSL.txt.
23+
*/
24+
25+
#ifndef CFENGINE_PROC_MANAGER_H
26+
#define CFENGINE_PROC_MANAGER_H
27+
28+
#include <sys/types.h>
29+
30+
/**
31+
* Struct to hold information about a subprocess.
32+
*/
33+
struct _Subprocess {
34+
char *id; /** unique ID of the subprocess [always not %NULL] */
35+
char *cmd; /** command used to create the subprocess [can be %NULL] */
36+
char *description; /** human-friendly description of the subprocess [can be %NULL] */
37+
pid_t pid; /** PID of the subprocess [always >= 0] */
38+
int in_fd; /** FD of the input to the subprocess [-1 if N/A] */
39+
int out_fd; /** FD of the output from the subprocess [-1 if N/A] */
40+
char lookup_fd; /** which FD to use for lookup by FD ['i', 'o' or '\0'] */
41+
};
42+
typedef struct _Subprocess Subprocess;
43+
44+
/**
45+
* Destroy a subprocess.
46+
*/
47+
void SubprocessDestroy(Subprocess *proc);
48+
49+
typedef struct _ProcManager ProcManager;
50+
51+
/**
52+
* Function to terminate a subprocess.
53+
*
54+
* @param subprocess_pid PID of the subprocess to terminate
55+
* @param in_fd FD of the input to the subprocess
56+
* @param out_fd FD of the output from the subprocess
57+
* @param data custom data passed to the function through the termination functions
58+
* @return whether the termination was successful or not
59+
*/
60+
typedef bool (*ProcessTerminator) (pid_t subprocess_pid, int in_fd, int out_fd, void *data);
61+
62+
/**
63+
* Get a new ProcManager.
64+
*/
65+
ProcManager *ProcManagerNew();
66+
67+
/**
68+
* Destroy a ProcManager.
69+
*/
70+
void ProcManagerDestroy();
71+
72+
/**
73+
* Add a subprocess to a ProcManager.
74+
*
75+
* @param manager the ProcManager to add the process to [cannot be %NULL]
76+
* @param id ID of the process to add [string(pid) is used if %NULL]
77+
* @param cmd command used to create the subprocess [can be %NULL]
78+
* @param description human-friendly description of the subprocess [can be %NULL]
79+
* @param pid PID of the subprocess [required to be >=0]
80+
* @param in_fd FD of the input to the subprocess [-1 if N/A]
81+
* @param out_fd FD of the output from the subprocess [-1 if N/A]
82+
* @param lookup_fd which FD to use for lookup by FD ['i', 'o' or '\0']
83+
*
84+
* @note Ownership of #id, #cmd and #description is transferred to the #manager.
85+
*/
86+
bool ProcManagerAddProcess(ProcManager *manager,
87+
char *id, char *cmd, char *description,
88+
pid_t pid, int in_fd, int out_fd, char lookup_fd);
89+
90+
/**
91+
* Getter functions for #ProcManager.
92+
*/
93+
Subprocess *ProcManagerGetProcessByPID(ProcManager *manager, pid_t pid);
94+
Subprocess *ProcManagerGetProcessById(ProcManager *manager, const char *id);
95+
Subprocess *ProcManagerGetProcessByFD(ProcManager *manager, int fd);
96+
97+
/**
98+
* Functions to get and remove subprocesses from a #ProcManager.
99+
*
100+
* @note Destroy the returned #Subprocess with SubprocessDestroy().
101+
*/
102+
Subprocess *ProcManagerPopProcessByPID(ProcManager *manager, pid_t pid);
103+
Subprocess *ProcManagerPopProcessById(ProcManager *manager, const char *id);
104+
Subprocess *ProcManagerPopProcessByFD(ProcManager *manager, int fd);
105+
106+
/**
107+
* Termination functions for ProcManager.
108+
*
109+
* @param data arbitrary data passed to the #terminator function
110+
*
111+
* If the #terminator function returns %false, the default hard termination is
112+
* attempted (closing FDs and doing kill -9). Once the subprocess is terminated,
113+
* or the hard termination is attempted, the subprocess is removed from the
114+
* #ProcManager.
115+
*/
116+
bool ProcManagerTerminateProcessById(ProcManager *manager, const char *id,
117+
ProcessTerminator *terminator, void *data);
118+
bool ProcManagerTerminateProcessByFD(ProcManager *manager, int fd,
119+
ProcessTerminator *terminator, void *data);
120+
bool ProcManagerTerminateAllProcesses(ProcManager *manager,
121+
ProcessTerminator *terminator, void *data);
122+
123+
#endif /* CFENGINE_PROC_MANAGER_H */

0 commit comments

Comments
 (0)