Skip to content

Commit

Permalink
Merge recv/send into read/write.
Browse files Browse the repository at this point in the history
Interesting bits:

 - End-of-stream (EOS, aka EOF) is indicated by returning the
  `$success_eos` errno value.

     - This differs from POSIX, where EOS is indicated by having `read`
       return 0, which is ambiguous in several cases. Of course, WASI
       libc will continue to provide POSIX semantics to C users.

     - This is a success condition being indicated with a non-zero errno
       value, which does not have precedent in POSIX.

 - `$fdstat`'s `$filetype` field is removed. The `$filetype` is still
   available through the `$filestat` type, though note that it's now
   protected by a rights flag. The goal is to start abstracting away more
   of the differences between files and sockets.

 - The `$peek` and `$waitall` flags are now available on `$read`, and can
   be used on files. This differs from POSIX.

 - POSIX says that `recv` and `send` fail if the stream is not a socket,
   but with `recv`/`send` merged into `read`/`write`, this restriction
   will no longer be automatically enforced. It could be manually enforced
   in WASI libc if needed, but in most cases, it's probably not important.

 - There is a new `$fd_line_oriented_terminal` right, to support
   `isatty`. I also anticipate expanding on this right in my upcoming
   terminal I/O proposal.

 - `$read`'s `$nread` result can be larger than the provided buffer to
   indicate a truncate message on a message-oriented socket (see also
   `$recv_data_truncated` in the current API, or `MSG_TRUNC` in POSIX
   `recv`. This avoids the need for a separate output value, but is
   it too surprising?

Out of scope for this proposal (but maybe in scope in the future!):

 - Streams vs datagrams - Having these merged is tricky, but this isn't
   new here; both `read` and `recv` already support both streams and
   datagrams.

 - Streams vs arrays - POSIX unifies the concept of a file as an array
   of bytes with the concept of file as a stream of data, which creates
   some tricky situations, but this also isn't new here.

 - Out-of-band messages and connectionless sockets aren't in WASI, and
   this proposal doesn't change that.

This implements WebAssembly#4.
  • Loading branch information
sunfishcode committed Mar 2, 2020
1 parent d5b0c23 commit 8b5ba2a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 73 deletions.
36 changes: 18 additions & 18 deletions phases/ephemeral/witx/typenames.witx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
(enum u16
;;; No error occurred. System call completed successfully.
$success
;;; End of stream was reached successfully.
$success_eos
;;; Argument list too long.
$2big
;;; Permission denied.
Expand Down Expand Up @@ -201,7 +203,7 @@
;;; If `path_open` is set, includes the right to invoke
;;; `path_open` with `fdflags::dsync`.
$fd_datasync
;;; The right to invoke `fd_read` and `sock_recv`.
;;; The right to invoke `fd_read`.
;;
;;; If `rights::fd_seek` is set, includes the right to invoke `fd_pread`.
$fd_read
Expand All @@ -218,7 +220,7 @@
;;; remains unaltered (i.e., `whence::cur` with offset zero), or to
;;; invoke `fd_tell`.
$fd_tell
;;; The right to invoke `fd_write` and `sock_send`.
;;; The right to invoke `fd_write`.
;;; If `rights::fd_seek` is set, includes the right to invoke `fd_pwrite`.
$fd_write
;;; The right to invoke `fd_advise`.
Expand Down Expand Up @@ -271,8 +273,10 @@
;;; If `rights::fd_read` is set, includes the right to invoke `poll_oneoff` to subscribe to `eventtype::fd_read`.
;;; If `rights::fd_write` is set, includes the right to invoke `poll_oneoff` to subscribe to `eventtype::fd_write`.
$poll_fd_readwrite
;;; The right to invoke `sock_shutdown`.
$sock_shutdown
;;; The right to invoke `fd_shutdown`.
$fd_shutdown
;;; The output supports line-oriented terminal output.
$fd_line_oriented_terminal
)
)

Expand Down Expand Up @@ -408,8 +412,6 @@
;;; File descriptor attributes.
(typename $fdstat
(struct
;;; File type.
(field $fs_filetype $filetype)
;;; File descriptor flags.
(field $fs_flags $fdflags)
;;; Rights that apply to this file descriptor.
Expand Down Expand Up @@ -638,25 +640,23 @@
;;; Exit code generated by a process when exiting.
(typename $exitcode u32)

;;; Flags provided to `sock_recv`.
;;; Flags provided to `fd_read`.
(typename $riflags
(flags u16
;;; Returns the message without removing it from the socket's receive queue.
$recv_peek
;;; On byte-stream sockets, block until the full amount of data can be returned.
$recv_waitall
)
)
;;;
;;; Note: This is similar to the `MSG_PEEK` flag in `recv` in POSIX.
$read_peek

;;; Flags returned by `sock_recv`.
(typename $roflags
(flags u16
;;; Returned by `sock_recv`: Message data has been truncated.
$recv_data_truncated
;;; On bytestream sockets, block until the full amount of data can be returned,
;;; unless an error or disconnect occurs.
;;;
;;; Note: This is similar to the `MSG_WAITALL` flag in `recv` in POSIX.
$read_waitall
)
)

;;; Flags provided to `sock_send`. As there are currently no flags
;;; Flags provided to `fd_write`. As there are currently no flags
;;; defined, it must be set to zero.
(typename $siflags u16)

Expand Down
29 changes: 26 additions & 3 deletions phases/ephemeral/witx/wasi_ephemeral_fd.witx
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,25 @@
)

;;; Read from a file descriptor.
;;; Note: This is similar to `readv` in POSIX.
;;;
;;; When reading from a datagram socket, returns `$msgsize` if a datagram
;;; is larger than the provided buffer
;;;
;;; When the end of the file or stream is reached, this function returns
;;; `$success_eos`.
;;;
;;; Note: This is similar to `readv` and `recv` in POSIX, except that a return
;;; value of 0 does not necessarily indicate end of file/stream.
(@interface func (export "read")
(param $fd $fd)
;;; List of scatter/gather vectors to which to store data.
(param $iovs $iovec_array)
;;; Message flags.
(param $ri_flags $riflags)
(result $error $errno)
;;; The number of bytes read.
;;; The number of bytes read. If this is greater than the total size of the
;;; provided buffer, it indicates the length of a message on a message-based
;;; socket, and data beyond the size of the buffer is discarded.
(result $nread $size)
)

Expand Down Expand Up @@ -249,13 +261,24 @@
)

;;; Write to a file descriptor.
;;; Note: This is similar to `writev` in POSIX.
;;; Note: This is similar to `writev` and `send` in POSIX.
(@interface func (export "write")
(param $fd $fd)
;;; List of scatter/gather vectors from which to retrieve data.
(param $iovs $ciovec_array)
;;; Message flags.
(param $si_flags $siflags)
(result $error $errno)
;;; The number of bytes written.
(result $nwritten $size)
)

;;; Shut down socket send and receive channels.
;;; Note: This is similar to `shutdown` in POSIX.
(@interface func (export "shutdown")
(param $fd $fd)
;;; Which channels on the socket to shut down.
(param $how $sdflags)
(result $error $errno)
)
)
52 changes: 0 additions & 52 deletions phases/ephemeral/witx/wasi_ephemeral_sock.witx

This file was deleted.

0 comments on commit 8b5ba2a

Please sign in to comment.