libamxp  1.4.0
Patterns C Implementation
test_proc_ctrl.c File Reference
#include <stdlib.h>
#include <stdio.h>
#include <setjmp.h>
#include <stdarg.h>
#include <cmocka.h>
#include <string.h>
#include <amxc/amxc.h>
#include <amxp/amxp.h>
#include "test_proc_ctrl.h"
#include <amxc/amxc_macros.h>

Go to the source code of this file.

Macros

#define _GNU_SOURCE
 

Functions

int __wrap_kill (pid_t pid, int sig)
 
FILE * __wrap_fopen (const char *pathname, const char *mode)
 
FILE * __real_fopen (const char *pathname, const char *mode)
 
ssize_t __wrap_getline (char **lineptr, size_t *n, FILE *stream)
 
ssize_t __real_getline (char **lineptr, size_t *n, FILE *stream)
 
int __wrap_amxc_var_init (amxc_var_t *const var)
 
int __real_amxc_var_init (amxc_var_t *const var)
 
static void read_sigalrm (void)
 
static void handle_events (void)
 
static int test_cmd_builder (amxc_array_t *cmd, UNUSED amxc_var_t *settings)
 
int __wrap_kill (UNUSED pid_t pid, UNUSED int sig)
 
void test_proc_ctrl_new_delete (UNUSED void **state)
 
void test_proc_ctrl_start_stop (UNUSED void **state)
 
void test_proc_is_stopped_when_timer_expires (UNUSED void **state)
 
void test_proc_is_stopped_when_timer_is_set_after_start (UNUSED void **state)
 
void test_proc_can_stop_timer (UNUSED void **state)
 
void test_proc_can_stop_children (UNUSED void **state)
 

Variables

static amxc_string_t path
 

Macro Definition Documentation

◆ _GNU_SOURCE

#define _GNU_SOURCE

Definition at line 56 of file test_proc_ctrl.c.

Function Documentation

◆ __real_amxc_var_init()

int __real_amxc_var_init ( amxc_var_t *const  var)

◆ __real_fopen()

FILE* __real_fopen ( const char *  pathname,
const char *  mode 
)

◆ __real_getline()

ssize_t __real_getline ( char **  lineptr,
size_t *  n,
FILE *  stream 
)

◆ __wrap_amxc_var_init()

int __wrap_amxc_var_init ( amxc_var_t *const  var)

Definition at line 142 of file test_proc_ctrl.c.

142  {
143  int rv = mock();
144 
145  if(rv == 0) {
146  rv = __real_amxc_var_init(var);
147  }
148 
149  return rv;
150 }
int __real_amxc_var_init(amxc_var_t *const var)

◆ __wrap_fopen()

FILE * __wrap_fopen ( const char *  pathname,
const char *  mode 
)

Definition at line 129 of file test_proc_ctrl.c.

129  {
130  const char* f = amxc_string_get(&path, 0);
131  if(f != NULL) {
132  if(strcmp(pathname, f) == 0) {
133  return __real_fopen("./test_child_pids.txt", mode);
134  } else {
135  return __real_fopen(pathname, mode);
136  }
137  } else {
138  return __real_fopen(pathname, mode);
139  }
140 }
FILE * __real_fopen(const char *pathname, const char *mode)
static amxc_string_t path

◆ __wrap_getline()

ssize_t __wrap_getline ( char **  lineptr,
size_t *  n,
FILE *  stream 
)

Definition at line 152 of file test_proc_ctrl.c.

152  {
153  ssize_t rv = mock();
154 
155  if(rv == 0) {
156  return __real_getline(lineptr, n, stream);
157  }
158 
159  return rv;
160 }
ssize_t __real_getline(char **lineptr, size_t *n, FILE *stream)

◆ __wrap_kill() [1/2]

int __wrap_kill ( pid_t  pid,
int  sig 
)

◆ __wrap_kill() [2/2]

int __wrap_kill ( UNUSED pid_t  pid,
UNUSED int  sig 
)

Definition at line 125 of file test_proc_ctrl.c.

125  {
126  return 0;
127 }

◆ handle_events()

static void handle_events ( void  )
static

Definition at line 106 of file test_proc_ctrl.c.

106  {
107  printf("Handling events ");
108  while(amxp_signal_read() == 0) {
109  printf(".");
110  }
111  printf("\n");
112 }
int amxp_signal_read(void)
Reads from the amxp signal file descriptor.
Definition: amxp_signal.c:769

◆ read_sigalrm()

static void read_sigalrm ( void  )
static

Definition at line 85 of file test_proc_ctrl.c.

85  {
86  sigset_t mask;
87  int sfd;
88  struct signalfd_siginfo fdsi;
89  ssize_t s;
90 
91  sigemptyset(&mask);
92  sigaddset(&mask, SIGALRM);
93 
94  sigprocmask(SIG_BLOCK, &mask, NULL);
95 
96  sfd = signalfd(-1, &mask, 0);
97  s = read(sfd, &fdsi, sizeof(struct signalfd_siginfo));
98  assert_int_equal(s, sizeof(struct signalfd_siginfo));
99  if(fdsi.ssi_signo == SIGALRM) {
100  printf("Got SIGALRM\n");
101  } else {
102  printf("Read unexpected signal\n");
103  }
104 }
static sigset_t mask
Definition: amxp_syssig.c:68

◆ test_cmd_builder()

static int test_cmd_builder ( amxc_array_t *  cmd,
UNUSED amxc_var_t *  settings 
)
static

Definition at line 114 of file test_proc_ctrl.c.

114  {
115  int rv = mock();
116 
117  if(rv == 0) {
118  amxc_array_append_data(cmd, strdup("sleep "));
119  amxc_array_append_data(cmd, strdup("10m"));
120  }
121 
122  return rv;
123 }

◆ test_proc_can_stop_children()

void test_proc_can_stop_children ( UNUSED void **  state)

Definition at line 302 of file test_proc_ctrl.c.

302  {
303  amxc_var_t settings;
304  amxp_proc_ctrl_t* ctrl = NULL;
305 
306  amxc_string_init(&path, 0);
307  will_return_always(__wrap_amxc_var_init, 0);
308  will_return_always(__wrap_getline, 0);
309  will_return_always(test_cmd_builder, 0);
310 
311  amxc_var_init(&settings);
312 
313  assert_int_equal(amxp_proc_ctrl_new(&ctrl, test_cmd_builder), 0);
314  assert_int_equal(amxp_proc_ctrl_start(ctrl, 0, &settings), 0);
315 
316  amxc_string_setf(&path, "/proc/%d/task/%d/children", ctrl->proc->pid, ctrl->proc->pid);
317  assert_int_equal(amxp_proc_ctrl_get_child_pids(ctrl), 3);
319 
320  amxp_proc_ctrl_delete(&ctrl);
321  amxc_string_clean(&path);
322  amxc_var_clean(&settings);
323 }
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.
int amxp_proc_ctrl_start(amxp_proc_ctrl_t *proc, uint32_t minutes, amxc_var_t *settings)
Launches the child process.
void amxp_proc_ctrl_delete(amxp_proc_ctrl_t **proc)
Clean-up and frees previously allocated memory.
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.
Structure containing the child process control.
amxp_subproc_t * proc
static int test_cmd_builder(amxc_array_t *cmd, UNUSED amxc_var_t *settings)
int __wrap_amxc_var_init(amxc_var_t *const var)
ssize_t __wrap_getline(char **lineptr, size_t *n, FILE *stream)

◆ test_proc_can_stop_timer()

void test_proc_can_stop_timer ( UNUSED void **  state)

Definition at line 276 of file test_proc_ctrl.c.

276  {
277  amxc_var_t settings;
278  amxp_proc_ctrl_t* ctrl = NULL;
279 
280  will_return_always(__wrap_amxc_var_init, 0);
281  will_return_always(__wrap_getline, 0);
282 
283  amxc_var_init(&settings);
284 
285  assert_int_equal(amxp_proc_ctrl_new(&ctrl, test_cmd_builder), 0);
286 
287  will_return(test_cmd_builder, 0);
288  assert_int_equal(amxp_proc_ctrl_start(ctrl, 1, &settings), 0);
289  assert_true(ctrl->proc->is_running);
290  assert_int_equal(amxp_timer_get_state(ctrl->timer), amxp_timer_running);
293  assert_int_equal(amxp_timer_get_state(ctrl->timer), amxp_timer_running);
294 
296  assert_int_equal(amxp_timer_get_state(ctrl->timer), amxp_timer_off);
297 
298  amxp_proc_ctrl_delete(&ctrl);
299  amxc_var_clean(&settings);
300 }
void amxp_proc_ctrl_set_active_duration(amxp_proc_ctrl_t *proc, uint32_t minutes)
Sets the active time durations.
amxp_timer_state_t amxp_timer_get_state(amxp_timer_t *timer)
Get the timer's state.
Definition: amxp_timer.c:334
void amxp_timers_check(void)
Check all timers and call the callback function when the timer is in Timer expired state.
Definition: amxp_timer.c:183
void amxp_timers_calculate(void)
Caclulates the remaining time of all timers.
Definition: amxp_timer.c:144
@ amxp_timer_off
Definition: amxp_timer.h:149
@ amxp_timer_running
Definition: amxp_timer.h:151
amxp_timer_t * timer
bool is_running
Definition: amxp_subproc.h:90

◆ test_proc_ctrl_new_delete()

void test_proc_ctrl_new_delete ( UNUSED void **  state)

Definition at line 162 of file test_proc_ctrl.c.

162  {
163  amxp_proc_ctrl_t* ctrl = NULL;
164 
165  will_return(__wrap_amxc_var_init, 0);
166  assert_int_equal(amxp_proc_ctrl_new(&ctrl, test_cmd_builder), 0);
167  assert_non_null(ctrl);
168  assert_non_null(ctrl->proc);
169  assert_non_null(ctrl->timer);
170  assert_int_equal(amxc_var_type_of(&ctrl->child_proc_pids), AMXC_VAR_ID_NULL);
171 
172  amxp_proc_ctrl_delete(&ctrl);
173  assert_null(ctrl);
174 
175  will_return(__wrap_amxc_var_init, -1);
176  assert_int_not_equal(amxp_proc_ctrl_new(&ctrl, test_cmd_builder), 0);
177  assert_null(ctrl);
178 
179  assert_int_not_equal(amxp_proc_ctrl_new(NULL, test_cmd_builder), 0);
180  assert_int_not_equal(amxp_proc_ctrl_new(&ctrl, NULL), 0);
181  assert_null(ctrl);
182  amxp_proc_ctrl_delete(NULL);
183 }
amxc_var_t child_proc_pids

◆ test_proc_ctrl_start_stop()

void test_proc_ctrl_start_stop ( UNUSED void **  state)

Definition at line 185 of file test_proc_ctrl.c.

185  {
186  amxc_var_t settings;
187  amxp_proc_ctrl_t* ctrl = NULL;
188 
189  will_return_always(__wrap_amxc_var_init, 0);
190  will_return_always(__wrap_getline, 0);
191 
192  amxc_var_init(&settings);
193 
194  assert_int_equal(amxp_proc_ctrl_new(&ctrl, test_cmd_builder), 0);
195 
196  will_return(test_cmd_builder, 0);
197  assert_int_equal(amxp_proc_ctrl_start(ctrl, 0, &settings), 0);
198  assert_true(ctrl->proc->is_running);
199 
200  assert_int_equal(amxp_proc_ctrl_stop(ctrl), 0);
201  assert_false(ctrl->proc->is_running);
202 
203  will_return(test_cmd_builder, -1);
204  assert_int_not_equal(amxp_proc_ctrl_start(ctrl, 0, &settings), 0);
205 
206  will_return(test_cmd_builder, 0);
207  assert_int_equal(amxp_proc_ctrl_start(ctrl, 0, NULL), 0);
208  assert_true(ctrl->proc->is_running);
209 
210  assert_int_equal(amxp_proc_ctrl_stop(ctrl), 0);
211  handle_events();
212  assert_false(ctrl->proc->is_running);
213 
214  amxp_proc_ctrl_delete(&ctrl);
215 
216  assert_int_not_equal(amxp_proc_ctrl_start(NULL, 0, &settings), 0);
217  assert_int_not_equal(amxp_proc_ctrl_stop(NULL), 0);
218 
219  amxc_var_clean(&settings);
220 }
int amxp_proc_ctrl_stop(amxp_proc_ctrl_t *proc)
Stops the child process.
static void handle_events(void)

◆ test_proc_is_stopped_when_timer_expires()

void test_proc_is_stopped_when_timer_expires ( UNUSED void **  state)

Definition at line 222 of file test_proc_ctrl.c.

222  {
223  amxc_var_t settings;
224  amxp_proc_ctrl_t* ctrl = NULL;
225 
226  will_return_always(__wrap_amxc_var_init, 0);
227  will_return_always(__wrap_getline, 0);
228 
229  amxc_var_init(&settings);
230 
231  assert_int_equal(amxp_proc_ctrl_new(&ctrl, test_cmd_builder), 0);
232 
233  will_return(test_cmd_builder, 0);
234  assert_int_equal(amxp_proc_ctrl_start(ctrl, 1, &settings), 0);
235  assert_true(ctrl->proc->is_running);
236 
237  read_sigalrm();
240  handle_events();
241 
242  assert_false(amxp_subproc_is_running(ctrl->proc));
243 
244  amxp_proc_ctrl_delete(&ctrl);
245  amxc_var_clean(&settings);
246 }
bool amxp_subproc_is_running(const amxp_subproc_t *const subproc)
Checks if the child process is running.
Definition: amxp_subproc.c:423
static void read_sigalrm(void)

◆ test_proc_is_stopped_when_timer_is_set_after_start()

void test_proc_is_stopped_when_timer_is_set_after_start ( UNUSED void **  state)

Definition at line 248 of file test_proc_ctrl.c.

248  {
249  amxc_var_t settings;
250  amxp_proc_ctrl_t* ctrl = NULL;
251 
252  will_return_always(__wrap_amxc_var_init, 0);
253  will_return_always(__wrap_getline, 0);
254 
255  amxc_var_init(&settings);
256 
257  assert_int_equal(amxp_proc_ctrl_new(&ctrl, test_cmd_builder), 0);
258 
259  will_return(test_cmd_builder, 0);
260  assert_int_equal(amxp_proc_ctrl_start(ctrl, 0, &settings), 0);
261  assert_true(ctrl->proc->is_running);
262 
264 
265  read_sigalrm();
268  handle_events();
269 
270  assert_false(amxp_subproc_is_running(ctrl->proc));
271 
272  amxp_proc_ctrl_delete(&ctrl);
273  amxc_var_clean(&settings);
274 }

Variable Documentation

◆ path

amxc_string_t path
static

Definition at line 71 of file test_proc_ctrl.c.