talos/pkg/cmd/cmd.go
Andrey Smirnov 3012851208 fix(machined): limit max stderr output, use pkg/cmd consistently
Use circular buffer instead of (unlimited) `bytes.Buffer` to limit
amount of stderr output captured. If command being run produces too much
output on stderr, this might consume too much RAM.

Use `pkg/cmd` to run command in `udevd` service. This should allow
easier udevd integration.

Signed-off-by: Andrey Smirnov <smirnov.andrey@gmail.com>
2019-09-02 19:01:15 -07:00

37 lines
817 B
Go

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package cmd
import (
"os/exec"
"github.com/armon/circbuf"
"github.com/pkg/errors"
)
// MaxStderrLen is maximum length of stderr output captured for error message
const MaxStderrLen = 4096
// Run executes a command.
func Run(name string, args ...string) error {
cmd := exec.Command(name, args...)
stderr, err := circbuf.NewBuffer(MaxStderrLen)
if err != nil {
return err
}
cmd.Stderr = stderr
if err = cmd.Start(); err != nil {
return errors.Errorf("%s: %s", err, stderr.String())
}
if err = cmd.Wait(); err != nil {
return errors.Errorf("%s: %s", err, stderr.String())
}
return nil
}