Skip to content

Commit

Permalink
feat: add DetachCtx
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanhitt committed Oct 3, 2024
1 parent f8158ba commit d08ce70
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions frida/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package frida
//#include <frida-core.h>
import "C"
import (
"context"
"runtime"
"unsafe"
)
Expand All @@ -18,10 +19,39 @@ func (s *Session) IsDetached() bool {
return int(detached) == 1
}

// DetachCtx runs Detach but with context.
// This function will properly handle cancelling the frida operation.
// It is advised to use this rather than handling Cancellable yourself.
func (s *Session) DetachCtx(ctx context.Context) error {
errC := make(chan error, 1)

c := NewCancellable()
go func() {
errC <- s.Detach(WithCancel(c))
}()

for {
select {
case <-ctx.Done():
c.Cancel()
return ErrContextCancelled
case err := <-errC:
c.Unref()
return err
}
}
}

// Detach detaches the current session.
func (s *Session) Detach() error {
func (s *Session) Detach(opts ...OptFunc) error {
o := setupOptions(opts)
return s.detach(o)
}

// detach
func (s *Session) detach(opts options) error {
var err *C.GError
C.frida_session_detach_sync(s.s, nil, &err)
C.frida_session_detach_sync(s.s, opts.cancellable, &err)
return handleGError(err)
}

Expand Down

0 comments on commit d08ce70

Please sign in to comment.