github.com/golang/gofrontend@v0.0.0-20240429183944-60f985a78526/libgo/misc/cgo/test/setgid_linux.go (about)

     1  // Copyright 2012 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  // Test that setgid does not hang on Linux.
     6  // See https://golang.org/issue/3871 for details.
     7  
     8  package cgotest
     9  
    10  /*
    11  #include <sys/types.h>
    12  #include <unistd.h>
    13  */
    14  import "C"
    15  
    16  import (
    17  	"os"
    18  	"os/signal"
    19  	"syscall"
    20  	"testing"
    21  	"time"
    22  )
    23  
    24  func runTestSetgid() bool {
    25  	c := make(chan bool)
    26  	go func() {
    27  		C.setgid(0)
    28  		c <- true
    29  	}()
    30  	select {
    31  	case <-c:
    32  		return true
    33  	case <-time.After(5 * time.Second):
    34  		return false
    35  	}
    36  
    37  }
    38  
    39  func testSetgid(t *testing.T) {
    40  	if !runTestSetgid() {
    41  		t.Error("setgid hung")
    42  	}
    43  
    44  	// Now try it again after using signal.Notify.
    45  	signal.Notify(make(chan os.Signal, 1), syscall.SIGINT)
    46  	if !runTestSetgid() {
    47  		t.Error("setgid hung after signal.Notify")
    48  	}
    49  }