From 008f29602600e449c6c07952c3a62e0b96b32f90 Mon Sep 17 00:00:00 2001 From: Andrew Bettison Date: Mon, 25 May 2015 18:35:10 +0930 Subject: [PATCH] Generic trigger functions "trigger.h" --- commandline.h | 37 ++++++++++++++----------------------- headerfiles.mk | 2 ++ rhizome.h | 8 +++++++- rhizome_database.c | 13 +++++++++++++ section.h | 38 ++++++++++++++++++++++++++++++++++++++ trigger.h | 42 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 116 insertions(+), 24 deletions(-) create mode 100644 section.h create mode 100644 trigger.h diff --git a/commandline.h b/commandline.h index 6bdb46d2..3c63dc0b 100644 --- a/commandline.h +++ b/commandline.h @@ -1,5 +1,5 @@ /* - Copyright (C) 2014 Serval Project Inc. + Copyright (C) 2014,2015 Serval Project Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License @@ -19,39 +19,30 @@ #ifndef __SERVAL_DNA__COMMANDLINE_H #define __SERVAL_DNA__COMMANDLINE_H +#include "section.h" + #define KEYRING_PIN_OPTION ,"[--keyring-pin=]" #define KEYRING_ENTRY_PIN_OPTION ,"[--entry-pin=]" #define KEYRING_PIN_OPTIONS KEYRING_PIN_OPTION KEYRING_ENTRY_PIN_OPTION "..." -// macros are weird sometimes .... -#define _APPEND(X,Y) X ## Y -#define _APPEND2(X,Y) _APPEND(X,Y) - struct cli_schema; struct cli_context; -#ifdef __APPLE__ -#define SECTION(X) section("__DATA,__" X ) -#define SECTION_START(X) __asm("section$start$__DATA$__" X) -#define SECTION_END(X) __asm("section$end$__DATA$__" X) -#else -#define SECTION(X) section(X) -#define SECTION_START(X) -#define SECTION_END(X) -#endif +// macros are weird sometimes .... +#define __APPEND_(X,Y) X ## Y +#define _APPEND(X,Y) __APPEND_(X,Y) #define DEFINE_CMD(FUNC, FLAGS, HELP, WORD1, ...) \ static int FUNC(const struct cli_parsed *parsed, struct cli_context *context); \ - struct cli_schema _APPEND2(FUNC, __LINE__) \ - __attribute__((used,aligned(sizeof(void *)),SECTION("commands"))) = {\ - .function = FUNC, \ - .words = {WORD1, ##__VA_ARGS__, NULL}, \ - .flags = FLAGS, \ - .description = HELP\ + static struct cli_schema _APPEND(FUNC, __LINE__) IN_SECTION(commands) = {\ + .function = FUNC, \ + .words = {WORD1, ##__VA_ARGS__, NULL}, \ + .flags = FLAGS, \ + .description = HELP \ } -extern struct cli_schema __start_commands[] SECTION_START("commands"); -extern struct cli_schema __stop_commands[] SECTION_END("commands"); +extern struct cli_schema __start_commands[] SECTION_START(commands); +extern struct cli_schema __stop_commands[] SECTION_STOP(commands); #define CMD_COUNT (__stop_commands - __start_commands) @@ -69,4 +60,4 @@ int cli_write(struct cli_context *context, const unsigned char *buf, size_t len) void cli_cleanup(); -#endif \ No newline at end of file +#endif diff --git a/headerfiles.mk b/headerfiles.mk index 773e64b3..5256ee44 100644 --- a/headerfiles.mk +++ b/headerfiles.mk @@ -3,6 +3,8 @@ HDRS= fifo.h \ overlay_address.h \ overlay_packet.h \ overlay_interface.h \ + section.h \ + trigger.h \ commandline.h \ limit.h \ rhizome_types.h \ diff --git a/rhizome.h b/rhizome.h index b2b14bd4..4433a452 100644 --- a/rhizome.h +++ b/rhizome.h @@ -31,6 +31,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include "uuid.h" #include "str.h" #include "strbuf.h" +#include "trigger.h" #ifndef __RHIZOME_INLINE # if __GNUC__ && !__GNUC_STDC_INLINE__ @@ -880,7 +881,7 @@ int rhizome_any_fetch_queued(); int rhizome_fetch_status_html(struct strbuf *b); int rhizome_fetch_has_queue_space(unsigned char log2_size); -/* rhizome storage methods */ +/* Rhizome storage methods */ int rhizome_exists(const rhizome_filehash_t *hashp); enum rhizome_payload_status rhizome_open_write(struct rhizome_write *write, const rhizome_filehash_t *expectedHashp, uint64_t file_length); @@ -921,4 +922,9 @@ int overlay_mdp_service_rhizome_sync(struct internal_mdp_header *header, struct void rhizome_sync_status(); DECLARE_ALARM(rhizome_fetch_status); + +/* Rhizome triggers */ + +DECLARE_TRIGGER(rhizome_bundle_added, rhizome_manifest*) + #endif //__SERVAL_DNA__RHIZOME_H diff --git a/rhizome_database.c b/rhizome_database.c index 04357070..05c09860 100644 --- a/rhizome_database.c +++ b/rhizome_database.c @@ -1357,6 +1357,7 @@ int rhizome_store_manifest(rhizome_manifest *m) alloca_tohex_rhizome_bid_t(m->cryptoSignPublic), m->version ); + CALL_TRIGGER(rhizome_bundle_added, m); monitor_announce_bundle(m); if (serverMode){ time_ms_t now = gettime_ms(); @@ -1372,6 +1373,18 @@ rollback: return -1; } +static void trigger_rhizome_bundle_added_debug(rhizome_manifest *m) +{ + if (config.debug.rhizome) + DEBUGF("TRIGGER rhizome_bundle_added service=%s bid=%s version=%"PRIu64, + m->service ? m->service : "NULL", + alloca_tohex_rhizome_bid_t(m->cryptoSignPublic), + m->version + ); +} + +DEFINE_TRIGGER(rhizome_bundle_added, trigger_rhizome_bundle_added_debug) + /* The cursor struct must be zerofilled and the query parameters optionally filled in prior to * calling this function. * diff --git a/section.h b/section.h new file mode 100644 index 00000000..723eb78b --- /dev/null +++ b/section.h @@ -0,0 +1,38 @@ +/* + Copyright (C) 2015 Serval Project Inc. + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#ifndef __SERVAL_DNA__SECTION_H +#define __SERVAL_DNA__SECTION_H + +/* Macros for creating named linkage sections. + */ + +#ifdef __APPLE__ +#define _SECTION_ATTRIBUTE(X) section("__DATA,__" #X ) +#define SECTION_START(X) __asm("section$start$__DATA$__" #X) +#define SECTION_STOP(X) __asm("section$end$__DATA$__" #X) +#else +#define _SECTION_ATTRIBUTE(X) section(#X) +#define SECTION_START(X) +#define SECTION_STOP(X) +#endif + +#define IN_SECTION(name) __attribute__((used,aligned(sizeof(void *)),_SECTION_ATTRIBUTE(name))) + +#endif // __SERVAL_DNA__SECTION_H + diff --git a/trigger.h b/trigger.h new file mode 100644 index 00000000..8f70ef67 --- /dev/null +++ b/trigger.h @@ -0,0 +1,42 @@ +/* + Copyright (C) 2015 Serval Project Inc. + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#ifndef __SERVAL_DNA__TRIGGER_H +#define __SERVAL_DNA__TRIGGER_H + +#include "section.h" + +/* Macros for triggers + */ + +#define DECLARE_TRIGGER(TRIG, ...) \ + typedef void TRIGGER_FUNC_##TRIG (__VA_ARGS__); \ + extern TRIGGER_FUNC_##TRIG *__start___trigger_section_##TRIG[] SECTION_START(trigger_##TRIG); \ + extern TRIGGER_FUNC_##TRIG *__stop___trigger_section_##TRIG[] SECTION_STOP(trigger_##TRIG); + +#define DEFINE_TRIGGER(TRIG, FUNC) \ + TRIGGER_FUNC_##TRIG * __trigger_##FUNC IN_SECTION(__trigger_section_##TRIG) = FUNC; + +#define CALL_TRIGGER(TRIG, ...) \ + do { \ + TRIGGER_FUNC_##TRIG **__trig; \ + for (__trig = __start___trigger_section_##TRIG; __trig < __stop___trigger_section_##TRIG; ++__trig) \ + (**__trig)(__VA_ARGS__); \ + } while (0); + +#endif // __SERVAL_DNA__TRIGGER_H