diff options
author | Daniel Borkmann <dborkman@redhat.com> | 2013-06-12 14:33:43 +0200 |
---|---|---|
committer | Daniel Borkmann <dborkman@redhat.com> | 2013-06-12 14:33:43 +0200 |
commit | dfb45a7ac75d17c3a8421bfb53840d6db271a121 (patch) | |
tree | 3d9882f7c077d7ff431a1379f89f2115640ce201 | |
parent | 44b463529a8a9f2ccd1ec2acb8a4dfc117a636cb (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.c | 8 | ||||
-rw-r--r-- | ioops.c | 16 | ||||
-rw-r--r-- | ioops.h | 2 | ||||
-rw-r--r-- | netsniff-ng.c | 8 | ||||
-rw-r--r-- | xmalloc.c | 8 | ||||
-rw-r--r-- | xmalloc.h | 1 |
6 files changed, 26 insertions, 17 deletions
@@ -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); @@ -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); @@ -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; @@ -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; -} @@ -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) { |