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.
|
|
|
|
*
|
2008-02-17 20:18:47 +01:00
|
|
|
* $Id: protocolext.c,v 1.24 2008/02/17 19:18:47 rahrenbe Exp $
|
2007-10-15 22:06:38 +02:00
|
|
|
*/
|
|
|
|
|
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"
|
|
|
|
|
|
|
|
cIptvProtocolExt::cIptvProtocolExt()
|
2007-10-18 21:33:15 +02:00
|
|
|
: pid(-1),
|
2008-02-17 20:18:47 +01:00
|
|
|
scriptFile(""),
|
2007-10-21 15:31:21 +02:00
|
|
|
scriptParameter(0)
|
2007-10-15 22:06:38 +02:00
|
|
|
{
|
|
|
|
debug("cIptvProtocolExt::cIptvProtocolExt()\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
cIptvProtocolExt::~cIptvProtocolExt()
|
|
|
|
{
|
|
|
|
debug("cIptvProtocolExt::~cIptvProtocolExt()\n");
|
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
|
|
|
{
|
2007-10-21 11:24:24 +02:00
|
|
|
debug("cIptvProtocolExt::ExecuteScript()\n");
|
2007-10-19 23:36:27 +02:00
|
|
|
// Check if already executing
|
|
|
|
if (pid > 0) {
|
2007-10-21 11:24:24 +02:00
|
|
|
error("ERROR: Cannot execute script!");
|
2007-10-19 23:36:27 +02:00
|
|
|
return;
|
|
|
|
}
|
2007-10-18 21:33:15 +02:00
|
|
|
// Let's fork
|
2007-10-21 01:25:14 +02:00
|
|
|
ERROR_IF_RET((pid = fork()) == -1, "fork()", return);
|
2007-10-18 21:33:15 +02:00
|
|
|
// Check if child process
|
|
|
|
if (pid == 0) {
|
|
|
|
// Close all dup'ed filedescriptors
|
|
|
|
int MaxPossibleFileDescriptors = getdtablesize();
|
|
|
|
for (int i = STDERR_FILENO + 1; i < MaxPossibleFileDescriptors; i++)
|
|
|
|
close(i);
|
|
|
|
// Execute the external script
|
2008-02-17 20:18:47 +01:00
|
|
|
cString cmd = cString::sprintf("%s %d %d", *scriptFile, scriptParameter, socketPort);
|
|
|
|
debug("cIptvProtocolExt::ExecuteScript(child): %s\n", *cmd);
|
|
|
|
if (execl("/bin/sh", "sh", "-c", *cmd, NULL) == -1) {
|
|
|
|
error("ERROR: Script execution failed: %s", *cmd);
|
2007-10-18 21:33:15 +02:00
|
|
|
_exit(-1);
|
|
|
|
}
|
|
|
|
_exit(0);
|
|
|
|
}
|
|
|
|
else {
|
2007-10-21 11:24:24 +02:00
|
|
|
debug("cIptvProtocolExt::ExecuteScript(): pid=%d\n", pid);
|
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
|
|
|
{
|
2007-10-21 11:24:24 +02:00
|
|
|
debug("cIptvProtocolExt::TerminateScript(): pid=%d\n", pid);
|
2007-10-18 21:33:15 +02:00
|
|
|
if (pid > 0) {
|
|
|
|
const unsigned int timeoutms = 100;
|
|
|
|
unsigned int waitms = 0;
|
2007-10-19 20:03:19 +02:00
|
|
|
siginfo_t waitStatus;
|
|
|
|
bool waitOver = false;
|
2007-10-18 21:33:15 +02:00
|
|
|
// signal and wait for termination
|
2007-10-19 20:26:27 +02:00
|
|
|
int retval = kill(pid, 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) {
|
2008-02-17 20:18:47 +01:00
|
|
|
error("ERROR: Script '%s' won't terminate - killing it!", *scriptFile);
|
2007-10-19 20:03:19 +02:00
|
|
|
kill(pid, SIGKILL);
|
|
|
|
}
|
|
|
|
// Clear wait status to make sure child exit status is accessible
|
|
|
|
memset(&waitStatus, '\0', sizeof(waitStatus));
|
|
|
|
// Wait for child termination
|
2007-10-19 23:36:27 +02:00
|
|
|
retval = waitid(P_PID, pid, &waitStatus, (WNOHANG | WEXITED));
|
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
|
2007-10-19 23:36:27 +02:00
|
|
|
if (!retval && waitStatus.si_pid && (waitStatus.si_pid == pid) &&
|
|
|
|
((waitStatus.si_code == CLD_EXITED) || (waitStatus.si_code == CLD_KILLED))) {
|
2007-10-19 20:03:19 +02:00
|
|
|
debug("Child (%d) exited as expected\n", pid);
|
|
|
|
waitOver = true;
|
|
|
|
}
|
|
|
|
// Unsuccessful wait, avoid busy looping
|
|
|
|
if (!waitOver)
|
|
|
|
cCondWait::SleepMs(timeoutms);
|
2007-10-19 23:36:27 +02:00
|
|
|
}
|
2007-10-18 21:33:15 +02:00
|
|
|
pid = -1;
|
|
|
|
}
|
|
|
|
}
|
2007-10-15 22:06:38 +02:00
|
|
|
|
|
|
|
bool cIptvProtocolExt::Open(void)
|
|
|
|
{
|
2007-10-19 23:36:27 +02:00
|
|
|
debug("cIptvProtocolExt::Open()\n");
|
|
|
|
// Reject empty script files
|
|
|
|
if (!strlen(scriptFile))
|
2008-01-30 22:57:33 +01:00
|
|
|
return false;
|
2007-10-16 22:03:59 +02:00
|
|
|
// Create the listening socket
|
2007-10-21 21:32:14 +02:00
|
|
|
OpenSocket(socketPort);
|
2007-10-21 11:24:24 +02:00
|
|
|
// Execute the external script
|
|
|
|
ExecuteScript();
|
2007-10-20 13:36:21 +02:00
|
|
|
isActive = true;
|
2007-10-19 23:36:27 +02:00
|
|
|
return true;
|
2007-10-15 22:06:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool cIptvProtocolExt::Close(void)
|
|
|
|
{
|
2007-10-19 23:36:27 +02:00
|
|
|
debug("cIptvProtocolExt::Close()\n");
|
2007-10-15 22:06:38 +02:00
|
|
|
// Close the socket
|
|
|
|
CloseSocket();
|
2007-10-19 23:36:27 +02:00
|
|
|
// Terminate the external script
|
2007-10-21 11:24:24 +02:00
|
|
|
TerminateScript();
|
2007-10-20 13:36:21 +02:00
|
|
|
isActive = false;
|
2007-10-19 23:36:27 +02:00
|
|
|
return true;
|
2007-10-15 22:06:38 +02:00
|
|
|
}
|
|
|
|
|
2007-10-21 19:32:43 +02:00
|
|
|
int cIptvProtocolExt::Read(unsigned char* *BufferAddr)
|
|
|
|
{
|
|
|
|
return cIptvUdpSocket::Read(BufferAddr);
|
|
|
|
}
|
|
|
|
|
2007-10-20 00:18:55 +02:00
|
|
|
bool cIptvProtocolExt::Set(const char* Location, const int Parameter, const int Index)
|
2007-10-15 22:06:38 +02:00
|
|
|
{
|
2007-10-20 00:18:55 +02:00
|
|
|
debug("cIptvProtocolExt::Set(): Location=%s Parameter=%d Index=%d\n", Location, Parameter, Index);
|
2007-10-19 23:36:27 +02:00
|
|
|
if (!isempty(Location)) {
|
2007-10-20 00:54:03 +02:00
|
|
|
struct stat stbuf;
|
|
|
|
// Update script file and parameter
|
2008-02-17 20:18:47 +01:00
|
|
|
scriptFile = cString::sprintf("%s/%s", IptvConfig.GetConfigDirectory(), Location);
|
2007-10-20 00:54:03 +02:00
|
|
|
if ((stat(scriptFile, &stbuf) != 0) || (strstr(scriptFile, "..") != 0)) {
|
2008-02-17 20:18:47 +01:00
|
|
|
error("ERROR: Non-existent or relative path script '%s'", *scriptFile);
|
2007-10-20 00:54:03 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
scriptParameter = Parameter;
|
|
|
|
// Update listen port
|
2007-10-21 15:31:21 +02:00
|
|
|
socketPort = IptvConfig.GetExtProtocolBasePort() + Index;
|
2007-10-20 00:54:03 +02:00
|
|
|
}
|
2007-10-15 22:06:38 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
cString cIptvProtocolExt::GetInformation(void)
|
|
|
|
{
|
|
|
|
//debug("cIptvProtocolExt::GetInformation()");
|
2008-02-17 20:18:47 +01:00
|
|
|
return cString::sprintf("ext://%s:%d", *scriptFile, scriptParameter);
|
2007-10-15 22:06:38 +02:00
|
|
|
}
|