From 6ab2671259ebfc683af69b57f400457bbbe169eb Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Mon, 23 Mar 2015 06:53:52 +0100 Subject: Improve sync/async execution of up/down scripts --- src/config.y | 31 +++++++++++-------------------- src/fastd.c | 6 +++--- src/peer.c | 31 ++++++++++++++++++------------- src/peer.h | 2 +- src/protocols/ec25519_fhmqvc/handshake.c | 2 +- 5 files changed, 34 insertions(+), 38 deletions(-) (limited to 'src') diff --git a/src/config.y b/src/config.y index a11460f..f471040 100644 --- a/src/config.y +++ b/src/config.y @@ -158,8 +158,6 @@ %type drop_capabilities_enabled %type autobool %type sync -%type sync_def_sync -%type sync_def_async %% start: START_CONFIG config @@ -396,42 +394,42 @@ protocol: TOK_STRING { secret: TOK_STRING { free(conf.secret); conf.secret = fastd_strdup($1->str); } ; -on_pre_up: sync_def_sync TOK_STRING { - fastd_shell_command_set(&conf.on_pre_up, $2->str, $1); +on_pre_up: TOK_STRING { + fastd_shell_command_set(&conf.on_pre_up, $1->str, true); } ; -on_up: sync_def_sync TOK_STRING { +on_up: sync TOK_STRING { fastd_shell_command_set(&conf.on_up, $2->str, $1); } ; -on_down: sync_def_sync TOK_STRING { +on_down: sync TOK_STRING { fastd_shell_command_set(&conf.on_down, $2->str, $1); } ; -on_post_down: sync_def_sync TOK_STRING { - fastd_shell_command_set(&conf.on_post_down, $2->str, $1); +on_post_down: TOK_STRING { + fastd_shell_command_set(&conf.on_post_down, $1->str, true); } ; -on_connect: sync_def_async TOK_STRING { +on_connect: sync TOK_STRING { fastd_shell_command_set(&conf.on_connect, $2->str, $1); } ; -on_establish: sync_def_async TOK_STRING { +on_establish: sync TOK_STRING { fastd_shell_command_set(&conf.on_establish, $2->str, $1); } ; -on_disestablish: sync_def_async TOK_STRING { +on_disestablish: sync TOK_STRING { fastd_shell_command_set(&conf.on_disestablish, $2->str, $1); } ; -on_verify: sync_def_async TOK_STRING { +on_verify: sync TOK_STRING { #ifdef WITH_DYNAMIC_PEERS fastd_shell_command_set(&conf.on_verify, $2->str, $1); #else @@ -627,16 +625,9 @@ maybe_ipv6: TOK_IPV6 | ; -sync_def_sync: sync { $$ = $1; } - | { $$ = true; } - ; - -sync_def_async: sync { $$ = $1; } - | { $$ = false; } - ; - sync: TOK_SYNC { $$ = true; } | TOK_ASYNC { $$ = false; } + | { $$ = false; } boolean: TOK_YES { $$ = true; } | TOK_NO { $$ = false; } diff --git a/src/fastd.c b/src/fastd.c index 19fa10f..5ac1967 100644 --- a/src/fastd.c +++ b/src/fastd.c @@ -207,14 +207,14 @@ static void close_sockets(void) { /** Calls the on-pre-up command */ static inline void on_pre_up(void) { - fastd_shell_command_exec(&conf.on_pre_up, NULL); + fastd_shell_command_exec_sync(&conf.on_pre_up, NULL, NULL); } /** Calls the on-up command */ static inline void on_up(fastd_iface_t *iface) { fastd_shell_env_t *env = fastd_shell_env_alloc(); fastd_shell_env_set_iface(env, iface); - fastd_shell_command_exec(&conf.on_up, env); + fastd_shell_command_exec_sync(&conf.on_up, env, NULL); fastd_shell_env_free(env); } @@ -222,7 +222,7 @@ static inline void on_up(fastd_iface_t *iface) { static inline void on_down(fastd_iface_t *iface) { fastd_shell_env_t *env = fastd_shell_env_alloc(); fastd_shell_env_set_iface(env, iface); - fastd_shell_command_exec(&conf.on_down, env); + fastd_shell_command_exec_sync(&conf.on_down, env, NULL); fastd_shell_env_free(env); } diff --git a/src/peer.c b/src/peer.c index 307d9b6..9f6c228 100644 --- a/src/peer.c +++ b/src/peer.c @@ -111,31 +111,36 @@ void fastd_peer_set_shell_env(fastd_shell_env_t *env, const fastd_peer_t *peer, } /** Executes a shell command, providing peer-specific enviroment fields */ -void fastd_peer_exec_shell_command(const fastd_shell_command_t *command, const fastd_peer_t *peer, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *peer_addr) { +void fastd_peer_exec_shell_command(const fastd_shell_command_t *command, const fastd_peer_t *peer, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *peer_addr, bool sync) { fastd_shell_env_t *env = fastd_shell_env_alloc(); fastd_peer_set_shell_env(env, peer, local_addr, peer_addr); - fastd_shell_command_exec(command, env); + + if (sync) + fastd_shell_command_exec_sync(command, env, NULL); + else + fastd_shell_command_exec(command, env); + fastd_shell_env_free(env); } /** Calls the on-up command */ -static inline void on_up(const fastd_peer_t *peer) { - fastd_peer_exec_shell_command(&conf.on_up, peer, NULL, NULL); +static inline void on_up(const fastd_peer_t *peer, bool sync) { + fastd_peer_exec_shell_command(&conf.on_up, peer, NULL, NULL, sync); } /** Calls the on-down command */ -static inline void on_down(const fastd_peer_t *peer) { - fastd_peer_exec_shell_command(&conf.on_down, peer, NULL, NULL); +static inline void on_down(const fastd_peer_t *peer, bool sync) { + fastd_peer_exec_shell_command(&conf.on_down, peer, NULL, NULL, sync); } /** Executes the on-establish command for a peer */ static inline void on_establish(const fastd_peer_t *peer) { - fastd_peer_exec_shell_command(&conf.on_establish, peer, &peer->local_address, &peer->address); + fastd_peer_exec_shell_command(&conf.on_establish, peer, &peer->local_address, &peer->address, false); } /** Executes the on-disestablish command for a peer */ static inline void on_disestablish(const fastd_peer_t *peer) { - fastd_peer_exec_shell_command(&conf.on_disestablish, peer, &peer->local_address, &peer->address); + fastd_peer_exec_shell_command(&conf.on_disestablish, peer, &peer->local_address, &peer->address, false); } /** Compares two peers by their peer ID */ @@ -324,9 +329,9 @@ static void reset_peer(fastd_peer_t *peer) { peer->local_address.sa.sa_family = AF_UNSPEC; peer->state = STATE_INACTIVE; - if (!conf.iface_persist || peer->config_state == CONFIG_DISABLED) { + if (!conf.iface_persist || peer->config_state == CONFIG_DISABLED || fastd_peer_is_dynamic(peer)) { if (peer->iface && peer->iface->peer) { - on_down(peer); + on_down(peer, false); fastd_iface_close(peer->iface); } @@ -405,7 +410,7 @@ static void setup_peer(fastd_peer_t *peer) { if (ctx.iface) { peer->iface = ctx.iface; } - else if (conf.iface_persist && !peer->iface) { + else if (conf.iface_persist && !peer->iface && !fastd_peer_is_dynamic(peer)) { const char *ifname = peer->ifname; if (!ifname && fastd_config_single_iface()) @@ -413,7 +418,7 @@ static void setup_peer(fastd_peer_t *peer) { peer->iface = fastd_iface_open(ifname, peer); if (peer->iface) - on_up(peer); + on_up(peer, true); else if (!peer->config_source_dir) /* Fail for statically configured peers; an error message has already been printed by fastd_iface_open() */ @@ -475,7 +480,7 @@ static void delete_peer(fastd_peer_t *peer) { conf.protocol->free_peer_state(peer); if (peer->iface && peer->iface->peer) { - on_down(peer); + on_down(peer, true); fastd_iface_close(peer->iface); } diff --git a/src/peer.h b/src/peer.h index b641bcb..1dcc43b 100644 --- a/src/peer.h +++ b/src/peer.h @@ -180,7 +180,7 @@ void fastd_peer_schedule_handshake(fastd_peer_t *peer, int delay); fastd_peer_t * fastd_peer_find_by_id(uint64_t id); void fastd_peer_set_shell_env(fastd_shell_env_t *env, const fastd_peer_t *peer, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *peer_addr); -void fastd_peer_exec_shell_command(const fastd_shell_command_t *command, const fastd_peer_t *peer, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *peer_addr); +void fastd_peer_exec_shell_command(const fastd_shell_command_t *command, const fastd_peer_t *peer, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *peer_addr, bool sync); /** Schedules a handshake with the default delay and jitter diff --git a/src/protocols/ec25519_fhmqvc/handshake.c b/src/protocols/ec25519_fhmqvc/handshake.c index 246336b..4d3d17a 100644 --- a/src/protocols/ec25519_fhmqvc/handshake.c +++ b/src/protocols/ec25519_fhmqvc/handshake.c @@ -508,7 +508,7 @@ void fastd_protocol_ec25519_fhmqvc_handshake_init(fastd_socket_t *sock, const fa fastd_handshake_add(&buffer, RECORD_SENDER_HANDSHAKE_KEY, PUBLICKEYBYTES, &ctx.protocol_state->handshake_key.key.public); if (!peer || !fastd_peer_is_established(peer)) - fastd_peer_exec_shell_command(&conf.on_connect, peer, (local_addr && local_addr->sa.sa_family) ? local_addr : sock->bound_addr, remote_addr); + fastd_peer_exec_shell_command(&conf.on_connect, peer, (local_addr && local_addr->sa.sa_family) ? local_addr : sock->bound_addr, remote_addr, false); fastd_send_handshake(sock, local_addr, remote_addr, peer, buffer.buffer); } -- cgit v1.2.3