...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/bpm/build_linux.go

Documentation: github.com/chaos-mesh/chaos-mesh/pkg/bpm

     1  // Copyright 2020 Chaos Mesh Authors.
     2  //
     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  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package bpm
    15  
    16  import (
    17  	"context"
    18  	"os/exec"
    19  	"strings"
    20  	"syscall"
    21  
    22  	"github.com/chaos-mesh/chaos-mesh/pkg/mock"
    23  )
    24  
    25  // Build builds the process
    26  func (b *ProcessBuilder) Build() *ManagedProcess {
    27  	args := b.args
    28  	cmd := b.cmd
    29  
    30  	if len(b.nsOptions) > 0 {
    31  		args = append([]string{"--", cmd}, args...)
    32  		for _, option := range b.nsOptions {
    33  			args = append([]string{"-" + nsArgMap[option.Typ], option.Path}, args...)
    34  		}
    35  
    36  		if b.localMnt {
    37  			args = append([]string{"-l"}, args...)
    38  		}
    39  		cmd = nsexecPath
    40  	}
    41  
    42  	if b.pause {
    43  		args = append([]string{cmd}, args...)
    44  		cmd = pausePath
    45  	}
    46  
    47  	if c := mock.On("MockProcessBuild"); c != nil {
    48  		f := c.(func(context.Context, string, ...string) *exec.Cmd)
    49  		return &ManagedProcess{
    50  			Cmd:        f(b.ctx, cmd, args...),
    51  			Identifier: b.identifier,
    52  		}
    53  	}
    54  
    55  	log.Info("build command", "command", cmd+" "+strings.Join(args, " "))
    56  
    57  	command := exec.CommandContext(b.ctx, cmd, args...)
    58  	command.SysProcAttr = &syscall.SysProcAttr{}
    59  	command.SysProcAttr.Pdeathsig = syscall.SIGTERM
    60  
    61  	return &ManagedProcess{
    62  		Cmd:        command,
    63  		Identifier: b.identifier,
    64  	}
    65  }
    66