libs/ctrl: Allow writev_exact() to change the iov array

Avoid having to allocate and copy the array if a partial write happens.

Signed-off-by: Frediano Ziglio <frediano.ziglio@citrix.com>
Reviewed-by: Andrew Cooper <andrew.cooper3@citrix.com>
This commit is contained in:
Frediano Ziglio authored and Andrew Cooper committed 2026-07-10 19:35:49 +01:00
1 parent 7d48ef45b6
commit 85bdb67eaa
2 files changed
+18 -55

No files matched your search

+16 -54
View File
@@ -635,7 +635,7 @@ int write_exact(int fd, const void *data, size_t size)
/*
* MiniOS's libc doesn't know about writev(). Implement it as multiple write()s.
*/
int writev_exact(int fd, const struct iovec *iov, int iovcnt)
int writev_exact(int fd, struct iovec *iov, int iovcnt)
{
int rc, i;
@@ -649,71 +649,33 @@ int writev_exact(int fd, const struct iovec *iov, int iovcnt)
return 0;
}
#else
int writev_exact(int fd, const struct iovec *iov, int iovcnt)
int writev_exact(int fd, struct iovec *iov, int iovcnt)
{
struct iovec *local_iov = NULL;
int rc = 0, iov_idx = 0, saved_errno = 0;
ssize_t len;
int iov_idx = 0;
ssize_t len = 0;
while ( iov_idx < iovcnt )
{
/*
* Skip over iov[] entries with 0 length.
*
* This is needed to cover the case where we took a partial write and
* all remaining vectors are of 0 length. In such a case, the results
* from writev() are indistinguishable from EOF.
*/
while ( iov[iov_idx].iov_len == 0 )
if ( ++iov_idx == iovcnt )
goto out;
/* Check iov[] to see whether we had a partial or complete write. */
if ( len >= iov[iov_idx].iov_len )
{
len -= iov[iov_idx++].iov_len;
continue;
}
/* Partial write of iov[iov_idx]. */
iov[iov_idx].iov_base += len;
iov[iov_idx].iov_len -= len;
len = writev(fd, &iov[iov_idx], min(iovcnt - iov_idx, IOV_MAX));
saved_errno = errno;
if ( (len == -1) && (errno == EINTR) )
continue;
if ( len <= 0 )
{
rc = -1;
goto out;
}
/* Check iov[] to see whether we had a partial or complete write. */
while ( (len > 0) && (iov_idx < iovcnt) )
{
if ( len >= iov[iov_idx].iov_len )
len -= iov[iov_idx++].iov_len;
else
{
/* Partial write of iov[iov_idx]. Copy iov so we can adjust
* element iov_idx and resubmit the rest. */
if ( !local_iov )
{
local_iov = malloc(iovcnt * sizeof(*iov));
if ( !local_iov )
{
saved_errno = ENOMEM;
rc = -1;
goto out;
}
iov = memcpy(local_iov, iov, iovcnt * sizeof(*iov));
}
local_iov[iov_idx].iov_base += len;
local_iov[iov_idx].iov_len -= len;
break;
}
}
return -1;
}
saved_errno = 0;
out:
free(local_iov);
errno = saved_errno;
return rc;
return 0;
}
#endif
+2 -1
View File
@@ -383,7 +383,8 @@ int xc_flush_mmu_updates(xc_interface *xch, struct xc_mmu *mmu);
/* Return 0 on success; -1 on error setting errno. */
int read_exact(int fd, void *data, size_t size); /* EOF => -1, errno=0 */
int write_exact(int fd, const void *data, size_t size);
int writev_exact(int fd, const struct iovec *iov, int iovcnt);
/* May alter @iov to cope with partial writes. */
int writev_exact(int fd, struct iovec *iov, int iovcnt);
int xc_ffs8(uint8_t x);
int xc_ffs16(uint16_t x);
Reference in new issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.