@@ -15,7 +15,6 @@ using namespace std;
1515
1616// Qt headers
1717#include < QCoreApplication>
18- #include < QProcess>
1918
2019// MythTV headers
2120#include " firewirechannel.h"
@@ -43,24 +42,17 @@ using namespace std;
4342ChannelBase::ChannelBase(TVRec *parent) :
4443 m_pParent(parent), m_curchannelname(" " ),
4544 m_currentInputID(-1 ), m_commfree(false ), m_cardid(0 ),
46- m_process_thread (NULL ), m_process( NULL ), m_process_status (0 )
45+ m_system (NULL ), m_system_status (0 )
4746{
4847}
4948
5049ChannelBase::~ChannelBase (void )
5150{
5251 ClearInputMap ();
5352
54- QMutexLocker locker (&m_process_lock);
55- if (m_process_thread)
56- {
57- // better to a risk zombie than risk blocking forever..
58- KillScript (500 );
59- m_process_thread->exit (0 );
60- if (m_process_thread->wait ())
61- delete m_process_thread;
62- m_process_thread = NULL ;
63- }
53+ QMutexLocker locker (&m_system_lock);
54+ if (m_system)
55+ KillScript ();
6456}
6557
6658bool ChannelBase::Init (QString &inputname, QString &startchannel, bool setchan)
@@ -631,47 +623,38 @@ DBChanList ChannelBase::GetChannels(const QString &inputname) const
631623 return GetChannels (inputid);
632624}
633625
634- // / \note m_process_lock must be held when this is called
635- bool ChannelBase::KillScript (uint timeout_ms )
626+ // / \note m_system_lock must be held when this is called
627+ bool ChannelBase::KillScript (void )
636628{
637- if (!m_process )
629+ if (!m_system )
638630 return true ;
639631
640- if (m_process->state () != QProcess::NotRunning)
641- {
642- m_process->terminate ();
643- if (!m_process->waitForFinished (max (timeout_ms/2 ,1U )))
644- {
645- m_process->kill ();
646- if (!m_process->waitForFinished (max (timeout_ms/2 ,1U )))
647- return false ;
648- }
649- }
632+ m_system->Term (true );
650633
651- delete m_process ;
652- m_process = NULL ;
634+ delete m_system ;
635+ m_system = NULL ;
653636 return true ;
654637}
655638
656- // / \note m_process_lock must NOT be held when this is called
639+ // / \note m_system_lock must NOT be held when this is called
657640void ChannelBase::HandleScript (const QString &freqid)
658641{
659- QMutexLocker locker (&m_process_lock );
642+ QMutexLocker locker (&m_system_lock );
660643
661644 bool ok = true ;
662- m_process_status = 0 ; // unknown
645+ m_system_status = 0 ; // unknown
663646
664647 InputMap::const_iterator it = m_inputs.find (m_currentInputID);
665648 if (it == m_inputs.end ())
666649 {
667- m_process_status = 2 ; // failed
650+ m_system_status = 2 ; // failed
668651 HandleScriptEnd (true );
669652 return ;
670653 }
671654
672655 if ((*it)->externalChanger .isEmpty ())
673656 {
674- m_process_status = 3 ; // success
657+ m_system_status = 3 ; // success
675658 HandleScriptEnd (true );
676659 return ;
677660 }
@@ -682,30 +665,30 @@ void ChannelBase::HandleScript(const QString &freqid)
682665 " A channel changer is set, but the freqid field is empty."
683666 " \n\t\t\t We will return success to ease setup pains, "
684667 " but no script is will actually run." );
685- m_process_status = 3 ; // success
668+ m_system_status = 3 ; // success
686669 HandleScriptEnd (true );
687670 return ;
688671 }
689672
690673 // It's possible we simply never reaped the process, check status first.
691- if (m_process )
674+ if (m_system )
692675 GetScriptStatus (true );
693676
694677 // If it's still running, try killing it
695- if (m_process )
696- ok = KillScript (50 );
678+ if (m_system )
679+ ok = KillScript ();
697680
698- // The GetScriptStatus() call above can reset m_process_status with
681+ // The GetScriptStatus() call above can reset m_system_status with
699682 // the exit status of the last channel change script invocation, so
700683 // we must set it to pending here.
701- m_process_status = 1 ; // pending
684+ m_system_status = 1 ; // pending
702685
703686 if (!ok)
704687 {
705688 LOG (VB_GENERAL, LOG_ERR, LOC +
706689 " Can not execute channel changer, previous call to script "
707690 " is still running." );
708- m_process_status = 2 ; // failed
691+ m_system_status = 2 ; // failed
709692 HandleScriptEnd (ok);
710693 }
711694 else
@@ -714,88 +697,80 @@ void ChannelBase::HandleScript(const QString &freqid)
714697 if (!ok)
715698 {
716699 LOG (VB_GENERAL, LOG_ERR, LOC + " Can not execute channel changer." );
717- m_process_status = 2 ; // failed
700+ m_system_status = 2 ; // failed
718701 HandleScriptEnd (ok);
719702 }
720703 }
721704}
722705
723- bool ChannelBase::ChangeExternalChannel (
724- const QString &changer, const QString &freqid)
706+ // / \note m_system_lock must be held when this is called
707+ bool ChannelBase::ChangeExternalChannel (const QString &changer,
708+ const QString &freqid)
725709{
726- if (m_process )
710+ if (m_system )
727711 return false ;
728712
729713 if (changer.isEmpty () || freqid.isEmpty ())
730714 return false ;
731715
732- QString command = QString (" /bin/sh -c \" %1 %2\" " ).arg (changer).arg (freqid);
716+ QString command = QString (" %1 %2" ).arg (changer).arg (freqid);
733717 LOG (VB_CHANNEL, LOG_INFO, LOC +
734718 QString (" Running command: %1" ).arg (command));
735719
736- if (!m_process_thread)
737- {
738- m_process_thread = new ProcessThread ();
739- m_process_thread->start ();
740- }
741-
742- m_process = m_process_thread->CreateProcess (command);
720+ m_system = new MythSystem (command, kMSRunShell );
721+ m_system->Run (30 );
743722
744723 return true ;
745724}
746725
747726uint ChannelBase::GetScriptStatus (bool holding_lock)
748727{
728+ if (!m_system)
729+ return m_system_status;
730+
749731 if (!holding_lock)
750- m_process_lock .lock ();
732+ m_system_lock .lock ();
751733
752- if (m_process && m_process->state () == QProcess::NotRunning)
734+ m_system_status = m_system->Wait ();
735+ if (m_system_status != GENERIC_EXIT_RUNNING &&
736+ m_system_status != GENERIC_EXIT_START)
753737 {
754- if (m_process->exitStatus () != QProcess::CrashExit &&
755- m_process->exitCode () == 0 )
756- {
757- m_process_status = 3 ; // success
758- }
759- else
760- {
761- m_process_status = 2 ; // failed
762- }
738+ delete m_system;
739+ m_system = NULL ;
763740
764- delete m_process ;
765- m_process = NULL ;
741+ HandleScriptEnd (m_system_status == GENERIC_EXIT_OK) ;
742+ }
766743
767- LOG (VB_CHANNEL, LOG_INFO, LOC + QString (" GetScriptStatus() %1" )
768- .arg (m_process_status ));
744+ LOG (VB_CHANNEL, LOG_INFO, LOC + QString (" GetScriptStatus() %1" )
745+ .arg (m_system_status ));
769746
770- HandleScriptEnd (3 == m_process_status);
771- }
772- else
747+ uint ret;
748+ switch (m_system_status)
773749 {
774- QString ps = " NULL" ;
775- if (m_process != NULL )
776- {
777- int s = m_process->state ();
778- ps = QString::number (s);
779- switch (s)
780- {
781- case QProcess::Running: ps = " running" ; break ;
782- case QProcess::NotRunning: ps = " not running" ; break ;
783- case QProcess::Starting: ps = " starting" ; break ;
784- }
785- }
786- LOG (VB_CHANNEL, LOG_INFO, LOC + QString (" GetScriptStatus() %1 (ps %2)" )
787- .arg (m_process_status).arg (ps));
750+ case GENERIC_EXIT_OK:
751+ ret = 3 ; // success
752+ break ;
753+ case GENERIC_EXIT_RUNNING:
754+ case GENERIC_EXIT_START:
755+ ret = 1 ; // pending
756+ break ;
757+ default :
758+ ret = 2 ; // fail
759+ break ;
788760 }
789761
790- uint ret = m_process_status;
762+ LOG (VB_CHANNEL, LOG_INFO, LOC + QString (" GetScriptStatus() %1 -> %2" )
763+ .arg (m_system_status). arg (ret));
764+
765+ m_system_status = ret;
791766
792767 if (!holding_lock)
793- m_process_lock .unlock ();
768+ m_system_lock .unlock ();
794769
795770 return ret;
796771}
797772
798- // / \note m_process_lock must be held when this is called
773+ // / \note m_system_lock must be held when this is called
799774void ChannelBase::HandleScriptEnd (bool ok)
800775{
801776 LOG (VB_CHANNEL, LOG_INFO, LOC + QString (" Channel change script %1" )
@@ -1221,31 +1196,3 @@ ChannelBase *ChannelBase::CreateChannel(
12211196 return channel;
12221197}
12231198
1224- bool ProcessThread::event (QEvent *e)
1225- {
1226- if (MythEvent::MythEventMessage == e->type ())
1227- {
1228- MythEvent *me = static_cast <MythEvent*>(e);
1229- if (me->Message () == " CreateProcess" )
1230- {
1231- QMutexLocker locker (&m_lock);
1232- m_proc = new QProcess ();
1233- m_proc->start (me->ExtraData (0 ));
1234- m_wait.wakeOne ();
1235- return true ;
1236- }
1237- }
1238-
1239- return QThread::event (e);
1240- }
1241-
1242- QProcess *ProcessThread::CreateProcess (const QString &command)
1243- {
1244- QMutexLocker locker (&m_lock);
1245- QStringList cmd (command);
1246- QCoreApplication::postEvent (this , new MythEvent (" CreateProcess" , cmd));
1247- m_wait.wait (&m_lock);
1248- QProcess *ret = m_proc;
1249- m_proc = NULL ;
1250- return ret;
1251- }
0 commit comments