Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use RunWithEnv instead of os.Setenv to avoid environment variable races #507

Merged
merged 1 commit into from
Jul 24, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions metrics/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bufio"
"encoding/json"
"fmt"
"os"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -123,10 +122,8 @@ var pluginMetaHeadlineReg = regexp.MustCompile(`^#\s*mackerel-agent-plugin\b(.*)
// }
func (g *pluginGenerator) loadPluginMeta() error {
// Set environment variable to make the plugin command generate its configuration
os.Setenv(pluginConfigurationEnvName, "1")
defer os.Setenv(pluginConfigurationEnvName, "")

stdout, stderr, exitCode, err := g.Config.Command.Run()
pluginMetaEnv := pluginConfigurationEnvName + "=1"
stdout, stderr, exitCode, err := g.Config.Command.RunWithEnv([]string{pluginMetaEnv})
if err != nil {
return fmt.Errorf("running %s failed: %s, exit=%d stderr=%q", g.Config.Command.CommandString(), err, exitCode, stderr)
}
Expand Down Expand Up @@ -218,8 +215,8 @@ func makeCreateGraphDefsPayload(meta *pluginMeta) []mackerel.CreateGraphDefsPayl
}

func (g *pluginGenerator) collectValues() (Values, error) {
os.Setenv(pluginConfigurationEnvName, "")
stdout, stderr, _, err := g.Config.Command.Run()
pluginMetaEnv := pluginConfigurationEnvName + "="
stdout, stderr, _, err := g.Config.Command.RunWithEnv([]string{pluginMetaEnv})

if stderr != "" {
pluginLogger.Infof("command %s outputted to STDERR: %q", g.Config.Command.CommandString(), stderr)
Expand Down