diff options
| author | Ophestra <cat@gensokyo.uk> | 2025-02-13 23:15:34 +0900 |
|---|---|---|
| committer | Ophestra <cat@gensokyo.uk> | 2025-02-13 23:34:15 +0900 |
| commit | fe7d208cf76fa6f24bb9d12ba29b5ed61d837ce3 (patch) | |
| tree | cac02af50a13b2078739a8f5a74d219f3b60833d /helper/seccomp/api.go | |
| parent | 60c287375048b21eab2bd82f1e7d43e36dcfb3a2 (diff) | |
helper: use generic extra files interface
This replaces the pipes object and integrates context into helper process lifecycle.
Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'helper/seccomp/api.go')
| -rw-r--r-- | helper/seccomp/api.go | 47 |
1 files changed, 32 insertions, 15 deletions
diff --git a/helper/seccomp/api.go b/helper/seccomp/api.go index 5b4a42c3..2799fc90 100644 --- a/helper/seccomp/api.go +++ b/helper/seccomp/api.go @@ -1,22 +1,15 @@ package seccomp import ( + "context" "errors" - "io" - "os" "syscall" + + "git.gensokyo.uk/security/fortify/helper/proc" ) -func Export(opts SyscallOpts) (f *os.File, err error) { - if f, err = tmpfile(); err != nil { - return - } - if err = exportFilter(f.Fd(), opts); err != nil { - return - } - _, err = f.Seek(0, io.SeekStart) - return -} +// New returns an inactive Encoder instance. +func New(opts SyscallOpts) *Encoder { return &Encoder{newExporter(opts)} } /* An Encoder writes a BPF program to an output stream. @@ -45,7 +38,31 @@ func (e *Encoder) Close() error { return errors.Join(e.closeWrite(), <-e.exportErr) } -// New returns an inactive Encoder instance. -func New(opts SyscallOpts) *Encoder { - return &Encoder{newExporter(opts)} +// NewFile returns an instance of exporter implementing [proc.File]. +func NewFile(opts SyscallOpts) proc.File { return &File{opts: opts} } + +// File implements [proc.File] and provides access to the read end of exporter pipe. +type File struct { + opts SyscallOpts + proc.BaseFile +} + +func (f *File) ErrCount() int { return 2 } +func (f *File) Fulfill(ctx context.Context, dispatchErr func(error)) error { + e := newExporter(f.opts) + if err := e.prepare(); err != nil { + return err + } + f.Set(e.r) + go func() { + select { + case err := <-e.exportErr: + dispatchErr(nil) + dispatchErr(err) + case <-ctx.Done(): + dispatchErr(e.closeWrite()) + dispatchErr(<-e.exportErr) + } + }() + return nil } |
