Skip to content

Commit

Permalink
Misc fixups
Browse files Browse the repository at this point in the history
  • Loading branch information
InKryption committed Feb 5, 2025
1 parent 85136f9 commit 706f136
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/rpc/server/basic.zig
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub fn acceptAndServeConnection(server_ctx: *server.Context) !void {
};
defer conn.stream.close();

if (!connection.have_accept4) {
if (!connection.HAVE_ACCEPT4) {
// make sure the accepted socket is in blocking mode
try connection.setSocketSync(conn.stream.handle, .blocking);
}
Expand Down
17 changes: 9 additions & 8 deletions src/rpc/server/connection.zig
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
const builtin = @import("builtin");
const std = @import("std");

/// When this is false, it means `accept[Handled]` can't apply
/// flags to the accepted socket, and the caller will have to
/// to ensure relevant flags are enabled/disabled after acceptance.
pub const HAVE_ACCEPT4 = !builtin.target.isDarwin();

pub const GetSockNameError = std.posix.GetSockNameError;

pub fn getSockName(
socket_handle: std.posix.socket_t,
) GetSockNameError!std.net.Address {
Expand All @@ -11,11 +17,6 @@ pub fn getSockName(
return addr;
}

/// When this is false, it means `accept[Handled]` can't apply
/// flags to the accepted socket, and the caller will have to
/// to ensure relevant flags are enabled/disabled after acceptance.
pub const have_accept4 = !builtin.target.isDarwin();

pub const AcceptHandledError = HandleAcceptError || error{
ConnectionAborted,
ProtocolFailure,
Expand All @@ -37,7 +38,7 @@ pub fn acceptHandled(
while (true) {
var addr: std.net.Address = .{ .any = undefined };
var addr_len: std.posix.socklen_t = @sizeOf(@TypeOf(addr.any));
const rc = if (have_accept4)
const rc = if (HAVE_ACCEPT4)
std.posix.system.accept4(
tcp_server.stream.handle,
&addr.any,
Expand All @@ -64,13 +65,13 @@ pub fn acceptHandled(
}
}

pub const SetSocketSync = std.posix.FcntlError;
pub const SetSocketSyncError = std.posix.FcntlError;

/// Ensure the socket is set to be blocking or nonblocking.
pub fn setSocketSync(
socket: std.posix.socket_t,
sync: enum { blocking, nonblocking },
) !void {
) SetSocketSyncError!void {
const FlagsInt = @typeInfo(std.posix.O).Struct.backing_integer.?;
var flags_int: FlagsInt = @intCast(try std.posix.fcntl(socket, std.posix.F.GETFL, 0));
const flags = std.mem.bytesAsValue(std.posix.O, std.mem.asBytes(&flags_int));
Expand Down
26 changes: 13 additions & 13 deletions src/rpc/server/linux_io_uring.zig
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,6 @@ const ConsumeOurCqeError =
connection.HandleSendError ||
connection.HandleSpliceError;

/// Panic message for handling `EAGAIN`; we're not using nonblocking sockets at all,
/// so it should be impossible to receive that error, or for such an error to be
/// triggered just from malicious connections.
const EAGAIN_PANIC_MSG =
"The file/socket should not be in nonblocking mode;" ++
" server or file/socket configuration error.";

/// On return, `cqe.user_data` is in an undefined state - this is to say,
/// it has either already been `deinit`ed, or it has been been re-submitted
/// in a new `SQE` and should not be modified; in either scenario, the caller
Expand Down Expand Up @@ -160,12 +153,19 @@ fn consumeOurCqe(
);
errdefer addr_err_logger.log("Dropping connection");

// Panic message for handling `EAGAIN`; we're not using nonblocking sockets at all,
// so it should be impossible to receive that error, or for such an error to be
// triggered just from malicious connections.
const eagain_panic_msg =
"The file/socket should not be in nonblocking mode;" ++
" server or file/socket configuration error.";

switch (entry_data.state) {
.recv_head => |*head| {
switch (try connection.handleRecvResult(cqe.err())) {
.success => {},

.again => std.debug.panic(EAGAIN_PANIC_MSG, .{}),
.again => std.debug.panic(eagain_panic_msg, .{}),

.intr => {
try head.prepRecv(entry, &liou.io_uring);
Expand Down Expand Up @@ -253,7 +253,7 @@ fn consumeOurCqe(
.recv_body => |*body| {
switch (try connection.handleRecvResult(cqe.err())) {
.success => {},
.again => std.debug.panic(EAGAIN_PANIC_MSG, .{}),
.again => std.debug.panic(eagain_panic_msg, .{}),
.intr => @panic("TODO:"),
.conn_refused,
.conn_reset,
Expand All @@ -276,7 +276,7 @@ fn consumeOurCqe(
.send_file_head => |*sfh| {
switch (try connection.handleSendResult(cqe.err())) {
.success => {},
.again => std.debug.panic(EAGAIN_PANIC_MSG, .{}),
.again => std.debug.panic(eagain_panic_msg, .{}),
.intr => @panic("TODO:"),
.conn_reset,
.broken_pipe,
Expand Down Expand Up @@ -315,7 +315,7 @@ fn consumeOurCqe(
.to_pipe => {
switch (try connection.handleSpliceResult(cqe.err())) {
.success => {},
.again => std.debug.panic(EAGAIN_PANIC_MSG, .{}),
.again => std.debug.panic(eagain_panic_msg, .{}),
.bad_file_descriptors,
.bad_fd_offset,
.invalid_splice,
Expand All @@ -334,7 +334,7 @@ fn consumeOurCqe(
.to_socket => {
switch (try connection.handleSpliceResult(cqe.err())) {
.success => {},
.again => std.debug.panic(EAGAIN_PANIC_MSG, .{}),
.again => std.debug.panic(eagain_panic_msg, .{}),
.bad_file_descriptors,
.bad_fd_offset,
.invalid_splice,
Expand All @@ -360,7 +360,7 @@ fn consumeOurCqe(
.send_no_body => |*snb| {
switch (try connection.handleSendResult(cqe.err())) {
.success => {},
.again => std.debug.panic(EAGAIN_PANIC_MSG, .{}),
.again => std.debug.panic(eagain_panic_msg, .{}),
.intr => @panic("TODO:"),
.conn_reset,
.broken_pipe,
Expand Down

0 comments on commit 706f136

Please sign in to comment.