You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add convenience methods for using Cmd.ExtraFiles that returns io.ReadCloser and io.WriteCloser and also deal with file descriptor numbers in the child process. Similar to existing StdinPipe and StdoutPipe methods.
An implementation in the exec package could look something like this:
// ExtraInPipe returns a pipe and fd number that will be connected to a// readable fd in the child process.//// See StdinPipe for how to handle close and exit.func (c*Cmd) ExtraInPipe() (io.WriteCloser, uintptr, error) {
pr, pw, err:=os.Pipe()
iferr!=nil {
returnnil, 0, err
}
c.ExtraFiles=append(c.ExtraFiles, pr)
c.closeAfterStart=append(c.closeAfterStart, pr)
wc:=&closeOnce{File: pw}
c.closeAfterWait=append(c.closeAfterWait, wc)
returnwc, uintptr(len(c.ExtraFiles)) +2, nil
}
// ExtraOutPipe returns a pipe and a fd number that will be connected to a// writable fd in the child process.//// See StdoutPipe for how to handle close and exit.func (c*Cmd) ExtraOutPipe() (io.ReadCloser, uintptr, error) {
pr, pw, err:=os.Pipe()
iferr!=nil {
returnnil, 0, err
}
c.ExtraFiles=append(c.ExtraFiles, pw)
c.closeAfterStart=append(c.closeAfterStart, pw)
c.closeAfterWait=append(c.closeAfterWait, pr)
returnpr, uintptr(len(c.ExtraFiles)) +2, nil
}
Both are very similar to how StdinPipe and StdoutPipe works but also returns the fd number that will be available in the forked child process.
Some design note and issues:
If you want to use exec.Command() and pass an extra pipe fd number as argument you will need to modify Cmd.Args field afterwards.
What happens if ExtraInPipe or ExtraOutPipe is used but Cmd.Start() is never called? enough with os.Pipe() newFile close finalizer for cleanup? user should close them? same issue for StdinPipe and StdoutPipe i guess?
Might be unintuitive that the public ExtraFiles field gets modified.
Uses uintptr for fd number. Same as File.Fd().
Will not work on Windows. Same as for Cmd.ExtraFiles.
@bradfitz Ok thanks. Close or wait for more comments or alternatives?
I did a "execextra" package with a Cmd type that embeds an exec.Cmd and wraps some of the methods. Seems to work quite well. Might add a link to it here if i feel happy with it.
Add convenience methods for using
Cmd.ExtraFiles
that returnsio.ReadCloser
andio.WriteCloser
and also deal with file descriptor numbers in the child process. Similar to existingStdinPipe
andStdoutPipe
methods.An implementation in the exec package could look something like this:
Both are very similar to how
StdinPipe
andStdoutPipe
works but also returns the fd number that will be available in the forked child process.Some design note and issues:
If you want to use
exec.Command()
and pass an extra pipe fd number as argument you will need to modifyCmd.Args
field afterwards.What happens if
ExtraInPipe
orExtraOutPipe
is used butCmd.Start()
is never called? enough withos.Pipe()
newFile close finalizer for cleanup? user should close them? same issue forStdinPipe
andStdoutPipe
i guess?Might be unintuitive that the public ExtraFiles field gets modified.
Uses
uintptr
for fd number. Same asFile.Fd()
.Will not work on Windows. Same as for
Cmd.ExtraFiles
.Related to #15460
The text was updated successfully, but these errors were encountered: