Exemple #1
0
	def __init__(self, reactor, proc, name, fileno, forceReadHack=False):
		"""
		Initialize, specifying a Process instance to connect to.
		"""
		abstract.FileDescriptor.__init__(self, reactor)
		fdesc.setNonBlocking(fileno)
		self.proc = proc
		self.name = name
		self.fd = fileno

		if not stat.S_ISFIFO(os.fstat(self.fileno()).st_mode):
			# If the fd is not a pipe, then the read hack is never
			# applicable.  This case arises when ProcessWriter is used by
			# StandardIO and stdout is redirected to a normal file.
			self.enableReadHack = False
		elif forceReadHack:
			self.enableReadHack = True
		else:
			# Detect if this fd is actually a write-only fd. If it's
			# valid to read, don't try to detect closing via read.
			# This really only means that we cannot detect a TTY's write
			# pipe being closed.
			try:
				os.read(self.fileno(), 0)
			except OSError:
				# It's a write-only pipe end, enable hack
				self.enableReadHack = True

		if self.enableReadHack:
			self.startReading()
Exemple #2
0
    def __init__(self, reactor, proc, name, fileno, forceReadHack=False):
        """
		Initialize, specifying a Process instance to connect to.
		"""
        abstract.FileDescriptor.__init__(self, reactor)
        fdesc.setNonBlocking(fileno)
        self.proc = proc
        self.name = name
        self.fd = fileno

        if not stat.S_ISFIFO(os.fstat(self.fileno()).st_mode):
            # If the fd is not a pipe, then the read hack is never
            # applicable.  This case arises when ProcessWriter is used by
            # StandardIO and stdout is redirected to a normal file.
            self.enableReadHack = False
        elif forceReadHack:
            self.enableReadHack = True
        else:
            # Detect if this fd is actually a write-only fd. If it's
            # valid to read, don't try to detect closing via read.
            # This really only means that we cannot detect a TTY's write
            # pipe being closed.
            try:
                os.read(self.fileno(), 0)
            except OSError:
                # It's a write-only pipe end, enable hack
                self.enableReadHack = True

        if self.enableReadHack:
            self.startReading()
Exemple #3
0
    def __init__(self, reactor):
        """Initialize.
		"""
        self.reactor = reactor
        self.i, self.o = os.pipe()
        fdesc.setNonBlocking(self.i)
        fdesc._setCloseOnExec(self.i)
        fdesc.setNonBlocking(self.o)
        fdesc._setCloseOnExec(self.o)
        self.fileno = lambda: self.i
Exemple #4
0
	def __init__(self, reactor, proc, name, fileno):
		"""
		Initialize, specifying a process to connect to.
		"""
		abstract.FileDescriptor.__init__(self, reactor)
		fdesc.setNonBlocking(fileno)
		self.proc = proc
		self.name = name
		self.fd = fileno
		self.startReading()
Exemple #5
0
    def __init__(self, reactor, proc, name, fileno):
        """
		Initialize, specifying a process to connect to.
		"""
        abstract.FileDescriptor.__init__(self, reactor)
        fdesc.setNonBlocking(fileno)
        self.proc = proc
        self.name = name
        self.fd = fileno
        self.startReading()
Exemple #6
0
	def __init__(self, reactor):
		"""Initialize.
		"""
		self.reactor = reactor
		self.i, self.o = os.pipe()
		fdesc.setNonBlocking(self.i)
		fdesc._setCloseOnExec(self.i)
		fdesc.setNonBlocking(self.o)
		fdesc._setCloseOnExec(self.o)
		self.fileno = lambda: self.i
Exemple #7
0
	def __init__(self, reactor, executable, args, environment, path, proto,
				 uid=None, gid=None, usePTY=None):
		"""
		Spawn an operating-system process.

		This is where the hard work of disconnecting all currently open
		files / forking / executing the new process happens.  (This is
		executed automatically when a Process is instantiated.)

		This will also run the subprocess as a given user ID and group ID, if
		specified.  (Implementation Note: this doesn't support all the arcane
		nuances of setXXuid on UNIX: it will assume that either your effective
		or real UID is 0.)
		"""
		if pty is None and not isinstance(usePTY, (tuple, list)):
			# no pty module and we didn't get a pty to use
			raise NotImplementedError(
				"cannot use PTYProcess on platforms without the pty module.")
		abstract.FileDescriptor.__init__(self, reactor)
		_BaseProcess.__init__(self, proto)

		if isinstance(usePTY, (tuple, list)):
			masterfd, slavefd, ttyname = usePTY
		else:
			masterfd, slavefd = pty.openpty()
			ttyname = os.ttyname(slavefd)

		try:
			self._fork(path, uid, gid, executable, args, environment,
					   masterfd=masterfd, slavefd=slavefd)
		except:
			if not isinstance(usePTY, (tuple, list)):
				os.close(masterfd)
				os.close(slavefd)
			raise

		# we are now in parent process:
		os.close(slavefd)
		fdesc.setNonBlocking(masterfd)
		self.fd = masterfd
		self.startReading()
		self.connected = 1
		self.status = -1
		try:
			self.proto.makeConnection(self)
		except:
			log.err()
		registerReapProcessHandler(self.pid, self)
Exemple #8
0
    def __init__(self, reactor=None):
        FileDescriptor.__init__(self, reactor=reactor)

        # Smart way to allow parametrization of libc so I can override
        # it and test for the system errors.
        self._fd = self._inotify.init()

        fdesc.setNonBlocking(self._fd)
        fdesc._setCloseOnExec(self._fd)

        # The next 2 lines are needed to have self.loseConnection()
        # to call connectionLost() on us. Since we already created the
        # fd that talks to inotify we want to be notified even if we
        # haven't yet started reading.
        self.connected = 1
        self._writeDisconnected = True

        self._buffer = ''
        self._watchpoints = {}
        self._watchpaths = {}
Exemple #9
0
	def __init__(self, reactor=None):
		FileDescriptor.__init__(self, reactor=reactor)

		# Smart way to allow parametrization of libc so I can override
		# it and test for the system errors.
		self._fd = self._inotify.init()

		fdesc.setNonBlocking(self._fd)
		fdesc._setCloseOnExec(self._fd)

		# The next 2 lines are needed to have self.loseConnection()
		# to call connectionLost() on us. Since we already created the
		# fd that talks to inotify we want to be notified even if we
		# haven't yet started reading.
		self.connected = 1
		self._writeDisconnected = True

		self._buffer = ''
		self._watchpoints = {}
		self._watchpaths = {}
Exemple #10
0
    def __init__(self,
                 reactor,
                 executable,
                 args,
                 environment,
                 path,
                 proto,
                 uid=None,
                 gid=None,
                 usePTY=None):
        """
		Spawn an operating-system process.

		This is where the hard work of disconnecting all currently open
		files / forking / executing the new process happens.  (This is
		executed automatically when a Process is instantiated.)

		This will also run the subprocess as a given user ID and group ID, if
		specified.  (Implementation Note: this doesn't support all the arcane
		nuances of setXXuid on UNIX: it will assume that either your effective
		or real UID is 0.)
		"""
        if pty is None and not isinstance(usePTY, (tuple, list)):
            # no pty module and we didn't get a pty to use
            raise NotImplementedError(
                "cannot use PTYProcess on platforms without the pty module.")
        abstract.FileDescriptor.__init__(self, reactor)
        _BaseProcess.__init__(self, proto)

        if isinstance(usePTY, (tuple, list)):
            masterfd, slavefd, ttyname = usePTY
        else:
            masterfd, slavefd = pty.openpty()
            ttyname = os.ttyname(slavefd)

        try:
            self._fork(path,
                       uid,
                       gid,
                       executable,
                       args,
                       environment,
                       masterfd=masterfd,
                       slavefd=slavefd)
        except:
            if not isinstance(usePTY, (tuple, list)):
                os.close(masterfd)
                os.close(slavefd)
            raise

        # we are now in parent process:
        os.close(slavefd)
        fdesc.setNonBlocking(masterfd)
        self.fd = masterfd
        self.startReading()
        self.connected = 1
        self.status = -1
        try:
            self.proto.makeConnection(self)
        except:
            log.err()
        registerReapProcessHandler(self.pid, self)