Conversation
Add `cwd:` to `run/2`, `stream/2`, `stream!/2`, and `Process.start/3`. `Daemon`
accepts it in `process_opts`.
The shepherd calls `chdir` after authentication and before `fork`. A failure
returns `{:error, {:shepherd_error, reason}}` before the command starts. The
selected directory controls relative paths and executables.
Accept non-empty binaries without NUL. Relative values use the BEAM working
directory. Paths travel as argv bytes, so non-UTF-8 paths remain valid. The
option does not change `PWD`.
`MSG_ERROR` carries at most 255 bytes. A failure before FD passing will send this frame without file descriptors.
If the OS rejects the arguments or environment for the shepherd, the port exits
before it connects to the Unix socket. NetRunner then waited 10 seconds and
returned `:shepherd_connect_timeout`. A failed token write could also exit the
Process server.
Monitor and unlink the port before sending the token. Wait for socket readiness
and port exit in the same receive block. If the port exits, check the listener
because no sender orders messages from the port and socket.
Return `{:error, {:shepherd_spawn_failed, reason}}` if the listener has no
connection. Preserve immediate child exits.
Accept `:env` as a map or a list of `{name, value}` pairs.
`Port.open` expects environment entries as character lists. The previous
byte-list conversion double-encoded non-ASCII text. Validate UTF-8 before
converting names and values with `String.to_charlist/1`.
An empty string and `nil` both remove a variable because a port cannot set an
empty value. The modified `PATH` controls executable lookup.
Add `env: {:replace, environment}` to define the complete child environment.
Untagged `env:` remains an overlay.
Normalize environment input during option validation. Pass values through the
port environment and selected names through a shepherd allowlist. After
authentication, the shepherd removes all unselected variables before `fork`.
If the replacement omits `PATH`, prevent `execvp` from using its default search
path.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds two child-process options:
cwd:selects the working directoryenv:accepts an overlay or a complete replacement environmentThis PR also fixes shepherd startup failure handling. If the operating system rejects the shepherd’s arguments or environment, NetRunner now reports the failure promptly instead of waiting for the Unix-socket timeout.
Note that the first commit documents existing
MSG_ERRORbehaviour; it does not change the wire format.Spawn sequence
BEAM validate cwd and env create the Unix-socket listener start the shepherd with the requested environment changes wait for either: ├─ an authenticated connection └─ shepherd exit → {:shepherd_spawn_failed, reason} shepherd, after authentication chdir(cwd), when present filter inherited variables, in replacement mode fork exec the childWaiting for socket readiness and shepherd exit in the same receive loop avoids the previous 10-second timeout when the shepherd could not start. An immediate child exit after a successful spawn remains a normal process result.
Temporary socket paths are removed after successful and failed spawns.
Working directory
cwd:is available throughrun/2,stream/2,stream!/2, andNetRunner.Process.start/3.NetRunner.Daemonaccepts it throughprocess_opts.The value must be a non-empty binary without NUL. It does not need to contain valid UTF-8, which permits valid filesystem paths with arbitrary bytes.
The shepherd calls
chdirafter authentication and beforefork. This means:cwd.cwd:resolves from the BEAM working directory.chdirreturns{:error, {:shepherd_error, reason}}before a child starts.PWDis not updated automatically.Environment
env:accepts a map or a list of{name, value}pairs.env: environmentPATHenv: {:replace, environment}PATHA binary value sets a variable.
niland""remove it. If a list contains the same name more than once, the last value wins.Names must be non-empty UTF-8 binaries without
=or NUL. Values must be UTF-8 binaries without NUL, ornil. Invalid input raisesArgumentErrorbefore NetRunner starts the shepherd.In replacement mode, omitting
PATHdisables the defaultexecvpsearch path. A command containing / is still attempted directly.Environment transport
Values pass through the
Port.open/2environment option and do not appear in the shepherd command line. They remain visible through normal process-environment inspection.Replacement mode passes the names of retained variables, but not their values, in the shepherd argument allowlist. Those names count toward both the operating system’s argument and environment size limits.
If the operating system rejects the shepherd because its arguments or environment are too large, NetRunner returns: