Skip to content

Commit

Permalink
fix(capture/subprogram): Fix capture by process via run target program (
Browse files Browse the repository at this point in the history
#166)

* fix(subprogram): Fix capture by process via run target program

* fix test
  • Loading branch information
mozillazg authored Oct 19, 2024
1 parent c3a5bca commit 2cb31ff
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 1 deletion.
5 changes: 5 additions & 0 deletions cmd/capture.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func capture(ctx context.Context, stopFunc context.CancelFunc, opts *Options) er

log.Info("start get current connections")
conns := getCurrentConnects(ctx, pcache, opts)
log.Infof("got %d connections", len(conns))

copts := opts.ToCapturerOptions()
copts.Connections = conns
Expand Down Expand Up @@ -155,6 +156,10 @@ func getCurrentConnects(ctx context.Context, pcache *metadata.ProcessCache, opts
var pids []int
var filterPid bool

if len(opts.subProgArgs) > 0 {
return nil
}

if len(opts.pids) > 0 {
filterPid = true
for _, pid := range opts.pids {
Expand Down
1 change: 1 addition & 0 deletions internal/capturer/capturer.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ func (c *Capturer) StartSubProcessLoader(ctx context.Context, program string, su
if err != nil {
return err
}
log.Infof("will filter by pid: %d", c.subProcessLoaderPid)
c.opts.Pids = []uint{uint(c.subProcessLoaderPid)}
c.opts.FollowForks = true

Expand Down
1 change: 1 addition & 0 deletions internal/utils/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func StartSubProcess(ctx context.Context, Args []string) error {
if err := cmd.Start(); err != nil {
return err
}
log.Infof("started sub program, his pid is %d", cmd.Process.Pid)

if err := cmd.Wait(); err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion testdata/test_sub_program.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ RNAME="${FILE_PREFIX}_sub_program.read.txt"


function test_ptcpdump() {
timeout 30s ${CMD} -i any -v --print -w "${FNAME}" \
timeout 30s ${CMD} -c 10 -i any -v --print -w "${FNAME}" \
-- curl -m 10 1.1.1.1 | tee "${LNAME}"

cat "${LNAME}"
Expand Down

0 comments on commit 2cb31ff

Please sign in to comment.