authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-05-05 12:50:50-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-05-05 12:50:50-04:00
logb13a02ed1aaab407a29b37bb5559639bf3a69715
tree4da25b198f947d548a0c636ac0d41f7c9ef2edac
parent0a2104689b4c3c686f2268b5053ad38fd01c1d55

avoid unnecessary fcntl syscalls when setting socket flags


1 files changed, 6 insertions(+), 6 deletions(-)

lib/std/os.zig+6-6
...@@ -2173,7 +2173,7 @@ pub fn socket(domain: u32, socket_type: u32, protocol: u32) SocketError!fd_t {...@@ -2173,7 +2173,7 @@ pub fn socket(domain: u32, socket_type: u32, protocol: u32) SocketError!fd_t {
2173 switch (errno(rc)) {2173 switch (errno(rc)) {
2174 0 => {2174 0 => {
2175 const fd = @intCast(fd_t, rc);2175 const fd = @intCast(fd_t, rc);
2176 if (!have_sock_flags and filtered_sock_type != socket_type) {2176 if (!have_sock_flags) {
2177 try setSockFlags(fd, socket_type);2177 try setSockFlags(fd, socket_type);
2178 }2178 }
2179 return fd;2179 return fd;
...@@ -2341,7 +2341,7 @@ pub fn accept(...@@ -2341,7 +2341,7 @@ pub fn accept(
2341 switch (errno(rc)) {2341 switch (errno(rc)) {
2342 0 => {2342 0 => {
2343 const fd = @intCast(fd_t, rc);2343 const fd = @intCast(fd_t, rc);
2344 if (!have_accept4 and flags != 0) {2344 if (!have_accept4) {
2345 try setSockFlags(fd, flags);2345 try setSockFlags(fd, flags);
2346 }2346 }
2347 return fd;2347 return fd;
...@@ -3277,26 +3277,26 @@ pub fn fcntl(fd: fd_t, cmd: i32, arg: usize) FcntlError!usize {...@@ -3277,26 +3277,26 @@ pub fn fcntl(fd: fd_t, cmd: i32, arg: usize) FcntlError!usize {
3277}3277}
32783278
3279fn setSockFlags(fd: fd_t, flags: u32) !void {3279fn setSockFlags(fd: fd_t, flags: u32) !void {
3280 {3280 if ((flags & SOCK_CLOEXEC) != 0) {
3281 var fd_flags = fcntl(fd, F_GETFD, 0) catch |err| switch (err) {3281 var fd_flags = fcntl(fd, F_GETFD, 0) catch |err| switch (err) {
3282 error.FileBusy => unreachable,3282 error.FileBusy => unreachable,
3283 error.Locked => unreachable,3283 error.Locked => unreachable,
3284 else => |e| return e,3284 else => |e| return e,
3285 };3285 };
3286 if ((flags & SOCK_CLOEXEC) != 0) fd_flags |= FD_CLOEXEC;3286 fd_flags |= FD_CLOEXEC;
3287 _ = fcntl(fd, F_SETFD, fd_flags) catch |err| switch (err) {3287 _ = fcntl(fd, F_SETFD, fd_flags) catch |err| switch (err) {
3288 error.FileBusy => unreachable,3288 error.FileBusy => unreachable,
3289 error.Locked => unreachable,3289 error.Locked => unreachable,
3290 else => |e| return e,3290 else => |e| return e,
3291 };3291 };
3292 }3292 }
3293 {3293 if ((flags & SOCK_NONBLOCK) != 0) {
3294 var fl_flags = fcntl(fd, F_GETFL, 0) catch |err| switch (err) {3294 var fl_flags = fcntl(fd, F_GETFL, 0) catch |err| switch (err) {
3295 error.FileBusy => unreachable,3295 error.FileBusy => unreachable,
3296 error.Locked => unreachable,3296 error.Locked => unreachable,
3297 else => |e| return e,3297 else => |e| return e,
3298 };3298 };
3299 if ((flags & SOCK_NONBLOCK) != 0) fl_flags |= O_NONBLOCK;3299 fl_flags |= O_NONBLOCK;
3300 _ = fcntl(fd, F_SETFL, fl_flags) catch |err| switch (err) {3300 _ = fcntl(fd, F_SETFL, fl_flags) catch |err| switch (err) {
3301 error.FileBusy => unreachable,3301 error.FileBusy => unreachable,
3302 error.Locked => unreachable,3302 error.Locked => unreachable,