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