On Wed, 15 Nov 2000, you wrote:
> On Tue, 14 Nov 2000, George Young wrote:
> > my pygtk app forks and execs another process which it
> > wants to completly disassociate from the parent.
> > It seems that one of the techniques necessary is for the child to
> > dup2 and then close all it's files, e.g.:
> > fdnull = open('/dev/null')
> > stderr_d = os.dup2(fdnull.fileno(), sys.stderr.fileno())
> > stdin_d = os.dup2(fdnull.fileno(), sys.stdin.fileno())
> > stdout_d = os.dup2(fdnull.fileno(), sys.stdout.fileno())
> > fdnull.close()
> > This works fine except for the connection to the X server.
> > Guessing and handcoding the number, e.g.:
> > foo = os.dup2(fdnull.fileno(), 5)
>
> You probably want to set the FD_CLOEXEC fcntl on all file descriptors >= 3
> (as 0,1,2 are stdin,out and err). There is a sysconf call to get the
> maximum file descriptor number, but I don't know if this is available from
> python. So except for getting the maximum file descriptor number, the
> rest can be done with the fcntl and FCNTL modules.
Yes, fcntl worked great! I'm stuck with python 1.5.2 for a while, so I can't use
the sysconf from later releases but for now a guess is OK for the greatest file
descriptor:
pid = os.fork()
if pid: # in parent
time.sleep(0.1) # block briefly(100 ms.) to give it time to fail...
wpid,exitstatus = os.waitpid(pid, os.WNOHANG)
os._exit(exitstatus)
else: # in child
# Set close-on-exec for all (guess 30 max) file descriptors.
for fd in range(30):
try:
fcntl.fcntl(fd, FCNTL.F_SETFD, 1)
except IOError: # Probably fd is not open.
pass
os.setsid()
try:
os.execvp(cmd, args) # does not return except on error
except OSError:
sys.exit(2)
_______________________________________________
pygtk mailing list [EMAIL PROTECTED]
http://www.daa.com.au/mailman/listinfo/pygtk