-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
04296e3
commit 09c75b3
Showing
5 changed files
with
112 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#+TITLE: timeout | ||
#+DATE: 2025-01-23T22:28:53+0800 | ||
#+LASTMOD: 2025-01-23T22:35:32+0800 | ||
#+TYPE: docs | ||
#+DESCRIPTION: Run a command with bounded time | ||
|
||
#+begin_example | ||
timeout SECONDS COMMAND [ARG]... | ||
#+end_example | ||
|
||
Start a command, and kill it if the specified timeout expires. | ||
|
||
The =timeout= command is crucial for: | ||
|
||
- Process Control | ||
- Limits execution time of commands | ||
- Prevents resource-consuming tasks from running indefinitely | ||
- Provides automatic process termination |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
//! Run a command with bounded time | ||
//! https://github.com/coreutils/coreutils/blob/v9.6/src/timeout.c | ||
|
||
const std = @import("std"); | ||
const posix = std.posix; | ||
const Child = std.process.Child; | ||
|
||
pub var child: Child = undefined; | ||
pub var spawn_success = false; | ||
|
||
pub fn main() !void { | ||
try posix.sigaction(posix.SIG.ALRM, &posix.Sigaction{ | ||
.handler = .{ | ||
.handler = struct { | ||
pub fn handler(got: c_int) callconv(.C) void { | ||
std.debug.assert(got == posix.SIG.ALRM); | ||
_ = child.kill() catch |e| { | ||
std.log.err("Kill child failed, err:{any}", .{e}); | ||
return; | ||
}; | ||
posix.exit(124); // timeout | ||
} | ||
}.handler, | ||
}, | ||
.mask = posix.empty_sigset, | ||
.flags = 0, | ||
}, null); | ||
|
||
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; | ||
defer if (gpa.deinit() != .ok) @panic("leak"); | ||
const allocator = gpa.allocator(); | ||
|
||
const args = try std.process.argsAlloc(allocator); | ||
defer std.process.argsFree(allocator, args); | ||
|
||
if (args.len < 3) { | ||
std.debug.print( | ||
\\Usage: | ||
\\ {s} SECONDS COMMAND [ARG]... | ||
\\ | ||
, .{args[0]}); | ||
posix.exit(1); | ||
} | ||
|
||
const ttl_seconds = try std.fmt.parseInt(c_uint, args[1], 10); | ||
const cmds = args[2..]; | ||
const ret = std.c.alarm(ttl_seconds); | ||
if (ret != 0) { | ||
std.log.err("Set alarm signal failed, retcode:{d}", .{ret}); | ||
posix.exit(1); | ||
} | ||
|
||
child = Child.init(cmds, allocator); | ||
try child.spawn(); | ||
spawn_success = true; | ||
const term = try child.wait(); | ||
switch (term) { | ||
.Exited => |status| { | ||
posix.exit(status); | ||
}, | ||
else => { | ||
std.log.err("Child internal error, term:{any}", .{term}); | ||
posix.exit(125); | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters