summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Borkmann <dborkman@redhat.com>2013-06-12 14:33:43 +0200
committerDaniel Borkmann <dborkman@redhat.com>2013-06-12 14:33:43 +0200
commitdfb45a7ac75d17c3a8421bfb53840d6db271a121 (patch)
tree3d9882f7c077d7ff431a1379f89f2115640ce201
parent44b463529a8a9f2ccd1ec2acb8a4dfc117a636cb (diff)
ioops: misc: add dup{,2}_or_die to ioops
Bail out if it should ever fail. Detected by coverty in the translate_pcap_to_txf() path. Signed-off-by: Daniel Borkmann <dborkman@redhat.com>
-rw-r--r--geoip.c8
-rw-r--r--ioops.c16
-rw-r--r--ioops.h2
-rw-r--r--netsniff-ng.c8
-rw-r--r--xmalloc.c8
-rw-r--r--xmalloc.h1
6 files changed, 26 insertions, 17 deletions
diff --git a/geoip.c b/geoip.c
index b403ad1..d027e14 100644
--- a/geoip.c
+++ b/geoip.c
@@ -348,10 +348,10 @@ static int fdout, fderr;
static void geoip_open_prepare(void)
{
fflush(stdout);
- fdout = dup(1);
+ fdout = dup_or_die(1);
fflush(stderr);
- fderr = dup(2);
+ fderr = dup_or_die(2);
close(1);
close(2);
@@ -359,8 +359,8 @@ static void geoip_open_prepare(void)
static void geoip_open_restore(void)
{
- dup2(fdout, 1);
- dup2(fderr, 2);
+ dup2_or_die(fdout, 1);
+ dup2_or_die(fderr, 2);
close(fdout);
close(fderr);
diff --git a/ioops.c b/ioops.c
index 7cec2fd..4ac0f3a 100644
--- a/ioops.c
+++ b/ioops.c
@@ -12,6 +12,7 @@
#include "dev.h"
#include "ioops.h"
#include "str.h"
+#include "built_in.h"
int open_or_die(const char *file, int flags)
{
@@ -30,6 +31,21 @@ int open_or_die_m(const char *file, int flags, mode_t mode)
return ret;
}
+int dup_or_die(int oldfd)
+{
+ int newfd = dup(oldfd);
+ if (unlikely(newfd < 0))
+ panic("Cannot dup old file descriptor!\n");
+ return newfd;
+}
+
+void dup2_or_die(int oldfd, int newfd)
+{
+ int ret = dup2(oldfd, newfd);
+ if (unlikely(ret < 0))
+ panic("Cannot dup2 old/new file descriptor!\n");
+}
+
void create_or_die(const char *file, mode_t mode)
{
int fd = open_or_die_m(file, O_WRONLY | O_CREAT, mode);
diff --git a/ioops.h b/ioops.h
index 6de6c62..8e12448 100644
--- a/ioops.h
+++ b/ioops.h
@@ -5,6 +5,8 @@
extern int open_or_die(const char *file, int flags);
extern int open_or_die_m(const char *file, int flags, mode_t mode);
+extern int dup_or_die(int oldfd);
+extern void dup2_or_die(int oldfd, int newfd);
extern void create_or_die(const char *file, mode_t mode);
extern int tun_open_or_die(char *name, int type);
extern void pipe_or_die(int pipefd[2], int flags);
diff --git a/netsniff-ng.c b/netsniff-ng.c
index 14ce8f2..2738b9a 100644
--- a/netsniff-ng.c
+++ b/netsniff-ng.c
@@ -184,7 +184,7 @@ static void pcap_to_xmit(struct ctx *ctx)
tx_sock = pf_socket();
if (!strncmp("-", ctx->device_in, strlen("-"))) {
- fd = dup(fileno(stdin));
+ fd = dup_or_die(fileno(stdin));
close(fileno(stdin));
if (ctx->pcap == PCAP_OPS_MM)
ctx->pcap = PCAP_OPS_SG;
@@ -544,7 +544,7 @@ static void read_pcap(struct ctx *ctx)
bug_on(!__pcap_io);
if (!strncmp("-", ctx->device_in, strlen("-"))) {
- fd = dup(fileno(stdin));
+ fd = dup_or_die(fileno(stdin));
close(fileno(stdin));
if (ctx->pcap == PCAP_OPS_MM)
ctx->pcap = PCAP_OPS_SG;
@@ -579,7 +579,7 @@ static void read_pcap(struct ctx *ctx)
if (ctx->device_out) {
if (!strncmp("-", ctx->device_out, strlen("-"))) {
- fdo = dup(fileno(stdout));
+ fdo = dup_or_die(fileno(stdout));
close(fileno(stdout));
} else {
fdo = open_or_die_m(ctx->device_out, O_RDWR | O_CREAT |
@@ -769,7 +769,7 @@ static int begin_single_pcap_file(struct ctx *ctx)
bug_on(!__pcap_io);
if (!strncmp("-", ctx->device_out, strlen("-"))) {
- fd = dup(fileno(stdout));
+ fd = dup_or_die(fileno(stdout));
close(fileno(stdout));
if (ctx->pcap == PCAP_OPS_MM)
ctx->pcap = PCAP_OPS_SG;
diff --git a/xmalloc.c b/xmalloc.c
index d5805b8..02d6ce4 100644
--- a/xmalloc.c
+++ b/xmalloc.c
@@ -141,11 +141,3 @@ char *xstrndup(const char *str, size_t size)
return cp;
}
-
-int xdup(int fd)
-{
- int ret = dup(fd);
- if (unlikely(ret < 0))
- panic("xdup: dup failed\n");
- return ret;
-}
diff --git a/xmalloc.h b/xmalloc.h
index 21ce84d..e1e4f8f 100644
--- a/xmalloc.h
+++ b/xmalloc.h
@@ -16,7 +16,6 @@ extern void *xrealloc(void *ptr, size_t nmemb, size_t size) __hidden;
extern void xfree_func(void *ptr) __hidden;
extern char *xstrdup(const char *str) __hidden;
extern char *xstrndup(const char *str, size_t size) __hidden;
-extern int xdup(int fd) __hidden;
static inline void __xfree(void *ptr)
{