From 34f26e6e0fa490e7881b3ecf929b8175433ecb42 Mon Sep 17 00:00:00 2001 From: sgf Date: Mon, 4 Dec 2023 23:51:48 +0300 Subject: [PATCH] chg(ptrace): Move c files out from go directory and fix build. --- .../guide_to_syscalls/.gitignore | 1 + .../guide_to_syscalls/hello/.gitignore | 1 + .../guide_to_syscalls/hello/go.mod | 3 + .../guide_to_syscalls/hello/hello.go | 10 +++ .../guide_to_syscalls/myStrace/.gitignore | 1 + .../guide_to_syscalls/myStrace/go.mod | 5 ++ .../guide_to_syscalls/myStrace/go.sum | 2 + .../guide_to_syscalls/myStrace/main.go | 77 +++++++++++++++++++ .../{myStrace => }/ptrace-chain.c | 0 9 files changed, 100 insertions(+) create mode 100644 containers_from_scratch/guide_to_syscalls/.gitignore create mode 100644 containers_from_scratch/guide_to_syscalls/hello/.gitignore create mode 100644 containers_from_scratch/guide_to_syscalls/hello/go.mod create mode 100644 containers_from_scratch/guide_to_syscalls/hello/hello.go create mode 100644 containers_from_scratch/guide_to_syscalls/myStrace/.gitignore create mode 100644 containers_from_scratch/guide_to_syscalls/myStrace/go.mod create mode 100644 containers_from_scratch/guide_to_syscalls/myStrace/go.sum create mode 100644 containers_from_scratch/guide_to_syscalls/myStrace/main.go rename containers_from_scratch/guide_to_syscalls/{myStrace => }/ptrace-chain.c (100%) diff --git a/containers_from_scratch/guide_to_syscalls/.gitignore b/containers_from_scratch/guide_to_syscalls/.gitignore new file mode 100644 index 0000000..cba7efc --- /dev/null +++ b/containers_from_scratch/guide_to_syscalls/.gitignore @@ -0,0 +1 @@ +a.out diff --git a/containers_from_scratch/guide_to_syscalls/hello/.gitignore b/containers_from_scratch/guide_to_syscalls/hello/.gitignore new file mode 100644 index 0000000..ce01362 --- /dev/null +++ b/containers_from_scratch/guide_to_syscalls/hello/.gitignore @@ -0,0 +1 @@ +hello diff --git a/containers_from_scratch/guide_to_syscalls/hello/go.mod b/containers_from_scratch/guide_to_syscalls/hello/go.mod new file mode 100644 index 0000000..f0a0072 --- /dev/null +++ b/containers_from_scratch/guide_to_syscalls/hello/go.mod @@ -0,0 +1,3 @@ +module hello + +go 1.21.4 diff --git a/containers_from_scratch/guide_to_syscalls/hello/hello.go b/containers_from_scratch/guide_to_syscalls/hello/hello.go new file mode 100644 index 0000000..15a1421 --- /dev/null +++ b/containers_from_scratch/guide_to_syscalls/hello/hello.go @@ -0,0 +1,10 @@ + +package main + +import ( + "fmt" +) + +func main() { + fmt.Println("Hello world") +} diff --git a/containers_from_scratch/guide_to_syscalls/myStrace/.gitignore b/containers_from_scratch/guide_to_syscalls/myStrace/.gitignore new file mode 100644 index 0000000..83bad49 --- /dev/null +++ b/containers_from_scratch/guide_to_syscalls/myStrace/.gitignore @@ -0,0 +1 @@ +myStrace diff --git a/containers_from_scratch/guide_to_syscalls/myStrace/go.mod b/containers_from_scratch/guide_to_syscalls/myStrace/go.mod new file mode 100644 index 0000000..9a51c5b --- /dev/null +++ b/containers_from_scratch/guide_to_syscalls/myStrace/go.mod @@ -0,0 +1,5 @@ +module myStrace + +go 1.21.4 + +require github.com/seccomp/libseccomp-golang v0.10.0 diff --git a/containers_from_scratch/guide_to_syscalls/myStrace/go.sum b/containers_from_scratch/guide_to_syscalls/myStrace/go.sum new file mode 100644 index 0000000..f5e6023 --- /dev/null +++ b/containers_from_scratch/guide_to_syscalls/myStrace/go.sum @@ -0,0 +1,2 @@ +github.com/seccomp/libseccomp-golang v0.10.0 h1:aA4bp+/Zzi0BnWZ2F1wgNBs5gTpm+na2rWM6M9YjLpY= +github.com/seccomp/libseccomp-golang v0.10.0/go.mod h1:JA8cRccbGaA1s33RQf7Y1+q9gHmZX1yB/z9WDN1C6fg= diff --git a/containers_from_scratch/guide_to_syscalls/myStrace/main.go b/containers_from_scratch/guide_to_syscalls/myStrace/main.go new file mode 100644 index 0000000..f935ce8 --- /dev/null +++ b/containers_from_scratch/guide_to_syscalls/myStrace/main.go @@ -0,0 +1,77 @@ + +package main + +import ( + "fmt" + "os" + "os/exec" + "syscall" + "time" + + sec "github.com/seccomp/libseccomp-golang" +) + +func SsGetName(ss uint64) string { + v := sec.ScmpSyscall(ss) + name, err := v.GetName() + if err != nil { + panic(err) + } + return name +} + +func main() { + fmt.Printf("Run %v\n", os.Args[1:]) + + cmd := exec.Command(os.Args[1], os.Args[2:]...) + cmd.SysProcAttr = &syscall.SysProcAttr { + Ptrace: true, + } + cmd.Stderr = os.Stderr + cmd.Stdin = os.Stdin + cmd.Stdout = os.Stdout + + cmd.Start() + if err := cmd.Wait(); err != nil { + fmt.Printf("Wait returned: %v\n", err) + } + exit := true + + time.Sleep(time.Second * 3) + + pid := cmd.Process.Pid + + var regs syscall.PtraceRegs + var ws syscall.WaitStatus + for { + if exit { + if err := syscall.PtraceGetRegs(pid, ®s); err != nil { + panic(err) + } + //time.Sleep(700 * time.Millisecond) + time.Sleep(time.Second) + fmt.Printf("%#v\n", SsGetName(regs.Orig_rax)) + } + + if err := syscall.PtraceSyscall(pid, 0); err != nil { + panic(err) + } + + if _, err := syscall.Wait4(pid, &ws, 0, nil); err != nil { + panic(err) + } + + if ws.Exited() { + fmt.Printf("Exited: %v\n", ws.ExitStatus()) + break + } else if ws.Signaled() { + fmt.Printf("Killed by signal: %v\n", ws.Signal().String()) + } else if ws.Stopped() { + fmt.Printf("Stopped by signal: %v\n", ws.StopSignal().String()) + } else if ws.Continued() { + fmt.Printf("Continued\n") + } + + exit = !exit + } +} diff --git a/containers_from_scratch/guide_to_syscalls/myStrace/ptrace-chain.c b/containers_from_scratch/guide_to_syscalls/ptrace-chain.c similarity index 100% rename from containers_from_scratch/guide_to_syscalls/myStrace/ptrace-chain.c rename to containers_from_scratch/guide_to_syscalls/ptrace-chain.c -- 2.20.1