|
| 1 | +// Copyright 2009 The Go Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +//go:build linux && !baremetal && !tinygo.wasm |
| 6 | + |
| 7 | +package os |
| 8 | + |
| 9 | +import ( |
| 10 | + "errors" |
| 11 | + "runtime" |
| 12 | + "syscall" |
| 13 | +) |
| 14 | + |
| 15 | +// The only signal values guaranteed to be present in the os package on all |
| 16 | +// systems are os.Interrupt (send the process an interrupt) and os.Kill (force |
| 17 | +// the process to exit). On Windows, sending os.Interrupt to a process with |
| 18 | +// os.Process.Signal is not implemented; it will return an error instead of |
| 19 | +// sending a signal. |
| 20 | +var ( |
| 21 | + Interrupt Signal = syscall.SIGINT |
| 22 | + Kill Signal = syscall.SIGKILL |
| 23 | +) |
| 24 | + |
| 25 | +// Keep compatible with golang and always succeed and return new proc with pid on Linux. |
| 26 | +func findProcess(pid int) (*Process, error) { |
| 27 | + return &Process{Pid: pid}, nil |
| 28 | +} |
| 29 | + |
| 30 | +func (p *Process) release() error { |
| 31 | + // NOOP for unix. |
| 32 | + p.Pid = -1 |
| 33 | + // no need for a finalizer anymore |
| 34 | + runtime.SetFinalizer(p, nil) |
| 35 | + return nil |
| 36 | +} |
| 37 | + |
| 38 | +// This function is a wrapper around the forkExec function, which is a wrapper around the fork and execve system calls. |
| 39 | +// The StartProcess function creates a new process by forking the current process and then calling execve to replace the current process with the new process. |
| 40 | +// It thereby replaces the newly created process with the specified command and arguments. |
| 41 | +// Differences to upstream golang implementation (https://cs.opensource.google/go/go/+/master:src/syscall/exec_unix.go;l=143): |
| 42 | +// * No setting of Process Attributes |
| 43 | +// * Ignoring Ctty |
| 44 | +// * No ForkLocking (might be introduced by #4273) |
| 45 | +// * No parent-child communication via pipes (TODO) |
| 46 | +// * No waiting for crashes child processes to prohibit zombie process accumulation / Wait status checking (TODO) |
| 47 | +func forkExec(argv0 string, argv []string, attr *ProcAttr) (pid int, err error) { |
| 48 | + if argv == nil { |
| 49 | + return 0, errors.New("exec: no argv") |
| 50 | + } |
| 51 | + |
| 52 | + if len(argv) == 0 { |
| 53 | + return 0, errors.New("exec: no argv") |
| 54 | + } |
| 55 | + |
| 56 | + if attr == nil { |
| 57 | + attr = new(ProcAttr) |
| 58 | + } |
| 59 | + |
| 60 | + p, err := fork() |
| 61 | + pid = int(p) |
| 62 | + |
| 63 | + if err != nil { |
| 64 | + return 0, err |
| 65 | + } |
| 66 | + |
| 67 | + // else code runs in child, which then should exec the new process |
| 68 | + err = execve(argv0, argv, attr.Env) |
| 69 | + if err != nil { |
| 70 | + // exec failed |
| 71 | + return 0, err |
| 72 | + } |
| 73 | + // 3. TODO: use pipes to communicate back child status |
| 74 | + return pid, nil |
| 75 | +} |
| 76 | + |
| 77 | +// In Golang, the idiomatic way to create a new process is to use the StartProcess function. |
| 78 | +// Since the Model of operating system processes in tinygo differs from the one in Golang, we need to implement the StartProcess function differently. |
| 79 | +// The startProcess function is a wrapper around the forkExec function, which is a wrapper around the fork and execve system calls. |
| 80 | +// The StartProcess function creates a new process by forking the current process and then calling execve to replace the current process with the new process. |
| 81 | +// It thereby replaces the newly created process with the specified command and arguments. |
| 82 | +func startProcess(name string, argv []string, attr *ProcAttr) (p *Process, err error) { |
| 83 | + if attr != nil { |
| 84 | + if attr.Dir != "" { |
| 85 | + return nil, ErrNotImplementedDir |
| 86 | + } |
| 87 | + |
| 88 | + if attr.Sys != nil { |
| 89 | + return nil, ErrNotImplementedSys |
| 90 | + } |
| 91 | + |
| 92 | + if len(attr.Files) != 0 { |
| 93 | + return nil, ErrNotImplementedFiles |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + pid, err := forkExec(name, argv, attr) |
| 98 | + if err != nil { |
| 99 | + return nil, err |
| 100 | + } |
| 101 | + |
| 102 | + return findProcess(pid) |
| 103 | +} |
0 commit comments