From f86c35a51ff92718e36ff6075339300be11e09b3 Mon Sep 17 00:00:00 2001 From: Michael Pratt Date: Mon, 22 Apr 2019 18:17:25 -0700 Subject: Clean up state error handling PiperOrigin-RevId: 244773836 Change-Id: I32223f79d2314fe1ac4ddfc63004fc22ff634adf --- pkg/sentry/control/state.go | 2 +- pkg/sentry/kernel/kernel.go | 23 ++++++++++++----------- pkg/state/state.go | 20 +++++++++++++++----- 3 files changed, 28 insertions(+), 17 deletions(-) (limited to 'pkg') diff --git a/pkg/sentry/control/state.go b/pkg/sentry/control/state.go index 0a480c84a..b6bbf69fa 100644 --- a/pkg/sentry/control/state.go +++ b/pkg/sentry/control/state.go @@ -64,7 +64,7 @@ func (s *State) Save(o *SaveOpts, _ *struct{}) error { log.Infof("Save succeeded: exiting...") } else { log.Warningf("Save failed: exiting...") - s.Kernel.SetExitError(err) + s.Kernel.SetSaveError(err) } s.Kernel.Kill(kernel.ExitStatus{}) }, diff --git a/pkg/sentry/kernel/kernel.go b/pkg/sentry/kernel/kernel.go index 290c4a53c..ee6334509 100644 --- a/pkg/sentry/kernel/kernel.go +++ b/pkg/sentry/kernel/kernel.go @@ -175,9 +175,9 @@ type Kernel struct { // netlinkPorts manages allocation of netlink socket port IDs. netlinkPorts *port.Manager - // exitErr is the error causing the sandbox to exit, if any. It is - // protected by extMu. - exitErr error `state:"nosave"` + // saveErr is the error causing the sandbox to exit during save, if + // any. It is protected by extMu. + saveErr error `state:"nosave"` // danglingEndpoints is used to save / restore tcpip.DanglingEndpoints. danglingEndpoints struct{} `state:".([]tcpip.Endpoint)"` @@ -1029,20 +1029,21 @@ func (k *Kernel) NetlinkPorts() *port.Manager { return k.netlinkPorts } -// ExitError returns the sandbox error that caused the kernel to exit. -func (k *Kernel) ExitError() error { +// SaveError returns the sandbox error that caused the kernel to exit during +// save. +func (k *Kernel) SaveError() error { k.extMu.Lock() defer k.extMu.Unlock() - return k.exitErr + return k.saveErr } -// SetExitError sets the sandbox error that caused the kernel to exit, if one is -// not already set. -func (k *Kernel) SetExitError(err error) { +// SetSaveError sets the sandbox error that caused the kernel to exit during +// save, if one is not already set. +func (k *Kernel) SetSaveError(err error) { k.extMu.Lock() defer k.extMu.Unlock() - if k.exitErr == nil { - k.exitErr = err + if k.saveErr == nil { + k.saveErr = err } } diff --git a/pkg/state/state.go b/pkg/state/state.go index 4b141777e..4486f83a7 100644 --- a/pkg/state/state.go +++ b/pkg/state/state.go @@ -60,8 +60,8 @@ import ( // ErrState is returned when an error is encountered during encode/decode. type ErrState struct { - // Err is the underlying error. - Err error + // err is the underlying error. + err error // path is the visit path from root to the current object. path string @@ -72,7 +72,17 @@ type ErrState struct { // Error returns a sensible description of the state error. func (e *ErrState) Error() string { - return fmt.Sprintf("%v:\nstate path: %s\n%s", e.Err, e.path, e.trace) + return fmt.Sprintf("%v:\nstate path: %s\n%s", e.err, e.path, e.trace) +} + +// UnwrapErrState returns the underlying error in ErrState. +// +// If err is not *ErrState, err is returned directly. +func UnwrapErrState(err error) error { + if e, ok := err.(*ErrState); ok { + return e.err + } + return err } // Save saves the given object state. @@ -318,9 +328,9 @@ func (sr *recoverable) safely(fn func()) (err error) { if r := recover(); r != nil { es := new(ErrState) if e, ok := r.(error); ok { - es.Err = e + es.err = e } else { - es.Err = fmt.Errorf("%v", r) + es.err = fmt.Errorf("%v", r) } es.path = sr.path() -- cgit v1.2.3