Ejemplo n.º 1
0
 def test_set_wakeup_fd(self):
     import signal, posix, fcntl
     def myhandler(signum, frame):
         pass
     signal.signal(signal.SIGUSR1, myhandler)
     #
     def cannot_read():
         try:
             posix.read(fd_read, 1)
         except OSError:
             pass
         else:
             raise AssertionError, "os.read(fd_read, 1) succeeded?"
     #
     fd_read, fd_write = posix.pipe()
     flags = fcntl.fcntl(fd_write, fcntl.F_GETFL, 0)
     flags = flags | posix.O_NONBLOCK
     fcntl.fcntl(fd_write, fcntl.F_SETFL, flags)
     flags = fcntl.fcntl(fd_read, fcntl.F_GETFL, 0)
     flags = flags | posix.O_NONBLOCK
     fcntl.fcntl(fd_read, fcntl.F_SETFL, flags)
     #
     old_wakeup = signal.set_wakeup_fd(fd_write)
     try:
         cannot_read()
         posix.kill(posix.getpid(), signal.SIGUSR1)
         res = posix.read(fd_read, 1)
         assert res == '\x00'
         cannot_read()
     finally:
         old_wakeup = signal.set_wakeup_fd(old_wakeup)
     #
     signal.signal(signal.SIGUSR1, signal.SIG_DFL)
Ejemplo n.º 2
0
def main():
	if len(sys.argv) < 2:
		print "Argument missing, usage:\n"
		print "\t$ python %s process_name\n" %(sys.argv[0])
		sys.exit(0)

	readfd, writefd = posix.pipe()
	pid = os.fork()

	if pid:
    		os.close(writefd) # use os.close() to close a file descriptor

    		readfd = os.fdopen(readfd)
    		output = readfd.read()
		output = output.replace("\n", " ")		
    		
		os.waitpid(pid, 0) # make sure the child process gets cleaned up

		print "Killing " + output
		os.system("/bin/kill -9 " + output)
	else:
    		os.close(readfd)
		posix.dup2(writefd, 1)

		os.system("/usr/bin/pgrep " + sys.argv[1])
Ejemplo n.º 3
0
def execute_command():
    pipe_fd = posix.pipe()
    pid = posix.fork()
    if pid == 0:
        cmdline = ["lsblk"]
        if check_fs_val.get() or check_UUID_val.get():
            cmdline.append("-o")
            col = "+"
            if check_fs_val.get():
                col += "fstype"
            if check_fs_val.get() and check_UUID_val.get():
                col += ","
            if check_UUID_val.get():
                col += "UUID"
            cmdline.append(col)
        posix.dup2(pipe_fd[1], 1)
        posix.close(pipe_fd[0])
        posix.close(pipe_fd[1])
        posix.execv("/bin/lsblk", cmdline)
        quit()
    else:
        posix.close(pipe_fd[1])
        ret = bytearray()
        readbytes = posix.read(pipe_fd[0], 1000)
        while readbytes != b"":
            ret += readbytes
            readbytes = posix.read(pipe_fd[0], 1000)
        posix.close(pipe_fd[0])
        posix.wait()
        return str(ret, sys.stdout.encoding)
Ejemplo n.º 4
0
 def do_init(self, license=None):
     # We ignore the license, not needed in the player
     import MMurl, windowinterface
     opts, files = self.tmpopts, self.tmpfiles
     del self.tmpopts
     del self.tmpfiles
     self._tracing = 0
     self.nocontrol = 0  # For player compatability
     self._closing = 0
     self._mm_callbacks = {}
     self.tops = []
     self.last_location = ''
     try:
         import mm, posix, fcntl
     except ImportError:
         pass
     else:
         pipe_r, pipe_w = posix.pipe()
         mm.setsyncfd(pipe_w)
         self._mmfd = pipe_r
         windowinterface.select_setcallback(
             pipe_r, self._mmcallback, (posix.read, fcntl.fcntl, fcntl))
     self.commandlist = [
         OPEN(callback=(self.open_callback, ())),
         OPENFILE(callback=(self.openfile_callback, ())),
         OPEN_RECENT(callback=self.open_recent_callback),  # Dynamic cascade
         #                       RELOAD(callback = (self.reload_callback, ())),
         PREFERENCES(callback=(self.preferences_callback, ())),
         CHECKVERSION(callback=(self.checkversion_callback, ())),
         EXIT(callback=(self.close_callback, ())),
         CHOOSESKIN(callback=(self.skin_callback, ())),
         ##             CHOOSECOMPONENTS(callback = (self.components_callback, ())),
     ]
     import settings
     if hasattr(windowinterface,
                'is_embedded') and windowinterface.is_embedded():
         settings.factory_defaults()
     if settings.get('debug'):
         self.commandlist = self.commandlist + [
             TRACE(callback=(self.trace_callback, ())),
             DEBUG(callback=(self.debug_callback, ())),
             CRASH(callback=(self.crash_callback, ())),
         ]
     MainDialog.__init__(self, 'GRiNS')
     # first open all files
     for file in files:
         self.openURL_callback(MMurl.guessurl(file), 0)
     if not files and settings.get('skin'):
         self.openURL_callback('data:application/smil,<smil/>', 0, 0, 0)
     self._update_recent(None)
     # then play them
     if not hasattr(windowinterface,
                    'is_embedded') or not windowinterface.is_embedded():
         for top in self.tops:
             top.player.playsubtree(top.root)
Ejemplo n.º 5
0
 def test_FileIO_fd_does_not_change_inheritable(self):
     import _io, posix
     fd1, fd2 = posix.pipe()
     posix.set_inheritable(fd1, True)
     posix.set_inheritable(fd2, False)
     f1 = _io.FileIO(fd1, 'r')
     f2 = _io.FileIO(fd2, 'w')
     assert posix.get_inheritable(fd1) == True
     assert posix.get_inheritable(fd2) == False
     f1.close()
     f2.close()
Ejemplo n.º 6
0
    def test_pass_fds_make_inheritable(self):
        import subprocess, posix

        fd1, fd2 = posix.pipe()
        assert posix.get_inheritable(fd1) is False
        assert posix.get_inheritable(fd2) is False

        subprocess.check_call(['/usr/bin/env', 'python', '-c',
                               'import os;os.write(%d,b"K")' % fd2],
                              close_fds=True, pass_fds=[fd2])
        res = posix.read(fd1, 1)
        assert res == b"K"
        posix.close(fd1)
        posix.close(fd2)
Ejemplo n.º 7
0
    def Add(self, p):
        """Append a process to the pipeline."""
        if len(self.procs) == 0:
            self.procs.append(p)
            return

        r, w = posix.pipe()
        #log('pipe for %s: %d %d', p, r, w)
        prev = self.procs[-1]

        prev.AddStateChange(StdoutToPipe(r, w))  # applied on Start()
        p.AddStateChange(StdinFromPipe(r, w))  # applied on Start()

        p.AddPipeToClose(r, w)  # ClosePipe() on Start()

        self.procs.append(p)
Ejemplo n.º 8
0
    def AddLast(self, thunk):
        """Append the last node to the pipeline.

    This is run in the CURRENT process.  It is OPTIONAL, because pipelines in
    the background are run uniformly.
    """
        self.last_thunk = thunk

        if len(self.procs) == 0:  # No pipe: if ! foo
            return

        r, w = posix.pipe()
        #log('last pipe %d %d', r, w)

        prev = self.procs[-1]
        prev.AddStateChange(StdoutToPipe(r, w))

        self.last_pipe = (r, w)  # So we can connect it to last_thunk
Ejemplo n.º 9
0
  def RunCommandSub(self, node):
    p = self._MakeProcess(node,
                          disable_errexit=not self.exec_opts.strict_errexit)

    r, w = posix.pipe()
    p.AddStateChange(process.StdoutToPipe(r, w))
    pid = p.Start()
    #log('Command sub started %d', pid)
    self.waiter.Register(pid, p.WhenDone)

    chunks = []
    posix.close(w)  # not going to write
    while True:
      byte_str = posix.read(r, 4096)
      if not byte_str:
        break
      chunks.append(byte_str)
    posix.close(r)

    status = p.WaitUntilDone(self.waiter)

    # OSH has the concept of aborting in the middle of a WORD.  We're not
    # waiting until the command is over!
    if self.exec_opts.strict_errexit:
      if self.exec_opts.ErrExit() and status != 0:
        raise util.ErrExitFailure(
            'Command sub exited with status %d (%r)', status,
            node.__class__.__name__)
    else:
      # Set a flag so we check errexit at the same time as bash.  Example:
      #
      # a=$(false)
      # echo foo  # no matter what comes here, the flag is reset
      #
      # Set ONLY until this command node has finished executing.
      self.check_command_sub_status = True
      self.mem.last_status = status

    # Runtime errors test case: # $("echo foo > $@")
    # Why rstrip()?
    # https://unix.stackexchange.com/questions/17747/why-does-shell-command-substitution-gobble-up-a-trailing-newline-char
    return ''.join(chunks).rstrip('\n')
Ejemplo n.º 10
0
    def test_set_wakeup_fd(self):
        try:
            import _signal as signal, posix, fcntl
        except ImportError:
            skip('cannot import posix or fcntl')

        def myhandler(signum, frame):
            pass

        signal.signal(signal.SIGINT, myhandler)

        #
        def cannot_read():
            try:
                posix.read(fd_read, 1)
            except OSError:
                pass
            else:
                raise AssertionError("os.read(fd_read, 1) succeeded?")

        #
        fd_read, fd_write = posix.pipe()
        flags = fcntl.fcntl(fd_write, fcntl.F_GETFL, 0)
        flags = flags | posix.O_NONBLOCK
        fcntl.fcntl(fd_write, fcntl.F_SETFL, flags)
        flags = fcntl.fcntl(fd_read, fcntl.F_GETFL, 0)
        flags = flags | posix.O_NONBLOCK
        fcntl.fcntl(fd_read, fcntl.F_SETFL, flags)
        #
        old_wakeup = signal.set_wakeup_fd(fd_write)
        try:
            cannot_read()
            posix.kill(posix.getpid(), signal.SIGINT)
            res = posix.read(fd_read, 1)
            assert res == bytes([signal.SIGINT])
            cannot_read()
        finally:
            old_wakeup = signal.set_wakeup_fd(old_wakeup)
        #
        signal.signal(signal.SIGINT, signal.SIG_DFL)
Ejemplo n.º 11
0
    def _ApplyRedirect(self, r, waiter):
        ok = True

        if r.tag == redirect_e.PathRedirect:
            if r.op_id in (Id.Redir_Great, Id.Redir_AndGreat):  # >   &>
                # NOTE: This is different than >| because it respects noclobber, but
                # that option is almost never used.  See test/wild.sh.
                mode = posix.O_CREAT | posix.O_WRONLY | posix.O_TRUNC
            elif r.op_id == Id.Redir_Clobber:  # >|
                mode = posix.O_CREAT | posix.O_WRONLY | posix.O_TRUNC
            elif r.op_id in (Id.Redir_DGreat, Id.Redir_AndDGreat):  # >>   &>>
                mode = posix.O_CREAT | posix.O_WRONLY | posix.O_APPEND
            elif r.op_id == Id.Redir_Less:  # <
                mode = posix.O_RDONLY
            else:
                raise NotImplementedError(r.op_id)

            # NOTE: 0666 is affected by umask, all shells use it.
            try:
                target_fd = posix.open(r.filename, mode, 0666)
            except OSError as e:
                util.error("Can't open %r: %s", r.filename,
                           posix.strerror(e.errno))
                return False

            # Apply redirect
            if not self._PushDup(target_fd, r.fd):
                ok = False

            # Now handle the extra redirects for aliases &> and &>>.
            #
            # We can rewrite
            #   stdout_stderr.py &> out-err.txt
            # as
            #   stdout_stderr.py > out-err.txt 2>&1
            #
            # And rewrite
            #   stdout_stderr.py 3&> out-err.txt
            # as
            #   stdout_stderr.py 3> out-err.txt 2>&3
            if ok:
                if r.op_id == Id.Redir_AndGreat:
                    if not self._PushDup(r.fd, 2):
                        ok = False
                elif r.op_id == Id.Redir_AndDGreat:
                    if not self._PushDup(r.fd, 2):
                        ok = False

            posix.close(target_fd)  # We already made a copy of it.
            # I don't think we need to close(0) because it will be restored from its
            # saved position (10), which closes it.
            #self._PushClose(r.fd)

        elif r.tag == redirect_e.DescRedirect:  # e.g. echo hi 1>&2

            if r.op_id == Id.Redir_GreatAnd:  # 1>&2
                if not self._PushDup(r.target_fd, r.fd):
                    ok = False
            elif r.op_id == Id.Redir_LessAnd:  # 0<&5
                # The only difference between >& and <& is the default file
                # descriptor argument.
                if not self._PushDup(r.target_fd, r.fd):
                    ok = False
            else:
                raise NotImplementedError

        elif r.tag == redirect_e.HereRedirect:
            # NOTE: Do these descriptors have to be moved out of the range 0-9?
            read_fd, write_fd = posix.pipe()

            if not self._PushDup(read_fd, r.fd):  # stdin is now the pipe
                ok = False

            # We can't close like we do in the filename case above?  The writer can
            # get a "broken pipe".
            self._PushClose(read_fd)

            thunk = _HereDocWriterThunk(write_fd, r.body)

            # TODO: Use PIPE_SIZE to save a process in the case of small here docs,
            # which are the common case.  (dash does this.)
            start_process = True
            #start_process = False

            if start_process:
                here_proc = Process(thunk)

                # NOTE: we could close the read pipe here, but it doesn't really
                # matter because we control the code.
                # here_proc.StateChange()
                pid = here_proc.Start()
                # no-op callback
                waiter.Register(pid, here_proc.WhenDone)
                #log('Started %s as %d', here_proc, pid)
                self._PushWait(here_proc, waiter)

                # Now that we've started the child, close it in the parent.
                posix.close(write_fd)

            else:
                posix.write(write_fd, r.body)
                posix.close(write_fd)

        return ok
Ejemplo n.º 12
0
    def do_init(self, license):
        opts, files = self.tmpopts, self.tmpfiles
        del self.tmpopts
        del self.tmpfiles
        ##         del self.tmplicensedialog
        import MMurl
        import windowinterface
        if features.lightweight and len(files) > 1:
            windowinterface.showmessage(
                'Cannot open multiple files in this product.')
            files = files[:1]
        self._license = license
        ##         if not self._license.have('save'):
        ##             windowinterface.showmessage(
        ##                 'This is a demo version.\n'+
        ##                 'You will not be able to save your changes.',
        ##                 title='CMIFed license')
        self._tracing = 0
        self.tops = []
        self._mm_callbacks = {}
        self.last_location = ''
        self._untitled_counter = 1
        self.template_info = None
        try:
            import mm, posix, fcntl
        except ImportError:
            pass
        else:
            pipe_r, pipe_w = posix.pipe()
            mm.setsyncfd(pipe_w)
            self._mmfd = pipe_r
            windowinterface.select_setcallback(
                pipe_r, self._mmcallback,
                (posix.read, fcntl.fcntl, fcntl.F_SETFL, posix.O_NDELAY))
        self.commandlist = [
            EXIT(callback=(self.close_callback, ())),
            NEW_DOCUMENT(callback=(self.new_callback, ())),
            OPEN(callback=(self.open_callback, ())),
            OPENFILE(callback=(self.openfile_callback, ())),
            OPEN_RECENT(callback=self.open_recent_callback),  # Dynamic cascade
            PREFERENCES(callback=(self.preferences_callback, ())),
            CHECKVERSION(callback=(self.checkversion_callback, ())),
        ]
        import settings
        if self._license.have('preregistered'):
            settings.set('registered', 'preregistered')
        if not self._license.is_evaluation_license() and \
                        settings.get('registered') != 'preregistered':
            self.commandlist.append(
                REGISTER(callback=(self.register_callback, ())))
        import Help
        if hasattr(Help, 'hashelp') and Help.hashelp():
            self.commandlist.append(HELP(callback=(self.help_callback, ())))
        if __debug__:
            if settings.get('debug'):
                self.commandlist = self.commandlist + [
                    TRACE(callback=(self.trace_callback, ())),
                    DEBUG(callback=(self.debug_callback, ())),
                    CRASH(callback=(self.crash_callback, ())),
                ]

        if self.splash is not None:
            self.splash.unsplash()
            self.splash = None

        MainDialog.__init__(self, 'CMIFed', (not not files))

        for file in files:
            self.openURL_callback(MMurl.guessurl(file))
        self._update_recent(None)

        if ENABLE_FNORB_SUPPORT:
            import CORBA.services
            self.corba_services = CORBA.services.services(sys.argv)

        if settings.get('registered') == 'notyet' and \
                        not self._license.is_evaluation_license():
            answer = windowinterface.RegisterDialog()
            astr = ['yes', 'no', 'notyet'][answer]
            settings.set('registered', astr)
            settings.save()
            if astr == 'yes':
                self.register_callback()
Ejemplo n.º 13
0
def StartEmulator(exe_name='emulator', kernel=None,
                  ramdisk=None, image=None, userdata=None, system=None):
  """Runs the emulator with the specified arguments.

  Args:
    exe_name: The name of the emulator to run.  May be absolute, relative,
              or unqualified (and left to exec() to find).
    kernel: If set, passed to the emulator as "-kernel".
    ramdisk: If set, passed to the emulator as "-ramdisk".
    image: If set, passed to the emulator as "-system".
    userdata: If set, passed to the emulator as "-initdata" and "-data".
    system: If set, passed to the emulator as "-sysdir".

  Returns:
    A subprocess.Popen that refers to the emulator process, or None if
    a problem occurred.
  """
  #exe_name = './stuff'
  args = [exe_name]
  if kernel: args += ['-kernel', kernel]
  if ramdisk: args += ['-ramdisk', ramdisk]
  if image: args += ['-system', image]
  if userdata: args += ['-initdata', userdata, '-data', userdata]
  if system: args += ['-sysdir', system]
  args += ['-partition-size', '128']
  args += ['-no-window', '-netfast', '-noaudio']

  _USE_PIPE = True

  if _USE_PIPE:
    # Use dedicated fds instead of stdin/out to talk to the
    # emulator so that the emulator doesn't try to tty-cook
    # the data.
    em_stdin_r, em_stdin_w = posix.pipe()
    em_stdout_r, em_stdout_w = posix.pipe()
    args += ['-shell-serial', 'fdpair:%d:%d' % (em_stdin_r, em_stdout_w)]
  else:
    args += ['-shell']

  # This is a work-around for the ARMv7 emulation bug.
  # XXX: It only works by chance, if any ! A real emulation fix is on the way
  args += ['-qemu', '-singlestep']

  # Ensure that this environment variable isn't set;
  # if it is, the emulator will print the log to stdout.
  if os.environ.get('ANDROID_LOG_TAGS'):
    del os.environ['ANDROID_LOG_TAGS']

  try:
    # bufsize=1 line-buffered, =0 unbuffered,
    # <0 system default (fully buffered)
    Trace('Running emulator: %s' % ' '.join(args))
    if _USE_PIPE:
      ep = subprocess.Popen(args)
    else:
      ep = subprocess.Popen(args, close_fds=True,
                            stdin=subprocess.PIPE,
                            stdout=subprocess.PIPE,
                            stderr=subprocess.PIPE)
    if ep:
      if _USE_PIPE:
        # Hijack the Popen.stdin/.stdout fields to point to our
        # pipes.  These are the same fields that would have been set
        # if we called Popen() with stdin=subprocess.PIPE, etc.
        # Note that these names are from the point of view of the
        # child process.
        #
        # Since we'll be using select.select() to read data a byte
        # at a time, it's important that these files are unbuffered
        # (bufsize=0).  If Popen() took care of the pipes, they're
        # already unbuffered.
        ep.stdin = os.fdopen(em_stdin_w, 'w', 0)
        ep.stdout = os.fdopen(em_stdout_r, 'r', 0)
      return ep
  except OSError, e:
    print >>sys.stderr, 'Could not start emulator:', e
Ejemplo n.º 14
0
def StartEmulator(exe_name='emulator', kernel=None,
                  ramdisk=None, image=None, userdata=None, system=None):
  """Runs the emulator with the specified arguments.

  Args:
    exe_name: The name of the emulator to run.  May be absolute, relative,
              or unqualified (and left to exec() to find).
    kernel: If set, passed to the emulator as "-kernel".
    ramdisk: If set, passed to the emulator as "-ramdisk".
    image: If set, passed to the emulator as "-image".
    userdata: If set, passed to the emulator as "-initdata" and "-data".
    system: If set, passed to the emulator as "-system".

  Returns:
    A subprocess.Popen that refers to the emulator process, or None if
    a problem occurred.
  """
  #exe_name = './stuff'
  args = [exe_name]
  if kernel: args += ['-kernel', kernel]
  if ramdisk: args += ['-ramdisk', ramdisk]
  if image: args += ['-image', image]
  if userdata: args += ['-initdata', userdata, '-data', userdata]
  if system: args += ['-system', system]
  args += ['-no-window', '-netfast', '-noaudio']

  _USE_PIPE = True

  if _USE_PIPE:
    # Use dedicated fds instead of stdin/out to talk to the
    # emulator so that the emulator doesn't try to tty-cook
    # the data.
    em_stdin_r, em_stdin_w = posix.pipe()
    em_stdout_r, em_stdout_w = posix.pipe()
    args += ['-shell-serial', 'fdpair:%d:%d' % (em_stdin_r, em_stdout_w)]
  else:
    args += ['-shell']

  # Ensure that this environment variable isn't set;
  # if it is, the emulator will print the log to stdout.
  if os.environ.get('ANDROID_LOG_TAGS'):
    del os.environ['ANDROID_LOG_TAGS']

  try:
    # bufsize=1 line-buffered, =0 unbuffered,
    # <0 system default (fully buffered)
    Trace('Running emulator: %s' % ' '.join(args))
    if _USE_PIPE:
      ep = subprocess.Popen(args)
    else:
      ep = subprocess.Popen(args, close_fds=True,
                            stdin=subprocess.PIPE,
                            stdout=subprocess.PIPE,
                            stderr=subprocess.PIPE)
    if ep:
      if _USE_PIPE:
        # Hijack the Popen.stdin/.stdout fields to point to our
        # pipes.  These are the same fields that would have been set
        # if we called Popen() with stdin=subprocess.PIPE, etc.
        # Note that these names are from the point of view of the
        # child process.
        #
        # Since we'll be using select.select() to read data a byte
        # at a time, it's important that these files are unbuffered
        # (bufsize=0).  If Popen() took care of the pipes, they're
        # already unbuffered.
        ep.stdin = os.fdopen(em_stdin_w, 'w', 0)
        ep.stdout = os.fdopen(em_stdout_r, 'r', 0)
      return ep
  except OSError, e:
    print >>sys.stderr, 'Could not start emulator:', e
Ejemplo n.º 15
0
  def RunProcessSub(self, node, op_id):
    """Process sub creates a forks a process connected to a pipe.

    The pipe is typically passed to another process via a /dev/fd/$FD path.

    TODO:

    sane-proc-sub:
    - wait for all the words

    Otherwise, set $!  (mem.last_job_id)

    strict-proc-sub:
    - Don't allow it anywhere except SimpleCommand, any redirect, or
    Assignment?  And maybe not even assignment?

    Should you put return codes in @PROCESS_SUB_STATUS?  You need two of them.
    """
    p = self._MakeProcess(node)

    r, w = posix.pipe()

    if op_id == Id.Left_ProcSubIn:
      # Example: cat < <(head foo.txt)
      #
      # The head process should write its stdout to a pipe.
      redir = process.StdoutToPipe(r, w)

    elif op_id == Id.Left_ProcSubOut:
      # Example: head foo.txt > >(tac)
      #
      # The tac process should read its stdin from a pipe.
      #
      # NOTE: This appears to hang in bash?  At least when done interactively.
      # It doesn't work at all in osh interactively?
      redir = process.StdinFromPipe(r, w)

    else:
      raise AssertionError

    p.AddStateChange(redir)

    # Fork, letting the child inherit the pipe file descriptors.
    pid = p.Start()

    # After forking, close the end of the pipe we're not using.
    if op_id == Id.Left_ProcSubIn:
      posix.close(w)
    elif op_id == Id.Left_ProcSubOut:
      posix.close(r)
    else:
      raise AssertionError

    #log('I am %d', posix.getpid())
    #log('Process sub started %d', pid)
    self.waiter.Register(pid, p.WhenDone)

    # NOTE: Like bash, we never actually wait on it!
    # TODO: At least set $! ?

    # Is /dev Linux-specific?
    if op_id == Id.Left_ProcSubIn:
      return '/dev/fd/%d' % r

    elif op_id == Id.Left_ProcSubOut:
      return '/dev/fd/%d' % w

    else:
      raise AssertionError
Ejemplo n.º 16
0
 def test_pipe(self):
     if hasattr(posix, 'pipe'):
         reader, writer = posix.pipe()
         os.close(reader)
         os.close(writer)
Ejemplo n.º 17
0
def StartEmulator(exe_name="emulator", kernel=None, ramdisk=None, image=None, userdata=None, system=None):
    """Runs the emulator with the specified arguments.

  Args:
    exe_name: The name of the emulator to run.  May be absolute, relative,
              or unqualified (and left to exec() to find).
    kernel: If set, passed to the emulator as "-kernel".
    ramdisk: If set, passed to the emulator as "-ramdisk".
    image: If set, passed to the emulator as "-system".
    userdata: If set, passed to the emulator as "-initdata" and "-data".
    system: If set, passed to the emulator as "-sysdir".

  Returns:
    A subprocess.Popen that refers to the emulator process, or None if
    a problem occurred.
  """
    # exe_name = './stuff'
    args = [exe_name]
    if kernel:
        args += ["-kernel", kernel]
    if ramdisk:
        args += ["-ramdisk", ramdisk]
    if image:
        args += ["-system", image]
    if userdata:
        args += ["-initdata", userdata, "-data", userdata]
    if system:
        args += ["-sysdir", system]
    args += ["-partition-size", "128"]
    args += ["-no-window", "-netfast", "-noaudio"]

    _USE_PIPE = True

    if _USE_PIPE:
        # Use dedicated fds instead of stdin/out to talk to the
        # emulator so that the emulator doesn't try to tty-cook
        # the data.
        em_stdin_r, em_stdin_w = posix.pipe()
        em_stdout_r, em_stdout_w = posix.pipe()
        args += ["-shell-serial", "fdpair:%d:%d" % (em_stdin_r, em_stdout_w)]
    else:
        args += ["-shell"]

    # This is a work-around for the ARMv7 emulation bug.
    # XXX: It only works by chance, if any ! A real emulation fix is on the way
    args += ["-qemu", "-singlestep"]

    # Ensure that this environment variable isn't set;
    # if it is, the emulator will print the log to stdout.
    if os.environ.get("ANDROID_LOG_TAGS"):
        del os.environ["ANDROID_LOG_TAGS"]

    try:
        # bufsize=1 line-buffered, =0 unbuffered,
        # <0 system default (fully buffered)
        Trace("Running emulator: %s" % " ".join(args))
        if _USE_PIPE:
            ep = subprocess.Popen(args)
        else:
            ep = subprocess.Popen(
                args, close_fds=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE
            )
        if ep:
            if _USE_PIPE:
                # Hijack the Popen.stdin/.stdout fields to point to our
                # pipes.  These are the same fields that would have been set
                # if we called Popen() with stdin=subprocess.PIPE, etc.
                # Note that these names are from the point of view of the
                # child process.
                #
                # Since we'll be using select.select() to read data a byte
                # at a time, it's important that these files are unbuffered
                # (bufsize=0).  If Popen() took care of the pipes, they're
                # already unbuffered.
                ep.stdin = os.fdopen(em_stdin_w, "w", 0)
                ep.stdout = os.fdopen(em_stdout_r, "r", 0)
            return ep
    except OSError, e:
        print >> sys.stderr, "Could not start emulator:", e