libamxp  1.4.0
Patterns C Implementation
amxp_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 
55 #ifndef _GNU_SOURCE
56 #define _GNU_SOURCE
57 #endif
58 
59 #include <stdlib.h>
60 #include <unistd.h>
61 #include <string.h>
62 
63 #include <amxc/amxc_macros.h>
64 #include <amxc/amxc.h>
65 #include <amxp/amxp.h>
66 
67 static int sig_fd = -1;
68 static sigset_t mask;
69 
70 int amxp_syssig_enable(const int sigid, const bool enable) {
71  int retval = -1;
72  amxp_signal_t* signal = NULL;
73  sigset_t change_mask;
74  when_true(sigid > AMXP_SYSSIG_MAX, exit);
75  when_true(sigid == SIGKILL || sigid == SIGSTOP, exit);
76 
77  sigemptyset(&change_mask);
78  sigaddset(&change_mask, sigid);
79 
80  amxp_signal_new(NULL, &signal, strsignal(sigid));
81 
82  if(enable) {
83  when_failed(sigprocmask(SIG_BLOCK, &change_mask, NULL), exit);
84  sigaddset(&mask, sigid);
85  } else {
86  when_failed(sigprocmask(SIG_UNBLOCK, &change_mask, NULL), exit);
87  sigdelset(&mask, sigid);
88  }
89 
90  sig_fd = signalfd(sig_fd, &mask, SFD_NONBLOCK | SFD_CLOEXEC);
91  retval = 0;
92 
93 exit:
94  return retval;
95 }
96 
97 bool amxp_syssig_is_enabled(const int sigid) {
98  bool enabled = false;
99  int inset = -1;
100  when_true(sigid > AMXP_SYSSIG_MAX, exit);
101 
102  inset = sigismember(&mask, sigid);
103  when_true(inset < 0, exit);
104 
105  enabled = (inset == 1);
106 exit:
107  return enabled;
108 }
109 
111  return sig_fd;
112 }
113 
114 int amxp_syssig_read(void) {
115  int retval = -1;
116  struct signalfd_siginfo si;
117  ssize_t res = 0;
118  amxc_var_t sig_var;
119  int len = sizeof(amxp_siginfo_t);
120  amxp_signal_t* signal = NULL;
121 
122  amxc_var_init(&sig_var);
123 
124  res = read(sig_fd, &si, len);
125  when_true(res != len, exit);
126 
127  amxc_var_set(amxp_siginfo_t, &sig_var, &si);
128 
129  signal = amxp_sigmngr_find_signal(NULL, strsignal(si.ssi_signo));
130  when_null(signal, exit);
131  amxp_signal_trigger(signal, &sig_var);
132 
133  retval = 0;
134 
135 exit:
136  amxc_var_clean(&sig_var);
137  return retval;
138 }
139 
140 CONSTRUCTOR_LVL(200) static void amxp_syssig_init(void) {
141  sigemptyset(&mask);
142  sig_fd = signalfd(sig_fd, &mask, SFD_NONBLOCK | SFD_CLOEXEC);
143 }
144 
145 DESTRUCTOR_LVL(200) static void amxp_syssig_cleanup(void) {
146  close(sig_fd);
147  sigemptyset(&mask);
148 }
static int sig_fd
Definition: amxp_syssig.c:67
static sigset_t mask
Definition: amxp_syssig.c:68
DESTRUCTOR_LVL(200)
Definition: amxp_syssig.c:145
CONSTRUCTOR_LVL(200)
Definition: amxp_syssig.c:140
amxp_signal_t * amxp_sigmngr_find_signal(const amxp_signal_mngr_t *const sig_mngr, const char *name)
Get the pointer to the signal.
Definition: amxp_signal.c:475
int amxp_signal_new(amxp_signal_mngr_t *sig_mngr, amxp_signal_t **signal, const char *name)
Constructor function, creates a new signal.
Definition: amxp_signal.c:620
void amxp_signal_trigger(amxp_signal_t *const signal, const amxc_var_t *const data)
Triggers a signal.
Definition: amxp_signal.c:691
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
Structure containing the signal information.
Definition: amxp_signal.h:119
struct signalfd_siginfo amxp_siginfo_t