Sockstat UI Improvements
Student: Damin Rido (rido@freebsd.org)
- Mentor: Alan Somers (asomers@)
Project description
This project aims to enhance the sockstat command-line utility in FreeBSD by improving its output formatting, ensuring dynamically sized columns for better readability, and integrating libxo for structured output support. The modifications will enable users to extract and process network socket information in various formats (JSON, XML, etc.), making sockstat more versatile and user-friendly.
Approach to solving the problem
The project was divided into two parts: implementing dynamic column sizing and adding structured output via libxo.
- First, I implemented dynamic column sizing to replace the old fixed-width layout. This made sockstat's output more readable, especially with long addresses of unix sockets. I removed the original -w flag, then reintroduced it with new behavior to support optional auto-sizing. Buffer handling and other edge cases were carefully considered.
- Next, I integrated libxo to support structured output formats like JSON and XML. I replaced printf() calls with xo_emit() and used proper field tagging. I tested the output for warnings using --libxo:JPW and --libxo:XPW options to ensure format correctness.
Throughout the project, I followed FreeBSD’s style(9) coding guidelines and referred to tools like wc, ps and netstat for reference. Each feature was submitted in a separate pull request for clarity and review
Deliverables
- Implement dynamic column sizing
- Convert to use libxo
- Documentation and Submission
Milestones
- June 2: Start of coding
- June 12: Add automatic column sizing and remove -w option
- June 27: Pull request successfully merged and closed
- June 30 - July 4: Mid-term Evaluations
- July 15 - Convert to use libxo
- July 30: Pull request successfully merged and closed
- July 28 - August 4: Final Evaluations
The Code
Add automatic column sizing and remove -w option - https://github.com/freebsd/freebsd-src/pull/1720
Reintroduce -w flag to automatically size the columns - https://github.com/freebsd/freebsd-src/pull/1746
Add libxo support - https://github.com/freebsd/freebsd-src/pull/1770
Notes
- Implemented dynamic column width calculation using a two-pass algorithm.
- Introduced col_widths struct to manage column dimensions centrally.
- Used snprintf(NULL, 0, ...) for dynamic string length computation.
- Replaced strcpy() with strlcpy() for safe string handling.
- Standardized unknown values to "??" for consistency.
- Fixed protocol field misalignment by accurately calculating full string length.
- Resolved cross-architecture build issues using correct format specifiers (e.g., PRIx64).
- Reintroduced -w flag for wide output using dynamic sizing.
- Default output mode uses fixed-width columns for terminal compatibility.
- Added truncation for LOCAL and FOREIGN ADDRESS fields in compact mode.
- Applied dynamic sizing selectively based on -w flag.
- Integrated libxo for JSON, XML, and HTML structured output formats.
- Used xo_get_style() to detect output format and conditionally format.
- Removed placeholders like "??" or "(not connected)" from structured output.
- Followed FreeBSD’s style(9) coding standards and secure coding practices.
- Updated man page with detailed examples and libxo references.
Useful links
sockstat manual - https://man.freebsd.org/cgi/man.cgi?sockstat
libxo documentation - https://juniper.github.io/libxo/libxo-manual.html