Skip to content

Add ProgramHandle for lifetime-independent program fd ownership #1391

Merged
danielocfb merged 1 commit into
libbpf:masterfrom
cong-or:add-program-handle
May 13, 2026
Merged

Add ProgramHandle for lifetime-independent program fd ownership #1391
danielocfb merged 1 commit into
libbpf:masterfrom
cong-or:add-program-handle

Conversation

@cong-or
Copy link
Copy Markdown
Contributor

@cong-or cong-or commented May 11, 2026

Summary

I needed a way to hold onto a program fd after the Object goes out of scope. MapHandle already solves this for maps — ProgramHandle does the same thing for programs.

The implementation queries bpf_prog_info via bpf_obj_get_info_by_fd and caches name, type, tag, and id. Constructors cover the three ways you'd typically get a program: by kernel id, from
a pinned bpffs path, or by converting from an existing Program reference. Pin/unpin and AsFd round it out.

Kept it scoped — no ProgramCore trait, no attach methods (those need the libbpf bpf_program pointer), no ProgramInfo renames. Those can come separately if wanted.

Closes #920

@cong-or cong-or changed the title Add ProgramHandle: owned, lifetime-independent handle for loaded BPF … Add ProgramHandle for lifetime-independent program fd ownership May 11, 2026
@cong-or cong-or force-pushed the add-program-handle branch from 1e34825 to 2555822 Compare May 11, 2026 14:28
…programs

  Mirrors MapHandle pattern — owns fd via OwnedFd, caches name/type/tag/id.
  Constructors: from_prog_id, from_pinned_path, TryFrom<&Program>.
  Implements AsFd, pin/unpin, TryFrom<&Self> for cloning.

  Closes libbpf#920
Copy link
Copy Markdown
Collaborator

@danielocfb danielocfb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems fine to me, thank you!

@danielocfb danielocfb merged commit 7cbce8c into libbpf:master May 13, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Introduce ProgramHandle abstraction

2 participants