summaryrefslogtreecommitdiffstats
path: root/src/shell.c
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2013-02-25 04:48:11 +0100
committerMatthias Schiffer <mschiffer@universe-factory.net>2013-02-25 04:48:11 +0100
commit698ede3ce4ab7ff88e5c69b3910544094c7f9814 (patch)
treed3a7bd1835ecf25e16c35ab658e7d56cc2c69ad2 /src/shell.c
parentb3ba14d47370e156ef7366954c1160e088e92b49 (diff)
downloadfastd-698ede3ce4ab7ff88e5c69b3910544094c7f9814.tar
fastd-698ede3ce4ab7ff88e5c69b3910544094c7f9814.zip
Get rid of some duplicate code for calling shell commands
Diffstat (limited to 'src/shell.c')
-rw-r--r--src/shell.c98
1 files changed, 98 insertions, 0 deletions
diff --git a/src/shell.c b/src/shell.c
new file mode 100644
index 0000000..c80a32a
--- /dev/null
+++ b/src/shell.c
@@ -0,0 +1,98 @@
+/*
+ Copyright (c) 2012-2013, Matthias Schiffer <mschiffer@universe-factory.net>
+ All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+ 2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+
+#define _GNU_SOURCE
+
+#include "peer.h"
+
+#include <arpa/inet.h>
+
+
+bool fastd_shell_exec(fastd_context_t *ctx, const fastd_peer_t *peer, const char *command, const char *dir, int *ret) {
+ int result = -1;
+ bool ok = false;
+ char *cwd = get_current_dir_name();
+
+ if(!chdir(dir)) {
+ setenv("INTERFACE", ctx->ifname, 1);
+
+ char buf[INET6_ADDRSTRLEN];
+
+ if (peer && peer->config && peer->config->name)
+ setenv("PEER_NAME", peer->config->name, 1);
+ else
+ unsetenv("PEER_NAME");
+
+ switch(peer ? peer->address.sa.sa_family : AF_UNSPEC) {
+ case AF_INET:
+ inet_ntop(AF_INET, &peer->address.in.sin_addr, buf, sizeof(buf));
+ setenv("PEER_ADDRESS", buf, 1);
+
+ snprintf(buf, sizeof(buf), "%u", ntohs(peer->address.in.sin_port));
+ setenv("PEER_PORT", buf, 1);
+
+ break;
+
+ case AF_INET6:
+ inet_ntop(AF_INET6, &peer->address.in6.sin6_addr, buf, sizeof(buf));
+ setenv("PEER_ADDRESS", buf, 1);
+
+ snprintf(buf, sizeof(buf), "%u", ntohs(peer->address.in6.sin6_port));
+ setenv("PEER_PORT", buf, 1);
+
+ break;
+
+ default:
+ unsetenv("PEER_ADDRESS");
+ unsetenv("PEER_PORT");
+ }
+
+ result = system(command);
+
+ if (ret) {
+ *ret = result;
+ ok = true;
+ }
+ else {
+ if (WIFSIGNALED(result))
+ pr_error(ctx, "command exited with signal %i", WTERMSIG(result));
+ else if (result)
+ pr_warn(ctx, "command exited with status %i", WEXITSTATUS(ret));
+ else
+ ok = true;
+ }
+
+ if(chdir(cwd))
+ pr_error(ctx, "can't chdir to `%s': %s", cwd, strerror(errno));
+ }
+ else {
+ pr_error(ctx, "can't chdir to `%s': %s", dir, strerror(errno));
+ }
+
+ free(cwd);
+
+ return ok;
+}