[mythtv] [patch] fd 0 is OK, some cleanups
    Christopher Pascoe 
    c.pascoe at itee.uq.edu.au
       
    Thu Oct 21 08:01:44 UTC 2004
    
    
  
A number of places throughout the code presume that a filedescriptor of 0
is invalid.  We're lucky in that this doesn't cause any problems at the
present time, as fd 0 gets allocated to something that doesn't care, but
one may not always be so lucky - better to be safe.
The attached patch corrects codepaths which treat 0 as an invalid fd, as
well as putting some close()s in error paths, catching a case where a loop
could terminate incorrectly even if a (deferred) open was successful, and
closing an extra copy of the log filedescriptor that we have open.
Chris
-------------- next part --------------
Index: libs/libmyth/mythmedia.cpp
===================================================================
RCS file: /var/lib/mythcvs/mythtv/libs/libmyth/mythmedia.cpp,v
retrieving revision 1.8
diff -u -p -r1.8 mythmedia.cpp
--- libs/libmyth/mythmedia.cpp	7 Aug 2004 13:08:32 -0000	1.8
+++ libs/libmyth/mythmedia.cpp	21 Oct 2004 07:14:18 -0000
@@ -62,7 +62,7 @@ bool MythMediaDevice::closeDevice()
 
 bool MythMediaDevice::isDeviceOpen() const 
 { 
-    return (m_DeviceHandle > 0) ? true : false; 
+    return (m_DeviceHandle >= 0) ? true : false; 
 }
 
 bool MythMediaDevice::performMountCmd(bool DoMount)
Index: libs/libmyth/volumecontrol.cpp
===================================================================
RCS file: /var/lib/mythcvs/mythtv/libs/libmyth/volumecontrol.cpp,v
retrieving revision 1.12
diff -u -p -r1.12 volumecontrol.cpp
--- libs/libmyth/volumecontrol.cpp	7 Aug 2004 13:08:33 -0000	1.12
+++ libs/libmyth/volumecontrol.cpp	21 Oct 2004 07:14:19 -0000
@@ -16,7 +16,7 @@ using namespace std;
 
 VolumeControl::VolumeControl(bool setstartingvolume)
 {
-    mixerfd = 0;
+    mixerfd = -1;
     volume = 0;
 
 #ifdef USING_OSS
@@ -66,7 +66,7 @@ VolumeControl::VolumeControl(bool setsta
 
 VolumeControl::~VolumeControl()
 {
-    if (mixerfd > 0)
+    if (mixerfd >= 0)
         close(mixerfd);
 }
 
@@ -105,7 +104,7 @@ void VolumeControl::SetCurrentVolume(int
         volume = 0;
 
     internal_volume = volume;
-    if (mixerfd > 0)
+    if (mixerfd >= 0)
     {
         if (!mute)
         {
@@ -144,7 +143,7 @@ void VolumeControl::SetMute(bool on)
     {
         realvol = (internal_volume << 8) + internal_volume;
     }
-    if (mixerfd > 0)
+    if (mixerfd >= 0)
     {
         int ret = ioctl(mixerfd, MIXER_WRITE(control), &realvol);
         if (ret < 0)
@@ -188,7 +187,7 @@ kMuteState VolumeControl::IterateMutedCh
            break;
     }
 
-    if (mixerfd > 0)
+    if (mixerfd >= 0)
     {
         int ret = ioctl(mixerfd, MIXER_WRITE(control), &realvol);
         if (ret < 0)
Index: libs/libmythtv/NuppelVideoRecorder.cpp
===================================================================
RCS file: /var/lib/mythcvs/mythtv/libs/libmythtv/NuppelVideoRecorder.cpp,v
retrieving revision 1.180
diff -u -p -r1.180 NuppelVideoRecorder.cpp
--- libs/libmythtv/NuppelVideoRecorder.cpp	14 Oct 2004 04:58:04 -0000	1.180
+++ libs/libmythtv/NuppelVideoRecorder.cpp	21 Oct 2004 07:14:28 -0000
@@ -48,8 +48,8 @@ NuppelVideoRecorder::NuppelVideoRecorder
     channelObj = channel;
 
     encoding = false;
-    fd = 0;
-    channelfd = 0;
+    fd = -1;
+    channelfd = -1;
     lf = tf = 0;
     M1 = 0, M2 = 0, Q = 255;
     pid = pid2 = 0;
@@ -166,7 +166,7 @@ NuppelVideoRecorder::~NuppelVideoRecorde
         delete [] strm;
     if (commDetect)
         delete commDetect;
-    if (fd > 0)
+    if (fd >= 0)
         close(fd);
     if (seektable)
     {
@@ -611,6 +611,7 @@ int NuppelVideoRecorder::AudioInit(bool 
         ioctl(afd, SNDCTL_DSP_SETFMT, &afmt);
         if (afmt != AFMT_S16_LE) 
         {
+            close(afd);
             cerr << "Can't get 16 bit DSP, exiting\n";
             return 1;
         }
@@ -619,6 +620,7 @@ int NuppelVideoRecorder::AudioInit(bool 
             ioctl(afd, SNDCTL_DSP_CHANNELS, &audio_channels) < 0 ||
             ioctl(afd, SNDCTL_DSP_SPEED, &audio_samplerate) < 0)
         {
+            close(afd);
             cerr << "recorder: " << audiodevice 
                  << ": error setting audio input device to "
                  << audio_samplerate << "kHz/" 
@@ -629,6 +631,7 @@ int NuppelVideoRecorder::AudioInit(bool 
 
         if (-1 == ioctl(afd, SNDCTL_DSP_GETBLKSIZE, &blocksize)) 
         {
+            close(afd);
             cerr << "Can't get DSP blocksize, exiting\n";
             return(1);
         }
@@ -671,7 +674,7 @@ int NuppelVideoRecorder::AudioInit(bool 
 int NuppelVideoRecorder::MJPEGInit(void)
 {
     fd = open(videodevice.ascii(), O_RDWR);
-    if (fd <= 0)
+    if (fd < 0)
     {
         cerr << "Can't open video device: " << videodevice << endl;
         perror("open video:");
@@ -900,11 +903,11 @@ void NuppelVideoRecorder::StartRecording
 
     int retries = 0;
     fd = open(videodevice.ascii(), O_RDWR);
-    while (fd <= 0)
+    while (fd < 0)
     {
         usleep(30000);
         fd = open(videodevice.ascii(), O_RDWR);
-        if (retries++ > 5)
+        if (fd < 0 && retries++ > 5)
         { 
             cerr << "Can't open video device: " << videodevice << endl;
             perror("open video:");
Index: libs/libmythtv/RingBuffer.cpp
===================================================================
RCS file: /var/lib/mythcvs/mythtv/libs/libmythtv/RingBuffer.cpp,v
retrieving revision 1.105
diff -u -p -r1.105 RingBuffer.cpp
--- libs/libmythtv/RingBuffer.cpp	30 Sep 2004 10:22:56 -0000	1.105
+++ libs/libmythtv/RingBuffer.cpp	21 Oct 2004 07:14:29 -0000
@@ -134,7 +134,7 @@ ThreadedFileWriter::ThreadedFileWriter(c
 
     fd = open(filename, flags, mode);
 
-    if (fd <= 0)
+    if (fd < 0)
     {
         /* oops! */
         VERBOSE(VB_IMPORTANT,"ERROR opening file in ThreadedFileWriter.");
@@ -159,7 +159,8 @@ ThreadedFileWriter::~ThreadedFileWriter(
 
     pthread_join(writer, NULL);
 
-    close(fd);
+    if (fd >= 0)
+        close(fd);
     fd = -1;
 
     if (buf)
@@ -394,7 +395,7 @@ RingBuffer::RingBuffer(const QString &lf
                 
                 fd2 = open(filename.ascii(), O_RDONLY|O_LARGEFILE|O_STREAMING);
                 
-                if (!fd2)
+                if (fd2 < 0)
                 {
                     VERBOSE( VB_IMPORTANT, QString("Could not open %1.  %2 retries remaining.")
                                            .arg(filename).arg(openAttempts));
@@ -408,8 +409,8 @@ RingBuffer::RingBuffer(const QString &lf
                                                        "%2 retries remaining.")
                                                        .arg(filename).arg(openAttempts));
                         close(fd2);
+                        fd2 = -1;
                         usleep(500000);
-                        fd2 = 0;
                     }
                     else
                     {
@@ -503,7 +504,7 @@ RingBuffer::~RingBuffer(void)
         delete tfw;
         tfw = NULL;
     }
-    if (fd2 > 0)
+    if (fd2 >= 0)
     {
         close(fd2);
     }
Index: libs/libmythtv/dvbrecorder.cpp
===================================================================
RCS file: /var/lib/mythcvs/mythtv/libs/libmythtv/dvbrecorder.cpp,v
retrieving revision 1.38
diff -u -p -r1.38 dvbrecorder.cpp
--- libs/libmythtv/dvbrecorder.cpp	6 Aug 2004 17:34:55 -0000	1.38
+++ libs/libmythtv/dvbrecorder.cpp	21 Oct 2004 07:14:29 -0000
@@ -211,7 +211,7 @@ void DVBRecorder::Close()
 
     CloseFilters();
 
-    if (fd_dvr > 0)
+    if (fd_dvr >= 0)
         close(fd_dvr);
 
     if (dvb_on_demand && dvbchannel)
@@ -223,7 +223,7 @@ void DVBRecorder::Close()
 void DVBRecorder::CloseFilters()
 {
     for(unsigned int i=0; i<fd_demux.size(); i++)
-        if (fd_demux[i] > 0)
+        if (fd_demux[i] >= 0)
             close(fd_demux[i]);
     fd_demux.clear();
 
Index: libs/libmythtv/dvbsections.cpp
===================================================================
RCS file: /var/lib/mythcvs/mythtv/libs/libmythtv/dvbsections.cpp,v
retrieving revision 1.7
diff -u -p -r1.7 dvbsections.cpp
--- libs/libmythtv/dvbsections.cpp	19 Aug 2004 01:43:04 -0000	1.7
+++ libs/libmythtv/dvbsections.cpp	21 Oct 2004 07:14:29 -0000
@@ -135,7 +135,7 @@ void DVBSections::Stop()
 
     for (int i=0; i<pollLength; i++)
     {
-        if (pollArray[i].fd > 0)
+        if (pollArray[i].fd >= 0)
             close(pollArray[i].fd);
     }
 
Index: libs/libmythtv/hdtvrecorder.cpp
===================================================================
RCS file: /var/lib/mythcvs/mythtv/libs/libmythtv/hdtvrecorder.cpp,v
retrieving revision 1.26
diff -u -p -r1.26 hdtvrecorder.cpp
--- libs/libmythtv/hdtvrecorder.cpp	6 Jul 2004 04:44:36 -0000	1.26
+++ libs/libmythtv/hdtvrecorder.cpp	21 Oct 2004 07:14:30 -0000
@@ -115,7 +115,7 @@ HDTVRecorder::HDTVRecorder()
 
 HDTVRecorder::~HDTVRecorder()
 {
-    if (chanfd > 0)
+    if (chanfd >= 0)
         close(chanfd);
 }
 
@@ -151,7 +151,7 @@ void HDTVRecorder::StartRecording(void)
     int ret;
 
     chanfd = open(videodevice.ascii(), O_RDWR);
-    if (chanfd <= 0)
+    if (chanfd < 0)
     {
         cerr << "HD1 Can't open video device: " << videodevice 
              << " chanfd = "<< chanfd << endl;
@@ -846,7 +846,7 @@ void HDTVRecorder::Reset(void)
         pthread_mutex_unlock(db_lock);
     }
 
-    if (chanfd > 0) 
+    if (chanfd >= 0) 
     {
         int ret = close(chanfd);
         if (ret < 0) 
@@ -855,7 +855,7 @@ void HDTVRecorder::Reset(void)
             return;
         }
         chanfd = open(videodevice.ascii(), O_RDWR);
-        if (chanfd <= 0)
+        if (chanfd < 0)
         {
             cerr << "HD1 Can't open video device: " << videodevice 
                  << " chanfd = "<< chanfd << endl;
Index: libs/libmythtv/mpegrecorder.cpp
===================================================================
RCS file: /var/lib/mythcvs/mythtv/libs/libmythtv/mpegrecorder.cpp,v
retrieving revision 1.42
diff -u -p -r1.42 mpegrecorder.cpp
--- libs/libmythtv/mpegrecorder.cpp	14 Oct 2004 04:58:04 -0000	1.42
+++ libs/libmythtv/mpegrecorder.cpp	21 Oct 2004 07:14:30 -0000
@@ -76,9 +76,9 @@ MpegRecorder::MpegRecorder()
 
 MpegRecorder::~MpegRecorder()
 {
-    if (chanfd > 0)
+    if (chanfd >= 0)
         close(chanfd);
-    if (readfd > 0)
+    if (readfd >= 0)
         close(readfd);
 }
 
@@ -227,7 +227,7 @@ void MpegRecorder::openMpegFileAsInput(v
 {
     chanfd = readfd = open(videodevice.ascii(), O_RDONLY);
 
-    if (readfd <= 0)
+    if (readfd < 0)
     {
         cerr << "Can't open MPEG File: " << videodevice << endl;
         perror("open mpeg file:");
@@ -238,7 +238,7 @@ void MpegRecorder::openMpegFileAsInput(v
 void MpegRecorder::openV4L2DeviceAsInput(void)
 {
     chanfd = open(videodevice.ascii(), O_RDWR);
-    if (chanfd <= 0)
+    if (chanfd < 0)
     {
         cerr << "Can't open video device: " << videodevice << endl;
         perror("open video:");
@@ -342,7 +342,7 @@ void MpegRecorder::openV4L2DeviceAsInput
     }
 
     readfd = open(videodevice.ascii(), O_RDWR);
-    if (readfd <= 0)
+    if (readfd < 0)
     {
         cerr << "Can't open video device: " << videodevice << endl;
         perror("open video:");
@@ -357,7 +357,7 @@ void MpegRecorder::StartRecording(void)
     else
         openV4L2DeviceAsInput();
 
-    if ((chanfd <= 0) || (readfd <= 0))
+    if ((chanfd < 0) || (readfd < 0))
         return;
 
     if (!SetupRecording())
@@ -385,7 +385,7 @@ void MpegRecorder::StartRecording(void)
             pauseWait.wakeAll();
 
             if ((!deviceIsMpegFile) &&
-                (readfd > 0))
+                (readfd >= 0))
             {
                 close(readfd);
                 readfd = -1;
@@ -415,7 +415,7 @@ void MpegRecorder::StartRecording(void)
             close(readfd);
             readfd = open(videodevice.ascii(), O_RDONLY);
 
-            if (readfd > 0)
+            if (readfd >= 0)
                 ret = read(readfd, buffer, bufferSize);
             if (ret <= 0)
             {
Index: programs/mythbackend/main.cpp
===================================================================
RCS file: /var/lib/mythcvs/mythtv/programs/mythbackend/main.cpp,v
retrieving revision 1.76
diff -u -p -r1.76 main.cpp
--- programs/mythbackend/main.cpp	13 Oct 2004 01:49:55 -0000	1.76
+++ programs/mythbackend/main.cpp	21 Oct 2004 07:14:31 -0000
@@ -414,6 +414,10 @@ int main(int argc, char **argv)
         // Send stdout and stderr to the logfile
         dup2(logfd, 1);
         dup2(logfd, 2);
+
+        // Close the unduplicated logfd
+        if (logfd != 1 && logfd != 2)
+            close(logfd);
     }
 
     gContext = new MythContext(MYTH_BINARY_VERSION, false, false);
Index: programs/mythfrontend/main.cpp
===================================================================
RCS file: /var/lib/mythcvs/mythtv/programs/mythfrontend/main.cpp,v
retrieving revision 1.168
diff -u -p -r1.168 main.cpp
--- programs/mythfrontend/main.cpp	19 Oct 2004 07:37:31 -0000	1.168
+++ programs/mythfrontend/main.cpp	21 Oct 2004 07:14:32 -0000
@@ -879,6 +879,10 @@ int main(int argc, char **argv)
         // Send stdout and stderr to the logfile
         dup2(logfd, 1);
         dup2(logfd, 2);
+
+        // Close the unduplicated logfd
+        if (logfd != 1 && logfd != 2)
+            close(logfd);
     }
 
     if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
    
    
More information about the mythtv-dev
mailing list