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 #include <unistd.h>
59 
60 #include <event2/event.h>
61 
62 #include <amxc/amxc.h>
63 #include <amxp/amxp_signal.h>
64 #include <amxp/amxp_slot.h>
65 #include <amxp/amxp_syssig.h>
66 #include <amxp/amxp_subproc.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 child_stdout_cb(evutil_socket_t fd,
79  UNUSED short event,
80  UNUSED void* arg) {
81  char buffer[1024] = {0};
82  ssize_t size = read(fd, buffer, 1023);
83 
84  if(size > 0) {
85  buffer[size + 1] = 0;
86  printf("%s", buffer);
87  }
88 }
89 
90 static void slot_proc_stop(const char* const sig_name,
91  const amxc_var_t* const data,
92  UNUSED void* priv) {
93  int pid = amxc_var_dyncast(int64_t, amxc_var_get_key(data, "PID", AMXC_VAR_FLAG_DEFAULT));
94  printf("AMX Signal recieved : %s\n", sig_name);
95  printf(" => process id = %d\n", pid);
96  event_base_loopbreak(base);
97 }
98 
99 int main(void) {
100  int ret = 0;
101  int sig_fd = -1;
102  struct event* sig_event = NULL;
103 
104  int child_stdout_fd = -1;
105  struct event* child_stdout_event = NULL;
106  amxp_subproc_t* proc = NULL;
107  char* cmd[] = { "ls", "-la", NULL };
108 
109  // allocate new proc object and connect to stop signal
110  if(amxp_subproc_new(&proc) != 0) {
111  exit(1);
112  }
113  amxp_slot_connect(amxp_subproc_get_sigmngr(proc), "stop", NULL, slot_proc_stop, NULL);
114 
115  // allocate new eventloop
116  base = event_base_new();
117  if(base == NULL) {
118  exit(2);
119  }
120 
121  // add file descriptors to event loop
122  // add signal fd to eventloop
123  sig_fd = amxp_syssig_get_fd(); // needed for SIGCHLD
124  sig_event = event_new(base, sig_fd, EV_READ | EV_PERSIST, signal_cb, NULL);
125  event_add(sig_event, NULL);
126  // add child proc stdout to eventloop
127  child_stdout_fd = amxp_subproc_open_fd(proc, STDOUT_FILENO);
128  child_stdout_event = event_new(base, child_stdout_fd, EV_READ | EV_PERSIST, child_stdout_cb, NULL);
129  event_add(child_stdout_event, NULL);
130 
131  // launch the process
132  amxp_subproc_vstart(proc, cmd);
133  printf("Child process started pid = %d\n", amxp_subproc_get_pid(proc));
134 
135  // start the eventloop
136  event_base_dispatch(base);
137 
138  // process is stopped, delete proc object
139  amxp_subproc_delete(&proc);
140 
141  // cleanup event loop
142  event_del(sig_event);
143  event_free(sig_event);
144  event_del(child_stdout_event);
145  event_free(child_stdout_event);
146  event_base_free(base);
147 
148  return ret;
149 }
Ambiorix signal manager and signal API header file.
Ambiorix slot API header file.
Ambiorix sub-proccess API header file.
static int sig_fd
Definition: amxp_syssig.c:67
Ambiorix Linux Signal Handling.
static void signal_cb(__attribute__((unused)) evutil_socket_t fd, __attribute__((unused)) short event, __attribute__((unused)) void *arg)
Definition: main.c:72
static void slot_proc_stop(const char *const sig_name, const amxc_var_t *const data, __attribute__((unused)) void *priv)
Definition: main.c:90
int main(void)
Definition: main.c:99
static struct event_base * base
Definition: main.c:70
static void child_stdout_cb(evutil_socket_t fd, __attribute__((unused)) short event, __attribute__((unused)) void *arg)
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_subproc_open_fd(amxp_subproc_t *subproc, int requested)
Opens standard file descriptor to the child process.
Definition: amxp_subproc.c:263
int amxp_subproc_new(amxp_subproc_t **subproc)
Constructor function, creates a new child process data structure.
Definition: amxp_subproc.c:215
int amxp_subproc_vstart(amxp_subproc_t *const subproc, char **argv)
Start a child process.
Definition: amxp_subproc.c:288
amxp_signal_mngr_t * amxp_subproc_get_sigmngr(const amxp_subproc_t *const subproc)
Get the Signal managers of the child process.
Definition: amxp_subproc.c:419
int amxp_subproc_delete(amxp_subproc_t **subproc)
Destructor function, deletes a child process data structure.
Definition: amxp_subproc.c:239
pid_t amxp_subproc_get_pid(const amxp_subproc_t *const subproc)
Get the PID of a child process.
Definition: amxp_subproc.c:415
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
Child process information structure.
Definition: amxp_subproc.h:87