Commit Graph

31 Commits

Author SHA1 Message Date
Marcel Hellwig
af6ace6209 convert custom try macro to ?
resolves #60580
2019-05-06 14:54:27 +02:00
Taiki Endo
93b6d9e086 libstd => 2018 2019-02-28 04:06:15 +09:00
Josh Stone
a301655c8a Use posix_spawn_file_actions_addchdir_np when possible
This is a non-POSIX extension implemented in Solaris and in glibc 2.29.
With this we can still use `posix_spawn()` when `Command::current_dir()`
has been set, otherwise we fallback to `fork(); chdir(); exec()`.
2019-02-13 12:20:23 -08:00
Alexander Regueiro
99ed06eb88 libs: doc comments 2019-02-10 23:57:25 +00:00
Mark Rousskov
2a663555dd Remove licenses 2018-12-25 21:08:33 -07:00
ljedrz
8c4129cd9a cleanup: remove static lifetimes from consts in libstd 2018-12-04 10:21:42 +01:00
Alex Crichton
4032b7a429 std: Synchronize access to global env during exec
This commit, after reverting #55359, applies a different fix for #46775
while also fixing #55775. The basic idea was to go back to pre-#55359
libstd, and then fix #46775 in a way that doesn't expose #55775.

The issue described in #46775 boils down to two problems:

* First, the global environment is reset during `exec` but, but if the
  `exec` call fails then the global environment was a dangling pointer
  into free'd memory as the block of memory was deallocated when
  `Command` is dropped. This is fixed in this commit by installing a
  `Drop` stack object which ensures that the `environ` pointer is
  preserved on a failing `exec`.

* Second, the global environment was accessed in an unsynchronized
  fashion during `exec`. This was fixed by ensuring that the
  Rust-specific environment lock is acquired for these system-level
  operations.

Thanks to Alex Gaynor for pioneering the solution here!

Closes #55775

Co-authored-by: Alex Gaynor <alex.gaynor@gmail.com>
2018-11-14 12:46:57 -08:00
Alex Crichton
5856797bda Revert "Fixes #46775 -- don't mutate the process's environment in Command::exec"
This reverts commit 36fe3b605a.
2018-11-14 07:21:01 -08:00
Alex Gaynor
36fe3b605a Fixes #46775 -- don't mutate the process's environment in Command::exec
Instead, pass the environment to execvpe, so the kernel can apply it directly to the new process. This avoids a use-after-free in the case where exec'ing the new process fails for any reason, as well as a race condition if there are other threads alive during the exec.
2018-11-01 12:51:24 +00:00
ljedrz
d28aed6dc4 Prefer unwrap_or_else to unwrap_or in case of function calls/allocations 2018-10-19 09:45:45 +02:00
Bryan Drewery
6212904dd8 Don't use posix_spawn() if PATH was modified in the environment.
The expected behavior is that the environment's PATH should be used
to find the process.  posix_spawn() could be used if we iterated
PATH to search for the binary to execute.  For now just skip
posix_spawn() if PATH is modified.
2018-03-19 15:40:09 -07:00
Bryan Drewery
d740083fc8 Support posix_spawn() for Linux glibc 2.24+.
The relevant support was added in https://sourceware.org/bugzilla/show_bug.cgi?id=10354#c12
2018-03-02 13:08:04 -08:00
Bryan Drewery
99b50efb6e Use _ 2018-03-02 08:50:37 -08:00
Bryan Drewery
ef73b3ae2e Add comment explaining when posix_spawn() can be supported. 2018-03-01 09:18:16 -08:00
Bryan Drewery
2e2d9260f9 posix_spawn() on OSX supports returning ENOENT. 2018-03-01 09:17:49 -08:00
Bryan Drewery
a9ea876960 posix_spawn() always returns its error rather than setting errno. 2018-02-28 22:16:35 -08:00
Bryan Drewery
85b82f254e Support posix_spawn() for FreeBSD.
spawn() is expected to return an error if the specified file could not be
executed.  FreeBSD's posix_spawn() supports returning ENOENT/ENOEXEC if
the exec() fails, which not all platforms support.  This brings a very
significant performance improvement for FreeBSD, involving heavy use of
Command in threads, due to fork() invoking jemalloc fork handlers and
causing lock contention.  FreeBSD's posix_spawn() avoids this problem
due to using vfork() internally.
2018-02-28 15:36:32 -08:00
Bryan Drewery
b3ecf5f57c Remove excess newline 2018-02-28 15:36:32 -08:00
Bryan Drewery
8e3fa0d3c4 Pass proper pointer for envp. 2018-02-28 15:36:32 -08:00
Bryan Drewery
94630e4ca5 No need to zero when an initializer for the object is already used. 2018-02-28 15:36:32 -08:00
Bryan Drewery
f4633865d3 Avoid error for unused variables 2018-02-28 15:36:31 -08:00
Alex Crichton
11696acd6d Support posix_spawn() when possible. 2018-02-28 15:35:59 -08:00
Diggory Blake
ccc91d7b48 Capture environment at spawn 2017-12-24 14:24:31 +00:00
est31
aad1c998c7 Remove nacl from libstd 2017-10-05 05:01:41 +02:00
Sebastian Humenda
2cf0a4ad46 Match c_char definitions and enable signal reset for L4Re
*   Match definition of c_char in os/raw.rs with the libc definition

    Due to historic reasons, os/raw.rs redefines types for c_char from
    libc, but these didn't match. Now they do :).

*   Enable signal reset on exec for L4Re

    L4Re has full signal emulation and hence it needs to reset the
    signal set of the child with sigemptyset. However, gid and uid
    should *not* be set.
2017-09-08 14:36:56 +02:00
Alex Crichton
e5b123cba2 Update the libc submodule
Brings in a few fixes for wasm/asmjs
2017-08-28 07:58:19 -07:00
Marco A L Barbosa
24be89980e Avoid using libc::sigemptyset on Android 2017-03-25 17:15:26 -03:00
Corey Farwell
97a1b6a055 Update usages of 'OSX' (and other old names) to 'macOS'.
As of last year with version 'Sierra', the Mac operating system is now
called 'macOS'.
2017-03-12 14:59:04 -04:00
Jack O'Connor
2a345bbcc1 make Child::try_wait return io::Result<Option<ExitStatus>>
This is much nicer for callers who want to short-circuit real I/O errors
with `?`, because they can write this

    if let Some(status) = foo.try_wait()? {
        ...
    } else {
        ...
    }

instead of this

    match foo.try_wait() {
        Ok(status) => {
            ...
        }
        Err(err) if err.kind() == io::ErrorKind::WouldBlock => {
            ...
        }
        Err(err) => return Err(err),
    }

The original design of `try_wait` was patterned after the `Read` and
`Write` traits, which support both blocking and non-blocking
implementations in a single API. But since `try_wait` is never blocking,
it makes sense to optimize for the non-blocking case.

Tracking issue: https://github.com/rust-lang/rust/issues/38903
2017-02-06 23:04:47 -05:00
Alex Crichton
abb9189083 std: Add a nonblocking Child::try_wait method
This commit adds a new method to the `Child` type in the `std::process` module
called `try_wait`. This method is the same as `wait` except that it will not
block the calling thread and instead only attempt to collect the exit status. On
Unix this means that we call `waitpid` with the `WNOHANG` flag and on Windows it
just means that we pass a 0 timeout to `WaitForSingleObject`.

Currently it's possible to build this method out of tree, but it's unfortunately
tricky to do so. Specifically on Unix you essentially lose ownership of the pid
for the process once a call to `waitpid` has succeeded. Although `Child` tracks
this state internally to be resilient to multiple calls to `wait` or a `kill`
after a successful wait, if the child is waited on externally then the state
inside of `Child` is not updated. This means that external implementations of
this method must be extra careful to essentially not use a `Child`'s methods
after a call to `waitpid` has succeeded (even in a nonblocking fashion).

By adding this functionality to the standard library it should help canonicalize
these external implementations and ensure they can continue to robustly reuse
the `Child` type from the standard library without worrying about pid ownership.
2017-01-06 21:20:39 -08:00
Theodore DeRego
5c1c48532f Separated fuchsia-specific process stuff into 'process_fuchsia.rs' and refactored out some now-duplicated code into a 'process_common.rs' 2016-11-23 13:58:13 -08:00