libamxp  1.4.0
Patterns C Implementation
main.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 #include <stdlib.h>
56 #include <stdio.h>
57 #include <string.h>
58 
59 #include <event2/event.h>
60 
61 #include <amxc/amxc.h>
62 
63 #include <amxp/amxp_signal.h>
64 #include <amxp/variant_siginfo.h>
65 #include <amxp/amxp_syssig.h>
66 #include <amxp/amxp_slot.h>
67 
68 #define UNUSED __attribute__((unused))
69 
70 static struct event_base* base = NULL;
71 
72 static void signal_cb(UNUSED evutil_socket_t fd,
73  UNUSED short event,
74  UNUSED void* arg) {
76 }
77 
78 static void slot_sigint(UNUSED const char* const sig_name,
79  UNUSED const amxc_var_t* const data,
80  UNUSED void* priv) {
81  printf("Signal SIGINT recieved\n\n");
82 }
83 
84 static void slot_sigterm(UNUSED const char* const sig_name,
85  UNUSED const amxc_var_t* const data,
86  UNUSED void* priv) {
87  printf("Signal SIGTERM recieved - stop eventloop\n\n");
88  event_base_loopbreak(base);
89 }
90 
91 static void slot_system_signal(const char* const sig_name,
92  const amxc_var_t* const data,
93  UNUSED void* priv) {
94  const amxp_siginfo_t* siginfo = amxc_var_constcast(amxp_siginfo_t, data);
95  printf("Signal recieved [%s]\n", sig_name);
96  printf("\tssi_signo = %u\n", siginfo->ssi_signo);
97  printf("\tssi_errno = %d\n", siginfo->ssi_errno);
98  printf("\tssi_code = %d\n", siginfo->ssi_code);
99  printf("\tssi_pid = %u\n", siginfo->ssi_pid);
100  printf("\tssi_uid = %u\n", siginfo->ssi_uid);
101  printf("\tssi_fd = %d\n", siginfo->ssi_fd);
102  printf("\tssi_tid = %u\n", siginfo->ssi_tid);
103  printf("\tssi_band = %u\n", siginfo->ssi_band);
104  printf("\tssi_overrun = %u\n", siginfo->ssi_overrun);
105  printf("\tssi_trapno = %u\n", siginfo->ssi_trapno);
106  printf("\tssi_status = %u\n", siginfo->ssi_status);
107  printf("\tssi_int = %u\n", siginfo->ssi_int);
108  printf("\n");
109 }
110 
111 int main(void) {
112  int ret = 0;
113  int sig_fd = -1;
114  struct event* sig_event = NULL;
115 
116  base = event_base_new();
117  if(base == NULL) {
118  exit(1);
119  }
120 
122  sig_event = event_new(base, sig_fd, EV_READ | EV_PERSIST, signal_cb, NULL);
123 
124  amxp_syssig_enable(SIGINT, true);
125  amxp_syssig_enable(SIGTERM, true);
126 
127  amxp_slot_connect(NULL, strsignal(SIGINT), NULL, slot_system_signal, NULL);
128  amxp_slot_connect(NULL, strsignal(SIGTERM), NULL, slot_system_signal, NULL);
129 
130  amxp_slot_connect(NULL, strsignal(SIGINT), NULL, slot_sigint, NULL);
131  amxp_slot_connect(NULL, strsignal(SIGTERM), NULL, slot_sigterm, NULL);
132 
133  event_add(sig_event, NULL);
134 
135  event_base_dispatch(base);
136 
137  event_del(sig_event);
138  event_free(sig_event);
139  event_base_free(base);
140 
141  return ret;
142 }
Ambiorix signal manager and signal API header file.
Ambiorix slot API header file.
static int sig_fd
Definition: amxp_syssig.c:67
Ambiorix Linux Signal Handling.
int main(void)
Definition: main.c:99
static void slot_system_signal(const char *const sig_name, const amxc_var_t *const data, __attribute__((unused)) void *priv)
Definition: main.c:91
static void signal_cb(__attribute__((unused)) evutil_socket_t fd, __attribute__((unused)) short event, __attribute__((unused)) void *arg)
Definition: main.c:72
static struct event_base * base
Definition: main.c:70
static void slot_sigterm(__attribute__((unused)) const char *const sig_name, __attribute__((unused)) const amxc_var_t *const data, __attribute__((unused)) void *priv)
Definition: main.c:84
static void slot_sigint(__attribute__((unused)) const char *const sig_name, __attribute__((unused)) const amxc_var_t *const data, __attribute__((unused)) void *priv)
Definition: main.c:78
#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
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
struct signalfd_siginfo amxp_siginfo_t