File size: 8,057 Bytes
e36aeda | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 | // Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build unix
package exec_test
import (
"fmt"
"internal/testenv"
"io"
"os"
"os/exec"
"os/signal"
"os/user"
"path/filepath"
"runtime"
"slices"
"strconv"
"strings"
"sync"
"syscall"
"testing"
"time"
)
func init() {
registerHelperCommand("pwd", cmdPwd)
registerHelperCommand("signaltest", cmdSignalTest)
}
func cmdPwd(...string) {
pwd, err := os.Getwd()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
fmt.Println(pwd)
}
func TestCredentialNoSetGroups(t *testing.T) {
if runtime.GOOS == "android" {
maySkipHelperCommand("echo")
t.Skip("unsupported on Android")
}
t.Parallel()
u, err := user.Current()
if err != nil {
t.Fatalf("error getting current user: %v", err)
}
uid, err := strconv.Atoi(u.Uid)
if err != nil {
t.Fatalf("error converting Uid=%s to integer: %v", u.Uid, err)
}
gid, err := strconv.Atoi(u.Gid)
if err != nil {
t.Fatalf("error converting Gid=%s to integer: %v", u.Gid, err)
}
// If NoSetGroups is true, setgroups isn't called and cmd.Run should succeed
cmd := helperCommand(t, "echo", "foo")
cmd.SysProcAttr = &syscall.SysProcAttr{
Credential: &syscall.Credential{
Uid: uint32(uid),
Gid: uint32(gid),
NoSetGroups: true,
},
}
if err = cmd.Run(); err != nil {
t.Errorf("Failed to run command: %v", err)
}
}
// For issue #19314: make sure that SIGSTOP does not cause the process
// to appear done.
func TestWaitid(t *testing.T) {
t.Parallel()
cmd := helperCommand(t, "pipetest")
stdin, err := cmd.StdinPipe()
if err != nil {
t.Fatal(err)
}
stdout, err := cmd.StdoutPipe()
if err != nil {
t.Fatal(err)
}
if err := cmd.Start(); err != nil {
t.Fatal(err)
}
// Wait for the child process to come up and register any signal handlers.
const msg = "O:ping\n"
if _, err := io.WriteString(stdin, msg); err != nil {
t.Fatal(err)
}
buf := make([]byte, len(msg))
if _, err := io.ReadFull(stdout, buf); err != nil {
t.Fatal(err)
}
// Now leave the pipes open so that the process will hang until we close stdin.
if err := cmd.Process.Signal(syscall.SIGSTOP); err != nil {
cmd.Process.Kill()
t.Fatal(err)
}
ch := make(chan error)
go func() {
ch <- cmd.Wait()
}()
// Give a little time for Wait to block on waiting for the process.
// (This is just to give some time to trigger the bug; it should not be
// necessary for the test to pass.)
if testing.Short() {
time.Sleep(1 * time.Millisecond)
} else {
time.Sleep(10 * time.Millisecond)
}
// This call to Signal should succeed because the process still exists.
// (Prior to the fix for #19314, this would fail with os.ErrProcessDone
// or an equivalent error.)
if err := cmd.Process.Signal(syscall.SIGCONT); err != nil {
t.Error(err)
syscall.Kill(cmd.Process.Pid, syscall.SIGCONT)
}
// The SIGCONT should allow the process to wake up, notice that stdin
// is closed, and exit successfully.
stdin.Close()
err = <-ch
if err != nil {
t.Fatal(err)
}
}
// https://go.dev/issue/50599: if Env is not set explicitly, setting Dir should
// implicitly update PWD to the correct path, and Environ should list the
// updated value.
func TestImplicitPWD(t *testing.T) {
t.Parallel()
cwd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
cases := []struct {
name string
dir string
want string
}{
{"empty", "", cwd},
{"dot", ".", cwd},
{"dotdot", "..", filepath.Dir(cwd)},
{"PWD", cwd, cwd},
{"PWDdotdot", cwd + string(filepath.Separator) + "..", filepath.Dir(cwd)},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
cmd := helperCommand(t, "pwd")
if cmd.Env != nil {
t.Fatalf("test requires helperCommand not to set Env field")
}
cmd.Dir = tc.dir
var pwds []string
for _, kv := range cmd.Environ() {
if strings.HasPrefix(kv, "PWD=") {
pwds = append(pwds, strings.TrimPrefix(kv, "PWD="))
}
}
wantPWDs := []string{tc.want}
if tc.dir == "" {
if _, ok := os.LookupEnv("PWD"); !ok {
wantPWDs = nil
}
}
if !slices.Equal(pwds, wantPWDs) {
t.Errorf("PWD entries in cmd.Environ():\n\t%s\nwant:\n\t%s", strings.Join(pwds, "\n\t"), strings.Join(wantPWDs, "\n\t"))
}
cmd.Stderr = new(strings.Builder)
out, err := cmd.Output()
if err != nil {
t.Fatalf("%v:\n%s", err, cmd.Stderr)
}
got := strings.Trim(string(out), "\r\n")
t.Logf("in\n\t%s\n`pwd` reported\n\t%s", tc.dir, got)
if got != tc.want {
t.Errorf("want\n\t%s", tc.want)
}
})
}
}
// However, if cmd.Env is set explicitly, setting Dir should not override it.
// (This checks that the implementation for https://go.dev/issue/50599 doesn't
// break existing users who may have explicitly mismatched the PWD variable.)
func TestExplicitPWD(t *testing.T) {
t.Parallel()
maySkipHelperCommand("pwd")
testenv.MustHaveSymlink(t)
cwd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
link := filepath.Join(t.TempDir(), "link")
if err := os.Symlink(cwd, link); err != nil {
t.Fatal(err)
}
// Now link is another equally-valid name for cwd. If we set Dir to one and
// PWD to the other, the subprocess should report the PWD version.
cases := []struct {
name string
dir string
pwd string
}{
{name: "original PWD", pwd: cwd},
{name: "link PWD", pwd: link},
{name: "in link with original PWD", dir: link, pwd: cwd},
{name: "in dir with link PWD", dir: cwd, pwd: link},
// Ideally we would also like to test what happens if we set PWD to
// something totally bogus (or the empty string), but then we would have no
// idea what output the subprocess should actually produce: cwd itself may
// contain symlinks preserved from the PWD value in the test's environment.
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
cmd := helperCommand(t, "pwd")
// This is intentionally opposite to the usual order of setting cmd.Dir
// and then calling cmd.Environ. Here, we *want* PWD not to match cmd.Dir,
// so we don't care whether cmd.Dir is reflected in cmd.Environ.
cmd.Env = append(cmd.Environ(), "PWD="+tc.pwd)
cmd.Dir = tc.dir
var pwds []string
for _, kv := range cmd.Environ() {
if strings.HasPrefix(kv, "PWD=") {
pwds = append(pwds, strings.TrimPrefix(kv, "PWD="))
}
}
wantPWDs := []string{tc.pwd}
if !slices.Equal(pwds, wantPWDs) {
t.Errorf("PWD entries in cmd.Environ():\n\t%s\nwant:\n\t%s", strings.Join(pwds, "\n\t"), strings.Join(wantPWDs, "\n\t"))
}
cmd.Stderr = new(strings.Builder)
out, err := cmd.Output()
if err != nil {
t.Fatalf("%v:\n%s", err, cmd.Stderr)
}
got := strings.Trim(string(out), "\r\n")
t.Logf("in\n\t%s\nwith PWD=%s\nsubprocess os.Getwd() reported\n\t%s", tc.dir, tc.pwd, got)
if got != tc.pwd {
t.Errorf("want\n\t%s", tc.pwd)
}
})
}
}
// Issue 71828.
func TestSIGCHLD(t *testing.T) {
cmd := helperCommand(t, "signaltest")
out, err := cmd.CombinedOutput()
t.Logf("%s", out)
if err != nil {
t.Error(err)
}
}
// cmdSignaltest is for TestSIGCHLD.
// This runs in a separate process because the bug only happened
// the first time that a child process was started.
func cmdSignalTest(...string) {
chSig := make(chan os.Signal, 1)
signal.Notify(chSig, syscall.SIGCHLD)
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
c := 0
for range chSig {
c++
fmt.Printf("SIGCHLD %d\n", c)
if c > 1 {
fmt.Println("too many SIGCHLD signals")
os.Exit(1)
}
}
}()
defer func() {
signal.Reset(syscall.SIGCHLD)
close(chSig)
wg.Wait()
}()
exe, err := os.Executable()
if err != nil {
fmt.Printf("os.Executable failed: %v\n", err)
os.Exit(1)
}
cmd := exec.Command(exe, "hang", "200ms")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
fmt.Printf("failed to run child process: %v\n", err)
os.Exit(1)
}
}
|