From 64dd6a5f3f7792af8b2852eabeb8252b2754c29d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antti=20Sepp=C3=A4l=C3=A4?= Date: Sat, 20 Oct 2007 23:16:28 +0000 Subject: [PATCH] Reduce duplicated code by using an error -macro. --- common.c | 7 ++---- common.h | 14 +++++++++++- protocolext.c | 57 ++++++++++--------------------------------------- protocolfile.c | 8 ++----- protocolhttp.c | 42 ++++++------------------------------ protocoludp.c | 51 +++++++++---------------------------------- sectionfilter.c | 13 +++-------- 7 files changed, 48 insertions(+), 144 deletions(-) diff --git a/common.c b/common.c index 1e64017..9484599 100644 --- a/common.c +++ b/common.c @@ -3,7 +3,7 @@ * * See the README file for copyright information and how to reach the author. * - * $Id: common.c,v 1.5 2007/10/20 20:43:22 ajhseppa Exp $ + * $Id: common.c,v 1.6 2007/10/20 23:16:28 ajhseppa Exp $ */ #include @@ -55,10 +55,7 @@ int select_single_desc(int descriptor, const int usecs, const bool selectWrite) else retval = select(descriptor + 1, &fds, NULL, NULL, &tv); // Check if error - if (retval < 0) { - char tmp[64]; - error("ERROR: select(): %s", strerror_r(errno, tmp, sizeof(tmp))); - } + ERROR_IF(retval < 0, "select()", return retval); return retval; } diff --git a/common.h b/common.h index fab134b..024394f 100644 --- a/common.h +++ b/common.h @@ -3,7 +3,7 @@ * * See the README file for copyright information and how to reach the author. * - * $Id: common.h,v 1.11 2007/10/20 20:43:22 ajhseppa Exp $ + * $Id: common.h,v 1.12 2007/10/20 23:16:28 ajhseppa Exp $ */ #ifndef __IPTV_COMMON_H @@ -29,6 +29,18 @@ #define SECTION_FILTER_TABLE_SIZE 7 +#define ERROR_IF_FUNC(exp, errstr, func, ret) \ + do { \ + if (exp) { \ + char tmp[64]; \ + error("ERROR: "errstr": %s", strerror_r(errno, tmp, sizeof(tmp))); \ + func; \ + ret; \ + } \ + } while (0) + +#define ERROR_IF(exp, errstr, ret) ERROR_IF_FUNC(exp, errstr, ,ret); + uint16_t ts_pid(const uint8_t *buf); uint8_t payload(const uint8_t *tsp); const char *id_pid(const u_short Pid); diff --git a/protocolext.c b/protocolext.c index ce30ae3..7e499b0 100644 --- a/protocolext.c +++ b/protocolext.c @@ -3,7 +3,7 @@ * * See the README file for copyright information and how to reach the author. * - * $Id: protocolext.c,v 1.14 2007/10/20 20:43:22 ajhseppa Exp $ + * $Id: protocolext.c,v 1.15 2007/10/20 23:16:28 ajhseppa Exp $ */ #include @@ -55,38 +55,19 @@ bool cIptvProtocolExt::OpenSocket(void) int yes = 1; // Create socket socketDesc = socket(PF_INET, SOCK_DGRAM, 0); - if (socketDesc < 0) { - char tmp[64]; - error("ERROR: socket(): %s", strerror_r(errno, tmp, sizeof(tmp))); - return false; - } + ERROR_IF(socketDesc < 0, "socket()", return false); // Make it use non-blocking I/O to avoid stuck read calls - if (fcntl(socketDesc, F_SETFL, O_NONBLOCK)) { - char tmp[64]; - error("ERROR: fcntl(): %s", strerror_r(errno, tmp, sizeof(tmp))); - CloseSocket(); - return false; - } + ERROR_IF_FUNC(fcntl(socketDesc, F_SETFL, O_NONBLOCK), "fcntl()", CloseSocket(), return false); // Allow multiple sockets to use the same PORT number - if (setsockopt(socketDesc, SOL_SOCKET, SO_REUSEADDR, &yes, - sizeof(yes)) < 0) { - char tmp[64]; - error("ERROR: setsockopt(): %s", strerror_r(errno, tmp, sizeof(tmp))); - CloseSocket(); - return false; - } + ERROR_IF_FUNC(setsockopt(socketDesc, SOL_SOCKET, SO_REUSEADDR, &yes, + sizeof(yes)) < 0, "setsockopt()", CloseSocket(), return false); // Bind socket memset(&sockAddr, '\0', sizeof(sockAddr)); sockAddr.sin_family = AF_INET; sockAddr.sin_port = htons(listenPort); sockAddr.sin_addr.s_addr = htonl(INADDR_ANY); int err = bind(socketDesc, (struct sockaddr *)&sockAddr, sizeof(sockAddr)); - if (err < 0) { - char tmp[64]; - error("ERROR: bind(): %s", strerror_r(errno, tmp, sizeof(tmp))); - CloseSocket(); - return false; - } + ERROR_IF_FUNC(err < 0, "bind()", CloseSocket(), return false); } return true; } @@ -110,11 +91,7 @@ void cIptvProtocolExt::ExecuteCommand(void) return; } // Let's fork - if ((pid = fork()) == -1) { - char tmp[64]; - error("ERROR: fork(): %s", strerror_r(errno, tmp, sizeof(tmp))); - return; - } + ERROR_IF((pid = fork()) == -1, "fork()", return); // Check if child process if (pid == 0) { // Close all dup'ed filedescriptors @@ -148,11 +125,7 @@ void cIptvProtocolExt::TerminateCommand(void) bool waitOver = false; // signal and wait for termination int retval = kill(pid, SIGINT); - if (retval < 0) { - char tmp[64]; - error("ERROR: kill(): %s", strerror_r(errno, tmp, sizeof(tmp))); - waitOver = true; - } + ERROR_IF(retval < 0, "kill()", waitOver = true); while (!waitOver) { retval = 0; waitms += timeoutms; @@ -164,11 +137,7 @@ void cIptvProtocolExt::TerminateCommand(void) memset(&waitStatus, '\0', sizeof(waitStatus)); // Wait for child termination retval = waitid(P_PID, pid, &waitStatus, (WNOHANG | WEXITED)); - if (retval < 0) { - char tmp[64]; - error("ERROR: waitid(): %s", strerror_r(errno, tmp, sizeof(tmp))); - waitOver = true; - } + ERROR_IF(retval < 0, "waitid()", waitOver = true); // These are the acceptable conditions under which child exit is // regarded as successful if (!retval && waitStatus.si_pid && (waitStatus.si_pid == pid) && @@ -207,12 +176,8 @@ int cIptvProtocolExt::Read(unsigned char* *BufferAddr) if (isActive) len = recvfrom(socketDesc, readBuffer, readBufferLen, MSG_DONTWAIT, (struct sockaddr *)&sockAddr, &addrlen); - if (len < 0) { - char tmp[64]; - error("ERROR: recvfrom(): %s", strerror_r(errno, tmp, sizeof(tmp))); - return len; - } - else if ((len > 0) && (readBuffer[0] == 0x47)) { + ERROR_IF(len < 0, "recvfrom()", return len); + if ((len > 0) && (readBuffer[0] == 0x47)) { // Set argument point to read buffer *BufferAddr = &readBuffer[0]; return len; diff --git a/protocolfile.c b/protocolfile.c index 3f32a19..4829985 100644 --- a/protocolfile.c +++ b/protocolfile.c @@ -3,7 +3,7 @@ * * See the README file for copyright information and how to reach the author. * - * $Id: protocolfile.c,v 1.12 2007/10/19 22:18:55 rahrenbe Exp $ + * $Id: protocolfile.c,v 1.13 2007/10/20 23:16:28 ajhseppa Exp $ */ #include @@ -44,11 +44,7 @@ bool cIptvProtocolFile::OpenFile(void) // Check that stream address is valid if (!isActive && !isempty(fileLocation)) { fileStream = fopen(fileLocation, "rb"); - if (ferror(fileStream) || !fileStream) { - char tmp[64]; - error("ERROR: fopen(): %s", strerror_r(errno, tmp, sizeof(tmp))); - return false; - } + ERROR_IF(!fileStream || ferror(fileStream), "fopen()", return false); // Update active flag isActive = true; } diff --git a/protocolhttp.c b/protocolhttp.c index 8ae4eca..0b875a6 100644 --- a/protocolhttp.c +++ b/protocolhttp.c @@ -3,7 +3,7 @@ * * See the README file for copyright information and how to reach the author. * - * $Id: protocolhttp.c,v 1.15 2007/10/20 20:43:22 ajhseppa Exp $ + * $Id: protocolhttp.c,v 1.16 2007/10/20 23:16:28 ajhseppa Exp $ */ #include @@ -59,29 +59,12 @@ bool cIptvProtocolHttp::OpenSocket(const int Port) int yes = 1; // Create socket socketDesc = socket(PF_INET, SOCK_STREAM, 0); - if (socketDesc < 0) { - char tmp[64]; - error("ERROR: socket(): %s", strerror_r(errno, tmp, sizeof(tmp))); - return false; - } - + ERROR_IF(socketDesc < 0, "socket()", return false); // Make it use non-blocking I/O to avoid stuck read calls - if (fcntl(socketDesc, F_SETFL, O_NONBLOCK)) { - char tmp[64]; - error("ERROR: fcntl(): %s", strerror_r(errno, tmp, sizeof(tmp))); - CloseSocket(); - return false; - } - + ERROR_IF_FUNC(fcntl(socketDesc, F_SETFL, O_NONBLOCK), "fcntl()", CloseSocket(), return false); // Allow multiple sockets to use the same PORT number - if (setsockopt(socketDesc, SOL_SOCKET, SO_REUSEADDR, &yes, - sizeof(yes)) < 0) { - char tmp[64]; - error("ERROR: setsockopt(): %s", strerror_r(errno, tmp, sizeof(tmp))); - CloseSocket(); - return false; - } - + ERROR_IF_FUNC(setsockopt(socketDesc, SOL_SOCKET, SO_REUSEADDR, &yes, + sizeof(yes)) < 0, "setsockopt()", CloseSocket(), return false); // Create default socket memset(&sockAddr, '\0', sizeof(sockAddr)); sockAddr.sin_family = AF_INET; @@ -134,13 +117,7 @@ bool cIptvProtocolHttp::Connect(void) int err = connect(socketDesc, (struct sockaddr*)&sockAddr, sizeof(sockAddr)); // Non-blocking sockets always report in-progress error when connected - if (err < 0 && errno != EINPROGRESS) { - char tmp[64]; - error("ERROR: Connect(): %s", strerror_r(errno, tmp, sizeof(tmp))); - CloseSocket(); - return false; - } - + ERROR_IF_FUNC(err < 0 && errno != EINPROGRESS, "connect()", CloseSocket(), return false); // Select on the socket completion, check if it is writable int retval = select_single_desc(socketDesc, 800000, true); if (retval < 0) @@ -173,12 +150,7 @@ bool cIptvProtocolHttp::Connect(void) //debug("Sending http request: %s\n", buffer); err = send(socketDesc, buffer, strlen(buffer), 0); - if (err < 0) { - char tmp[64]; - error("ERROR: send(): %s", strerror_r(errno, tmp, sizeof(tmp))); - CloseSocket(); - return false; - } + ERROR_IF_FUNC(err < 0, "send()", CloseSocket(), return false); // Now process headers if (!ProcessHeaders()) { diff --git a/protocoludp.c b/protocoludp.c index 36cfce5..af82547 100644 --- a/protocoludp.c +++ b/protocoludp.c @@ -3,7 +3,7 @@ * * See the README file for copyright information and how to reach the author. * - * $Id: protocoludp.c,v 1.16 2007/10/20 20:43:22 ajhseppa Exp $ + * $Id: protocoludp.c,v 1.17 2007/10/20 23:16:28 ajhseppa Exp $ */ #include @@ -56,38 +56,19 @@ bool cIptvProtocolUdp::OpenSocket(const int Port) int yes = 1; // Create socket socketDesc = socket(PF_INET, SOCK_DGRAM, 0); - if (socketDesc < 0) { - char tmp[64]; - error("ERROR: socket(): %s", strerror_r(errno, tmp, sizeof(tmp))); - return false; - } + ERROR_IF(socketDesc < 0, "socket()", return false); // Make it use non-blocking I/O to avoid stuck read calls - if (fcntl(socketDesc, F_SETFL, O_NONBLOCK)) { - char tmp[64]; - error("ERROR: fcntl(): %s", strerror_r(errno, tmp, sizeof(tmp))); - CloseSocket(); - return false; - } + ERROR_IF_FUNC(fcntl(socketDesc, F_SETFL, O_NONBLOCK), "fcntl()", CloseSocket(), return false); // Allow multiple sockets to use the same PORT number - if (setsockopt(socketDesc, SOL_SOCKET, SO_REUSEADDR, &yes, - sizeof(yes)) < 0) { - char tmp[64]; - error("ERROR: setsockopt(): %s", strerror_r(errno, tmp, sizeof(tmp))); - CloseSocket(); - return false; - } + ERROR_IF_FUNC(setsockopt(socketDesc, SOL_SOCKET, SO_REUSEADDR, &yes, + sizeof(yes)) < 0, "setsockopt()", CloseSocket(), return false); // Bind socket memset(&sockAddr, '\0', sizeof(sockAddr)); sockAddr.sin_family = AF_INET; sockAddr.sin_port = htons(Port); sockAddr.sin_addr.s_addr = htonl(INADDR_ANY); int err = bind(socketDesc, (struct sockaddr *)&sockAddr, sizeof(sockAddr)); - if (err < 0) { - char tmp[64]; - error("ERROR: bind(): %s", strerror_r(errno, tmp, sizeof(tmp))); - CloseSocket(); - return false; - } + ERROR_IF_FUNC(err < 0, "bind()", CloseSocket(), return false); // Update stream port streamPort = Port; } @@ -117,11 +98,7 @@ bool cIptvProtocolUdp::JoinMulticast(void) mreq.imr_interface.s_addr = htonl(INADDR_ANY); int err = setsockopt(socketDesc, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)); - if (err < 0) { - char tmp[64]; - error("ERROR: setsockopt(): %s", strerror_r(errno, tmp, sizeof(tmp))); - return false; - } + ERROR_IF(err < 0, "setsockopt()", return false); // Update multicasting flag isActive = true; } @@ -141,11 +118,7 @@ bool cIptvProtocolUdp::DropMulticast(void) mreq.imr_interface.s_addr = htonl(INADDR_ANY); int err = setsockopt(socketDesc, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq)); - if (err < 0) { - char tmp[64]; - error("ERROR: setsockopt(): %s", strerror_r(errno, tmp, sizeof(tmp))); - return false; - } + ERROR_IF(err < 0, "setsockopt()", return false); // Update multicasting flag isActive = false; } @@ -175,12 +148,8 @@ int cIptvProtocolUdp::Read(unsigned char* *BufferAddr) if (isActive) len = recvfrom(socketDesc, readBuffer, readBufferLen, MSG_DONTWAIT, (struct sockaddr *)&sockAddr, &addrlen); - if (len < 0) { - char tmp[64]; - error("ERROR: recvfrom(): %s", strerror_r(errno, tmp, sizeof(tmp))); - return len; - } - else if ((len > 0) && (readBuffer[0] == 0x47)) { + ERROR_IF(len < 0, "recvfrom()", return len); + if ((len > 0) && (readBuffer[0] == 0x47)) { // Set argument point to read buffer *BufferAddr = &readBuffer[0]; return len; diff --git a/sectionfilter.c b/sectionfilter.c index 89b964c..a34966b 100644 --- a/sectionfilter.c +++ b/sectionfilter.c @@ -3,7 +3,7 @@ * * See the README file for copyright information and how to reach the author. * - * $Id: sectionfilter.c,v 1.13 2007/10/20 20:43:22 ajhseppa Exp $ + * $Id: sectionfilter.c,v 1.14 2007/10/20 23:16:28 ajhseppa Exp $ */ #include "sectionfilter.h" @@ -56,11 +56,7 @@ cIptvSectionFilter::cIptvSectionFilter(int Index, int devInd, if (S_ISFIFO(sb.st_mode)) unlink(pipeName); int err = mknod(pipeName, 0644 | S_IFIFO, 0); - if (err < 0) { - char tmp[64]; - error("ERROR: mknod(): %s", strerror_r(errno, tmp, sizeof(tmp))); - return; - } + ERROR_IF(err < 0, "mknod()", return); // Create descriptors fifoDescriptor = open(pipeName, O_RDWR | O_NONBLOCK); @@ -108,10 +104,7 @@ int cIptvSectionFilter::dmxdev_section_callback(const uint8_t *buffer1, size_t b printf("\n"); #endif retval = write(fifoDescriptor, buffer1, buffer1_len); - if (retval < 0) { - char tmp[64]; - error("ERROR: write(): %s", strerror_r(errno, tmp, sizeof(tmp))); - } + ERROR_IF(retval < 0, "write()", ); // Update statistics AddStatistic(retval, 1); }