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. * 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> #include <vdr/i18n.h>
@ -55,10 +55,7 @@ int select_single_desc(int descriptor, const int usecs, const bool selectWrite)
else else
retval = select(descriptor + 1, &fds, NULL, NULL, &tv); retval = select(descriptor + 1, &fds, NULL, NULL, &tv);
// Check if error // Check if error
if (retval < 0) { ERROR_IF(retval < 0, "select()", return retval);
char tmp[64];
error("ERROR: select(): %s", strerror_r(errno, tmp, sizeof(tmp)));
}
return retval; return retval;
} }

View File

@ -3,7 +3,7 @@
* *
* See the README file for copyright information and how to reach the author. * 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 #ifndef __IPTV_COMMON_H
@ -29,6 +29,18 @@
#define SECTION_FILTER_TABLE_SIZE 7 #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); uint16_t ts_pid(const uint8_t *buf);
uint8_t payload(const uint8_t *tsp); uint8_t payload(const uint8_t *tsp);
const char *id_pid(const u_short Pid); 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. * 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> #include <sys/wait.h>
@ -55,38 +55,19 @@ bool cIptvProtocolExt::OpenSocket(void)
int yes = 1; int yes = 1;
// Create socket // Create socket
socketDesc = socket(PF_INET, SOCK_DGRAM, 0); socketDesc = socket(PF_INET, SOCK_DGRAM, 0);
if (socketDesc < 0) { ERROR_IF(socketDesc < 0, "socket()", return false);
char tmp[64];
error("ERROR: socket(): %s", strerror_r(errno, tmp, sizeof(tmp)));
return false;
}
// Make it use non-blocking I/O to avoid stuck read calls // Make it use non-blocking I/O to avoid stuck read calls
if (fcntl(socketDesc, F_SETFL, O_NONBLOCK)) { ERROR_IF_FUNC(fcntl(socketDesc, F_SETFL, O_NONBLOCK), "fcntl()", CloseSocket(), return false);
char tmp[64];
error("ERROR: fcntl(): %s", strerror_r(errno, tmp, sizeof(tmp)));
CloseSocket();
return false;
}
// Allow multiple sockets to use the same PORT number // Allow multiple sockets to use the same PORT number
if (setsockopt(socketDesc, SOL_SOCKET, SO_REUSEADDR, &yes, ERROR_IF_FUNC(setsockopt(socketDesc, SOL_SOCKET, SO_REUSEADDR, &yes,
sizeof(yes)) < 0) { sizeof(yes)) < 0, "setsockopt()", CloseSocket(), return false);
char tmp[64];
error("ERROR: setsockopt(): %s", strerror_r(errno, tmp, sizeof(tmp)));
CloseSocket();
return false;
}
// Bind socket // Bind socket
memset(&sockAddr, '\0', sizeof(sockAddr)); memset(&sockAddr, '\0', sizeof(sockAddr));
sockAddr.sin_family = AF_INET; sockAddr.sin_family = AF_INET;
sockAddr.sin_port = htons(listenPort); sockAddr.sin_port = htons(listenPort);
sockAddr.sin_addr.s_addr = htonl(INADDR_ANY); sockAddr.sin_addr.s_addr = htonl(INADDR_ANY);
int err = bind(socketDesc, (struct sockaddr *)&sockAddr, sizeof(sockAddr)); int err = bind(socketDesc, (struct sockaddr *)&sockAddr, sizeof(sockAddr));
if (err < 0) { ERROR_IF_FUNC(err < 0, "bind()", CloseSocket(), return false);
char tmp[64];
error("ERROR: bind(): %s", strerror_r(errno, tmp, sizeof(tmp)));
CloseSocket();
return false;
}
} }
return true; return true;
} }
@ -110,11 +91,7 @@ void cIptvProtocolExt::ExecuteCommand(void)
return; return;
} }
// Let's fork // Let's fork
if ((pid = fork()) == -1) { ERROR_IF((pid = fork()) == -1, "fork()", return);
char tmp[64];
error("ERROR: fork(): %s", strerror_r(errno, tmp, sizeof(tmp)));
return;
}
// Check if child process // Check if child process
if (pid == 0) { if (pid == 0) {
// Close all dup'ed filedescriptors // Close all dup'ed filedescriptors
@ -148,11 +125,7 @@ void cIptvProtocolExt::TerminateCommand(void)
bool waitOver = false; bool waitOver = false;
// signal and wait for termination // signal and wait for termination
int retval = kill(pid, SIGINT); int retval = kill(pid, SIGINT);
if (retval < 0) { ERROR_IF(retval < 0, "kill()", waitOver = true);
char tmp[64];
error("ERROR: kill(): %s", strerror_r(errno, tmp, sizeof(tmp)));
waitOver = true;
}
while (!waitOver) { while (!waitOver) {
retval = 0; retval = 0;
waitms += timeoutms; waitms += timeoutms;
@ -164,11 +137,7 @@ void cIptvProtocolExt::TerminateCommand(void)
memset(&waitStatus, '\0', sizeof(waitStatus)); memset(&waitStatus, '\0', sizeof(waitStatus));
// Wait for child termination // Wait for child termination
retval = waitid(P_PID, pid, &waitStatus, (WNOHANG | WEXITED)); retval = waitid(P_PID, pid, &waitStatus, (WNOHANG | WEXITED));
if (retval < 0) { ERROR_IF(retval < 0, "waitid()", waitOver = true);
char tmp[64];
error("ERROR: waitid(): %s", strerror_r(errno, tmp, sizeof(tmp)));
waitOver = true;
}
// These are the acceptable conditions under which child exit is // These are the acceptable conditions under which child exit is
// regarded as successful // regarded as successful
if (!retval && waitStatus.si_pid && (waitStatus.si_pid == pid) && if (!retval && waitStatus.si_pid && (waitStatus.si_pid == pid) &&
@ -207,12 +176,8 @@ int cIptvProtocolExt::Read(unsigned char* *BufferAddr)
if (isActive) if (isActive)
len = recvfrom(socketDesc, readBuffer, readBufferLen, MSG_DONTWAIT, len = recvfrom(socketDesc, readBuffer, readBufferLen, MSG_DONTWAIT,
(struct sockaddr *)&sockAddr, &addrlen); (struct sockaddr *)&sockAddr, &addrlen);
if (len < 0) { ERROR_IF(len < 0, "recvfrom()", return len);
char tmp[64]; if ((len > 0) && (readBuffer[0] == 0x47)) {
error("ERROR: recvfrom(): %s", strerror_r(errno, tmp, sizeof(tmp)));
return len;
}
else if ((len > 0) && (readBuffer[0] == 0x47)) {
// Set argument point to read buffer // Set argument point to read buffer
*BufferAddr = &readBuffer[0]; *BufferAddr = &readBuffer[0];
return len; return len;

View File

@ -3,7 +3,7 @@
* *
* See the README file for copyright information and how to reach the author. * 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> #include <fcntl.h>
@ -44,11 +44,7 @@ bool cIptvProtocolFile::OpenFile(void)
// Check that stream address is valid // Check that stream address is valid
if (!isActive && !isempty(fileLocation)) { if (!isActive && !isempty(fileLocation)) {
fileStream = fopen(fileLocation, "rb"); fileStream = fopen(fileLocation, "rb");
if (ferror(fileStream) || !fileStream) { ERROR_IF(!fileStream || ferror(fileStream), "fopen()", return false);
char tmp[64];
error("ERROR: fopen(): %s", strerror_r(errno, tmp, sizeof(tmp)));
return false;
}
// Update active flag // Update active flag
isActive = true; isActive = true;
} }

View File

@ -3,7 +3,7 @@
* *
* See the README file for copyright information and how to reach the author. * 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> #include <sys/types.h>
@ -59,29 +59,12 @@ bool cIptvProtocolHttp::OpenSocket(const int Port)
int yes = 1; int yes = 1;
// Create socket // Create socket
socketDesc = socket(PF_INET, SOCK_STREAM, 0); socketDesc = socket(PF_INET, SOCK_STREAM, 0);
if (socketDesc < 0) { ERROR_IF(socketDesc < 0, "socket()", return false);
char tmp[64];
error("ERROR: socket(): %s", strerror_r(errno, tmp, sizeof(tmp)));
return false;
}
// Make it use non-blocking I/O to avoid stuck read calls // Make it use non-blocking I/O to avoid stuck read calls
if (fcntl(socketDesc, F_SETFL, O_NONBLOCK)) { ERROR_IF_FUNC(fcntl(socketDesc, F_SETFL, O_NONBLOCK), "fcntl()", CloseSocket(), return false);
char tmp[64];
error("ERROR: fcntl(): %s", strerror_r(errno, tmp, sizeof(tmp)));
CloseSocket();
return false;
}
// Allow multiple sockets to use the same PORT number // Allow multiple sockets to use the same PORT number
if (setsockopt(socketDesc, SOL_SOCKET, SO_REUSEADDR, &yes, ERROR_IF_FUNC(setsockopt(socketDesc, SOL_SOCKET, SO_REUSEADDR, &yes,
sizeof(yes)) < 0) { sizeof(yes)) < 0, "setsockopt()", CloseSocket(), return false);
char tmp[64];
error("ERROR: setsockopt(): %s", strerror_r(errno, tmp, sizeof(tmp)));
CloseSocket();
return false;
}
// Create default socket // Create default socket
memset(&sockAddr, '\0', sizeof(sockAddr)); memset(&sockAddr, '\0', sizeof(sockAddr));
sockAddr.sin_family = AF_INET; sockAddr.sin_family = AF_INET;
@ -134,13 +117,7 @@ bool cIptvProtocolHttp::Connect(void)
int err = connect(socketDesc, (struct sockaddr*)&sockAddr, int err = connect(socketDesc, (struct sockaddr*)&sockAddr,
sizeof(sockAddr)); sizeof(sockAddr));
// Non-blocking sockets always report in-progress error when connected // Non-blocking sockets always report in-progress error when connected
if (err < 0 && errno != EINPROGRESS) { ERROR_IF_FUNC(err < 0 && errno != EINPROGRESS, "connect()", CloseSocket(), return false);
char tmp[64];
error("ERROR: Connect(): %s", strerror_r(errno, tmp, sizeof(tmp)));
CloseSocket();
return false;
}
// Select on the socket completion, check if it is writable // Select on the socket completion, check if it is writable
int retval = select_single_desc(socketDesc, 800000, true); int retval = select_single_desc(socketDesc, 800000, true);
if (retval < 0) if (retval < 0)
@ -173,12 +150,7 @@ bool cIptvProtocolHttp::Connect(void)
//debug("Sending http request: %s\n", buffer); //debug("Sending http request: %s\n", buffer);
err = send(socketDesc, buffer, strlen(buffer), 0); err = send(socketDesc, buffer, strlen(buffer), 0);
if (err < 0) { ERROR_IF_FUNC(err < 0, "send()", CloseSocket(), return false);
char tmp[64];
error("ERROR: send(): %s", strerror_r(errno, tmp, sizeof(tmp)));
CloseSocket();
return false;
}
// Now process headers // Now process headers
if (!ProcessHeaders()) { if (!ProcessHeaders()) {

View File

@ -3,7 +3,7 @@
* *
* See the README file for copyright information and how to reach the author. * 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> #include <sys/types.h>
@ -56,38 +56,19 @@ bool cIptvProtocolUdp::OpenSocket(const int Port)
int yes = 1; int yes = 1;
// Create socket // Create socket
socketDesc = socket(PF_INET, SOCK_DGRAM, 0); socketDesc = socket(PF_INET, SOCK_DGRAM, 0);
if (socketDesc < 0) { ERROR_IF(socketDesc < 0, "socket()", return false);
char tmp[64];
error("ERROR: socket(): %s", strerror_r(errno, tmp, sizeof(tmp)));
return false;
}
// Make it use non-blocking I/O to avoid stuck read calls // Make it use non-blocking I/O to avoid stuck read calls
if (fcntl(socketDesc, F_SETFL, O_NONBLOCK)) { ERROR_IF_FUNC(fcntl(socketDesc, F_SETFL, O_NONBLOCK), "fcntl()", CloseSocket(), return false);
char tmp[64];
error("ERROR: fcntl(): %s", strerror_r(errno, tmp, sizeof(tmp)));
CloseSocket();
return false;
}
// Allow multiple sockets to use the same PORT number // Allow multiple sockets to use the same PORT number
if (setsockopt(socketDesc, SOL_SOCKET, SO_REUSEADDR, &yes, ERROR_IF_FUNC(setsockopt(socketDesc, SOL_SOCKET, SO_REUSEADDR, &yes,
sizeof(yes)) < 0) { sizeof(yes)) < 0, "setsockopt()", CloseSocket(), return false);
char tmp[64];
error("ERROR: setsockopt(): %s", strerror_r(errno, tmp, sizeof(tmp)));
CloseSocket();
return false;
}
// Bind socket // Bind socket
memset(&sockAddr, '\0', sizeof(sockAddr)); memset(&sockAddr, '\0', sizeof(sockAddr));
sockAddr.sin_family = AF_INET; sockAddr.sin_family = AF_INET;
sockAddr.sin_port = htons(Port); sockAddr.sin_port = htons(Port);
sockAddr.sin_addr.s_addr = htonl(INADDR_ANY); sockAddr.sin_addr.s_addr = htonl(INADDR_ANY);
int err = bind(socketDesc, (struct sockaddr *)&sockAddr, sizeof(sockAddr)); int err = bind(socketDesc, (struct sockaddr *)&sockAddr, sizeof(sockAddr));
if (err < 0) { ERROR_IF_FUNC(err < 0, "bind()", CloseSocket(), return false);
char tmp[64];
error("ERROR: bind(): %s", strerror_r(errno, tmp, sizeof(tmp)));
CloseSocket();
return false;
}
// Update stream port // Update stream port
streamPort = Port; streamPort = Port;
} }
@ -117,11 +98,7 @@ bool cIptvProtocolUdp::JoinMulticast(void)
mreq.imr_interface.s_addr = htonl(INADDR_ANY); mreq.imr_interface.s_addr = htonl(INADDR_ANY);
int err = setsockopt(socketDesc, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, int err = setsockopt(socketDesc, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq,
sizeof(mreq)); sizeof(mreq));
if (err < 0) { ERROR_IF(err < 0, "setsockopt()", return false);
char tmp[64];
error("ERROR: setsockopt(): %s", strerror_r(errno, tmp, sizeof(tmp)));
return false;
}
// Update multicasting flag // Update multicasting flag
isActive = true; isActive = true;
} }
@ -141,11 +118,7 @@ bool cIptvProtocolUdp::DropMulticast(void)
mreq.imr_interface.s_addr = htonl(INADDR_ANY); mreq.imr_interface.s_addr = htonl(INADDR_ANY);
int err = setsockopt(socketDesc, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, int err = setsockopt(socketDesc, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq,
sizeof(mreq)); sizeof(mreq));
if (err < 0) { ERROR_IF(err < 0, "setsockopt()", return false);
char tmp[64];
error("ERROR: setsockopt(): %s", strerror_r(errno, tmp, sizeof(tmp)));
return false;
}
// Update multicasting flag // Update multicasting flag
isActive = false; isActive = false;
} }
@ -175,12 +148,8 @@ int cIptvProtocolUdp::Read(unsigned char* *BufferAddr)
if (isActive) if (isActive)
len = recvfrom(socketDesc, readBuffer, readBufferLen, MSG_DONTWAIT, len = recvfrom(socketDesc, readBuffer, readBufferLen, MSG_DONTWAIT,
(struct sockaddr *)&sockAddr, &addrlen); (struct sockaddr *)&sockAddr, &addrlen);
if (len < 0) { ERROR_IF(len < 0, "recvfrom()", return len);
char tmp[64]; if ((len > 0) && (readBuffer[0] == 0x47)) {
error("ERROR: recvfrom(): %s", strerror_r(errno, tmp, sizeof(tmp)));
return len;
}
else if ((len > 0) && (readBuffer[0] == 0x47)) {
// Set argument point to read buffer // Set argument point to read buffer
*BufferAddr = &readBuffer[0]; *BufferAddr = &readBuffer[0];
return len; return len;

View File

@ -3,7 +3,7 @@
* *
* See the README file for copyright information and how to reach the author. * 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" #include "sectionfilter.h"
@ -56,11 +56,7 @@ cIptvSectionFilter::cIptvSectionFilter(int Index, int devInd,
if (S_ISFIFO(sb.st_mode)) if (S_ISFIFO(sb.st_mode))
unlink(pipeName); unlink(pipeName);
int err = mknod(pipeName, 0644 | S_IFIFO, 0); int err = mknod(pipeName, 0644 | S_IFIFO, 0);
if (err < 0) { ERROR_IF(err < 0, "mknod()", return);
char tmp[64];
error("ERROR: mknod(): %s", strerror_r(errno, tmp, sizeof(tmp)));
return;
}
// Create descriptors // Create descriptors
fifoDescriptor = open(pipeName, O_RDWR | O_NONBLOCK); 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"); printf("\n");
#endif #endif
retval = write(fifoDescriptor, buffer1, buffer1_len); retval = write(fifoDescriptor, buffer1, buffer1_len);
if (retval < 0) { ERROR_IF(retval < 0, "write()", );
char tmp[64];
error("ERROR: write(): %s", strerror_r(errno, tmp, sizeof(tmp)));
}
// Update statistics // Update statistics
AddStatistic(retval, 1); AddStatistic(retval, 1);
} }