Run zig fmt
This commit is contained in:
parent
60ecd10e67
commit
1b271ea203
4 changed files with 59 additions and 100 deletions
|
@ -21,9 +21,7 @@ pub fn build(b: *std.Build) void {
|
||||||
.target = target,
|
.target = target,
|
||||||
});
|
});
|
||||||
relib.addIncludePath(.{ .path = "lib" });
|
relib.addIncludePath(.{ .path = "lib" });
|
||||||
relib.addCSourceFile(.{
|
relib.addCSourceFile(.{ .file = b.path("lib/regex_slim.c") });
|
||||||
.file = b.path("lib/regex_slim.c")
|
|
||||||
});
|
|
||||||
relib.linkLibC();
|
relib.linkLibC();
|
||||||
|
|
||||||
const exe = b.addExecutable(.{
|
const exe = b.addExecutable(.{
|
||||||
|
@ -65,8 +63,6 @@ pub fn build(b: *std.Build) void {
|
||||||
const run_step = b.step("run", "Run the app");
|
const run_step = b.step("run", "Run the app");
|
||||||
run_step.dependOn(&run_cmd.step);
|
run_step.dependOn(&run_cmd.step);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Creates a step for unit testing. This only builds the test executable
|
// Creates a step for unit testing. This only builds the test executable
|
||||||
// but does not run it.
|
// but does not run it.
|
||||||
const procnet_unit_tests = b.addTest(.{
|
const procnet_unit_tests = b.addTest(.{
|
||||||
|
|
|
@ -6,9 +6,7 @@ const c = @cImport({
|
||||||
@cInclude("signal.h");
|
@cInclude("signal.h");
|
||||||
});
|
});
|
||||||
|
|
||||||
pub const std_options = .{
|
pub const std_options = .{ .log_level = .err };
|
||||||
.log_level = .err
|
|
||||||
};
|
|
||||||
|
|
||||||
pub fn main() !void {
|
pub fn main() !void {
|
||||||
var argsit = std.process.args();
|
var argsit = std.process.args();
|
||||||
|
@ -101,8 +99,7 @@ fn print_proc_net(entry: *const proc.ProcNet, addr_type: []const u8) !void {
|
||||||
switch (entry.*) {
|
switch (entry.*) {
|
||||||
.V4 => |v4| {
|
.V4 => |v4| {
|
||||||
const src_addr_pp = c.inet_ntoa(.{ .s_addr = v4.addr }); // allocates static global buffer, don't free
|
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",
|
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 });
|
||||||
.{v4.port, addr_type, src_addr_pp, v4.inode});
|
|
||||||
},
|
},
|
||||||
.V6 => |v6| {
|
.V6 => |v6| {
|
||||||
try stdio.writer().print("Port {d} clogged on {s} Address ", .{ v6.port, addr_type });
|
try stdio.writer().print("Port {d} clogged on {s} Address ", .{ v6.port, addr_type });
|
||||||
|
|
|
@ -7,18 +7,9 @@ const c = @cImport({
|
||||||
@cInclude("arpa/inet.h");
|
@cInclude("arpa/inet.h");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
pub const ProcNet = union(enum) {
|
pub const ProcNet = union(enum) {
|
||||||
V4: struct {
|
V4: struct { addr: u32, port: u16, inode: u32 },
|
||||||
addr: u32,
|
V6: struct { addr: u128, port: u16, inode: 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 {
|
pub fn format(value: @This(), comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) !void {
|
||||||
switch (value) {
|
switch (value) {
|
||||||
|
@ -38,11 +29,6 @@ pub const ProcNet = union(enum) {
|
||||||
|
|
||||||
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});
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -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);
|
var buf = try std.ArrayList(ProcNet).initCapacity(alloc, proc_net_parsed.len);
|
||||||
defer buf.deinit();
|
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_addr = try std.fmt.parseUnsigned(addr_len_t, line[0], 16);
|
||||||
const src_port = try std.fmt.parseUnsigned(u16, line[1], 16);
|
const src_port = try std.fmt.parseUnsigned(u16, line[1], 16);
|
||||||
const inode = try std.fmt.parseUnsigned(u32, line[2], 10);
|
const inode = try std.fmt.parseUnsigned(u32, line[2], 10);
|
||||||
|
|
||||||
|
try buf.append(switch (addr_len) {
|
||||||
try buf.append(
|
|
||||||
switch(addr_len) {
|
|
||||||
.V4 => ProcNet{ .V4 = .{ .addr = src_addr, .port = src_port, .inode = inode } },
|
.V4 => ProcNet{ .V4 = .{ .addr = src_addr, .port = src_port, .inode = inode } },
|
||||||
.V6 => ProcNet{ .V6 = .{ .addr = src_addr, .port = src_port, .inode = inode } },
|
.V6 => ProcNet{ .V6 = .{ .addr = src_addr, .port = src_port, .inode = inode } },
|
||||||
}
|
});
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return buf.toOwnedSlice();
|
return buf.toOwnedSlice();
|
||||||
|
@ -92,32 +78,21 @@ fn parse_proc_net(alloc: Allocator, path: []const u8) ![][3][]u8 {
|
||||||
defer arena.deinit();
|
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
|
var tokens = std.ArrayList([]const u8).init(arena.allocator()); // buffer for a line in the proc file split by tokenizer
|
||||||
defer tokens.deinit();
|
defer tokens.deinit();
|
||||||
var tokenizer = std.mem.tokenize(u8, line, " \t\n");
|
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], ':');
|
var src_it = std.mem.splitScalar(u8, tokens.items[1], ':');
|
||||||
|
|
||||||
try res.append(
|
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 alloc.dupe(u8, src_it.next().?),
|
|
||||||
try alloc.dupe(u8, src_it.next().?),
|
|
||||||
try alloc.dupe(u8, tokens.items[9])
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return res.toOwnedSlice();
|
return res.toOwnedSlice();
|
||||||
}
|
}
|
||||||
|
|
||||||
// test "basic 2" {
|
|
||||||
// std.testing.log_level = .info;
|
|
||||||
// const alloc = std.testing.allocator;
|
|
||||||
// try read_proc_net(alloc);
|
|
||||||
// }
|
|
||||||
|
|
||||||
test "basic 3" {
|
test "basic 3" {
|
||||||
std.testing.log_level = .info;
|
std.testing.log_level = .info;
|
||||||
const alloc = std.testing.allocator;
|
const alloc = std.testing.allocator;
|
||||||
|
|
|
@ -57,7 +57,7 @@ pub fn find_proc(alloc: Allocator, inode: u32) ![]ProcPid {
|
||||||
if (c.regexec(regex_t, entry.path, 0, null, 0) == 0) {
|
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,
|
error.AccessDenied => continue,
|
||||||
else => return err
|
else => return err,
|
||||||
};
|
};
|
||||||
|
|
||||||
if (stat.kind == .unix_domain_socket) {
|
if (stat.kind == .unix_domain_socket) {
|
||||||
|
@ -70,11 +70,7 @@ pub fn find_proc(alloc: Allocator, inode: u32) ![]ProcPid {
|
||||||
_ = compit.next().?.name; // skip /fd/
|
_ = compit.next().?.name; // skip /fd/
|
||||||
const fd = try std.fmt.parseInt(u32, compit.next().?.name, 10); // parse <fd>
|
const fd = try std.fmt.parseInt(u32, compit.next().?.name, 10); // parse <fd>
|
||||||
|
|
||||||
try buf.append(ProcPid{
|
try buf.append(ProcPid{ .pid = pid, .inode = inode, .fd = fd });
|
||||||
.pid = pid,
|
|
||||||
.inode = inode,
|
|
||||||
.fd = fd
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -104,10 +100,5 @@ pub fn resolve_process(alloc: Allocator, proc_pid: ProcPid) !Process {
|
||||||
try cmdline_buf.append(try arena_alloc.allocator().dupe(u8, cmdline_elem));
|
try cmdline_buf.append(try arena_alloc.allocator().dupe(u8, cmdline_elem));
|
||||||
}
|
}
|
||||||
|
|
||||||
return Process{
|
return Process{ .proc_pid = proc_pid, .comm = comm, .cmdline = try cmdline_buf.toOwnedSlice(), .alloc = arena_alloc };
|
||||||
.proc_pid = proc_pid,
|
|
||||||
.comm = comm,
|
|
||||||
.cmdline = try cmdline_buf.toOwnedSlice(),
|
|
||||||
.alloc = arena_alloc
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue