|  | 
|  | 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 | +	_, _ = file.Seek(0, 0) | 
|  | 44 | +	_ = file.Truncate(0) | 
|  | 45 | + | 
|  | 46 | +	exe, err := exec.LookPath("lsof") | 
|  | 47 | +	if err != nil { | 
|  | 48 | +		return nil, err | 
|  | 49 | +	} | 
|  | 50 | + | 
|  | 51 | +	//nolint:gosec | 
|  | 52 | +	cmd := exec.Command(exe, "-nP", "-p", strconv.Itoa(syscall.Getpid())) | 
|  | 53 | +	cmd.Stdout = file | 
|  | 54 | + | 
|  | 55 | +	_ = cmd.Run() | 
|  | 56 | + | 
|  | 57 | +	_, _ = file.Seek(0, 0) | 
|  | 58 | + | 
|  | 59 | +	return io.ReadAll(file) | 
|  | 60 | +} | 
|  | 61 | + | 
|  | 62 | +// Diff will return a slice of strings showing the diff between two strings. | 
|  | 63 | +func Diff(one, two string) []string { | 
|  | 64 | +	aone := strings.Split(one, "\n") | 
|  | 65 | +	atwo := strings.Split(two, "\n") | 
|  | 66 | + | 
|  | 67 | +	slices.Sort(aone) | 
|  | 68 | +	slices.Sort(atwo) | 
|  | 69 | + | 
|  | 70 | +	loss := make(map[string]bool, len(aone)) | 
|  | 71 | +	gain := map[string]bool{} | 
|  | 72 | + | 
|  | 73 | +	for _, v := range aone { | 
|  | 74 | +		loss[v] = true | 
|  | 75 | +	} | 
|  | 76 | + | 
|  | 77 | +	for _, v := range atwo { | 
|  | 78 | +		if _, ok := loss[v]; ok { | 
|  | 79 | +			delete(loss, v) | 
|  | 80 | +		} else { | 
|  | 81 | +			gain[v] = true | 
|  | 82 | +		} | 
|  | 83 | +	} | 
|  | 84 | + | 
|  | 85 | +	diff := []string{} | 
|  | 86 | + | 
|  | 87 | +	for key := range loss { | 
|  | 88 | +		legit := true | 
|  | 89 | + | 
|  | 90 | +		for wl := range whitelist { | 
|  | 91 | +			if strings.Contains(key, wl) { | 
|  | 92 | +				legit = false | 
|  | 93 | +			} | 
|  | 94 | +		} | 
|  | 95 | + | 
|  | 96 | +		if legit { | 
|  | 97 | +			diff = append(diff, "- "+key) | 
|  | 98 | +		} | 
|  | 99 | +	} | 
|  | 100 | + | 
|  | 101 | +	for key := range gain { | 
|  | 102 | +		legit := true | 
|  | 103 | + | 
|  | 104 | +		for wl := range whitelist { | 
|  | 105 | +			if strings.Contains(key, wl) { | 
|  | 106 | +				legit = false | 
|  | 107 | +			} | 
|  | 108 | +		} | 
|  | 109 | + | 
|  | 110 | +		if legit { | 
|  | 111 | +			diff = append(diff, "+ "+key) | 
|  | 112 | +		} | 
|  | 113 | +	} | 
|  | 114 | + | 
|  | 115 | +	return diff | 
|  | 116 | +} | 
0 commit comments