I have code that worked on windows but won't compile on linux because on line /zig/v-0.14.1/zig-0.14.1/lib/std/os/linux.zig:316 the struct O for the target x86_64 + linux does not include the NONBLOCK flag which I'm trying to use calling fcntl. I suppose that means the flag doesn't work on this target, how can I verify that? man fcntl doesn't help
#`posix.O.NONBLOCK` not available on linux x86_64. What does this mean?
1 messages · Page 1 of 1 (latest)
upgrade your zig version
I see it in the gh for 0.14
it's here in 0.15.1
oh its here for 0.14.x too https://github.com/ziglang/zig/blob/0.14.x/lib/std/os/linux.zig#L324
What, wait
can you send your code?
My error
src/network.zig:532:67: error: struct 'os.linux.O__struct_12974' has no member named 'NONBLOCK'
try posix.fcntl(sockfd, posix.F.SETFL, flags | posix.O.NONBLOCK);
~~~~~~~^~~~~~~~~
/home/jonatanc/.version-fox/cache/zig/v-0.14.1/zig-0.14.1/lib/std/os/linux.zig:316:23: note: struct declared here
.x86_64 => packed struct(u32) {
~~~~~~~^~~~~~
Yeah its there on the tag for 0.14
And here is /home/jonatanc/.version-fox/cache/zig/v-0.14.1/zig-0.14.1/lib/std/os/linux.zig:316:
315 pub const O = switch (native_arch) {
1 .x86_64 => packed struct(u32) {
2 ACCMODE: ACCMODE = .RDONLY,
3 _2: u4 = 0,
4 CREAT: bool = false,
5 EXCL: bool = false,
6 NOCTTY: bool = false,
7 TRUNC: bool = false,
8 APPEND: bool = false,
9 NONBLOCK: bool = false,
10 DSYNC: bool = false,
11 ASYNC: bool = false,
12 DIRECT: bool = false,
13 _15: u1 = 0,
14 DIRECTORY: bool = false,
15 NOFOLLOW: bool = false,
16 NOATIME: bool = false,
17 CLOEXEC: bool = false,
18 SYNC: bool = false,
19 PATH: bool = false,
20 TMPFILE: bool = false,
21 _: u9 = 0,
22 },
In fact, it is there!
oh you're using posix.O.NONBLOCK
I suppose posix gets the O value from linux?
are you linking libc? if you are it will use std.c
oh
you're using it as a value
var non_block_flags = flags;
non_block_flags.NONBLOCK = true;
it's a packed struct
right let me try now
in extern structs theyre bytes
Now with
const flags = try posix.fcntl(sockfd, posix.F.GETFL, 0);
flags.NONBLOCK = true;
try posix.fcntl(sockfd, posix.F.SETFL, flags);
src/network.zig:532:18: error: type 'usize' does not support field access
flags.NONBLOCK = true;
~~~~~^~~~~~~~~
flags is usize I gotta think a little better
oh bit cast it to an O
531 const flags_usize = try posix.fcntl(sockfd, posix.F.GETFL, 0);
1 const flags: posix.O = @bitCast(flags_usize);
~ 2 flags.NONBLOCK = true;
+ 3 try posix.fcntl(sockfd, posix.F.SETFL, flags);
Something like that? let me try
@bitCast size mismatch: destination type 'os.linux.O__struct_12974' has 32 bits but source type 'usize' has 64 bits
thanks so far, I understand more at least
src/network.zig:534:52: error: destination type 'usize' has more bits than source type 'u32'
try posix.fcntl(sockfd, posix.F.SETFL, @truncate(@as(u32, @bitCast(flags))));
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
How to truncate to a larger bitSize?
Decided to do
_ = try posix.fcntl(sockfd, posix.F.SETFL, @as(*usize, @alignCast(@ptrCast(&flags))).*);
But now I have other (unrelated) build errors before I can test
i dont think you need go truncate to bigger ints
@bitCast alone gives an error
But my pointerCast approach gives a runtime error as well:
thread 8127 panic: incorrect alignment
/mnt/c/Users/jonat/Desktop/Pessoal/programming/Programming Bitcoin book/zigimp/src/network.zig:534:68: 0x113cc30 in connect__anon_24540 (ZiglyNode)
_ = try posix.fcntl(sockfd, posix.F.SETFL, @as(*usize, @alignCast(@ptrCast(&flags))).*);
^
oh then bitcast to usize
@as(usize, @bitCast(...))
src/network.zig:534:67: error: @bitCast size mismatch: destination type 'usize' has 64 bits but source type 'os.linux.O__struct_12974' has 32 bits
_ = try posix.fcntl(sockfd, posix.F.SETFL, @as(usize, @bitCast(flags)));
^~~~~~~~~~~~~~~
oh oops
just this without truncate
I'll give another try later. Got some other problems interrupting me rn
setSockNONBLOCK for windows/posix after creation
or example of creation nonbl