AIO_WRITE(2)
π AIO_WRITE(2) β the system calls manual page, bestie edition π
okay so you opened AIO_WRITE(2) from the System Calls Manual section and you're about to learn how to write to a file asynchronously, which is honestly main character energy. sit down, grab a strawberry π, let's go.
β¨ NAME β¨
aio_write, aio_write2, aio_writev β asynchronous write to a file (REALTIME)
π LIBRARY π
Standard C Library (libc, -lc) β the classic, the icon, the one you already link against π
π¦ SYNOPSIS π¦
#include <aio.h>
int
aio_write(struct aiocb *iocb);
int
aio_write2(struct aiocb *iocb, int flags);
#include <sys/uio.h>
int
aio_writev(struct aiocb *iocb);
πΈ DESCRIPTION πΈ
So aio_write(), aio_write2(), and aio_writev() let your process write to the descriptor iocb->aio_fildes. and here's the vibe: the syscalls return immediately after the write request has been enqueued to the descriptor. the write may or may not have completed by the time the call comes back. it's giving "dropped it in the group chat and dipped" π
aio_write()writesiocb->aio_nbytesbytes from the buffer pointed to byiocb->aio_buf.aio_writev()gathers the data from theiocb->aio_iovcntbuffers specified by the members of theiocb->aio_iovarray.
if the request couldn't be enqueued β usually because the arguments were invalid β the call returns without having enqueued the request at all. rude but fair.
for aio_writev(), the iovec structure is defined in writev(2). iykyk π
if O_APPEND is set for iocb->aio_fildes, write operations append to the file in the same order as the calls were made. if O_APPEND is not set for the file descriptor, the write operation for aio_write() happens at the absolute position from the beginning of the file plus iocb->aio_offset. periodt.
π₯ the flags argument (aio_write2 only)
aio_write2() takes the flags argument. if flags is passed as zero, the call behaves identically to aio_write(). you can specify the following flags by logical or:
AIO_OP2_FOFFSETβ for non-O_APPENDfile descriptors, the write occurs at the file descriptor offset, which is advanced by the operation the waywrite(2)does it. theiocb->aio_offsetfield is ignored πAIO_OP2_VECTOREDβ similar toaio_writev(), the write buffers are specified by theaiocb->aio_iovarray.
β the iocb pointer glow-up
you can totally keep using that iocb pointer as an argument to aio_return() and aio_error() to figure out the return or error status of the enqueued operation while it's still in progress. multitasking icon π
β οΈ but listen: if the request is successfully enqueued, the value of iocb->aio_offset can be modified during the request as context. so likeβ¦ do not reference that value after the request is enqueued. it's not yours anymore, bestie.
the iocb->aio_sigevent structure can be used to request notification of the operation's completion, as described in aio(4). that's your little "it's done!" ping π«Ά
π§ RESTRICTIONS π§
- the Asynchronous I/O Control Block structure pointed to by
iocb, and the buffer that theiocb->aio_bufmember of that structure references, must remain valid until the operation has completed. no take-backs, no freeing early. - the asynchronous I/O control buffer
iocbshould be zeroed before the system calls to avoid passing bogus context information to the kernel. bogus context is not the vibe π - modifications of the Asynchronous I/O Control Block structure or the buffer contents are not allowed while the request is queued. hands off π«
- if the file offset in
iocb->aio_offsetis past the offset maximum foriocb->aio_fildes, no I/O will occur. literally nothing. crickets.
π RETURN VALUES π
aio_write() and aio_writev() return the value 0 if successful; otherwise the value -1 is returned and the global variable errno is set to indicate the error. classic.
π ERRORS π
aio_write(), aio_write2(), and aio_writev() will fail if:
[EAGAIN]β the request was not queued because of system resource limitations.[EFAULT]β part ofaio_iovpoints outside the process's allocated address space.[EINVAL]β the asynchronous notification method iniocb->aio_sigevent.sigev_notifyis invalid or not supported.[EOPNOTSUPP]β asynchronous write operations on the file descriptoriocb->aio_fildesare unsafe, and unsafe asynchronous I/O operations are disabled.
π©· the sneaky ones (sync or async detection)
these following conditions may be synchronously detected when the aio_write(), aio_write2(), or aio_writev() system call is made β or asynchronously, at any time thereafter. π
if they're detected at call time, the calls return -1 and set errno appropriately. otherwise you must call the aio_return() system call (which will return -1) and call aio_error() to determine the actual value that would have been returned in errno. two-step detective work, no shortcuts:
[EBADF]β theiocb->aio_fildesargument is invalid, or is not opened for writing.[EINVAL]β the offsetiocb->aio_offsetis not valid, the priority specified byiocb->aio_reqpriois not a valid priority, or the number of bytes specified byiocb->aio_nbytesis not valid.
π¦ if it got enqueued but then things went sideways
if the request is successfully enqueued, but subsequently canceled or an error occurs, the value returned by the aio_return() system call is per the write(2) system call, and the value returned by the aio_error() system call is either one of the error returns from the write(2) system call, or one of:
[EBADF]β theiocb->aio_fildesargument is invalid for writing.[ECANCELED]β the request was explicitly canceled via a call toaio_cancel().[EINVAL]β the offsetiocb->aio_offsetwould be invalid.
π SEE ALSO π
aio_cancel(2), aio_error(2), aio_return(2), aio_suspend(2), aio_waitcomplete(2), sigevent(3), siginfo(3), aio(4) β go read your siblings, they're all iconic β¨
πΊ STANDARDS πΊ
the aio_write() system call is expected to conform to the IEEE Std 1003.1 (βPOSIX.1β) standard. slay, portable.
the aio_write2() and aio_writev() system calls are FreeBSD extensions, and should not be used in portable code. adore them, but don't take them on tour π§³
π HISTORY π
aio_write()first appeared in FreeBSD 3.0.aio_writev()first appeared in FreeBSD 13.0.aio_write2()first appeared in FreeBSD 14.1.
a whole generational timeline, we love an evolution arc π¦
π AUTHORS π
this manual page was written by Wes Peters <wes@softweyr.com>. thank you Wes, you ate π
π«Ά BUGS π«Ά
invalid information in iocb->_aiocb_private may confuse the kernel. that's it. that's the bug. don't put nonsense in there and expect the kernel to be chill about it π
and that's the whole page bestie β no notes, no skips, every flag accounted for π β¨
this page came to you from the Debian edition, dated March 27, 2025 β mwah, go write something asynchronously ππ
π¦ where this page comes from
- freebsd 14.5: base 14.5
- freebsd 15.1: base 15.1
- Copyright (c) 1999 Softweyr LLC
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms