[TUHS] Unix APIs: elegant or not?

Chris Hanson cmhanson at eschatologist.net
Mon Nov 5 07:34:10 AEST 2018


On Nov 4, 2018, at 4:28 AM, arnold at skeeve.com wrote:
> 
> Chris Hanson <cmhanson at eschatologist.net> wrote:
> 
>> If anyone ever asks me about the elegance of UNIX, there’s one word, or really term, that springs to mind: EINTR.
>> 
>> The fact that man pages on modern systems still describe calls as returning -1 with errno set to EINTR baffles me.
> 
> Can you explain this some more?  This sounds like a claim that UNIX
> isn't elegant.  But I'm not following whatever it is you're saying.

Every piece of code that wants to call, say, read(2) needs to handle not only real errors but also needs to special-case EINTR and retry the read. Thus you should virtually never use read(2), only ever something like this:

    ssize_t safe_read(int fd, void *buf, size_t buf_size)
    {
        ssize_t rc;
        do {
            rc = read(fd, buf, buf_size);
        } while ((rc == -1) && (errno == EINTR));
        return rc;
    }

And do this for every classic system call, since virtually no client code should ever have to care about EINTR. It was early an implementation expediency that became API and that everyone now has to just deal with because you can’t expect the system call interface you use to do this for you.

This is the sort of wart that should’ve been fixed by System V and/or BSD 4 at latest.

  — Chris





More information about the TUHS mailing list