Skip to content

Commit

Permalink
optimize boot network detection
Browse files Browse the repository at this point in the history
  • Loading branch information
txthinking committed Sep 26, 2024
1 parent 4270da2 commit 66e80f3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
6 changes: 5 additions & 1 deletion src/helper.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}

Expand Down Expand Up @@ -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);
Expand Down
12 changes: 7 additions & 5 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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", .{});
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -58,7 +60,7 @@ pub fn _main() !void {
\\
\\ stop stop z daemon
\\
\\v20240727 https://github.com/txthinking/z
\\v20240927 https://github.com/txthinking/z
\\
\\
;
Expand Down Expand Up @@ -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| {
Expand Down

0 comments on commit 66e80f3

Please sign in to comment.