From: vippstar on
On Jun 20, 7:26 am, rahul <rahulsin...(a)gmail.com> wrote:
> On Jun 20, 3:50 am, kid joe <spamt...(a)spamtrap.invalid> wrote:
>
> > Hi,
>
> > Do I need to retry writev() on a blocking Unix-domain SOCK_STREAM socket,
> > or will it always write out the exact number of bytes I asked for?
>
> > For example:
>
> > if (write(fd, (const char *) presp, resp_size) != resp_size)
What's the type of 'presp' and why do you have to cast it to const
char *? (hint: you don't have to)
You compare unsigned with signed, *if* resp_size is ULONG_MAX then if
write() fails, it'll appear as the operation succeeded!
> > {
> > warn("Writing %lu bytes failed\n", resp_size);
> > return;
> > }
>
> > this seems to work, but I want to know if it's "really OK" in terms of
> > ANSI Standards etc.
ANSI has written many standards, which are you interested in?
You posted in comp.lang.c, so presumably ANSI C, but then again, you
asked a POSIX question so it's hard to tell...
>
> This topic is not about ANSI but POSIX standards.
> <off-topic>
> You asked about writev and you are using write. Nevertheless, the same
> errors are defined for both. write can fail in various cases: EPIPE(if
> pipe is broken), EINT(if interrupted by a signal) and EAGAIN ( if fd
> opened as O_NONBLOCK and the call would block). There are a few more
> error scenarios which the man pages document. Its always a good idea
> to consult the man pages(or any equivalent documentation).
> </off-topic>

Please, when the question is off-topic do not insert <offtopic> tags
to answer it, rather do not answer it at all.
One reason is that your answer might be wrong, and then someone will
have to correct you OR the person who asked the question will get
incorrect information.
And indeed, in this particular case, your answer was wrong.
writev() has two additional reasons it can fail,
EINVAL
o if the sum of iov_len values is larger than SSIZE_MAX,
o if the third argument of writev() is less than or equal to 0,
or greater than IOV_MAX

writev() returns the number of bytes written, not the number of struct
iovec objects written.

Follow-ups set to comp.unix.programmer only.