Skip to content

Commit

Permalink
Close stdin after write in MythSystemLegacy.
Browse files Browse the repository at this point in the history
We only write to the program whatever has been sent in via Write()
prior to calling Run(), but we never close the pipe. This is what
was causing the stdin unit test for MythSystemLegacy to fail.
  • Loading branch information
daniel-kristjansson committed Jun 2, 2013
1 parent 2fc542c commit fd032f1
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 19 deletions.
14 changes: 12 additions & 2 deletions mythtv/libs/libmythbase/mythsystemlegacy.cpp
Expand Up @@ -86,6 +86,13 @@ void MythSystemLegacy::SetCommand(const QString &command, uint flags)

SetCommand(abscommand, args, flags);
}

if (m_settings["UseStdin"])
m_stdbuff[0].open(QIODevice::WriteOnly);
if (m_settings["UseStdout"])
m_stdbuff[1].open(QIODevice::ReadOnly);
if (m_settings["UseStderr"])
m_stdbuff[2].open(QIODevice::ReadOnly);
}


Expand Down Expand Up @@ -390,13 +397,16 @@ QByteArray& MythSystemLegacy::ReadAllErr(void)
return m_stdbuff[2].buffer();
}

/** This writes to the standard input of the program being run.
* All calls to this must be done before Run() is called.
* All calls after Run() is called are silently ignored.
*/
int MythSystemLegacy::Write(const QByteArray &ba)
{
if (!GetSetting("UseStdin"))
return 0;

m_stdbuff[0].buffer().append(ba.constData());
return ba.size();
return m_stdbuff[0].write(ba.constData());
}

void MythSystemLegacy::HandlePreRun(void)
Expand Down
27 changes: 21 additions & 6 deletions mythtv/libs/libmythbase/mythsystemunix.cpp
Expand Up @@ -217,6 +217,17 @@ void MythSystemLegacyIOHandler::insert(int fd, QBuffer *buff)
wake();
}

void MythSystemLegacyIOHandler::Wait(int fd)
{
QMutexLocker locker(&m_pLock);
while (m_pMap.contains(fd))
{
locker.unlock();
usleep(10 * 1000);
locker.relock();
}
}

void MythSystemLegacyIOHandler::remove(int fd)
{
m_pLock.lock();
Expand Down Expand Up @@ -444,8 +455,16 @@ void MythSystemLegacyManager::append(MythSystemLegacyUnix *ms)
m_pMap.insert(ms->m_pid, ms);
m_mapLock.unlock();

if( ms->GetSetting("UseStdin") )
writeThread->insert(ms->m_stdpipe[0], ms->GetBuffer(0));
if (ms->m_stdpipe[0] >= 0)
{
QByteArray ba = ms->GetBuffer(0)->data();
QBuffer wtb(&ba);
wtb.open(QIODevice::ReadOnly);
writeThread->insert(ms->m_stdpipe[0], &wtb);
writeThread->Wait(ms->m_stdpipe[0]);
writeThread->remove(ms->m_stdpipe[0]);
CLOSE(ms->m_stdpipe[0]);
}

if( ms->GetSetting("UseStdout") )
{
Expand Down Expand Up @@ -764,10 +783,6 @@ void MythSystemLegacyUnix::Fork(time_t timeout)

LOG(VB_SYSTEM, LOG_DEBUG, QString("Launching: %1").arg(GetLogCmd()));

GetBuffer(0)->setBuffer(0);
GetBuffer(1)->setBuffer(0);
GetBuffer(2)->setBuffer(0);

int p_stdin[] = {-1,-1};
int p_stdout[] = {-1,-1};
int p_stderr[] = {-1,-1};
Expand Down
1 change: 1 addition & 0 deletions mythtv/libs/libmythbase/mythsystemunix.h
Expand Up @@ -35,6 +35,7 @@ class MythSystemLegacyIOHandler: public MThread
void run(void);

void insert(int fd, QBuffer *buff);
void Wait(int fd);
void remove(int fd);
void wake();

Expand Down
27 changes: 21 additions & 6 deletions mythtv/libs/libmythbase/mythsystemwindows.cpp
Expand Up @@ -198,6 +198,17 @@ void MythSystemLegacyIOHandler::insert(HANDLE h, QBuffer *buff)
wake();
}

void MythSystemLegacyIOHandler::Wait(HANDLE h)
{
QMutexLocker locker(&m_pLock);
while (m_pMap.contains(h))
{
locker.unlock();
usleep(10 * 1000);
locker.relock();
}
}

void MythSystemLegacyIOHandler::remove(HANDLE h)
{
m_pLock.lock();
Expand Down Expand Up @@ -402,8 +413,16 @@ void MythSystemLegacyManager::append(MythSystemLegacyWindows *ms)
ChildListRebuild();
m_mapLock.unlock();

if( ms->GetSetting("UseStdin") )
writeThread->insert(ms->m_stdpipe[0], ms->GetBuffer(0));
if (ms->m_stdpipe[0])
{
QByteArray ba = ms->GetBuffer(0)->data();
QBuffer wtb(&ba);
wtb.open(QIODevice::ReadOnly);
writeThread->insert(ms->m_stdpipe[0], &wtb);
writeThread->Wait(ms->m_stdpipe[0]);
writeThread->remove(ms->m_stdpipe[0]);
CLOSE(ms->m_stdpipe[0]);
}

if( ms->GetSetting("UseStdout") )
{
Expand Down Expand Up @@ -586,10 +605,6 @@ void MythSystemLegacyWindows::Fork(time_t timeout)

LOG(VB_SYSTEM, LOG_DEBUG, QString("Launching: %1").arg(GetLogCmd()));

GetBuffer(0)->setBuffer(0);
GetBuffer(1)->setBuffer(0);
GetBuffer(2)->setBuffer(0);

HANDLE p_stdin[2] = { NULL, NULL };
HANDLE p_stdout[2] = { NULL, NULL };
HANDLE p_stderr[2] = { NULL, NULL };
Expand Down
1 change: 1 addition & 0 deletions mythtv/libs/libmythbase/mythsystemwindows.h
Expand Up @@ -31,6 +31,7 @@ class MythSystemLegacyIOHandler: public MThread
void run(void);

void insert(HANDLE h, QBuffer *buff);
void Wait(HANDLE h);
void remove(HANDLE h);
void wake();

Expand Down
Expand Up @@ -141,10 +141,11 @@ class TestMythSystem: public QObject
QByteArray in = QString(__FUNCTION__).toLatin1();
QScopedPointer<MythSystem> cmd(
MythSystem::Create(QString("cat - > %1").arg(tempfile.fileName()),
kMSStdIn));
kMSStdIn | kMSRunShell));
cmd->GetStandardInputStream()->write(in);
cmd->GetStandardInputStream()->close();
cmd->Wait();
cerr << "stdin_works -- Wait starting" << endl;
cmd->Wait(0);
QVERIFY(cmd->GetExitCode() == 0);
QByteArray out = tempfile.readAll();
QVERIFY(QString(out).contains(QString(in)));
Expand Down
Expand Up @@ -130,14 +130,14 @@ class TestMythSystemLegacy: public QObject
// kMSStdIn -- allow access to stdin
void stdin_works(void)
{
MSKIP("stdin_works -- currently blocks forever");
QTemporaryFile tempfile;
tempfile.open();
QByteArray in = QString(__FUNCTION__).toLatin1();
MythSystemLegacy cmd(QString("cat - > %1").arg(tempfile.fileName()),
kMSStdIn);
kMSStdIn | kMSRunShell);
cmd.Write(in);
Go(cmd);
cmd.Run();
cmd.Wait();
QVERIFY(cmd.GetStatus() == 0);
QByteArray out = tempfile.readAll();
QVERIFY(QString(out).contains(QString(in)));
Expand Down

0 comments on commit fd032f1

Please sign in to comment.