tetragon
1// SPDX-License-Identifier: Apache-2.0
2// Copyright Authors of Tetragon
3
4package testutils5
6import (7"context"8"fmt"9"os/exec"10"strings"11"testing"12)
13
14type LseekPipeCmd struct {15Cmd *exec.Cmd16Pipes *CmdBufferedPipes17}
18
19// starts a new lseek-pipe command
20//
21//revive:disable:context-as-argument
22func NewLseekPipe(t *testing.T, ctx context.Context) *LseekPipeCmd {23bin := RepoRootPath("contrib/tester-progs/lseek-pipe")24cmd := exec.CommandContext(ctx, bin)25pipes, err := NewCmdBufferedPipes(cmd)26if err != nil {27t.Fatal(err)28}29if err := cmd.Start(); err != nil {30pipes.Close()31t.Fatal(err)32}33
34return &LseekPipeCmd{35Cmd: cmd,36Pipes: pipes,37}38}
39
40//revive:enable:context-as-argument
41
42func (lp *LseekPipeCmd) Pid() int {43return lp.Cmd.Process.Pid44}
45
46func (lp *LseekPipeCmd) Lseek(fd int, offset int64, whence int) string {47lp.Pipes.P.Stdin.Write([]byte(fmt.Sprintf("%d %d %d\n", fd, offset, whence)))48// NB: read the result from the lseek-pipe program. Doing so means that49// whenever we return, we know that the lseek syscall has been50// executed.51line, _ := lp.Pipes.StdoutRd.ReadString('\n')52return strings.TrimSuffix(line, "\n")53}
54
55func (lp *LseekPipeCmd) Close() error {56lp.Pipes.Close()57return lp.Cmd.Wait()58}
59