libamxrt  0.4.2
Ambiorix Run Time Library
amxrt.c File Reference
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <syslog.h>
#include <cap-ng.h>
#include <amxrt/amxrt.h>
#include "amxrt_priv.h"

Go to the source code of this file.

Macros

#define _GNU_SOURCE
 

Functions

static bool amxrt_file_exists (const char *filename)
 
static int amxrt_parse_odl_string (amxo_parser_t *parser, amxd_object_t *root)
 
static int amxrt_parse_odl_extensions (amxo_parser_t *parser, amxd_object_t *root)
 
static int amxrt_try_default_odl (amxo_parser_t *parser, amxd_object_t *root)
 
static int amxrt_parse_odl_files (amxo_parser_t *parser, int argc, char *argv[], int index, amxd_object_t *root)
 
static int amxrt_create_pid_file (pid_t pid, const char *name)
 
static void amxrt_remove_pid_file (const char *name)
 
static int amxrt_apply_process_settings (amxo_parser_t *parser)
 
static int amxrt_register (amxo_parser_t *parser, amxd_dm_t *dm)
 
static void amxrt_handle_events (amxd_dm_t *dm, amxo_parser_t *parser)
 
static void amxrt_wait_done (UNUSED const char *const s, UNUSED const amxc_var_t *const d, UNUSED void *const p)
 
amxrt_tamxrt_get (void)
 
amxc_var_t * amxrt_get_config (void)
 Gets the htable variant containing the configuration options. More...
 
amxo_parser_t * amxrt_get_parser (void)
 Gets runtime odl parser. More...
 
amxd_dm_t * amxrt_get_dm (void)
 Gets the runtime data model storage. More...
 
void amxrt_new (void)
 Create the ambiorix runtime. More...
 
int amxrt (int argc, char *argv[], amxrt_arg_fn_t fn)
 This function can create full ambiorix application (data model provider or client). More...
 
int amxrt_init (int argc, char *argv[], amxrt_arg_fn_t fn)
 Initializes the ambiorix runtime. More...
 
void amxrt_delete (void)
 Clean-up ambiorix runtime. More...
 
int amxrt_register_or_wait (void)
 Register the data model or wait for required data model objects. More...
 
int amxrt_run (void)
 Starts the event loop. More...
 
void amxrt_stop (void)
 Stops the runtime. More...
 
int amxrt_load_odl_files (int argc, char *argv[], int index)
 Load odls files mentioned on the command line or the default odl file. More...
 
int amxrt_connect (void)
 Connects to all bus sockets. More...
 
void amxrt_enable_syssigs (amxc_var_t *syssigs)
 Enables system signals that should be monitored by the eventloop. More...
 

Variables

static amxrt_t rt
 

Macro Definition Documentation

◆ _GNU_SOURCE

#define _GNU_SOURCE

Definition at line 56 of file amxrt.c.

Function Documentation

◆ amxrt_apply_process_settings()

static int amxrt_apply_process_settings ( amxo_parser_t *  parser)
static

Definition at line 209 of file amxrt.c.

209  {
210  int retval = 0;
211  int pid = -1;
212  bool pidfile = amxc_var_dyncast(bool, GET_ARG(&parser->config, AMXRT_COPT_PID_FILE));
213  char* name = amxc_var_dyncast(cstring_t, GET_ARG(&parser->config, AMXRT_COPT_NAME));
214 
215  int priority = amxc_var_dyncast(uint32_t, GET_ARG(&parser->config, AMXRT_COPT_PRIORITY));
216 
217  pid = getpid();
218  retval = setpriority(PRIO_PROCESS, pid, priority);
219  when_failed(retval, leave);
220 
221  if(pidfile && (name != NULL)) {
222  amxrt_create_pid_file(pid, name);
223  }
224 
225  if(GET_BOOL(&parser->config, AMXRT_COPT_LOG) &&
226  ( GET_CHAR(&parser->config, AMXRT_COPT_NAME) != NULL)) {
227  openlog(GET_CHAR(&parser->config, AMXRT_COPT_NAME), LOG_CONS | LOG_PID, LOG_USER);
228  syslog(LOG_USER | LOG_INFO, "** MARK **");
229  }
230 
231  retval = amxrt_caps_apply();
232 
233 leave:
234  free(name);
235  return retval;
236 }
static int amxrt_create_pid_file(pid_t pid, const char *name)
Definition: amxrt.c:164
#define AMXRT_COPT_PID_FILE
Definition: amxrt.h:84
#define AMXRT_COPT_PRIORITY
Definition: amxrt.h:83
#define AMXRT_COPT_NAME
Definition: amxrt.h:85
#define AMXRT_COPT_LOG
Definition: amxrt.h:98
int amxrt_caps_apply(void)
Apply the user, group and capabilities as defined in the configuration.
Definition: amxrt_cap.c:101

◆ amxrt_create_pid_file()

static int amxrt_create_pid_file ( pid_t  pid,
const char *  name 
)
static

Definition at line 164 of file amxrt.c.

164  {
165  amxc_string_t pidfile;
166  int retval = -1;
167  FILE* pf = NULL;
168 
169  amxc_string_init(&pidfile, 64);
170 
171  // create pidfile
172  amxc_string_appendf(&pidfile, "/var/run/%s.pid", name);
173  pf = fopen(amxc_string_get(&pidfile, 0), "w");
174  if((pf == NULL) && (errno == EACCES)) {
175  amxc_string_reset(&pidfile);
176  amxc_string_appendf(&pidfile, "/var/run/%s/%s.pid", name, name);
177  pf = fopen(amxc_string_get(&pidfile, 0), "w");
178  }
179  if(pf != NULL) {
180  fprintf(pf, "%d", pid);
181  fflush(pf);
182  fclose(pf);
183  retval = 0;
184  } else {
185  amxrt_print_error("Failed to create pidfile");
186  }
187 
188  amxc_string_clean(&pidfile);
189  return retval;
190 }
PRIVATE void amxrt_print_error(const char *fmt,...)

◆ amxrt_file_exists()

static bool amxrt_file_exists ( const char *  filename)
static

Definition at line 76 of file amxrt.c.

76  {
77  struct stat buffer;
78 
79  return (stat(filename, &buffer) == 0);
80 }

◆ amxrt_get()

amxrt_t* amxrt_get ( void  )

Definition at line 297 of file amxrt.c.

297  {
298  return &rt;
299 }
static amxrt_t rt
Definition: amxrt.c:74

◆ amxrt_handle_events()

static void amxrt_handle_events ( amxd_dm_t *  dm,
amxo_parser_t *  parser 
)
static

Definition at line 264 of file amxrt.c.

264  {
265  bool handle_events = GET_BOOL(&parser->config, AMXRT_COPT_HANDLE_EVENTS);
266 
267  if(handle_events) {
268  while(amxp_signal_read() == 0) {
269  }
270  }
271  amxp_sigmngr_trigger_signal(&dm->sigmngr, "app:start", NULL);
272 }
#define AMXRT_COPT_HANDLE_EVENTS
Definition: amxrt.h:100
static void handle_events(void)

◆ amxrt_parse_odl_extensions()

static int amxrt_parse_odl_extensions ( amxo_parser_t *  parser,
amxd_object_t *  root 
)
static

Definition at line 98 of file amxrt.c.

98  {
99  int retval = 0;
100 
101  const char* ext_dir = GET_CHAR(&parser->config, AMXRT_COPT_EXT_DIR);
102  amxc_string_t opt_include;
103 
104  amxc_string_init(&opt_include, 0);
105  when_str_empty(ext_dir, exit);
106 
107  amxc_string_setf(&opt_include, "#include '%s';", ext_dir);
108 
109  retval = amxo_parser_parse_string(parser, amxc_string_get(&opt_include, 0), root);
110 
111 exit:
112  amxc_string_clean(&opt_include);
113  return retval;
114 }
#define AMXRT_COPT_EXT_DIR
Definition: amxrt.h:102

◆ amxrt_parse_odl_files()

static int amxrt_parse_odl_files ( amxo_parser_t *  parser,
int  argc,
char *  argv[],
int  index,
amxd_object_t *  root 
)
static

Definition at line 142 of file amxrt.c.

146  {
147  int retval = 0;
148 
149  if(index >= argc) {
150  retval = amxrt_try_default_odl(parser, root);
151  } else {
152  while(index < argc) {
153  retval = amxo_parser_parse_file(parser, argv[index++], root);
154  if(retval != 0) {
155  amxrt_print_failure(parser, argv[index - 1]);
156  break;
157  }
158  }
159  }
160 
161  return retval;
162 }
static int amxrt_try_default_odl(amxo_parser_t *parser, amxd_object_t *root)
Definition: amxrt.c:116
PRIVATE void amxrt_print_failure(amxo_parser_t *parser, const char *string)

◆ amxrt_parse_odl_string()

static int amxrt_parse_odl_string ( amxo_parser_t *  parser,
amxd_object_t *  root 
)
static

Definition at line 82 of file amxrt.c.

83  {
84  const char* odl_str = NULL;
85  int retval = 0;
86 
87  odl_str = amxc_var_constcast(cstring_t, GET_ARG(&parser->config, AMXRT_COPT_ODL));
88  if(odl_str != NULL) {
89  retval = amxo_parser_parse_string(parser, odl_str, root);
90  if(retval != 0) {
91  amxrt_print_failure(parser, odl_str);
92  }
93  }
94 
95  return retval;
96 }
#define AMXRT_COPT_ODL
Definition: amxrt.h:81

◆ amxrt_register()

static int amxrt_register ( amxo_parser_t *  parser,
amxd_dm_t *  dm 
)
static

Definition at line 238 of file amxrt.c.

238  {
239  int retval = 0;
240 
241  retval = amxrt_connection_register_dm(parser, dm);
242  when_failed(retval, leave);
243  amxc_var_set(bool, GET_ARG(&parser->config, AMXRT_COPT_EVENT), true);
244  if(parser->post_includes != NULL) {
245  retval = amxo_parser_invoke_entry_points(parser, dm, AMXO_START);
246  when_failed(retval, leave);
247  retval = amxo_parser_invoke_entry_points(parser, dm, AMXO_ODL_LOADED);
248  when_failed(retval, leave);
249  } else {
250  retval = amxo_parser_invoke_entry_points(parser, dm, AMXO_START);
251  when_failed(retval, leave);
252  }
253 
254  retval = amxo_parser_start_synchronize(parser);
255  if(retval != 0) {
256  amxrt_print_error("Runtime - synchronizations failed to start (nr failed = %d)\n", retval);
257  amxrt_print_message("Runtime - Are all required objects available?");
258  }
259 
260 leave:
261  return retval;
262 }
#define AMXRT_COPT_EVENT
Definition: amxrt.h:90
PRIVATE void amxrt_print_message(const char *fmt,...)
PRIVATE int amxrt_connection_register_dm(amxo_parser_t *parser, amxd_dm_t *dm)

◆ amxrt_remove_pid_file()

static void amxrt_remove_pid_file ( const char *  name)
static

Definition at line 192 of file amxrt.c.

192  {
193  amxc_string_t pidfile;
194  amxc_string_init(&pidfile, 64);
195 
196  amxc_string_appendf(&pidfile, "/var/run/%s.pid", name);
197  if(!amxrt_file_exists(amxc_string_get(&pidfile, 0))) {
198  amxc_string_reset(&pidfile);
199  amxc_string_appendf(&pidfile, "/var/run/%s/%s.pid", name, name);
200  }
201 
202  if(amxrt_file_exists(amxc_string_get(&pidfile, 0))) {
203  unlink(amxc_string_get(&pidfile, 0));
204  }
205 
206  amxc_string_clean(&pidfile);
207 }
static bool amxrt_file_exists(const char *filename)
Definition: amxrt.c:76

◆ amxrt_try_default_odl()

static int amxrt_try_default_odl ( amxo_parser_t *  parser,
amxd_object_t *  root 
)
static

Definition at line 116 of file amxrt.c.

116  {
117  char* name = amxc_var_dyncast(cstring_t, GET_ARG(&parser->config, AMXRT_COPT_NAME));
118  char* prefix = amxc_var_dyncast(cstring_t, GET_ARG(&parser->config, AMXRT_COPT_PREFIX_PATH));
119  char* cfg_dir = amxc_var_dyncast(cstring_t, GET_ARG(&parser->config, AMXRT_COPT_CFG_DIR));
120  struct stat sb;
121  int retval = 0;
122 
123  amxc_string_t include;
124  amxc_string_init(&include, 32);
125  amxc_string_appendf(&include, "%s/%s/%s/%s.odl", prefix, cfg_dir, name, name);
126 
127  if(stat(amxc_string_get(&include, 0), &sb) == 0) {
128  retval = amxo_parser_parse_file(parser, amxc_string_get(&include, 0), root);
129  if(retval != 0) {
130  amxrt_print_failure(parser, amxc_string_get(&include, 0));
131  }
132  }
133  amxc_string_clean(&include);
134 
135  free(cfg_dir);
136  free(prefix);
137  free(name);
138 
139  return retval;
140 }
#define AMXRT_COPT_CFG_DIR
Definition: amxrt.h:88
#define AMXRT_COPT_PREFIX_PATH
Definition: amxrt.h:86

◆ amxrt_wait_done()

static void amxrt_wait_done ( UNUSED const char *const  s,
UNUSED const amxc_var_t *const  d,
UNUSED void *const  p 
)
static

Definition at line 274 of file amxrt.c.

276  {
277  amxo_parser_t* parser = amxrt_get_parser();
278  amxd_dm_t* dm = amxrt_get_dm();
279  amxc_var_t* req = GET_ARG(&parser->config, AMXRT_COPT_REQUIRES);
280 
281  amxc_var_clean(req);
282  amxc_var_set_type(req, AMXC_VAR_ID_LIST);
283 
284  amxrt_print_message("RunTime - All required objects available - continue");
285 
286  amxp_sigmngr_resume(&dm->sigmngr);
287 
288  if(amxrt_register(parser, dm) != 0) {
289  amxrt_print_error("Failed to register data model");
290  amxrt_el_stop();
291  } else {
292  amxrt_handle_events(dm, parser);
293  amxp_slot_disconnect(NULL, "wait:done", amxrt_wait_done);
294  }
295 }
static int amxrt_register(amxo_parser_t *parser, amxd_dm_t *dm)
Definition: amxrt.c:238
static void amxrt_handle_events(amxd_dm_t *dm, amxo_parser_t *parser)
Definition: amxrt.c:264
static void amxrt_wait_done(UNUSED const char *const s, UNUSED const amxc_var_t *const d, UNUSED void *const p)
Definition: amxrt.c:274
#define AMXRT_COPT_REQUIRES
Definition: amxrt.h:99
int amxrt_el_stop(void)
Stops the event loop.
amxd_dm_t * amxrt_get_dm(void)
Gets the runtime data model storage.
Definition: amxrt.c:309
amxo_parser_t * amxrt_get_parser(void)
Gets runtime odl parser.
Definition: amxrt.c:305

Variable Documentation

◆ rt

amxrt_t rt
static

Definition at line 74 of file amxrt.c.