1
0
mirror of https://github.com/rofafor/vdr-plugin-iptv.git synced 2023-10-10 13:37:03 +02:00
vdr-plugin-iptv/protocolext.c

184 lines
5.3 KiB
C
Raw Normal View History

/*
* protocolext.c: IPTV plugin for the Video Disk Recorder
*
* See the README file for copyright information and how to reach the author.
*
*/
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <fcntl.h>
#include <unistd.h>
#include <vdr/device.h>
#include <vdr/plugin.h>
#include "common.h"
#include "config.h"
#include "protocolext.h"
#ifndef EXTSHELL
#define EXTSHELL "/bin/bash"
#endif
cIptvProtocolExt::cIptvProtocolExt()
2013-02-23 14:31:11 +01:00
: pidM(-1),
scriptFileM(""),
scriptParameterM(0),
streamPortM(0)
{
2013-02-23 16:12:46 +01:00
debug("cIptvProtocolExt::%s()", __FUNCTION__);
}
cIptvProtocolExt::~cIptvProtocolExt()
{
2013-02-23 16:12:46 +01:00
debug("cIptvProtocolExt::%s()", __FUNCTION__);
// Drop the socket connection
2008-01-05 00:36:37 +01:00
cIptvProtocolExt::Close();
}
2007-10-21 11:24:24 +02:00
void cIptvProtocolExt::ExecuteScript(void)
{
2013-02-23 16:12:46 +01:00
debug("cIptvProtocolExt::%s()", __FUNCTION__);
2007-10-19 23:36:27 +02:00
// Check if already executing
2013-02-23 16:12:46 +01:00
if (isActiveM || isempty(scriptFileM))
return;
2013-02-23 14:31:11 +01:00
if (pidM > 0) {
error("Cannot execute script!");
2007-10-19 23:36:27 +02:00
return;
}
// Let's fork
2013-02-23 14:31:11 +01:00
ERROR_IF_RET((pidM = fork()) == -1, "fork()", return);
// Check if child process
2013-02-23 14:31:11 +01:00
if (pidM == 0) {
// Close all dup'ed filedescriptors
int MaxPossibleFileDescriptors = getdtablesize();
for (int i = STDERR_FILENO + 1; i < MaxPossibleFileDescriptors; i++)
close(i);
// Execute the external script
2013-02-23 14:31:11 +01:00
cString cmd = cString::sprintf("%s %d %d", *scriptFileM, scriptParameterM, streamPortM);
2013-02-23 16:12:46 +01:00
debug("cIptvProtocolExt::%s(child): %s", __FUNCTION__, *cmd);
// Create a new session for a process group
ERROR_IF_RET(setsid() == -1, "setsid()", _exit(-1));
if (execl(EXTSHELL, "sh", "-c", *cmd, (char *)NULL) == -1) {
error("Script execution failed: %s", *cmd);
_exit(-1);
}
_exit(0);
}
else {
2013-02-23 16:12:46 +01:00
debug("cIptvProtocolExt::%s(): pid=%d", __FUNCTION__, pidM);
}
}
2007-10-21 11:24:24 +02:00
void cIptvProtocolExt::TerminateScript(void)
{
2013-02-23 16:12:46 +01:00
debug("cIptvProtocolExt::%s(): pid=%d", __FUNCTION__, pidM);
if (!isActiveM || isempty(scriptFileM))
return;
2013-02-23 14:31:11 +01:00
if (pidM > 0) {
const unsigned int timeoutms = 100;
unsigned int waitms = 0;
bool waitOver = false;
// Signal and wait for termination
2013-02-23 14:31:11 +01:00
int retval = killpg(pidM, SIGINT);
ERROR_IF_RET(retval < 0, "kill()", waitOver = true);
while (!waitOver) {
retval = 0;
waitms += timeoutms;
if ((waitms % 2000) == 0) {
2013-02-23 14:31:11 +01:00
error("Script '%s' won't terminate - killing it!", *scriptFileM);
killpg(pidM, SIGKILL);
}
// Clear wait status to make sure child exit status is accessible
// and wait for child termination
#ifdef __FreeBSD__
int waitStatus = 0;
2013-02-23 14:31:11 +01:00
retval = waitpid(pidM, &waitStatus, WNOHANG);
#else // __FreeBSD__
siginfo_t waitStatus;
2013-02-23 16:12:46 +01:00
memset(&waitStatus, 0, sizeof(waitStatus));
2013-02-23 14:31:11 +01:00
retval = waitid(P_PID, pidM, &waitStatus, (WNOHANG | WEXITED));
#endif // __FreeBSD__
ERROR_IF_RET(retval < 0, "waitid()", waitOver = true);
// These are the acceptable conditions under which child exit is
// regarded as successful
#ifdef __FreeBSD__
if (retval > 0 && (WIFEXITED(waitStatus) || WIFSIGNALED(waitStatus))) {
#else // __FreeBSD__
2013-02-23 14:31:11 +01:00
if (!retval && waitStatus.si_pid && (waitStatus.si_pid == pidM) &&
2007-10-19 23:36:27 +02:00
((waitStatus.si_code == CLD_EXITED) || (waitStatus.si_code == CLD_KILLED))) {
#endif // __FreeBSD__
2013-02-23 16:12:46 +01:00
debug("cIptvProtocolExt::%s(): child (%d) exited as expected", __FUNCTION__, pidM);
waitOver = true;
}
// Unsuccessful wait, avoid busy looping
if (!waitOver)
cCondWait::SleepMs(timeoutms);
2007-10-19 23:36:27 +02:00
}
2013-02-23 14:31:11 +01:00
pidM = -1;
}
}
bool cIptvProtocolExt::Open(void)
{
2013-02-23 16:12:46 +01:00
debug("cIptvProtocolExt::%s()", __FUNCTION__);
2007-10-19 23:36:27 +02:00
// Reject empty script files
2013-02-23 14:31:11 +01:00
if (!strlen(*scriptFileM))
2008-01-30 22:57:33 +01:00
return false;
// Create the listening socket
2013-02-23 14:31:11 +01:00
OpenSocket(streamPortM);
2007-10-21 11:24:24 +02:00
// Execute the external script
ExecuteScript();
2013-02-23 16:12:46 +01:00
isActiveM = true;
2007-10-19 23:36:27 +02:00
return true;
}
bool cIptvProtocolExt::Close(void)
{
2013-02-23 16:12:46 +01:00
debug("cIptvProtocolExt::%s()", __FUNCTION__);
2007-10-19 23:36:27 +02:00
// Terminate the external script
2007-10-21 11:24:24 +02:00
TerminateScript();
2013-02-23 16:12:46 +01:00
isActiveM = false;
// Close the socket
CloseSocket();
2007-10-19 23:36:27 +02:00
return true;
}
2013-02-23 14:31:11 +01:00
int cIptvProtocolExt::Read(unsigned char* bufferAddrP, unsigned int bufferLenP)
{
2013-02-23 14:31:11 +01:00
return cIptvUdpSocket::Read(bufferAddrP, bufferLenP);
}
2014-02-09 18:22:02 +01:00
bool cIptvProtocolExt::SetSource(const char* locationP, const int parameterP, const int indexP)
{
2013-02-23 16:12:46 +01:00
debug("cIptvProtocolExt::%s(%s, %d, %d)", __FUNCTION__, locationP, parameterP, indexP);
2013-02-23 14:31:11 +01:00
if (!isempty(locationP)) {
struct stat stbuf;
// Update script file and parameter
2013-02-23 14:31:11 +01:00
scriptFileM = cString::sprintf("%s/%s", IptvConfig.GetConfigDirectory(), locationP);
if ((stat(*scriptFileM, &stbuf) != 0) || (strstr(*scriptFileM, "..") != 0)) {
error("Non-existent or relative path script '%s'", *scriptFileM);
return false;
}
2013-02-23 14:31:11 +01:00
scriptParameterM = parameterP;
// Update listen port
2014-02-19 20:38:11 +01:00
streamPortM = IptvConfig.GetProtocolBasePort() + indexP * 2;
}
return true;
}
2014-02-09 18:22:02 +01:00
bool cIptvProtocolExt::SetPid(int pidP, int typeP, bool onP)
{
//debug("cIptvProtocolExt::%s(%d, %d, %d)", __FUNCTION__, pidP, typeP, onP);
return true;
}
cString cIptvProtocolExt::GetInformation(void)
{
2013-02-23 16:12:46 +01:00
//debug("cIptvProtocolExt::%s()", __FUNCTION__);
2013-02-23 14:31:11 +01:00
return cString::sprintf("ext://%s:%d", *scriptFileM, scriptParameterM);
}