plugins.cpp: use popen2 with vfork for script plugins

Origin commit data
------------------
Branch: ni/coolstream
Commit: abed344501
Author: Jacek Jendrzej <overx300@gmail.com>
Date: 2012-08-22 (Wed, 22 Aug 2012)


------------------
No further description and justification available within origin commit message!

------------------
This commit was generated by Migit
This commit is contained in:
Jacek Jendrzej
2012-08-22 09:37:46 +02:00
parent 97fd0a377f
commit 561ae56cd1

View File

@@ -47,6 +47,8 @@
#include <stdio.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <signal.h>
#include <global.h>
#include <neutrino.h>
@@ -313,6 +315,53 @@ void CPlugins::startPlugin(const char * const name)
else
printf("[CPlugins] could not find %s\n", name);
}
#include <errno.h>
FILE* popen2( pid_t& pid, const char *cmdstring, const char *type) {
int pfd[2] ={-1,-1};
FILE *fp = NULL;
/* only allow "r" or "w" */
if ((type[0] != 'r' && type[0] != 'w') || type[1] != 0) {
errno = EINVAL; /* required by POSIX */
return(NULL);
}
if (pipe(pfd) < 0)
return(NULL); /* errno set by pipe() */
if ((pid = vfork()) < 0) {
return(NULL); /* errno set by vfork() */
} else if (pid == 0) { /* child */
if (*type == 'r') {
close(pfd[0]);
if (pfd[1] != STDOUT_FILENO) {
dup2(pfd[1], STDOUT_FILENO);
close(pfd[1]);
}
} else {
close(pfd[1]);
if (pfd[0] != STDIN_FILENO) {
dup2(pfd[0], STDIN_FILENO);
close(pfd[0]);
}
}
execl("/bin/sh", "sh", "-c", cmdstring, (char *)0);
exit(0);
}
/* parent continues... */
if (*type == 'r') {
close(pfd[1]);
if ((fp = fdopen(pfd[0], type)) == NULL)
return(NULL);
} else {
close(pfd[0]);
if ((fp = fdopen(pfd[1], type)) == NULL)
return(NULL);
}
return(fp);
}
void CPlugins::startScriptPlugin(int number)
@@ -325,7 +374,8 @@ void CPlugins::startScriptPlugin(int number)
script, plugin_list[number].cfgfile.c_str());
return;
}
FILE *f = popen(script,"r");
pid_t pid = 0;
FILE *f = popen2(pid,script,"r");
if (f != NULL)
{
char *output=NULL;
@@ -335,6 +385,7 @@ void CPlugins::startScriptPlugin(int number)
{
scriptOutput += output;
}
kill(pid, SIGINT );
pclose(f);
if(output)
free(output);