From 1ed4ac93ae35534397bc8267f255c380ef3193c4 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Fri, 6 Apr 2012 00:55:59 +0200 Subject: Add some missing error handling --- src/config.c | 10 +++-- src/fastd.c | 59 +++++++++++++++---------- src/peer.c | 140 ++++++++++++++++++++++++++++++++--------------------------- 3 files changed, 119 insertions(+), 90 deletions(-) diff --git a/src/config.c b/src/config.c index f18d975..8c63b4e 100644 --- a/src/config.c +++ b/src/config.c @@ -163,7 +163,8 @@ void fastd_read_peer_dir(fastd_context *ctx, fastd_config *conf, const char *dir read_peer_dir(ctx, conf, conf->peer_dirs->str); - chdir(oldcwd); + if(chdir(oldcwd)) + pr_error(ctx, "can't chdir to `%s': %s", oldcwd, strerror(errno)); } else { pr_error(ctx, "change from directory `%s' to `%s' failed: %s", oldcwd, dir, strerror(errno)); @@ -250,7 +251,8 @@ bool fastd_read_config(fastd_context *ctx, fastd_config *conf, const char *filen fastd_config_pstate_delete(ps); fastd_config_yylex_destroy(scanner); - chdir(oldcwd); + if(chdir(oldcwd)) + pr_error(ctx, "can't chdir to `%s': %s", oldcwd, strerror(errno)); free(filename2); free(oldcwd); @@ -568,7 +570,9 @@ static void reconfigure_read_peer_dirs(fastd_context *ctx, fastd_config *new_con pr_error(ctx, "change from directory `%s' to `%s' failed: %s", oldcwd, dir->str, strerror(errno)); } - chdir(oldcwd); + if (chdir(oldcwd)) + pr_error(ctx, "can't chdir to `%s': %s", oldcwd, strerror(errno)); + free(oldcwd); } diff --git a/src/fastd.c b/src/fastd.c index ca4edb8..b2cc196 100644 --- a/src/fastd.c +++ b/src/fastd.c @@ -191,22 +191,28 @@ static void on_up(fastd_context *ctx) { return; char *cwd = get_current_dir_name(); - chdir(ctx->conf->on_up_dir); - setenv("INTERFACE", ctx->ifname, 1); + if (!chdir(ctx->conf->on_up_dir)) { + setenv("INTERFACE", ctx->ifname, 1); - char buf[6]; - snprintf(buf, 6, "%u", ctx->conf->mtu); - setenv("MTU", buf, 1); + char buf[6]; + snprintf(buf, 6, "%u", ctx->conf->mtu); + setenv("MTU", buf, 1); - int ret = system(ctx->conf->on_up); + int ret = system(ctx->conf->on_up); - if (WIFSIGNALED(ret)) - pr_error(ctx, "on-up command exited with signal %i", WTERMSIG(ret)); - else if(ret) - pr_warn(ctx, "on-up command exited with status %i", WEXITSTATUS(ret)); + if (WIFSIGNALED(ret)) + pr_error(ctx, "on-up command exited with signal %i", WTERMSIG(ret)); + else if(ret) + pr_warn(ctx, "on-up 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_up_dir, strerror(errno)); + } - chdir(cwd); free(cwd); } @@ -215,22 +221,28 @@ static void on_down(fastd_context *ctx) { return; char *cwd = get_current_dir_name(); - chdir(ctx->conf->on_down_dir); - setenv("INTERFACE", ctx->ifname, 1); + if(!chdir(ctx->conf->on_down_dir)) { + setenv("INTERFACE", ctx->ifname, 1); - char buf[6]; - snprintf(buf, 6, "%u", ctx->conf->mtu); - setenv("MTU", buf, 1); + char buf[6]; + snprintf(buf, 6, "%u", ctx->conf->mtu); + setenv("MTU", buf, 1); - int ret = system(ctx->conf->on_down); + int ret = system(ctx->conf->on_down); - if (WIFSIGNALED(ret)) - pr_error(ctx, "on-down command exited with signal %i", WTERMSIG(ret)); - else if(ret) - pr_warn(ctx, "on-down command exited with status %i", WEXITSTATUS(ret)); + if (WIFSIGNALED(ret)) + pr_error(ctx, "on-down command exited with signal %i", WTERMSIG(ret)); + else if(ret) + pr_warn(ctx, "on-down 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_down_dir, strerror(errno)); + } - chdir(cwd); free(cwd); } @@ -306,7 +318,8 @@ static void handle_tasks(fastd_context *ctx) { fastd_peer_eth_addr_add(ctx, task->peer, src_addr); } - write(ctx->tunfd, task->handle_recv.buffer.data, task->handle_recv.buffer.len); + if (write(ctx->tunfd, task->handle_recv.buffer.data, task->handle_recv.buffer.len) < 0) + warn_errno(ctx, "write"); if (ctx->conf->mode == MODE_TAP && ctx->conf->peer_to_peer) { const fastd_eth_addr *dest_addr = fastd_get_dest_address(ctx, task->handle_recv.buffer); 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); } -- cgit v1.2.3