Reduce duplicated code by using an error -macro.

This commit is contained in:
Antti Seppälä 2007-10-20 23:16:28 +00:00
parent 30409fac2c
commit 64dd6a5f3f
7 changed files with 48 additions and 144 deletions

View File

@ -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 <vdr/i18n.h>
@ -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;
}

View File

@ -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);

View File

@ -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 <sys/wait.h>
@ -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;

View File

@ -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 <fcntl.h>
@ -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;
}

View File

@ -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 <sys/types.h>
@ -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()) {

View File

@ -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 <sys/types.h>
@ -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;

View File

@ -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);
}