1 // Copyright 2021 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 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 16 package server 17 18 import ( 19 "context" 20 "fmt" 21 "strings" 22 23 "github.com/chaos-mesh/chaos-mesh/pkg/bpm" 24 "github.com/chaos-mesh/chaos-mesh/pkg/ctrl/server/model" 25 ) 26 27 // GetFdsOfProcess returns fd-target pairs. 28 // The output looks like: 29 // ``` 30 // total 0 31 // lrwx------ 1 docker docker 64 Mar 3 16:11 0 -> /dev/pts/0 32 // lrwx------ 1 docker docker 64 Mar 3 16:11 1 -> /dev/pts/0 33 // lrwx------ 1 docker docker 64 Mar 3 16:11 2 -> /dev/pts/0 34 // lr-x------ 1 docker docker 64 Mar 3 16:11 3 -> /proc/642108/fd 35 // ``` 36 func (r *Resolver) GetFdsOfProcess(ctx context.Context, process *model.Process) []*model.Fd { 37 cmd := fmt.Sprintf("ls -l /proc/%s/fd", process.Pid) 38 out, err := r.ExecBypass(ctx, process.Pod, cmd, bpm.PidNS, bpm.MountNS) 39 if err != nil { 40 // errors often occur on some short-life process, ignored 41 r.Log.Error(err, "get fds of process", "pid", process.Pid) 42 return nil 43 } 44 var fds []*model.Fd 45 for _, line := range strings.Split(out, "\n") { 46 fields := strings.Fields(line) 47 length := len(fields) 48 if length < 3 { 49 // skip 50 continue 51 } 52 fd := &model.Fd{ 53 Fd: fields[length-3], 54 Target: fields[length-1], 55 } 56 fds = append(fds, fd) 57 } 58 59 return fds 60 } 61