Set non-blocking mode on the pty master file descriptor while
authorZac Medico <zmedico@gentoo.org>
Wed, 26 Sep 2007 20:01:23 +0000 (20:01 -0000)
committerZac Medico <zmedico@gentoo.org>
Wed, 26 Sep 2007 20:01:23 +0000 (20:01 -0000)
the slave file descriptor is still held open since otherwise
the fcntl call can fail on FreeBSD (the child process might
have already exited and closed the slave file descriptor so we
have to keep it open in order to avoid FreeBSD potentially
generating an EAGAIN exception). This appoach is cleaner than
triggering the exception and being forced to handle it
somehow.

svn path=/main/trunk/; revision=7835

pym/portage/__init__.py

index 7a2fa0855c06efb85875aaf64712a4dd033121bb..24b246be76a86a62bb6f247d39bd6a92ebb4a515 100644 (file)
@@ -2442,6 +2442,16 @@ def spawn(mystring, mysettings, debug=0, free=0, droppriv=0, sesandbox=0, fakero
                        writemsg("openpty failed: '%s'\n" % str(e), noiselevel=1)
                        del e
                        master_fd, slave_fd = os.pipe()
+
+               # We must set non-blocking mode before we close the slave_fd
+               # since otherwise the fcntl call can fail on FreeBSD (the child
+               # process might have already exited and closed slave_fd so we
+               # have to keep it open in order to avoid FreeBSD potentially
+               # generating an EAGAIN exception).
+               import fcntl
+               fcntl.fcntl(master_fd, fcntl.F_SETFL,
+                       fcntl.fcntl(master_fd, fcntl.F_GETFL) | os.O_NONBLOCK)
+
                fd_pipes.setdefault(0, sys.stdin.fileno())
                fd_pipes_orig = fd_pipes.copy()
                if got_pty and os.isatty(fd_pipes_orig[1]):
@@ -2488,7 +2498,7 @@ def spawn(mystring, mysettings, debug=0, free=0, droppriv=0, sesandbox=0, fakero
        try:
                mypids.extend(spawn_func(mystring, env=env, **keywords))
        finally:
-               if slave_fd:
+               if logfile:
                        os.close(slave_fd)
                if sesandbox:
                        selinux.setexec(None)
@@ -2503,26 +2513,14 @@ def spawn(mystring, mysettings, debug=0, free=0, droppriv=0, sesandbox=0, fakero
                iwtd = [master_file]
                owtd = []
                ewtd = []
-               import array, fcntl, select
-               fd_flags = {}
-               for f in iwtd:
-                       fd_flags[f] = fcntl.fcntl(f.fileno(), fcntl.F_GETFL)
+               import array, select
                buffsize = 65536
                eof = False
-               # Use non-blocking mode to prevent read
-               # calls from blocking indefinitely.
-               try:
-                       fcntl.fcntl(master_file.fileno(), fcntl.F_SETFL,
-                               fd_flags[master_file] | os.O_NONBLOCK)
-               except EnvironmentError, e:
-                       if e.errno != errno.EAGAIN:
-                               raise
-                       del e
-                       # The EAGAIN error signals eof on FreeBSD.
-                       eof = True
                while not eof:
                        events = select.select(iwtd, owtd, ewtd)
                        for f in events[0]:
+                               # Use non-blocking mode to prevent read
+                               # calls from blocking indefinitely.
                                buf = array.array('B')
                                try:
                                        buf.fromfile(f, buffsize)