command_linux.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. Copyright The containerd Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package runc
  14. import (
  15. "context"
  16. "os"
  17. "os/exec"
  18. "strings"
  19. "syscall"
  20. )
  21. func (r *Runc) command(context context.Context, args ...string) *exec.Cmd {
  22. command := r.Command
  23. if command == "" {
  24. command = DefaultCommand
  25. }
  26. cmd := exec.CommandContext(context, command, append(r.args(), args...)...)
  27. cmd.SysProcAttr = &syscall.SysProcAttr{
  28. Setpgid: r.Setpgid,
  29. }
  30. cmd.Env = filterEnv(os.Environ(), "NOTIFY_SOCKET") // NOTIFY_SOCKET introduces a special behavior in runc but should only be set if invoked from systemd
  31. if r.PdeathSignal != 0 {
  32. cmd.SysProcAttr.Pdeathsig = r.PdeathSignal
  33. }
  34. return cmd
  35. }
  36. func filterEnv(in []string, names ...string) []string {
  37. out := make([]string, 0, len(in))
  38. loop0:
  39. for _, v := range in {
  40. for _, k := range names {
  41. if strings.HasPrefix(v, k+"=") {
  42. continue loop0
  43. }
  44. }
  45. out = append(out, v)
  46. }
  47. return out
  48. }