Example #1
0
File: process.py Project: bsa3/oil
def ExecExternalProgram(argv, environ):
    """Execute a program and exit this process.

  Called by:
  ls /
  exec ls /
  ( ls / )
  """
    # TODO: If there is an error, like the file isn't executable, then we should
    # exit, and the parent will reap it.  Should it capture stderr?
    try:
        os_.execvpe(argv[0], argv, environ)
    except OSError as e:
        util.error('%r: %s', argv[0], posix.strerror(e.errno))
        # POSIX mentions 126 and 127 for two specific errors.  The rest are
        # unspecified.
        #
        # http://pubs.opengroup.org/onlinepubs/9699919799.2016edition/utilities/V3_chap02.html#tag_18_08_02

        if e.errno == errno.EACCES:
            status = 126
        elif e.errno == errno.ENOENT:
            status = 127  # e.g. command not found should be 127.
        else:
            # dash uses 2, but we use that for parse errors.  This seems to be
            # consistent with mksh and zsh.
            status = 127

        sys.exit(status)
Example #2
0
    def Exec(self, arg_vec, environ):
        """Execute a program and exit this process.

    Called by:
    ls /
    exec ls /
    ( ls / )
    """
        argv = arg_vec.strs
        if self.hijack_shebang:
            try:
                f = self.fd_state.Open(argv[0])
            except OSError as e:
                pass
            else:
                try:
                    line = f.read(40)
                    if _ShouldHijack(line):
                        self.debug_f.log('Hijacked: %s with %s', argv,
                                         self.hijack_shebang)
                        argv = [self.hijack_shebang] + argv
                    else:
                        #self.debug_f.log('Not hijacking %s (%r)', argv, line)
                        pass
                finally:
                    f.close()

        # TODO: If there is an error, like the file isn't executable, then we should
        # exit, and the parent will reap it.  Should it capture stderr?
        try:
            os_.execvpe(argv[0], argv, environ)
        except OSError as e:
            # TODO: Run with /bin/sh when ENOEXEC error (noshebang).  Because all
            # shells do it.

            # Would be nice: when the path is relative and ENOENT: print PWD and do
            # spelling correction?

            self.errfmt.Print("Can't execute %r: %s",
                              argv[0],
                              posix.strerror(e.errno),
                              span_id=arg_vec.spids[0])
            # POSIX mentions 126 and 127 for two specific errors.  The rest are
            # unspecified.
            #
            # http://pubs.opengroup.org/onlinepubs/9699919799.2016edition/utilities/V3_chap02.html#tag_18_08_02

            if e.errno == errno.EACCES:
                status = 126
            elif e.errno == errno.ENOENT:
                status = 127  # e.g. command not found should be 127.
            else:
                # dash uses 2, but we use that for parse errors.  This seems to be
                # consistent with mksh and zsh.
                status = 127

            sys.exit(status)  # raises SystemExit