libamxp  1.4.0
Patterns C Implementation
amxp_proc_ctrl.c File Reference
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <limits.h>
#include <sys/inotify.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <amxc/amxc.h>
#include <amxc/amxc_macros.h>
#include <amxp/amxp.h>

Go to the source code of this file.

Macros

#define _GNU_SOURCE
 
#define PROC_CHILDREN   "/proc/%d/task/%d/children"
 

Functions

static void amxp_proc_ctrl_free_char (amxc_array_it_t *it)
 
static void amxp_proc_ctrl_timer_callback (UNUSED amxp_timer_t *const timer, void *data)
 
static void amxp_proc_ctrl_stopped (UNUSED const char *const event_name, UNUSED const amxc_var_t *const event_data, void *const priv)
 
static int amxp_proc_ctrl_get_child_pids_proc_children (amxp_proc_ctrl_t *proc)
 
static int amxp_proc_ctrl_get_child_pids_scan_proc (amxp_proc_ctrl_t *proc)
 
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...
 

Macro Definition Documentation

◆ _GNU_SOURCE

#define _GNU_SOURCE

Definition at line 56 of file amxp_proc_ctrl.c.

◆ PROC_CHILDREN

#define PROC_CHILDREN   "/proc/%d/task/%d/children"

Definition at line 72 of file amxp_proc_ctrl.c.

Function Documentation

◆ amxp_proc_ctrl_free_char()

static void amxp_proc_ctrl_free_char ( amxc_array_it_t *  it)
static

Definition at line 74 of file amxp_proc_ctrl.c.

74  {
75  char* txt = (char*) amxc_array_it_get_data(it);
76  free(txt);
77 }

◆ amxp_proc_ctrl_get_child_pids_proc_children()

static int amxp_proc_ctrl_get_child_pids_proc_children ( amxp_proc_ctrl_t proc)
static

Definition at line 110 of file amxp_proc_ctrl.c.

110  {
111  int retval = -1;
112  FILE* fp = NULL;
113  amxc_string_t file;
114  pid_t ppid = 0;
115  ssize_t read = 0;
116  char* line = NULL;
117  size_t len = 0;
118 
119  amxc_string_init(&file, 0);
120 
121  ppid = amxp_subproc_get_pid(proc->proc);
122  amxc_var_set_type(&proc->child_proc_pids, AMXC_VAR_ID_LIST);
123 
124  amxc_string_setf(&file, PROC_CHILDREN, ppid, ppid);
125  fp = fopen(amxc_string_get(&file, 0), "r");
126  if(fp == NULL) {
127  goto leave;
128  }
129 
130  read = getline(&line, &len, fp);
131  if(read == -1) {
132  free(line);
133  goto leave;
134  }
135 
136  amxc_var_push(ssv_string_t, &proc->child_proc_pids, line);
137  amxc_var_cast(&proc->child_proc_pids, AMXC_VAR_ID_LIST);
138 
139  retval = 0;
140 
141 leave:
142  if(fp != NULL) {
143  fclose(fp);
144  }
145  amxc_string_clean(&file);
146  return retval;
147 }
#define PROC_CHILDREN
pid_t amxp_subproc_get_pid(const amxp_subproc_t *const subproc)
Get the PID of a child process.
Definition: amxp_subproc.c:415
amxc_var_t child_proc_pids
amxp_subproc_t * proc

◆ amxp_proc_ctrl_get_child_pids_scan_proc()

static int amxp_proc_ctrl_get_child_pids_scan_proc ( amxp_proc_ctrl_t proc)
static

Definition at line 149 of file amxp_proc_ctrl.c.

149  {
150  int retval = -1;
151  amxc_llist_t children;
152 
153  amxc_llist_init(&children);
154  retval = amxp_proci_findf(&children,
155  "ppid == %d", amxp_subproc_get_pid(proc->proc));
156 
157  when_failed(retval, leave);
158  amxc_var_set_type(&proc->child_proc_pids, AMXC_VAR_ID_LIST);
159 
160  amxc_llist_iterate(it, (&children)) {
161  amxp_proc_info_t* pi = amxc_container_of(it, amxp_proc_info_t, it);
162  amxc_var_add(int32_t, &proc->child_proc_pids, pi->pid);
163  }
164 
165 leave:
166  amxc_llist_clean(&children, amxp_proci_free_it);
167  return retval;
168 }
void amxp_proci_free_it(amxc_llist_it_t *it)
Delete a amxp_proc_info_t by it is linked list iterator.
int amxp_proci_findf(amxc_llist_t *result, const char *filter,...)
Build a linked list of running processes.
Structure containing the process information.
int32_t pid

◆ amxp_proc_ctrl_stopped()

static void amxp_proc_ctrl_stopped ( UNUSED const char *const  event_name,
UNUSED const amxc_var_t *const  event_data,
void *const  priv 
)
static

Definition at line 91 of file amxp_proc_ctrl.c.

93  {
94  amxp_proc_ctrl_t* proc = (amxp_proc_ctrl_t*) priv;
95  amxc_var_t pid;
96  amxc_var_init(&pid);
97 
98  when_null(proc, leave);
99 
100  amxc_var_set(uint32_t, &pid, proc->proc->pid);
101  amxp_timer_stop(proc->timer);
103 
104  amxp_sigmngr_emit_signal(NULL, "proc:stopped", &pid);
105 
106 leave:
107  amxc_var_clean(&pid);
108 }
void amxp_proc_ctrl_stop_childs(amxp_proc_ctrl_t *proc)
Stop all child processes of the child process.
int amxp_sigmngr_emit_signal(const amxp_signal_mngr_t *const sig_mngr, const char *name, const amxc_var_t *const data)
Emits a signal.
Definition: amxp_signal.c:514
int amxp_timer_stop(amxp_timer_t *timer)
Stops the timer.
Definition: amxp_timer.c:319
Structure containing the child process control.
amxp_timer_t * timer

◆ amxp_proc_ctrl_timer_callback()

static void amxp_proc_ctrl_timer_callback ( UNUSED amxp_timer_t *const  timer,
void *  data 
)
static

Definition at line 79 of file amxp_proc_ctrl.c.

80  {
81  amxp_proc_ctrl_t* proc = (amxp_proc_ctrl_t*) data;
82  amxc_var_t pid;
83  amxc_var_init(&pid);
84  amxc_var_set(uint32_t, &pid, proc->proc->pid);
85  amxp_sigmngr_emit_signal(NULL, "proc:disable", &pid);
86  amxp_proc_ctrl_stop(proc);
87 
88  amxc_var_clean(&pid);
89 }
int amxp_proc_ctrl_stop(amxp_proc_ctrl_t *proc)
Stops the child process.