2007-10-15 22:06:38 +02:00
|
|
|
/*
|
|
|
|
* protocolext.c: IPTV plugin for the Video Disk Recorder
|
|
|
|
*
|
|
|
|
* See the README file for copyright information and how to reach the author.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2007-10-18 21:33:15 +02:00
|
|
|
#include <sys/wait.h>
|
2007-10-15 22:06:38 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <vdr/device.h>
|
2007-10-16 22:03:59 +02:00
|
|
|
#include <vdr/plugin.h>
|
2007-10-15 22:06:38 +02:00
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
#include "config.h"
|
|
|
|
#include "protocolext.h"
|
|
|
|
|
2012-07-10 21:12:29 +02:00
|
|
|
#ifndef EXTSHELL
|
|
|
|
#define EXTSHELL "/bin/bash"
|
|
|
|
#endif
|
|
|
|
|
2007-10-15 22:06:38 +02:00
|
|
|
cIptvProtocolExt::cIptvProtocolExt()
|
2013-02-23 14:31:11 +01:00
|
|
|
: pidM(-1),
|
|
|
|
scriptFileM(""),
|
|
|
|
scriptParameterM(0),
|
|
|
|
streamPortM(0)
|
2007-10-15 22:06:38 +02:00
|
|
|
{
|
2013-02-23 16:12:46 +01:00
|
|
|
debug("cIptvProtocolExt::%s()", __FUNCTION__);
|
2007-10-15 22:06:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
cIptvProtocolExt::~cIptvProtocolExt()
|
|
|
|
{
|
2013-02-23 16:12:46 +01:00
|
|
|
debug("cIptvProtocolExt::%s()", __FUNCTION__);
|
2007-10-21 15:31:21 +02:00
|
|
|
// Drop the socket connection
|
2008-01-05 00:36:37 +01:00
|
|
|
cIptvProtocolExt::Close();
|
2007-10-15 22:06:38 +02:00
|
|
|
}
|
|
|
|
|
2007-10-21 11:24:24 +02:00
|
|
|
void cIptvProtocolExt::ExecuteScript(void)
|
2007-10-18 21:33:15 +02:00
|
|
|
{
|
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))
|
2010-10-25 13:43:38 +02:00
|
|
|
return;
|
2013-02-23 14:31:11 +01:00
|
|
|
if (pidM > 0) {
|
2009-10-01 17:48:28 +02:00
|
|
|
error("Cannot execute script!");
|
2007-10-19 23:36:27 +02:00
|
|
|
return;
|
|
|
|
}
|
2007-10-18 21:33:15 +02:00
|
|
|
// Let's fork
|
2013-02-23 14:31:11 +01:00
|
|
|
ERROR_IF_RET((pidM = fork()) == -1, "fork()", return);
|
2007-10-18 21:33:15 +02:00
|
|
|
// Check if child process
|
2013-02-23 14:31:11 +01:00
|
|
|
if (pidM == 0) {
|
2007-10-18 21:33:15 +02:00
|
|
|
// 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);
|
2012-07-10 21:12:29 +02:00
|
|
|
// 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) {
|
2009-10-01 17:48:28 +02:00
|
|
|
error("Script execution failed: %s", *cmd);
|
2007-10-18 21:33:15 +02:00
|
|
|
_exit(-1);
|
|
|
|
}
|
|
|
|
_exit(0);
|
|
|
|
}
|
|
|
|
else {
|
2013-02-23 16:12:46 +01:00
|
|
|
debug("cIptvProtocolExt::%s(): pid=%d", __FUNCTION__, pidM);
|
2007-10-18 21:33:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-21 11:24:24 +02:00
|
|
|
void cIptvProtocolExt::TerminateScript(void)
|
2007-10-18 21:33:15 +02:00
|
|
|
{
|
2013-02-23 16:12:46 +01:00
|
|
|
debug("cIptvProtocolExt::%s(): pid=%d", __FUNCTION__, pidM);
|
|
|
|
if (!isActiveM || isempty(scriptFileM))
|
2010-10-25 13:43:38 +02:00
|
|
|
return;
|
2013-02-23 14:31:11 +01:00
|
|
|
if (pidM > 0) {
|
2007-10-18 21:33:15 +02:00
|
|
|
const unsigned int timeoutms = 100;
|
|
|
|
unsigned int waitms = 0;
|
2007-10-19 20:03:19 +02:00
|
|
|
bool waitOver = false;
|
2012-07-10 21:12:29 +02:00
|
|
|
// Signal and wait for termination
|
2013-02-23 14:31:11 +01:00
|
|
|
int retval = killpg(pidM, SIGINT);
|
2007-10-21 01:25:14 +02:00
|
|
|
ERROR_IF_RET(retval < 0, "kill()", waitOver = true);
|
2007-10-19 20:26:27 +02:00
|
|
|
while (!waitOver) {
|
2007-10-19 20:03:19 +02:00
|
|
|
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);
|
2007-10-19 20:03:19 +02:00
|
|
|
}
|
|
|
|
// Clear wait status to make sure child exit status is accessible
|
2012-07-10 21:12:29 +02:00
|
|
|
// and wait for child termination
|
|
|
|
#ifdef __FreeBSD__
|
|
|
|
int waitStatus = 0;
|
2013-02-23 14:31:11 +01:00
|
|
|
retval = waitpid(pidM, &waitStatus, WNOHANG);
|
2012-07-10 21:12:29 +02:00
|
|
|
#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));
|
2012-07-10 21:12:29 +02:00
|
|
|
#endif // __FreeBSD__
|
2007-10-21 01:25:14 +02:00
|
|
|
ERROR_IF_RET(retval < 0, "waitid()", waitOver = true);
|
2007-10-19 20:03:19 +02:00
|
|
|
// These are the acceptable conditions under which child exit is
|
|
|
|
// regarded as successful
|
2012-07-10 21:12:29 +02:00
|
|
|
#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))) {
|
2012-07-10 21:12:29 +02:00
|
|
|
#endif // __FreeBSD__
|
2013-02-23 16:12:46 +01:00
|
|
|
debug("cIptvProtocolExt::%s(): child (%d) exited as expected", __FUNCTION__, pidM);
|
2007-10-19 20:03:19 +02:00
|
|
|
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;
|
2007-10-18 21:33:15 +02:00
|
|
|
}
|
|
|
|
}
|
2007-10-15 22:06:38 +02:00
|
|
|
|
|
|
|
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;
|
2007-10-16 22:03:59 +02:00
|
|
|
// 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;
|
2007-10-15 22:06:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2010-10-25 13:43:38 +02:00
|
|
|
// Close the socket
|
|
|
|
CloseSocket();
|
2007-10-19 23:36:27 +02:00
|
|
|
return true;
|
2007-10-15 22:06:38 +02:00
|
|
|
}
|
|
|
|
|
2013-02-23 14:31:11 +01:00
|
|
|
int cIptvProtocolExt::Read(unsigned char* bufferAddrP, unsigned int bufferLenP)
|
2007-10-21 19:32:43 +02:00
|
|
|
{
|
2013-02-23 14:31:11 +01:00
|
|
|
return cIptvUdpSocket::Read(bufferAddrP, bufferLenP);
|
2007-10-21 19:32:43 +02:00
|
|
|
}
|
|
|
|
|
2013-02-23 14:31:11 +01:00
|
|
|
bool cIptvProtocolExt::Set(const char* locationP, const int parameterP, const int indexP)
|
2007-10-15 22:06:38 +02:00
|
|
|
{
|
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)) {
|
2007-10-20 00:54:03 +02:00
|
|
|
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);
|
2007-10-20 00:54:03 +02:00
|
|
|
return false;
|
|
|
|
}
|
2013-02-23 14:31:11 +01:00
|
|
|
scriptParameterM = parameterP;
|
2007-10-20 00:54:03 +02:00
|
|
|
// Update listen port
|
2013-02-23 14:31:11 +01:00
|
|
|
streamPortM = IptvConfig.GetExtProtocolBasePort() + indexP;
|
2007-10-20 00:54:03 +02:00
|
|
|
}
|
2007-10-15 22:06:38 +02:00
|
|
|
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);
|
2007-10-15 22:06:38 +02:00
|
|
|
}
|