Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Commit

Permalink
Fixes #1614 - Protect cleanup code of plugin unload
Browse files Browse the repository at this point in the history
  • Loading branch information
rashmigottipati committed Apr 27, 2017
1 parent 43a3504 commit 9f64383
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
2 changes: 1 addition & 1 deletion control/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func New(cfg *Config) *pluginControl {
}).Debug("metric catalog created")

// Plugin Manager
c.pluginManager = newPluginManager(OptSetPprof(cfg.Pprof))
c.pluginManager = newPluginManager(OptSetPprof(cfg.Pprof), OptSetTempDirPath(cfg.TempDirPath))
controlLogger.WithFields(log.Fields{
"_block": "new",
}).Debug("plugin manager created")
Expand Down
38 changes: 28 additions & 10 deletions control/plugin_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ type pluginManager struct {
pluginConfig *pluginConfig
pluginTags map[string]map[string]string
pprof bool
tempDirPath string
}

func newPluginManager(opts ...pluginManagerOpt) *pluginManager {
Expand All @@ -260,6 +261,13 @@ func newPluginManager(opts ...pluginManagerOpt) *pluginManager {

type pluginManagerOpt func(*pluginManager)

// OptSetPprof sets the pprof flag on the plugin manager
func OptSetTempDirPath(path string) pluginManagerOpt {
return func(p *pluginManager) {
p.tempDirPath = path
}
}

// OptSetPprof sets the pprof flag on the plugin manager
func OptSetPprof(pprof bool) pluginManagerOpt {
return func(p *pluginManager) {
Expand Down Expand Up @@ -582,22 +590,32 @@ func (p *pluginManager) UnloadPlugin(pl core.Plugin) (*loadedPlugin, serror.Snap
"plugin-version": plugin.Version(),
"plugin-path": plugin.Details.Path,
}).Debugf("Removing plugin")
if err := os.RemoveAll(filepath.Dir(plugin.Details.Path)); err != nil {
if strings.Contains(plugin.Details.Path, p.tempDirPath) {
if err := os.RemoveAll(filepath.Dir(plugin.Details.Path)); err != nil {
pmLogger.WithFields(log.Fields{
"plugin-type": plugin.TypeName(),
"plugin-name": plugin.Name(),
"plugin-version": plugin.Version(),
"plugin-path": plugin.Details.Path,
}).Error(err)
se := serror.New(err)
se.SetFields(map[string]interface{}{
"plugin-type": plugin.TypeName(),
"plugin-name": plugin.Name(),
"plugin-version": plugin.Version(),
"plugin-path": plugin.Details.Path,
})
return nil, se
}
} else {
pmLogger.WithFields(log.Fields{
"plugin-type": plugin.TypeName(),
"plugin-name": plugin.Name(),
"plugin-version": plugin.Version(),
"plugin-path": plugin.Details.Path,
}).Error(err)
se := serror.New(err)
se.SetFields(map[string]interface{}{
"plugin-type": plugin.TypeName(),
"plugin-name": plugin.Name(),
"plugin-version": plugin.Version(),
"plugin-path": plugin.Details.Path,
})
return nil, se
}).Debug("Nothing to delete as temp path is empty")
}

p.loadedPlugins.remove(plugin.Key())

// Remove any metrics from the catalog if this was a collector
Expand Down

0 comments on commit 9f64383

Please sign in to comment.