Steve
Steve

Reputation: 1810

glibc and system calls interrupted by signals

I'm new to Linux and I'm learning about system calls and signals. For example the read system call can be interrupted by a signal. If it's interrupted before anything was read it fails and sets errno to EINTR. In glibc this is mostly handled by the TEMP_FAILURE_RETRY macro. But if it's interrupted when only some data was read the functions succeeds reading less than was requested. In this case the caller should issue another read for the missing portion, continuing this until all the data is read. Nevertheless the glibc source contains numerous calls like this:

if (TEMP_FAILURE_RETRY (read (fd, &word, 4)) != 4)
    error (EXIT_FAILURE, errno, _("cannot read header"));

This would seem to mean it's, in numerous locations, not restarting the system call properly. Am I missing something? To me it would seem that the partial-read interruption case would be more common than the none-read-yet scenario, and increasingly so as the amount to read requested gets larger.

Upvotes: 2

Views: 487

Answers (1)

Employed Russian
Employed Russian

Reputation: 213626

But if it's interrupted when only some data was read the functions succeeds reading less than was requested.

Your understanding of this subject is incomplete. You'll want to carefully read this man page (the "Interruption of system calls and library functions by signal handlers" section of it).

In particular note the distinction between "slow" devices and local disk (which is assumed to be fast).

This would seem to mean it's, in numerous locations, not restarting the system call properly.

If the read was from a "fast" device and SA_RESTART was used, then the system call will have been restarted automatically, and no partial reads are possible.

Upvotes: 2

Related Questions