#`posix.O.NONBLOCK` not available on linux x86_64. What does this mean?

1 messages · Page 1 of 1 (latest)

hidden trellis
#

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

drowsy hornet
#

I see it in the gh for 0.14

hidden trellis
#

What, wait

fast condor
#

can you send your code?

hidden trellis
#

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) {
               ~~~~~~~^~~~~~
drowsy hornet
#

Yeah its there on the tag for 0.14

hidden trellis
#

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!

fast condor
#

oh you're using posix.O.NONBLOCK

hidden trellis
#

I suppose posix gets the O value from linux?

fast condor
#

are you linking libc? if you are it will use std.c

hidden trellis
#

I'm not

#

I should not, at least

#

No references to libc on build.zig so ig not

fast condor
#

you're using it as a value

#
var non_block_flags = flags;
non_block_flags.NONBLOCK = true;
#

it's a packed struct

hidden trellis
#

ooooooh

#

so bools are bits

#

ok

fast condor
#

yeah

#

in packed structs

hidden trellis
#

right let me try now

fast condor
#

in extern structs theyre bytes

hidden trellis
#

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

fast condor
#

oh bit cast it to an O

hidden trellis
#
  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

fast condor
#

oh right

#

you might need to truncate it

#

not sure though

hidden trellis
#

thanks so far, I understand more at least

hidden trellis
#
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

fast condor
hidden trellis
#

@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))).*);
                                                                   ^
fast condor
#

@as(usize, @bitCast(...))

hidden trellis
#
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)));
                                                                  ^~~~~~~~~~~~~~~
fast condor
#

oh oops

hidden trellis
#

I'll give another try later. Got some other problems interrupting me rn

fossil steeple