usr.bin: add flock and ts utilities - #506
Conversation
Add flock(1) for portable advisory locking and ts(1) for timestamping input streams. Include manual pages and ATF coverage, and register both programs in the userland build. The flock implementation is based on NetBSD's BSD-licensed utility. The ts implementation is based on the BSD-licensed OpenBSD source imported by FreeBSD. AI-Assisted-by: OpenAI GPT-5 Signed-off-by: Lucas Holt <luke@foolishgames.com>
Reviewer's GuideIntroduces and registers flock(1) and ts(1), including their implementations, manual pages, build metadata, and ATF tests; flock wraps flock(2) across command and descriptor workflows with configurable locking and timeouts, while ts formats line-oriented input using wall-clock or monotonic elapsed timestamps. The existing shuf(1) return statement is also normalized to style(9). Sequence diagram for flock command lockingsequenceDiagram
participant User
participant flock
participant Kernel as flock(2)
participant Command
User->>flock: invoke with file and command
flock->>flock: open(path)
opt timeout configured
flock->>flock: start_timer(timeout)
end
flock->>Kernel: flock(fd, lock)
alt lock acquired
flock->>Command: execvp(command, args)
Command-->>flock: exit status
else lock unavailable
Kernel-->>flock: failure or EINTR
flock-->>User: failure status
end
Sequence diagram for flock descriptor workflowsequenceDiagram
participant ShellScript
participant flock
participant Kernel as flock(2)
ShellScript->>flock: invoke with descriptor
flock->>Kernel: flock(fd, lock)
alt lock acquired
flock-->>ShellScript: success
else nonblocking or timeout failure
Kernel-->>flock: failure
flock-->>ShellScript: failure status
end
Flow diagram for ts timestamp processingflowchart TD
A["Read line from stdin"] --> B["clock_gettime(clock)"]
B --> C{"Timestamp mode"}
C -->|absolute| D["Use current realtime timestamp"]
C -->|elapsed| E["timespecsub(now, start, ts)"]
C -->|incremental| F["timespecsub(now, previous, ts)"]
C -->|monotonic absolute| G["timespecadd(now, utc_offset, ts)"]
D --> H["fmtfmt(ts)"]
E --> H
F --> H
G --> H
H --> I["Write timestamp and input line"]
I --> A
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
|
Claude finished @laffer1's task in 2m 9s —— View job Code Review:
|
There was a problem hiding this comment.
🟡 Changes recommended
usr.bin/flock/flock.1 is added but not listed in usr.bin/flock/Makefile’s MAN, so it won’t be installed with the utility.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
Adds two new userland utilities to the MidnightBSD base system: flock(1) (file/descriptor locking wrapper) and ts(1) (timestamp prefixing filter), including ATF-based regression tests and build integration, plus a small shuf(1) style(9) return normalization.
Changes:
- Add
usr.bin/flockutility, its manual page, and ATF shell tests; register it inusr.bin/Makefile. - Add
usr.bin/tsbuild glue for the importedcontrib/tsimplementation, with ATF shell tests; register it inusr.bin/Makefile. - Normalize
shuf(1)return style toreturn (0);.
File summaries
| File | Description |
|---|---|
| usr.bin/ts/tests/ts_test.sh | Adds ATF coverage for core ts(1) behaviors and option validation. |
| usr.bin/ts/tests/Makefile | Registers ts ATF shell tests for the test build. |
| usr.bin/ts/Makefile | Builds ts(1) from contrib/ts and installs its man page; wires in tests. |
| usr.bin/shuf/shuf.c | Style(9) return normalization in main(). |
| usr.bin/Makefile | Adds flock and ts to the usr.bin build. |
| usr.bin/flock/tests/Makefile | Registers flock ATF shell tests for the test build. |
| usr.bin/flock/tests/flock_test.sh | Adds ATF coverage for command execution, nonblocking behavior, and timeout validation. |
| usr.bin/flock/Makefile | Adds build glue for flock(1) and wires in tests. |
| usr.bin/flock/flock.c | New flock(1) implementation (NetBSD-derived) with timeout/nonblocking/descriptor modes. |
| usr.bin/flock/flock.1 | New flock(1) manual page. |
| contrib/ts/ts.c | Imported ts(1) implementation (OpenBSD/FreeBSD provenance) with timestamp formatting. |
| contrib/ts/ts.1 | Imported ts(1) manual page. |
Review details
- Files reviewed: 12/12 changed files
- Comments generated: 1
- Review effort level: Lite
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Signed-off-by: Lucas Holt <luke@foolishgames.com>
|
Claude finished @laffer1's task in 1m 10s —— View job Claude's task
StatusPR #506 is already merged (squash commit Looking at the merge history, this squash included two commits:
That second commit already fixes the one real issue flagged during review — No other findings from the earlier automated review (malloc/free symmetry, bounds checks, style(9), timeout/signal handling in I don't see any further actionable request in this comment thread — if there's a specific follow-up you'd like reviewed or changed, let me know and I'll pick it up. |
There was a problem hiding this comment.
🔵 Needs a closer look
The new ts(1) ATF test for elapsed timestamps is overly strict and can be flaky under load, risking intermittent CI failures.
Review details
Suppressed comments (1)
Previously missed (1) — in code that hasn't changed since the last review.
usr.bin/ts/tests/ts_test.sh:48
- The elapsed test is too strict: it assumes the seconds field will be "00", but on slower/loaded systems
ts -s %.Scan legitimately print01.xxxxxx(or higher), making this test flaky. Broaden the regex to accept any valid%Sseconds value (00–59).
- Files reviewed: 12/12 changed files
- Comments generated: 0 new
- Review effort level: Lite

Summary
flock(1)with command, shell-command, descriptor, shared/exclusive, nonblocking, and timeout modests(1)with absolute, elapsed, incremental, and fractional timestampsshuf(1)implementation for style(9)base64(1)andshuf(1)were already present after updating this branch from current master.Provenance
flock(1)is based on the BSD-2-Clause NetBSD implementation, with MidnightBSD portability, validation, and test changests(1)is based on the ISC-licensed OpenBSD implementation imported by FreeBSD, with additional error handling, bounds checks, and testsTesting
flock,ts,base64, andshufusing host-built binaries-Wall -Wextra -Werrorpassedtools/build/checkstyle9.pl --strictpassedcppcheckpassed for the changed C sourcessh -npassed for the test scriptsmandoc -Tlintpassed apart from host manual-database lookup warningsgit diff --checkpassedA native MidnightBSD cross-build could not be completed because this host lacks a configured target compiler/sysroot. The repository precommit wrappers for
clang-formatand Splint could not run because those executables are not installed.AI contribution checklist
AI-Assisted-by: OpenAI GPT-5
Obtained from: NetBSD and OpenBSD/FreeBSD
Summary by Sourcery
Add flock(1) and ts(1) userland utilities with documentation, tests, and build integration.
New Features:
Enhancements:
Build:
Documentation:
Tests: