Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a return value of 0 from write(2) in C an error?

Tags:

c

linux

posix

tcp

In the man page for the system call write(2) -

ssize_t write(int fd, const void *buf, size_t count);

it says the following:

Return Value

On success, the number of bytes written are returned (zero indicates nothing was written). On error, -1 is returned, and errno is set appropriately. If count is zero and the file descriptor refers to a regular file, 0 may be returned, or an error could be detected. For a special file, the results are not portable.

I would interpret this to mean that returning 0 simply means that nothing was written, for whatever arbitrary reason.

However, Stevens in UNP treats a return value of 0 as a fatal error when dealing with a file descriptor that is a TCP socket ( this is wrapped by another function which calls exit(1) on a short count ):

ssize_t /* Write "n" bytes to a descriptor. */
writen(int fd, const void *vptr, size_t n)
{
    size_t      nleft;
    ssize_t     nwritten;
    const char  *ptr;

    ptr = vptr;
    nleft = n;
    while (nleft > 0) {
        if ( (nwritten = write(fd, ptr, nleft)) <= 0) {
            if (nwritten < 0 && errno == EINTR)
                nwritten = 0;       /* and call write() again */
            else
                return(-1);         /* error */
        }

        nleft -= nwritten;
        ptr   += nwritten;
    }
    return(n);
}

He only treats 0 as a legit return value if the errno indicates that the call to write was interrupted by the process receiving a signal.

Why?

like image 233
Robert S. Barnes Avatar asked Feb 01 '10 12:02

Robert S. Barnes


People also ask

Is return 0 an error?

return 0 in the main function means that the program executed successfully. return 1 in the main function means that the program does not execute successfully and there is some error. return 0 means that the user-defined function is returning false.

What does return 0 do in C?

The main function is generally supposed to return a value and after it returns something it finishes execution. The return 0 means success and returning a non-zero number means failure.

What is the meaning of return 2 in C?

In your case, it appears that the statements are taken from the body of a function returning an int (or a related type convertible from int ). So, return 2 simply returns the value 2. Thus, if you have.

What is the return value of write?

The write function returns the number of bytes successfully written into the file, which may at times be less than the specified nbytes. It returns -1 if an exceptional condition is encountered, see section on errors below.


2 Answers

Stevens probably does this to catch old implementations of write() that behaved differently. For instance, the Single Unix Spec says (http://www.opengroup.org/onlinepubs/000095399/functions/write.html)

Where this volume of IEEE Std 1003.1-2001 requires -1 to be returned and errno set to [EAGAIN], most historical implementations return zero

like image 92
Per Knytt Avatar answered Sep 28 '22 07:09

Per Knytt


This will ensure that the code does not spin indefinitely, even if the file descriptor is not a TCP socket or unexpected non-blocking flags are in effect. On some systems, certain legacy non-blocking modes (e.g. O_NDELAY) cause write() to return 0 (without setting errno) if no data can be written without blocking, at least for certain types of file descriptors. (The POSIX standard O_NONBLOCK uses an error return for this case.) And some of the non-blocking modes on some systems apply to the underlying object (e.g. socket, fifo) rather than the file descriptor, and so could even have been enabled by another process having an open file descriptor for the same object. The code protects itself from spinning in such a situation by simply treating it as an error, since it is not intended for use with non-blocking modes.

like image 38
mark4o Avatar answered Sep 28 '22 06:09

mark4o