From 66e80f38d9a287e622101ac733fbee053b9c9b2e Mon Sep 17 00:00:00 2001 From: txthinking Date: Fri, 27 Sep 2024 07:58:01 +0800 Subject: [PATCH] optimize boot network detection --- src/helper.zig | 6 +++++- src/main.zig | 12 +++++++----- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/helper.zig b/src/helper.zig index 679e3e6..1cfdb26 100644 --- a/src/helper.zig +++ b/src/helper.zig @@ -10,7 +10,6 @@ const datetime = @import("datetime"); var mutex = std.Thread.Mutex{}; pub fn syslog(err: anyerror) void { - std.debug.print("{}\n", .{err}); std.c.syslog(c.LOG_ERR, "zhen: %s", @as([*c]const u8, @errorName(err))); } @@ -83,6 +82,11 @@ pub fn testNetwork(dns: []const u8) !void { const addr = try std.net.Address.parseIp6(dns, 53); const s = try std.posix.socket(std.posix.AF.INET6, std.posix.SOCK.DGRAM, std.posix.IPPROTO.UDP); defer std.posix.close(s); + const recv_timeout = std.posix.timeval{ + .tv_sec = 3, + .tv_usec = 0, + }; + try std.posix.setsockopt(s, std.posix.SOL.SOCKET, std.posix.SO.RCVTIMEO, &std.mem.toBytes(recv_timeout)); try std.posix.connect(s, &addr.any, addr.getOsSockLen()); const in = .{ 0x67, 0x88, 0x1, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xa, 0x74, 0x78, 0x74, 0x68, 0x69, 0x6e, 0x6b, 0x69, 0x6e, 0x67, 0x3, 0x63, 0x6f, 0x6d, 0x0, 0x0, 0x1c, 0x0, 0x1 }; _ = try std.posix.send(s, &in, 0); diff --git a/src/main.zig b/src/main.zig index 1730e40..5e84b1a 100644 --- a/src/main.zig +++ b/src/main.zig @@ -5,6 +5,7 @@ const client = @import("./client.zig"); pub fn main() void { _main() catch |err| { + std.debug.print("{}\n", .{err}); helper.syslog(err); if (err == error.ConnectionRefused) { std.debug.print("z might not be running?\n", .{}); @@ -30,6 +31,7 @@ pub fn _main() !void { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; defer { if (gpa.deinit() != .ok) { + std.debug.print("{}\n", .{error.MemoryLeak}); helper.syslog(error.MemoryLeak); } } @@ -58,7 +60,7 @@ pub fn _main() !void { \\ \\ stop stop z daemon \\ - \\v20240727 https://github.com/txthinking/z + \\v20240927 https://github.com/txthinking/z \\ \\ ; @@ -149,10 +151,10 @@ pub fn _main() !void { std.c.exit(-1); } _ = std.c.umask(0); - const n = helper.getdtablesize(); - for (0..@intCast(n)) |i| { - _ = std.c.close(@intCast(i)); - } + // const n = std.c.sysconf(std.c.OPEN_MAX); + // for (0..@intCast(n)) |i| { + // _ = std.c.close(@intCast(i)); + // } var s = try Server.init(allocator); defer s.deinit(); s.start() catch |err| {