summaryrefslogtreecommitdiffstats
path: root/src/peer.c
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2012-04-06 00:55:59 +0200
committerMatthias Schiffer <mschiffer@universe-factory.net>2012-04-06 00:55:59 +0200
commit1ed4ac93ae35534397bc8267f255c380ef3193c4 (patch)
tree1ac441024715ace282ef52587ddcfec678f4d8ed /src/peer.c
parenta6a5b5e00e98aa27644a1afa4a461819221b758d (diff)
downloadfastd-1ed4ac93ae35534397bc8267f255c380ef3193c4.tar
fastd-1ed4ac93ae35534397bc8267f255c380ef3193c4.zip
Add some missing error handling
Diffstat (limited to 'src/peer.c')
-rw-r--r--src/peer.c140
1 files changed, 76 insertions, 64 deletions
diff --git a/src/peer.c b/src/peer.c
index fd8577b..4c85db2 100644
--- a/src/peer.c
+++ b/src/peer.c
@@ -38,51 +38,57 @@ static void on_establish(fastd_context *ctx, fastd_peer *peer) {
return;
char *cwd = get_current_dir_name();
- chdir(ctx->conf->on_establish_dir);
- setenv("INTERFACE", ctx->ifname, 1);
+ if(!chdir(ctx->conf->on_establish_dir)) {
+ setenv("INTERFACE", ctx->ifname, 1);
- char buf[INET6_ADDRSTRLEN];
- snprintf(buf, sizeof(buf), "%u", ctx->conf->mtu);
- setenv("MTU", buf, 1);
+ char buf[INET6_ADDRSTRLEN];
+ snprintf(buf, sizeof(buf), "%u", ctx->conf->mtu);
+ setenv("MTU", buf, 1);
- if (peer->config && peer->config->name)
- setenv("PEER_NAME", peer->config->name, 1);
- else
- unsetenv("PEER_NAME");
+ if (peer->config && peer->config->name)
+ setenv("PEER_NAME", peer->config->name, 1);
+ else
+ unsetenv("PEER_NAME");
- switch(peer->address.sa.sa_family) {
- case AF_INET:
- inet_ntop(AF_INET, &peer->address.in.sin_addr, buf, sizeof(buf));
- setenv("PEER_ADDRESS", buf, 1);
+ switch(peer->address.sa.sa_family) {
+ 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);
+ snprintf(buf, sizeof(buf), "%u", ntohs(peer->address.in.sin_port));
+ setenv("PEER_PORT", buf, 1);
- break;
+ break;
- case AF_INET6:
- inet_ntop(AF_INET6, &peer->address.in6.sin6_addr, buf, sizeof(buf));
- setenv("PEER_ADDRESS", buf, 1);
+ 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);
+ snprintf(buf, sizeof(buf), "%u", ntohs(peer->address.in6.sin6_port));
+ setenv("PEER_PORT", buf, 1);
- break;
+ break;
- default:
- unsetenv("PEER_ADDRESS");
- unsetenv("PEER_PORT");
- }
+ default:
+ unsetenv("PEER_ADDRESS");
+ unsetenv("PEER_PORT");
+ }
- int ret = system(ctx->conf->on_establish);
+ int ret = system(ctx->conf->on_establish);
- if (WIFSIGNALED(ret))
- pr_error(ctx, "on-establish command exited with signal %i", WTERMSIG(ret));
- else if(ret)
- pr_warn(ctx, "on-establish command exited with status %i", WEXITSTATUS(ret));
+ if (WIFSIGNALED(ret))
+ pr_error(ctx, "on-establish command exited with signal %i", WTERMSIG(ret));
+ else if(ret)
+ pr_warn(ctx, "on-establish command exited with status %i", WEXITSTATUS(ret));
+
+ 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", ctx->conf->on_establish_dir, strerror(errno));
+ }
- chdir(cwd);
free(cwd);
}
@@ -91,51 +97,57 @@ static void on_disestablish(fastd_context *ctx, fastd_peer *peer) {
return;
char *cwd = get_current_dir_name();
- chdir(ctx->conf->on_disestablish_dir);
- setenv("INTERFACE", ctx->ifname, 1);
+ if(!chdir(ctx->conf->on_disestablish_dir)) {
+ setenv("INTERFACE", ctx->ifname, 1);
- char buf[INET6_ADDRSTRLEN];
- snprintf(buf, sizeof(buf), "%u", ctx->conf->mtu);
- setenv("MTU", buf, 1);
+ char buf[INET6_ADDRSTRLEN];
+ snprintf(buf, sizeof(buf), "%u", ctx->conf->mtu);
+ setenv("MTU", buf, 1);
- if (peer->config && peer->config->name)
- setenv("PEER_NAME", peer->config->name, 1);
- else
- unsetenv("PEER_NAME");
+ if (peer->config && peer->config->name)
+ setenv("PEER_NAME", peer->config->name, 1);
+ else
+ unsetenv("PEER_NAME");
- switch(peer->address.sa.sa_family) {
- case AF_INET:
- inet_ntop(AF_INET, &peer->address.in.sin_addr, buf, sizeof(buf));
- setenv("PEER_ADDRESS", buf, 1);
+ switch(peer->address.sa.sa_family) {
+ 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);
+ snprintf(buf, sizeof(buf), "%u", ntohs(peer->address.in.sin_port));
+ setenv("PEER_PORT", buf, 1);
- break;
+ break;
- case AF_INET6:
- inet_ntop(AF_INET6, &peer->address.in6.sin6_addr, buf, sizeof(buf));
- setenv("PEER_ADDRESS", buf, 1);
+ 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);
+ snprintf(buf, sizeof(buf), "%u", ntohs(peer->address.in6.sin6_port));
+ setenv("PEER_PORT", buf, 1);
- break;
+ break;
- default:
- unsetenv("PEER_ADDRESS");
- unsetenv("PEER_PORT");
- }
+ default:
+ unsetenv("PEER_ADDRESS");
+ unsetenv("PEER_PORT");
+ }
- int ret = system(ctx->conf->on_disestablish);
+ int ret = system(ctx->conf->on_disestablish);
- if (WIFSIGNALED(ret))
- pr_error(ctx, "on-disestablish command exited with signal %i", WTERMSIG(ret));
- else if(ret)
- pr_warn(ctx, "on-disestablish command exited with status %i", WEXITSTATUS(ret));
+ if (WIFSIGNALED(ret))
+ pr_error(ctx, "on-disestablish command exited with signal %i", WTERMSIG(ret));
+ else if(ret)
+ pr_warn(ctx, "on-disestablish command exited with status %i", WEXITSTATUS(ret));
+
+ 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", ctx->conf->on_disestablish_dir, strerror(errno));
+ }
- chdir(cwd);
free(cwd);
}