|
| 1 | +/* |
| 2 | + Copyright The containerd Authors. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package highk |
| 18 | + |
| 19 | +import ( |
| 20 | + "io" |
| 21 | + "os" |
| 22 | + "os/exec" |
| 23 | + "slices" |
| 24 | + "strconv" |
| 25 | + "strings" |
| 26 | + "syscall" |
| 27 | +) |
| 28 | + |
| 29 | +// FIXME: it seems that lsof (or go test) is interefering and showing false positive KQUEUE / inodes |
| 30 | +// |
| 31 | +//nolint:gochecknoglobals |
| 32 | +var whitelist = map[string]bool{ |
| 33 | + "5u KQUEUE": true, |
| 34 | + "10u a_inode": true, |
| 35 | +} |
| 36 | + |
| 37 | +// SnapshotOpenFiles will capture the list of currently open-files for the process. |
| 38 | +// |
| 39 | +//nolint:wrapcheck |
| 40 | +func SnapshotOpenFiles(file *os.File) ([]byte, error) { |
| 41 | + // Using a buffer would add a pipe to the list of files |
| 42 | + // Reimplement this stuff in go ASAP and toss lsof instead of passing around fd |
| 43 | + // defer fd.Close() |
| 44 | + _, _ = file.Seek(0, 0) |
| 45 | + _ = file.Truncate(0) |
| 46 | + |
| 47 | + exe, err := exec.LookPath("lsof") |
| 48 | + if err != nil { |
| 49 | + return nil, err |
| 50 | + } |
| 51 | + |
| 52 | + //nolint:gosec |
| 53 | + cmd := exec.Command(exe, "-nP", "-p", strconv.Itoa(syscall.Getpid())) |
| 54 | + cmd.Stdout = file |
| 55 | + |
| 56 | + _ = cmd.Run() |
| 57 | + |
| 58 | + _, _ = file.Seek(0, 0) |
| 59 | + |
| 60 | + return io.ReadAll(file) |
| 61 | +} |
| 62 | + |
| 63 | +// Diff will return a slice of strings showing the diff between two strings. |
| 64 | +func Diff(one, two string) []string { |
| 65 | + aone := strings.Split(one, "\n") |
| 66 | + atwo := strings.Split(two, "\n") |
| 67 | + |
| 68 | + slices.Sort(aone) |
| 69 | + slices.Sort(atwo) |
| 70 | + |
| 71 | + loss := make(map[string]bool, len(aone)) |
| 72 | + gain := map[string]bool{} |
| 73 | + |
| 74 | + for _, v := range aone { |
| 75 | + loss[v] = true |
| 76 | + } |
| 77 | + |
| 78 | + for _, v := range atwo { |
| 79 | + if _, ok := loss[v]; ok { |
| 80 | + delete(loss, v) |
| 81 | + } else { |
| 82 | + gain[v] = true |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + diff := []string{} |
| 87 | + |
| 88 | + for key := range loss { |
| 89 | + legit := true |
| 90 | + |
| 91 | + for wl := range whitelist { |
| 92 | + if strings.Contains(key, wl) { |
| 93 | + legit = false |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + if legit { |
| 98 | + diff = append(diff, "- "+key) |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + for key := range gain { |
| 103 | + legit := true |
| 104 | + |
| 105 | + for wl := range whitelist { |
| 106 | + if strings.Contains(key, wl) { |
| 107 | + legit = false |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + if legit { |
| 112 | + diff = append(diff, "+ "+key) |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + return diff |
| 117 | +} |
0 commit comments