diff --git a/crit/cli/cli.go b/crit/cli/cli.go index 12b692748..0fbc380d0 100644 --- a/crit/cli/cli.go +++ b/crit/cli/cli.go @@ -220,9 +220,9 @@ var infoCmd = &cobra.Command{ // The `crit x` command var xCmd = &cobra.Command{ - Use: "x DIR {ps|fds|mems|rss}", + Use: "x DIR {ps|fd|mem|rss}", Short: "Explore the image directory", - Long: "Explore the image directory with one of (ps, fds, mems, rss) options", + Long: "Explore the image directory with one of (ps, fd, mem, rss) options", // Exactly two arguments are required: // * Path of the input directory // * Explore type @@ -242,14 +242,14 @@ var xCmd = &cobra.Command{ switch args[1] { case "ps": xData, err = c.ExplorePs() - case "fds": + case "fd": xData, err = c.ExploreFds() - case "mems": + case "mem": xData, err = c.ExploreMems() case "rss": xData, err = c.ExploreRss() default: - err = errors.New("error exploring directory: invalid explore type") + err = errors.New("invalid explore type (supported: {ps|fd|mem|rss})") } if err != nil { log.Fatal(fmt.Errorf("error exploring directory: %w", err)) diff --git a/crit/explore.go b/crit/explore.go index 3d2b98799..cd234e585 100644 --- a/crit/explore.go +++ b/crit/explore.go @@ -71,7 +71,7 @@ func (c *crit) ExplorePs() (*PsTree, error) { // Fd represents the file descriptors opened in a single process type Fd struct { PId uint32 `json:"pId"` - Files []*File `json:"files,omitempty"` + Files []*File `json:"files"` } // File represents a single opened file @@ -145,6 +145,11 @@ func (c *crit) ExploreFds() ([]*Fd, error) { Path: filePath, }) + // Omit if the process has no file descriptors + if len(fdEntry.Files) == 0 { + continue + } + fds = append(fds, &fdEntry) } diff --git a/crit/utils.go b/crit/utils.go index f76c3a7df..5fa817a43 100644 --- a/crit/utils.go +++ b/crit/utils.go @@ -44,13 +44,13 @@ func ReadMagic(f *os.File) (string, error) { // Helper to convert bytes into a more readable unit func countBytes(n int64) string { - const UNIT int64 = 1024 - if n < UNIT { + const unit int64 = 1024 + if n < unit { return fmt.Sprint(n, " B") } - div, exp := UNIT, 0 - for i := n / UNIT; i >= UNIT; i /= UNIT { - div *= UNIT + div, exp := unit, 0 + for i := n / unit; i >= unit; i /= unit { + div *= unit exp++ } return fmt.Sprintf("%.1f %cB", float64(n)/float64(div), "KMGTPE"[exp]) @@ -152,7 +152,7 @@ func getRegFilePath(dir string, file *fdinfo.FileEntry, fID uint32) (string, err if file.GetReg() != nil { return file.GetReg().GetName(), nil } - return "Unknown path", nil + return "unknown", nil } if regImg == nil { @@ -168,7 +168,7 @@ func getRegFilePath(dir string, file *fdinfo.FileEntry, fID uint32) (string, err } } - return "Unknown path", nil + return "unknown", nil } // Helper to get file path of pipe files diff --git a/test/crit/crit-test.sh b/test/crit/crit-test.sh index e89102827..7fddde690 100755 --- a/test/crit/crit-test.sh +++ b/test/crit/crit-test.sh @@ -73,8 +73,8 @@ function command_test { # explore image directory $CRIT x "$TEST_IMG_DIR" ps || exit 1 - $CRIT x "$TEST_IMG_DIR" fds || exit 1 - $CRIT x "$TEST_IMG_DIR" mems || exit 1 + $CRIT x "$TEST_IMG_DIR" fd || exit 1 + $CRIT x "$TEST_IMG_DIR" mem || exit 1 $CRIT x "$TEST_IMG_DIR" rss || exit 1 } diff --git a/test/loop/loop.c b/test/loop/loop.c index 736e7f8cb..733caad4c 100644 --- a/test/loop/loop.c +++ b/test/loop/loop.c @@ -35,7 +35,7 @@ int main(void) exit(1); } - // Create a file descriptor for "crit x ./ fds" test + // Create a file descriptor for "crit x ./ fd" test open("/dev/null", O_RDONLY); chdir("/");