Skip to content

Freebsd: Fixed 'write' syscall to cause 'ESPIPE' on the pipe fd. #5575

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

wisonye
Copy link
Contributor

@wisonye wisonye commented Aug 12, 2025

If you use posix.pipe() to create a bidirectional pipe that allows you to write to one end and read from the other end, you will find that the write syscall will cause espipe error!

Here is the minimal example to prove that:

import "core:sys/freebsd"
import "core:sys/posix"

WriteSysCall :: freebsd.write

pipe_fds: [2]PosixFd,

// ... Run the following code in a function:

//
// Sample bytes
//
event_bytes := [?]u8{0x0A, 0x0B, 0x0C, 0x0D};

//
// Write the event bytes to the `WRITE_PIPE`
//
count, error := freebsd.write(
    cast(Fd)self.pipe_fds[1],
    event_bytes[:]
)

Then the error will be espipe!!!

But if you use posix.write, it works expected:

 count := posix.write(
     self.pipe_fds[1],
     &event_bytes[0],
     len(event_bytes)
 )

So, if you look into $ODIN_ROOT/core/sys/freebsd/syscalls.odin, you will see that the write procedure actually call SYS_pwrite syscall which is wrong!

Since pwrite requires an offset, which in turn triggers a lseek/seek call, it will fail when the file descriptor belongs to a pipe.

If you run man 2 write, you should see the following description about that:

The pwrite() and pwritev() system calls may also return the following
errors:

[EINVAL]           The offset value was negative.

[ESPIPE]           The file descriptor is associated with a pipe, socket,
                or FIFO.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant