aboutsummaryrefslogtreecommitdiff
path: root/go/psx-signals.go
blob: 486f2847807850e445657906b2d9ef0b49cc1f0f (plain)
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
// Program psx-signals validates that the psx mechanism can coexist
// with Go use of signals. This is an unprivilaged program derived
// from the sample code provided in this bug report:
//
//   https://bugzilla.kernel.org/show_bug.cgi?id=210533
package main

import (
	"fmt"
	"log"
	"os"
	"os/signal"
	"syscall"
	"time"

	"kernel.org/pub/linux/libs/security/libcap/psx"
)

const maxSig = 10
const prSetKeepCaps = 8

func main() {
	sig := make(chan os.Signal, maxSig)
	signal.Notify(sig, os.Interrupt)

	fmt.Print("Toggling KEEP_CAPS ")
	for i := 0; i < maxSig; i++ {
		fmt.Print(".")
		_, _, err := psx.Syscall3(syscall.SYS_PRCTL, prSetKeepCaps, uintptr(i&1), 0)
		if err != 0 {
			log.Fatalf("[%d] attempt to set KEEPCAPS (to %d) failed: %v", i, i%2, err)
		}
	}

	fmt.Println(" done")
	fmt.Print("Wait 1 second to see if unwanted signals arrive...")
	// Confirm no signals are delivered.
	select {
	case <-time.After(1 * time.Second):
		break
	case info := <-sig:
		log.Fatalf("signal received: %v", info)
	}
	fmt.Println(" none arrived")
	fmt.Println("PASSED")
}