Documentation
¶
Overview ¶
Package proc starts a child process for hail. It is the only package of hail that starts a child (AGENTS.md, rule 14).
Two paths ¶
A build by Go starts a child through os/exec. A build by TinyGo cannot do that, because os.StartProcess of TinyGo refuses a directory, a SysProcAttr and any file. So a build by TinyGo on Linux starts a child with a spawner of its own, on raw system calls (D24). A build by TinyGo for another platform, and every WebAssembly build, cannot start a child. There, Start returns ErrNotSupported, and CanStart is false. TinyGo 0.44.0 for Windows cannot call a function of a DLL, which golang.org/x/sys/windows and CreateProcess need, so no spawner is written for it.
Only the files that start a child carry a build constraint on tinygo. The API is the same for both compilers, and so is the behavior that the tests of this package check.
The promise of Start ¶
Start returns after the child called execve, or with the error of the step that failed, such as the error of execve. When Start fails, no child is left behind: the child either never started, or Start waited for it. When Start returns with no error, the process group and the terminal of Attr are already set.
Start does not search $PATH. The shell finds the file and gives its path. Start changes no state of the whole process of hail, such as its directory, its environment or its descriptors (D7). The child gets each of them from Attr.
The starter ¶
A child gets its umask and its limits of resources from the process that starts it, and hail does not change its own (D7). syscall.SysProcAttr has no field for either. So a child with a umask or limits of its own in Attr starts through a starter: a copy of hail, at the path Attr.Starter, that sets them and then runs the child in its own process with execve (D6). The main function of hail calls RunStarter first, and a test binary that is a starter does the same in its TestMain.
On FreeBSD and DragonFly, the package syscall gives a child its path as argv[0] when argv[0] is longer than the path. A child with such an argv[0], as from exec -a, starts through the starter too, when Attr.Starter is set.
The starter gets one more descriptor, the end of a pipe, which closes when execve succeeds. When a step fails, the starter writes the step and the errno on it. So Start keeps its promise through a starter too, and the error of execve, such as ENOEXEC, reaches the shell as it does without a starter. Windows has no umask and no such limits, and leaves them out.
Windows ¶
A program on Windows gets its arguments as one command line, and splits it itself. Start joins the arguments by the rules of the C runtime of Microsoft, which CommandLineToArgvW follows, and runs a batch file, .bat or .cmd, through cmd.exe with a command line quoted by the rules of cmd.exe, as the standard library of Rust does since the fault known as BatBadBut (cmdline.go). The child gets only descriptors 0, 1 and 2.
Windows has no signals between processes, no process groups as Unix has them, and no stops. Each child starts suspended, joins a job object, and then runs, so that every process that it starts joins the job too. A child that ignores SIGINT, as a program of a job of & does, or that asks for a process group, leads a console process group of its own, which Ctrl-C at the console does not reach. [Kill] sends Ctrl-Break for SIGINT to such a child, ends the whole job object for most other signals, and fails for the signals that stop a process. A child that Ctrl-C ended, and a program of Cygwin that a signal ended, report the signal in Status. A build by TinyGo for Windows cannot start a child (D24).
The starter also sets the action of a signal in the child. os/exec cannot make a child ignore a signal that hail does not ignore, as a shell starts the programs of a list that ends with & with SIGINT and SIGQUIT ignored (Attr.Ignore), nor give the default action to a signal that hail ignores (Attr.Default). Under Go, such a child starts through the starter. The spawner of TinyGo sets the actions itself.
Job control ¶
Process.WaitChange reports a child that stops or goes on, as a shell with job control waits for its jobs. hail waits for each child with wait4 itself, and never with Wait of exec.Cmd, which does not report a stop. Attr.Setpgid and Attr.Foreground give a child a process group and the terminal as it starts, and SetForeground gives the terminal back to hail. Attr.Setsid starts a child in a session of its own, as a test starts hail on a pseudo-terminal.
A keeper (StartKeeper) is a copy of hail that stands for a job of the shell, which runs on goroutines (D6). Its process ID is the process ID of the job, and with job control its process group is the group of the job. It sends hail each signal that it gets, and hail sees it stop.
Index ¶
Constants ¶
const CanStart = true
CanStart reports whether this build can start a child. It is false where Start returns ErrNotSupported.
Variables ¶
var ErrNotSupported = fmt.Errorf("this build cannot start a process: %w", errors.ErrUnsupported)
ErrNotSupported is the error of Start in a build that cannot start a child: a build by TinyGo for a platform other than Linux, and the WebAssembly builds. It wraps errors.ErrUnsupported.
Functions ¶
func Foreground ¶
Foreground returns the process group in the foreground of the terminal on the descriptor fd, as tcgetpgrp does.
func RunStarter ¶
RunStarter runs a copy of hail as a starter or as a keeper (keeper.go), when args, the arguments of the process, ask for one. It returns false when they do not. Otherwise it does not return. A starter sets the umask, the limits and the ignored signals that args give, and replaces the process with the child, or ends the process with status 127 and writes the error on its pipe of errors.
The main function of hail calls RunStarter first. A starter changes the umask and the limits of its own process before it runs the child, which is the start-up of that process, as D7 allows.
func SetForeground ¶
SetForeground makes the process group pgid the group in the foreground of the terminal on the descriptor fd of hail, as tcsetpgrp does. hail calls it to take its terminal back from a job, while hail is in the background, so SIGTTOU does not stop or interrupt hail during the call. On a Unix system other than Linux, the call ignores SIGTTOU for a moment, which takes away a handler that os/signal set for it: the caller sets it again.
func SetOwnGroup ¶
func SetOwnGroup() error
SetOwnGroup puts hail in a process group of its own, as an interactive shell with job control does when it starts. It changes the process, so hail calls it only at start-up (D7).
Types ¶
type Attr ¶
type Attr struct {
// Dir is the working directory of the child, as a path of the operating
// system. An empty Dir gives the child the directory of hail. A relative
// path of [Start] is relative to Dir.
Dir string
// Env is the environment of the child, as "KEY=value" strings. The child
// gets exactly these strings. A nil Env gives the child an empty
// environment, not the environment of hail.
Env []string
// Files gives the descriptors of the child: Files[i] becomes descriptor
// i of the child. A nil entry leaves descriptor i closed in the child.
// When Files has fewer than three entries, the descriptors from len(Files)
// to 2 are closed too. The child gets no other descriptor of hail.
//
// On Windows, the child gets only the first three entries, as a program
// of Windows has no other descriptors, and a nil entry gives the null
// device instead of a closed descriptor.
Files []*os.File
// Setpgid puts the child in the process group Pgid. When Pgid is 0, the
// child starts a new group, with its process ID as the ID of the group.
// On Windows, the child leads a console process group of its own, and
// joins the job object of the group Pgid, or a new one when Pgid is 0.
Setpgid bool
// Pgid is the process group of the child when Setpgid is set.
Pgid int
// Setsid starts the child in a new session, as the leader of its own
// process group. It cannot go with Setpgid. Setctty then makes
// descriptor 0 of the child its controlling terminal. A test of an
// interactive shell starts the shell this way on a pseudo-terminal.
// Windows has neither, and Start there fails when Setsid is set.
Setsid bool
Setctty bool
// Foreground gives the terminal on the descriptor TTY of hail to the
// process group of the child. It needs Setpgid.
Foreground bool
// TTY is the descriptor of hail that holds the terminal when Foreground
// is set.
TTY int
// SetUmask gives the child the umask Umask. Limits are limits of
// resources for the child. A child with either one starts through a
// copy of hail at the path Starter, which sets them and then runs the
// child in its own process (D6). Windows has neither, and leaves them
// out. On FreeBSD, a child whose argv[0] is longer than its path
// starts through the starter too, when Starter is set, so that it
// keeps its argv[0].
SetUmask bool
Umask uint32
Limits []Limit
Starter string
// Ignore holds the signals that the child starts with ignored, as a
// shell starts the programs of a list that ends with & with SIGINT
// and SIGQUIT ignored. A signal that hail itself ignores stays ignored
// in the child anyway. Under Go, a child that must ignore a signal
// that hail does not ignore starts through the starter, as for a
// umask, because os/exec cannot set the action of a signal in the
// child. On Windows, a child that ignores SIGINT leads a console
// process group of its own, which Ctrl-C at the console does not
// reach. Windows leaves out the other signals.
Ignore []syscall.Signal
// Default holds the signals that the child starts with their default
// action, though hail ignores them, as an interactive shell with job
// control ignores SIGTTIN and SIGTTOU and its programs do not. A
// signal of Ignore wins. Under Go, such a child starts through the
// starter.
Default []syscall.Signal
}
Attr is what the child gets from hail when it starts.
type Keeper ¶
type Keeper struct {
// Process is the keeper. Wait for it with WaitChange until it ends.
*Process
// Signals gives each signal that the keeper got, in order. It closes
// when the keeper ends. A 0 answers a call of Flush.
Signals <-chan syscall.Signal
// contains filtered or unexported fields
}
Keeper is a copy of hail that stands for a job.
func StartKeeper ¶
StartKeeper starts a keeper from the copy of hail at attr.Starter. The keeper gets the process group of attr, and no other attribute: it starts with no descriptor of hail but its two pipes.
type Limit ¶
type Limit struct {
// Resource is the number of the resource, such as RLIMIT_NOFILE.
Resource int
// Soft and Hard are the limits. RLIM_INFINITY is no limit.
Soft, Hard uint64
}
Limit is a limit of a resource for a child, as setrlimit takes it.
type Process ¶
type Process struct {
// Pid is the process ID of the child.
Pid int
// contains filtered or unexported fields
}
Process is a child that Start started.
func Start ¶
Start starts the file at path, with the arguments argv and the attributes attr. path is the file to run, already found by the caller: Start does not search $PATH. argv[0] is the name that the child sees. A nil attr is the same as an empty Attr.
Start returns after the child called execve, or with the error of the step that failed. The error of execve wraps its errno, such as syscall.ENOENT, syscall.EACCES or syscall.ENOEXEC. When Start fails, no child is left behind.
func (*Process) Wait ¶
Wait waits for the child to end and gives its status. The error is not nil only when the wait itself failed. A child that ended with a status other than 0 is not an error. Call Wait or WaitChange until the child ended, or it stays a zombie. Call one of them from one goroutine at a time.
func (*Process) WaitChange ¶
WaitChange waits until the child ends, stops or goes on after a stop, and gives its status, as a shell with job control waits. Status.Stopped and Status.Continued tell the two changes that are not an end. Windows has no stops, and there WaitChange waits for the end.
type Status ¶
type Status struct {
// Code is the exit status of the child, or -1 when a signal ended it.
Code int
// Signal is the signal that ended the child, or nil when the child
// exited. Where the package syscall has the type Signal, Signal holds a
// [syscall.Signal].
Signal os.Signal
// CoreDumped is true when the signal that ended the child also wrote a
// core file.
CoreDumped bool
// Stopped is true when the child did not end but stopped, from
// WaitChange. Signal then holds the signal that stopped it, and Code
// is -1.
Stopped bool
// Continued is true when the child did not end but went on after a
// stop, from WaitChange. Code is then -1.
Continued bool
}
Status is how a child ended.