libamxp  1.4.0
Patterns C Implementation
Process Control

Data Structures

struct  _amxp_proc_ctrl
 Structure containing the child process control. More...
 

Typedefs

typedef int(* amxp_proc_ctrl_cmd_t) (amxc_array_t *cmd, amxc_var_t *settings)
 Command builder callback function signature. More...
 
typedef struct _amxp_proc_ctrl amxp_proc_ctrl_t
 Structure containing the child process control. More...
 

Functions

int amxp_proc_ctrl_new (amxp_proc_ctrl_t **proc, amxp_proc_ctrl_cmd_t cmd_build_fn)
 Allocates and initializes an amxp_proc_ctrl_t. More...
 
void amxp_proc_ctrl_delete (amxp_proc_ctrl_t **proc)
 Clean-up and frees previously allocated memory. More...
 
int amxp_proc_ctrl_start (amxp_proc_ctrl_t *proc, uint32_t minutes, amxc_var_t *settings)
 Launches the child process. More...
 
int amxp_proc_ctrl_stop (amxp_proc_ctrl_t *proc)
 Stops the child process. More...
 
void amxp_proc_ctrl_set_active_duration (amxp_proc_ctrl_t *proc, uint32_t minutes)
 Sets the active time durations. More...
 
void amxp_proc_ctrl_stop_childs (amxp_proc_ctrl_t *proc)
 Stop all child processes of the child process. More...
 
int amxp_proc_ctrl_get_child_pids (amxp_proc_ctrl_t *proc)
 Fetches the process ids of the children of the launched child process. More...
 

Detailed Description

Child Process Control and monitor

Typedef Documentation

◆ amxp_proc_ctrl_cmd_t

typedef int(* amxp_proc_ctrl_cmd_t) (amxc_array_t *cmd, amxc_var_t *settings)

Command builder callback function signature.

A callback function is used to build the command that must be launched.

In the callback function a configuration file can be built.

An array must be filled with the command and the command arguments.

Definition at line 87 of file amxp_proc_ctrl.h.

◆ amxp_proc_ctrl_t

Structure containing the child process control.

Function Documentation

◆ amxp_proc_ctrl_delete()

void amxp_proc_ctrl_delete ( amxp_proc_ctrl_t **  proc)

Clean-up and frees previously allocated memory.

Each allocated amxp_proc_trl_t structure with amxp_proc_ctrl_new must be freed when not needed anymore using this function.

Parameters
procpointer to previously allocated amxp_proc_ctrl_t struct, will be set to NULL.

Definition at line 201 of file amxp_proc_ctrl.c.

201  {
202  when_null(proc, leave);
203  when_null(*proc, leave);
204 
205  amxp_slot_disconnect((*proc)->proc->sigmngr, "stop", amxp_proc_ctrl_stopped);
206 
207  amxp_proc_ctrl_stop(*proc);
208  amxp_subproc_delete(&(*proc)->proc);
209  amxp_timer_delete(&(*proc)->timer);
210  amxc_array_clean(&(*proc)->cmd, amxp_proc_ctrl_free_char);
211  amxc_var_clean(&(*proc)->child_proc_pids);
212 
213  free(*proc);
214  *proc = NULL;
215 
216 leave:
217  return;
218 }
static void amxp_proc_ctrl_stopped(UNUSED const char *const event_name, UNUSED const amxc_var_t *const event_data, void *const priv)
static void amxp_proc_ctrl_free_char(amxc_array_it_t *it)
int amxp_proc_ctrl_stop(amxp_proc_ctrl_t *proc)
Stops the child process.
int amxp_slot_disconnect(amxp_signal_mngr_t *const sig_mngr, const char *const sig_name, amxp_slot_fn_t fn)
Disconnects a slot from (a) signal(s).
Definition: amxp_slot.c:380
int amxp_subproc_delete(amxp_subproc_t **subproc)
Destructor function, deletes a child process data structure.
Definition: amxp_subproc.c:239
void amxp_timer_delete(amxp_timer_t **timer)
Deletes a timer.
Definition: amxp_timer.c:247

◆ amxp_proc_ctrl_get_child_pids()

int amxp_proc_ctrl_get_child_pids ( amxp_proc_ctrl_t proc)

Fetches the process ids of the children of the launched child process.

The list of found process ids is stored in the amxp_proc_ctrl_t structure.

The function returns the number of children found.

Two methods are used to find the list of children:

  • reading file /proc/[pid]/task/[tid]/children
  • scanning all processes in /proc/ and read /proc/[pid]/stat and check the field parent pid.
Parameters
procpointer to amxp_proc_trl_t structure, previously allocated with amxp_proc_ctrl_new
Returns
the number of children found

Definition at line 294 of file amxp_proc_ctrl.c.

294  {
295  int retval = -1;
296  when_null(proc, leave);
297 
298  when_false(amxp_subproc_is_running(proc->proc), leave);
300  if(retval == 0) {
301  goto leave;
302  }
303 
305 
306 leave:
307  if(retval == 0) {
308  const amxc_llist_t* list = amxc_var_constcast(amxc_llist_t, &proc->child_proc_pids);
309  retval = amxc_llist_size(list);
310  }
311  return retval;
312 }
static int amxp_proc_ctrl_get_child_pids_scan_proc(amxp_proc_ctrl_t *proc)
static int amxp_proc_ctrl_get_child_pids_proc_children(amxp_proc_ctrl_t *proc)
bool amxp_subproc_is_running(const amxp_subproc_t *const subproc)
Checks if the child process is running.
Definition: amxp_subproc.c:423
amxc_var_t child_proc_pids
amxp_subproc_t * proc

◆ amxp_proc_ctrl_new()

int amxp_proc_ctrl_new ( amxp_proc_ctrl_t **  proc,
amxp_proc_ctrl_cmd_t  cmd_build_fn 
)

Allocates and initializes an amxp_proc_ctrl_t.

Allocates memory on the heap for an amxp_proc_ctrl_t structure and initializes it.

The pointer to the allocated memory will be stored in the proc argument. The *proc must be initialized to NULL before calling this function.

To free all allocated memory use amxp_proc_ctrl_delete.

Parameters
procwill be filled with the pointer to the new allocated amxp_proc_ctrl_t structure
cmd_build_fncallback function that fills the command array
Returns
0 when successful, otherwise an error code

Definition at line 170 of file amxp_proc_ctrl.c.

170  {
171  int retval = -1;
172  when_null(proc, leave);
173  when_null(cmd_build_fn, leave);
174 
175  *proc = (amxp_proc_ctrl_t*) calloc(1, sizeof(amxp_proc_ctrl_t));
176  when_null(*proc, leave);
177 
178  when_failed(amxp_subproc_new(&(*proc)->proc), leave);
179  when_failed(amxp_timer_new(&(*proc)->timer, amxp_proc_ctrl_timer_callback, *proc), leave);
180  when_failed(amxc_array_init(&(*proc)->cmd, 10), leave);
181  when_failed(amxc_var_init(&(*proc)->child_proc_pids), leave);
182 
183  (*proc)->build = cmd_build_fn;
184  amxp_slot_connect((*proc)->proc->sigmngr, "stop", NULL, amxp_proc_ctrl_stopped, *proc);
185 
186  retval = 0;
187 
188 leave:
189  if((retval != 0) &&
190  ((proc != NULL) && (*proc != NULL))) {
191  amxp_subproc_delete(&(*proc)->proc);
192  amxp_timer_delete(&(*proc)->timer);
193  amxc_array_clean(&(*proc)->cmd, NULL);
194  amxc_var_clean(&(*proc)->child_proc_pids);
195  free(*proc);
196  *proc = NULL;
197  }
198  return retval;
199 }
static void amxp_proc_ctrl_timer_callback(UNUSED amxp_timer_t *const timer, void *data)
int amxp_slot_connect(amxp_signal_mngr_t *const sig_mngr, const char *const sig_name, const char *const expression, amxp_slot_fn_t fn, void *const priv)
Connects a slot (function) to a named signal of a signal manager.
Definition: amxp_slot.c:300
int amxp_subproc_new(amxp_subproc_t **subproc)
Constructor function, creates a new child process data structure.
Definition: amxp_subproc.c:215
int amxp_timer_new(amxp_timer_t **timer, amxp_timer_cb_t cb, void *priv)
Allocate and initializes a new timer.
Definition: amxp_timer.c:229
Structure containing the child process control.

◆ amxp_proc_ctrl_set_active_duration()

void amxp_proc_ctrl_set_active_duration ( amxp_proc_ctrl_t proc,
uint32_t  minutes 
)

Sets the active time durations.

Sets or changes the active duration.

If the process was already started with an active time (see amxp_proc_ctrl_start), the running timer is stopped and restarted with the new provided time.

If the process was running without an active time set, the timer is started with the given time and the child process will be stopped when the timer expires.

When the new time is set to 0 the timer is stopped and the child process will keep running until stopped manually.

Parameters
procpointer to amxp_proc_ctrl_t structure, previously allocated with amxp_proc_ctrl_new
minutesnew active time

Definition at line 264 of file amxp_proc_ctrl.c.

264  {
265  when_null(proc, leave);
266 
267  if(minutes == 0) {
268  amxp_timer_stop(proc->timer);
269  } else {
270  if(amxp_subproc_is_running(proc->proc)) {
271  // reset timer to new duration time
272  amxp_timer_start(proc->timer, minutes * 60 * 1000);
273  }
274  }
275 
276 leave:
277  return;
278 }
int amxp_timer_start(amxp_timer_t *timer, unsigned int timeout_msec)
Starts or resets a timer.
Definition: amxp_timer.c:296
int amxp_timer_stop(amxp_timer_t *timer)
Stops the timer.
Definition: amxp_timer.c:319
amxp_timer_t * timer

◆ amxp_proc_ctrl_start()

int amxp_proc_ctrl_start ( amxp_proc_ctrl_t proc,
uint32_t  minutes,
amxc_var_t *  settings 
)

Launches the child process.

Will call the amxp_proc_ctrl_cmd_t callback function. That callback function must fill the command array. The provided variant containing child process settings will be passed to the callback function as is.

When an active time is provided in the argument minutes, a timer is started. When the timer expires, the launched process is stopped.

When the time in minutes is set to 0, no timer is started and the child process will keep runnning until stopped.

Parameters
procpointer to amxp_proc_ctrl_t structure, previously allocated with amxp_proc_ctrl_new
minutestime in minutes the launched process can keep running
settingsa variant containing settings, passed to the callback function as is.
Returns
0 when successful, otherwise an error code

Definition at line 220 of file amxp_proc_ctrl.c.

220  {
221  int retval = -1;
222  when_null(proc, leave);
223 
224  amxc_array_clean(&proc->cmd, amxp_proc_ctrl_free_char);
225  retval = proc->build(&proc->cmd, settings);
226  when_failed(retval, leave);
227  when_true(amxc_array_is_empty(&proc->cmd), leave);
228 
229  retval = amxp_subproc_astart(proc->proc, &proc->cmd);
230  when_failed(retval, leave);
231 
232  if(minutes != 0) {
233  amxp_timer_start(proc->timer, minutes * 60 * 1000);
234  }
235 
236  retval = 0;
237 
238 leave:
239  return retval;
240 }
int amxp_subproc_astart(amxp_subproc_t *const subproc, amxc_array_t *cmd)
Start a child process.
Definition: amxp_subproc.c:365
amxp_proc_ctrl_cmd_t build
amxc_array_t cmd

◆ amxp_proc_ctrl_stop()

int amxp_proc_ctrl_stop ( amxp_proc_ctrl_t proc)

Stops the child process.

Stops the child process and the children of the child process.

Parameters
procpointer to amxp_proc_trl_t structure, previously allocated with amxp_proc_ctrl_new
Returns
0 when successful, otherwise an error code

Definition at line 242 of file amxp_proc_ctrl.c.

242  {
243  int retval = -1;
244  when_null(proc, leave);
245 
246  amxp_timer_stop(proc->timer);
247 
250 
251  amxp_subproc_kill(proc->proc, SIGTERM);
252  retval = amxp_subproc_wait(proc->proc, 2000);
253  if(retval == 1) {
254  // not stopped after waiting for 2 seconds
255  // forcekill
256  amxp_subproc_kill(proc->proc, SIGKILL);
257  retval = amxp_subproc_wait(proc->proc, 2000);
258  }
259 
260 leave:
261  return retval;
262 }
void amxp_proc_ctrl_stop_childs(amxp_proc_ctrl_t *proc)
Stop all child processes of the child process.
int amxp_proc_ctrl_get_child_pids(amxp_proc_ctrl_t *proc)
Fetches the process ids of the children of the launched child process.
int amxp_subproc_kill(const amxp_subproc_t *const subproc, const int sig)
Sends a Linux signal to the child process.
Definition: amxp_subproc.c:391
int amxp_subproc_wait(amxp_subproc_t *subproc, int timeout_msec)
Waits until the child process has stopped.
Definition: amxp_subproc.c:428

◆ amxp_proc_ctrl_stop_childs()

void amxp_proc_ctrl_stop_childs ( amxp_proc_ctrl_t proc)

Stop all child processes of the child process.

A child process monitored with the amxp_proc_ctrl can launch its own children. These child processes can be stopped using this function.

Parameters
procpointer to amxp_proc_trl_t structure, previously allocated with amxp_proc_ctrl_new

Definition at line 280 of file amxp_proc_ctrl.c.

280  {
281  when_null(proc, leave);
282 
283  if(amxc_var_type_of(&proc->child_proc_pids) == AMXC_VAR_ID_LIST) {
284  amxc_var_for_each(child_pid, (&proc->child_proc_pids)) {
285  pid_t pid = amxc_var_dyncast(uint32_t, child_pid);
286  kill(pid, SIGTERM);
287  }
288  }
289 
290 leave:
291  return;
292 }