Skip to content

Commit

Permalink
feat(user): Add support for filtering traffic by multiple PIDs via --…
Browse files Browse the repository at this point in the history
…pid flag (#115)
  • Loading branch information
mozillazg authored Aug 24, 2024
1 parent 01f0890 commit b4685a5
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 7 deletions.
8 changes: 5 additions & 3 deletions cmd/capture.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func capture(ctx context.Context, stop context.CancelFunc, opts Options) error {
if err != nil {
return err
}
opts.pid = uint(subProcessLoaderPid)
opts.pids = []uint{uint(subProcessLoaderPid)}
opts.followForks = true
}

Expand Down Expand Up @@ -167,9 +167,11 @@ func getCurrentConnects(ctx context.Context, pcache *metadata.ProcessCache, opts
var pids []int
var filterPid bool

if opts.pid != 0 {
if len(opts.pids) > 0 {
filterPid = true
pids = append(pids, int(opts.pid))
for _, pid := range opts.pids {
pids = append(pids, int(pid))
}
}
if opts.comm != "" {
filterPid = true
Expand Down
5 changes: 3 additions & 2 deletions cmd/ebpf.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func attachHooks(btfSpec *btftype.Spec, currentConns []metadata.Connection, opts
return nil, err
}
bpfopts := &bpf.Options{}
bpfopts = bpfopts.WithPids([]uint{opts.pid}).
bpfopts = bpfopts.WithPids(opts.pids).
WithComm(opts.comm).
WithFollowFork(opts.followForks).
WithPidNsIds(opts.pidnsIds).
Expand Down Expand Up @@ -67,7 +67,8 @@ func attachHooks(btfSpec *btftype.Spec, currentConns []metadata.Connection, opts
for _, iface := range devices {
if err := bf.AttachTcHooks(iface.Ifindex, opts.DirectionOut(), opts.DirectionIn()); err != nil {
// TODO: use errors.Is(xxx) or ==
if strings.Contains(err.Error(), "netlink receive: no such file or directory") {
if strings.Contains(err.Error(), "netlink receive: no such file or directory") ||
strings.Contains(err.Error(), "netlink receive: no such device") {
log.Warnf("skip interface %s due to %s", iface.Name, err)
continue
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const (

type Options struct {
ifaces []string
pid uint
pids []uint
comm string
followForks bool
writeFilePath string
Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func init() {
"Read packets from file (which was created with the -w option). e.g. ptcpdump.pcapng")
rootCmd.Flags().StringSliceVarP(&opts.ifaces, "interface", "i", []string{"lo"},
"Interfaces to capture")
rootCmd.Flags().UintVar(&opts.pid, "pid", 0, "Filter by process ID (only TCP and UDP packets are supported)")
rootCmd.Flags().UintSliceVar(&opts.pids, "pid", nil, "Filter by process IDs (only TCP and UDP packets are supported)")
rootCmd.Flags().StringVar(&opts.comm, "pname", "", "Filter by process name (only TCP and UDP packets are supported)")
rootCmd.Flags().BoolVarP(&opts.followForks, "follow-forks", "f", false,
"Trace child processes as they are created by currently traced processes when filter by process")
Expand Down

0 comments on commit b4685a5

Please sign in to comment.