libamxp  1.4.0
Patterns C Implementation
test_syssig.c
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** SPDX-License-Identifier: BSD-2-Clause-Patent
4 **
5 ** SPDX-FileCopyrightText: Copyright (c) 2023 SoftAtHome
6 **
7 ** Redistribution and use in source and binary forms, with or without modification,
8 ** are permitted provided that the following conditions are met:
9 **
10 ** 1. Redistributions of source code must retain the above copyright notice,
11 ** this list of conditions and the following disclaimer.
12 **
13 ** 2. Redistributions in binary form must reproduce the above copyright notice,
14 ** this list of conditions and the following disclaimer in the documentation
15 ** and/or other materials provided with the distribution.
16 **
17 ** Subject to the terms and conditions of this license, each copyright holder
18 ** and contributor hereby grants to those receiving rights under this license
19 ** a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable
20 ** (except for failure to satisfy the conditions of this license) patent license
21 ** to make, have made, use, offer to sell, sell, import, and otherwise transfer
22 ** this software, where such license applies only to those patent claims, already
23 ** acquired or hereafter acquired, licensable by such copyright holder or contributor
24 ** that are necessarily infringed by:
25 **
26 ** (a) their Contribution(s) (the licensed copyrights of copyright holders and
27 ** non-copyrightable additions of contributors, in source or binary form) alone;
28 ** or
29 **
30 ** (b) combination of their Contribution(s) with the work of authorship to which
31 ** such Contribution(s) was added by such copyright holder or contributor, if,
32 ** at the time the Contribution is added, such addition causes such combination
33 ** to be necessarily infringed. The patent license shall not apply to any other
34 ** combinations which include the Contribution.
35 **
36 ** Except as expressly stated above, no rights or licenses from any copyright
37 ** holder or contributor is granted under this license, whether expressly, by
38 ** implication, estoppel or otherwise.
39 **
40 ** DISCLAIMER
41 **
42 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
43 ** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 ** ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
46 ** LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
48 ** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
49 ** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50 ** OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
51 ** USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52 **
53 ****************************************************************************/
54 #include <sys/time.h>
55 #include <sys/resource.h>
56 
57 #include <string.h>
58 #include <stdlib.h>
59 #include <stdio.h>
60 #include <stdarg.h>
61 #include <stddef.h>
62 #include <setjmp.h>
63 #include <inttypes.h>
64 #include <limits.h>
65 #include <unistd.h>
66 #include <fcntl.h>
67 #include <cmocka.h>
68 
69 #include <amxc/amxc_variant_type.h>
70 #include <amxc/amxc_variant.h>
71 
72 #include <amxp/amxp_syssig.h>
73 #include <amxp/variant_siginfo.h>
74 #include <amxp_signal_priv.h>
75 
76 #include "test_syssig.h"
77 
78 #include <amxc/amxc_macros.h>
79 int sig_pipe[20] = { -1, -1 };
80 int count = 0;
81 
82 int __wrap_signalfd(int fd, const sigset_t* mask, int flags);
83 
84 static void test_slot_sigchld(const char* const sig_name,
85  const amxc_var_t* const data,
86  UNUSED void* const priv) {
87  const amxp_siginfo_t* siginfo = amxc_var_constcast(amxp_siginfo_t, data);
88 
89  assert_ptr_not_equal(siginfo, NULL);
90  assert_string_equal(sig_name, strsignal(SIGCHLD));
91  assert_int_equal(siginfo->ssi_signo, SIGCHLD);
92  assert_int_equal(siginfo->ssi_pid, 5012);
93 
94  count++;
95 }
96 
97 int __wrap_signalfd(int fd, UNUSED const sigset_t* mask, UNUSED int flags) {
98  if(fd == -1) {
99  assert_int_equal(pipe(sig_pipe), 0);
100  } else {
101  assert_int_equal(fd, sig_pipe[0]);
102  }
103 
104  return sig_pipe[0];
105 }
106 
107 void test_syssig_get_fd(UNUSED void** state) {
108  int fd = amxp_syssig_get_fd();
109 
110  assert_int_equal(fd, sig_pipe[0]);
111 }
112 
113 void test_syssig_enable(UNUSED void** state) {
114  sigset_t mask;
115  assert_int_not_equal(amxp_syssig_enable(AMXP_SYSSIG_MAX + 1, true), 0);
116 
117  assert_int_equal(amxp_syssig_enable(SIGINT, true), 0);
118  assert_int_equal(amxp_syssig_enable(SIGTERM, true), 0);
119  assert_int_equal(amxp_syssig_enable(SIGTERM, true), 0);
120  assert_int_not_equal(amxp_syssig_enable(SIGKILL, true), 0);
121  assert_int_not_equal(amxp_syssig_enable(SIGSTOP, true), 0);
122 
123  assert_true(amxp_syssig_is_enabled(SIGINT));
124  assert_true(amxp_syssig_is_enabled(SIGTERM));
125  assert_false(amxp_syssig_is_enabled(SIGKILL));
126  assert_false(amxp_syssig_is_enabled(SIGSTOP));
127 
128  sigprocmask(SIG_BLOCK, NULL, &mask);
129  assert_int_equal(sigismember(&mask, SIGINT), 1);
130  assert_int_equal(sigismember(&mask, SIGTERM), 1);
131 
132  assert_false(amxp_syssig_is_enabled(AMXP_SYSSIG_MAX + 1));
133 
134  assert_int_equal(amxp_syssig_enable(SIGINT, false), 0);
135  assert_int_equal(amxp_syssig_enable(SIGTERM, false), 0);
136  assert_int_equal(amxp_syssig_enable(SIGTERM, false), 0);
137 
138  assert_false(amxp_syssig_is_enabled(SIGINT));
139  assert_false(amxp_syssig_is_enabled(SIGTERM));
140 
141  sigprocmask(SIG_BLOCK, NULL, &mask);
142  assert_int_equal(sigismember(&mask, SIGINT), 0);
143  assert_int_equal(sigismember(&mask, SIGTERM), 0);
144 }
145 
146 void test_syssig_read(UNUSED void** state) {
147  amxp_siginfo_t siginfo = {
148  .ssi_signo = SIGCHLD,
149  .ssi_errno = 0,
150  .ssi_code = 123,
151  .ssi_pid = 5012,
152  .ssi_uid = 100
153  };
154 
155  count = 0;
156  assert_int_equal(amxp_syssig_enable(SIGCHLD, true), 0);
157  assert_int_equal(amxp_slot_connect(NULL, strsignal(SIGCHLD), NULL, test_slot_sigchld, NULL), 0);
158  assert_int_equal(write(sig_pipe[1], &siginfo, sizeof(amxp_siginfo_t)), sizeof(amxp_siginfo_t));
159  assert_int_equal(amxp_syssig_read(), 0);
160  assert_int_equal(count, 1);
161 }
static sigset_t mask
Definition: amxp_syssig.c:68
Ambiorix Linux Signal Handling.
#define UNUSED
Definition: main.c:68
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
bool amxp_syssig_is_enabled(const int sigid)
Checks if a system signal is being monitored.
Definition: amxp_syssig.c:97
#define AMXP_SYSSIG_MAX
Defines highest possible system signal.
Definition: amxp_syssig.h:95
int amxp_syssig_get_fd(void)
Returns a file descriptor that can be used in an eventloop.
Definition: amxp_syssig.c:110
int amxp_syssig_read(void)
Reads from the file descriptor.
Definition: amxp_syssig.c:114
int amxp_syssig_enable(const int sigid, const bool enable)
Enables or disables monitoring of a system signal.
Definition: amxp_syssig.c:70
void test_syssig_read(UNUSED void **state)
Definition: test_syssig.c:146
static void test_slot_sigchld(const char *const sig_name, const amxc_var_t *const data, UNUSED void *const priv)
Definition: test_syssig.c:84
void test_syssig_enable(UNUSED void **state)
Definition: test_syssig.c:113
int sig_pipe[20]
Definition: test_syssig.c:79
void test_syssig_get_fd(UNUSED void **state)
Definition: test_syssig.c:107
int __wrap_signalfd(int fd, const sigset_t *mask, int flags)
int count
Definition: test_syssig.c:80
struct signalfd_siginfo amxp_siginfo_t