Run zig fmt

This commit is contained in:
Armin Friedl 2024-07-01 22:32:29 +02:00
parent 60ecd10e67
commit 1b271ea203
4 changed files with 59 additions and 100 deletions

View file

@ -21,9 +21,7 @@ pub fn build(b: *std.Build) void {
.target = target,
});
relib.addIncludePath(.{ .path = "lib" });
relib.addCSourceFile(.{
.file = b.path("lib/regex_slim.c")
});
relib.addCSourceFile(.{ .file = b.path("lib/regex_slim.c") });
relib.linkLibC();
const exe = b.addExecutable(.{
@ -65,8 +63,6 @@ pub fn build(b: *std.Build) void {
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
// Creates a step for unit testing. This only builds the test executable
// but does not run it.
const procnet_unit_tests = b.addTest(.{

View file

@ -6,9 +6,7 @@ const c = @cImport({
@cInclude("signal.h");
});
pub const std_options = .{
.log_level = .err
};
pub const std_options = .{ .log_level = .err };
pub fn main() !void {
var argsit = std.process.args();
@ -27,8 +25,8 @@ pub fn main() !void {
const tcp = try proc.read_proc_net(alloc, .V4, "/proc/net/tcp");
defer alloc.free(tcp);
for(tcp) |pn| {
if(pn.V4.port == port) {
for (tcp) |pn| {
if (pn.V4.port == port) {
clogged = true;
try print_proc_net(&pn, "TCP/IPv4");
try append_processes(alloc, pn.V4.inode, &procs);
@ -37,8 +35,8 @@ pub fn main() !void {
const tcp6 = try proc.read_proc_net(alloc, .V6, "/proc/net/tcp6");
defer alloc.free(tcp6);
for(tcp6) |pn| {
if(pn.V6.port == port) {
for (tcp6) |pn| {
if (pn.V6.port == port) {
clogged = true;
try print_proc_net(&pn, "TCP/IPv6");
try append_processes(alloc, pn.V6.inode, &procs);
@ -47,8 +45,8 @@ pub fn main() !void {
const udp = try proc.read_proc_net(alloc, .V4, "/proc/net/udp");
defer alloc.free(udp);
for(udp) |pn| {
if(pn.V4.port == port) {
for (udp) |pn| {
if (pn.V4.port == port) {
clogged = true;
try print_proc_net(&pn, "UDP/IPv4");
try append_processes(alloc, pn.V4.inode, &procs);
@ -57,22 +55,22 @@ pub fn main() !void {
const udp6 = try proc.read_proc_net(alloc, .V6, "/proc/net/udp6");
defer alloc.free(udp6);
for(udp6) |pn| {
if(pn.V6.port == port) {
for (udp6) |pn| {
if (pn.V6.port == port) {
clogged = true;
try print_proc_net(&pn, "UDP/IPv6");
try append_processes(alloc, pn.V6.inode, &procs);
}
}
if(!clogged){
if (!clogged) {
try std.io.getStdOut().writer().print("Port {d} looks unclogged already\n", .{port});
} else {
_ = try std.io.getStdOut().writer().write("Kill? ");
var buf: [10]u8 = undefined;
if(try std.io.getStdIn().reader().readUntilDelimiterOrEof(buf[0..], '\n')) |input| {
if (try std.io.getStdIn().reader().readUntilDelimiterOrEof(buf[0..], '\n')) |input| {
const killproc = try std.fmt.parseInt(usize, input, 10);
const process = procs.items[killproc-1];
const process = procs.items[killproc - 1];
_ = c.kill(@intCast(process.proc_pid.pid), c.SIGTERM);
}
}
@ -81,16 +79,16 @@ pub fn main() !void {
fn append_processes(alloc: std.mem.Allocator, inode: u32, buf: *std.ArrayList(pid.Process)) !void {
const pids = try pid.find_proc(alloc, inode);
defer alloc.free(pids);
try std.io.getStdOut().writer().print("\t{s: <5}{s: <10}{s: <20}{s}\n", .{"#", "PID", "CMD", "ARGS"});
try std.io.getStdOut().writer().print("\t{s: <5}{s: <10}{s: <20}{s}\n", .{ "#", "PID", "CMD", "ARGS" });
for(pids) |p| {
for (pids) |p| {
const process = try pid.resolve_process(alloc, p);
defer process.deinit();
try buf.append(process);
const cmdline = try std.mem.join(alloc, " ", process.cmdline[1..]);
defer alloc.free(cmdline);
try std.io.getStdOut().writer().print("\t{d: <5}{d: <10}{s: <20}{s}\n", .{buf.items.len, process.proc_pid.pid, process.comm[0..process.comm.len-1], cmdline});
try std.io.getStdOut().writer().print("\t{d: <5}{d: <10}{s: <20}{s}\n", .{ buf.items.len, process.proc_pid.pid, process.comm[0 .. process.comm.len - 1], cmdline });
}
_ = try std.io.getStdOut().writer().write("\n");
}
@ -98,16 +96,15 @@ fn append_processes(alloc: std.mem.Allocator, inode: u32, buf: *std.ArrayList(pi
fn print_proc_net(entry: *const proc.ProcNet, addr_type: []const u8) !void {
var stdio = std.io.getStdOut();
switch(entry.*) {
switch (entry.*) {
.V4 => |v4| {
const src_addr_pp = c.inet_ntoa(.{.s_addr = v4.addr}); // allocates static global buffer, don't free
try stdio.writer().print("Port {d} clogged on {s} Address {s} with socket inode {d}\n",
.{v4.port, addr_type, src_addr_pp, v4.inode});
const src_addr_pp = c.inet_ntoa(.{ .s_addr = v4.addr }); // allocates static global buffer, don't free
try stdio.writer().print("Port {d} clogged on {s} Address {s} with socket inode {d}\n", .{ v4.port, addr_type, src_addr_pp, v4.inode });
},
.V6 => |v6| {
try stdio.writer().print("Port {d} clogged on {s} Address ", .{v6.port, addr_type});
for(@as(*const [16]u8, @ptrCast(&v6.addr)), 0..) |h, idx| {
if(idx % 2 == 0 and idx != 0) {
try stdio.writer().print("Port {d} clogged on {s} Address ", .{ v6.port, addr_type });
for (@as(*const [16]u8, @ptrCast(&v6.addr)), 0..) |h, idx| {
if (idx % 2 == 0 and idx != 0) {
_ = try stdio.writer().write(":");
}

View file

@ -7,47 +7,33 @@ const c = @cImport({
@cInclude("arpa/inet.h");
});
pub const ProcNet = union(enum) {
V4: struct {
addr: u32,
port: u16,
inode: u32
},
V6: struct {
addr: u128,
port: u16,
inode: u32
},
V4: struct { addr: u32, port: u16, inode: u32 },
V6: struct { addr: u128, port: u16, inode: u32 },
pub fn format(value: @This(), comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) !void {
switch(value) {
switch (value) {
.V4 => |v4| {
const src_addr_pp = c.inet_ntoa(.{.s_addr = v4.addr}); // allocates static global buffer, don't free
try writer.print("{s: <15}{d: <10}{d}", .{src_addr_pp, v4.port, v4.inode});
const src_addr_pp = c.inet_ntoa(.{ .s_addr = v4.addr }); // allocates static global buffer, don't free
try writer.print("{s: <15}{d: <10}{d}", .{ src_addr_pp, v4.port, v4.inode });
},
.V6 => |v6| {
for(@as(*const [16]u8, @ptrCast(&v6.addr)), 0..) |h, idx| {
if(idx % 2 == 0 and idx != 0) {
for (@as(*const [16]u8, @ptrCast(&v6.addr)), 0..) |h, idx| {
if (idx % 2 == 0 and idx != 0) {
_ = try writer.write(":");
}
try writer.print("{X:0<2}", .{h});
}
try writer.print("\t{d: <10}{d}", .{v6.port, v6.inode});
try writer.print("\t{d: <10}{d}", .{ v6.port, v6.inode });
},
.RAW => |raw| {
try writer.print("{d} {d}", .{raw.port, raw.inode});
}
}
}
};
pub fn read_proc_net(alloc: Allocator, comptime addr_len: enum{V4, V6}, path: []const u8) ![]ProcNet {
pub fn read_proc_net(alloc: Allocator, comptime addr_len: enum { V4, V6 }, path: []const u8) ![]ProcNet {
var arena = std.heap.ArenaAllocator.init(alloc);
defer arena.deinit();
const proc_net_parsed: [][3][]u8 = try parse_proc_net(arena.allocator(), path);
@ -55,20 +41,20 @@ pub fn read_proc_net(alloc: Allocator, comptime addr_len: enum{V4, V6}, path: []
var buf = try std.ArrayList(ProcNet).initCapacity(alloc, proc_net_parsed.len);
defer buf.deinit();
const addr_len_t = switch(addr_len){.V4 => u32, .V6 => u128};
const addr_len_t = switch (addr_len) {
.V4 => u32,
.V6 => u128,
};
for(proc_net_parsed) |line| {
for (proc_net_parsed) |line| {
const src_addr = try std.fmt.parseUnsigned(addr_len_t, line[0], 16);
const src_port = try std.fmt.parseUnsigned(u16, line[1], 16);
const inode = try std.fmt.parseUnsigned(u32, line[2], 10);
try buf.append(
switch(addr_len) {
.V4 => ProcNet{.V4 = .{.addr = src_addr, .port = src_port, .inode = inode}},
.V6 => ProcNet{.V6 = .{.addr = src_addr, .port = src_port, .inode = inode}},
}
);
try buf.append(switch (addr_len) {
.V4 => ProcNet{ .V4 = .{ .addr = src_addr, .port = src_port, .inode = inode } },
.V6 => ProcNet{ .V6 = .{ .addr = src_addr, .port = src_port, .inode = inode } },
});
}
return buf.toOwnedSlice();
@ -91,33 +77,22 @@ fn parse_proc_net(alloc: Allocator, path: []const u8) ![][3][]u8 {
var arena = std.heap.ArenaAllocator.init(alloc);
defer arena.deinit();
while(try reader.readUntilDelimiterOrEofAlloc(arena.allocator(), '\n', 256)) |line| {
while (try reader.readUntilDelimiterOrEofAlloc(arena.allocator(), '\n', 256)) |line| {
var tokens = std.ArrayList([]const u8).init(arena.allocator()); // buffer for a line in the proc file split by tokenizer
defer tokens.deinit();
var tokenizer = std.mem.tokenize(u8, line, " \t\n");
while(tokenizer.next()) |elem| { try tokens.append(elem); }
while (tokenizer.next()) |elem| {
try tokens.append(elem);
}
var src_it = std.mem.splitScalar(u8, tokens.items[1], ':');
try res.append(
.{
try alloc.dupe(u8, src_it.next().?),
try alloc.dupe(u8, src_it.next().?),
try alloc.dupe(u8, tokens.items[9])
}
);
try res.append(.{ try alloc.dupe(u8, src_it.next().?), try alloc.dupe(u8, src_it.next().?), try alloc.dupe(u8, tokens.items[9]) });
}
return res.toOwnedSlice();
}
// test "basic 2" {
// std.testing.log_level = .info;
// const alloc = std.testing.allocator;
// try read_proc_net(alloc);
// }
test "basic 3" {
std.testing.log_level = .info;
const alloc = std.testing.allocator;
@ -126,8 +101,8 @@ test "basic 3" {
defer arena.deinit();
const res = try parse_proc_net(arena.allocator(), "/proc/net/tcp");
for(res) |line| {
for(line) |field| {
for (res) |line| {
for (line) |field| {
std.debug.print("{s}\t", .{field});
}
std.debug.print("\n", .{});
@ -142,7 +117,7 @@ test "basic 4" {
defer arena.deinit();
const res = try read_proc_net(arena.allocator(), .V4, "/proc/net/tcp");
for(res) |pn| {
for (res) |pn| {
std.debug.print("{any}\n", .{pn});
}
}
@ -155,7 +130,7 @@ test "basic 5" {
defer arena.deinit();
const res = try read_proc_net(arena.allocator(), .V6, "/proc/net/tcp6");
for(res) |pn| {
for (res) |pn| {
std.debug.print("{any}\n", .{pn});
}
}
@ -168,7 +143,7 @@ test "basic 6" {
defer arena.deinit();
const res = try read_proc_net(arena.allocator(), .V4, "/proc/net/udp");
for(res) |pn| {
for (res) |pn| {
std.debug.print("{any}\n", .{pn});
}
}
@ -181,7 +156,7 @@ test "basic 7" {
defer arena.deinit();
const res = try read_proc_net(arena.allocator(), .V6, "/proc/net/udp6");
for(res) |pn| {
for (res) |pn| {
std.debug.print("{any}\n", .{pn});
}
}

View file

@ -55,13 +55,13 @@ pub fn find_proc(alloc: Allocator, inode: u32) ![]ProcPid {
};
if (c.regexec(regex_t, entry.path, 0, null, 0) == 0) {
const stat = proc_dir.statFile(entry.path) catch |err| switch(err) {
const stat = proc_dir.statFile(entry.path) catch |err| switch (err) {
error.AccessDenied => continue,
else => return err
else => return err,
};
if(stat.kind == .unix_domain_socket) {
if(stat.inode == inode) {
if (stat.kind == .unix_domain_socket) {
if (stat.inode == inode) {
log.info("Found procpid path {s}", .{entry.path});
// <pid>/fd/<fd>
@ -70,11 +70,7 @@ pub fn find_proc(alloc: Allocator, inode: u32) ![]ProcPid {
_ = compit.next().?.name; // skip /fd/
const fd = try std.fmt.parseInt(u32, compit.next().?.name, 10); // parse <fd>
try buf.append(ProcPid{
.pid = pid,
.inode = inode,
.fd = fd
});
try buf.append(ProcPid{ .pid = pid, .inode = inode, .fd = fd });
}
}
}
@ -84,7 +80,7 @@ pub fn find_proc(alloc: Allocator, inode: u32) ![]ProcPid {
}
pub fn resolve_process(alloc: Allocator, proc_pid: ProcPid) !Process {
var fmt_buf = [_]u8{0}**20;
var fmt_buf = [_]u8{0} ** 20;
const path = try std.fmt.bufPrint(&fmt_buf, "/proc/{d}", .{proc_pid.pid});
const proc_dir = try std.fs.openDirAbsolute(path, .{});
@ -100,14 +96,9 @@ pub fn resolve_process(alloc: Allocator, proc_pid: ProcPid) !Process {
var cmdline_buf = std.ArrayList([]u8).init(arena_alloc.allocator());
defer cmdline_buf.deinit();
while(cmdline_it.next()) |cmdline_elem| {
while (cmdline_it.next()) |cmdline_elem| {
try cmdline_buf.append(try arena_alloc.allocator().dupe(u8, cmdline_elem));
}
return Process{
.proc_pid = proc_pid,
.comm = comm,
.cmdline = try cmdline_buf.toOwnedSlice(),
.alloc = arena_alloc
};
return Process{ .proc_pid = proc_pid, .comm = comm, .cmdline = try cmdline_buf.toOwnedSlice(), .alloc = arena_alloc };
}