[mythtv] Closing FDs before exec

Christopher Pascoe c.pascoe at itee.uq.edu.au
Thu Oct 21 03:00:53 UTC 2004


When the mythtvbackend calls system() to run an external program, that
program inherits all the open FDs of the backend.  This may include file
descriptors that are connected to DVB demultiplexers, frontends, etc.

The problem with this is not so much that the file descriptors are then
accessible to the external program, but that the external program may
hang around for a while (e.g. a transcode) and when the backend
subsequently closes that fd it still remains open system-wide.  In the
case of the DVB demultiplexers' FDs, this means that a particular filter
slot may remain in use in the kernel, and subsequent attempts to program
another PID may fail.  This results in a "Failed to set filter for pid"
message and a recording being abandoned.

There are probably a few ways to address this.  One is to set the
close-on-exec flag for all file descriptors as we open them.  This is
moderately intrusive (30kB patch for mythtv as it stands) and needs
maintainence every time a new open() or socket() call, etc, is added
to the code.

A second way is to brute-force close all possible file descriptors in
myth_system() after a fork but before calling system().  This has the
advantage that it is race-free, catches all open filedescriptors now and
in the future, but has the slight disadvantage of incurring a slightly
increased penalty at fork time, and should there ever be a need to pass
FDs via exec, then a mechanism will need to be introduced to do this.
The attached myth_system.diff patch implements this.

Either of these alternatives seems to address my problems with the dvb
demux devices being held open for too long, causing back-to-back
recordings to fail whilst transcoding is taking place.

Chris
-------------- next part --------------
A non-text attachment was scrubbed...
Name: myth_system.diff
Type: application/octet-stream
Size: 1776 bytes
Desc: not available
Url : http://mythtv.org/pipermail/mythtv-dev/attachments/20041021/277708e6/myth_system.obj


More information about the mythtv-dev mailing list