Re: blocking file lock functions (lockf,flock,fcntl) do not return after timer signal

From: linux-os (Dick Johnson)
Date: Wed Oct 12 2005 - 10:22:41 EST

On Wed, 12 Oct 2005, Alex Riesen wrote:

> On 10/12/05, Trond Myklebust <trond.myklebust@xxxxxxxxxx> wrote:
>> on den 12.10.2005 Klokka 14:48 (+0200) skreiv Alex Riesen:
>>> On 10/12/05, "Dieter Müller (BOI GmbH)" <dieter.mueller@xxxxxx> wrote:
>>>> bug description:
>>>> flock, lockf, fcntl do not return even after the signal SIGALRM has
>>>> been raised and the signal handler function has been executed
>>>> the functions should return with a return value EWOULDBLOCK as described
>>>> in the man pages
>> Works for me on a local filesystem.
>> Desktop$ ./gnurr gnarg
>> locking...
>> timeout
>> timeout
>> timeout
>> timeout
>> timeout
> Doesn't look so. I'd expect "flock: EWOULDBLOCK" and "sleeping" after
> the first timeout.

As I told you, you use sigaction(). Also flock() will not block
unless there is another open on the file. The code will run to
your blocking read(), wait 10 seconds, get your "timeout" from
the signal handler, then read() will return with -1 and ERESTARTSYS
in errno as required.

Also, using a 'C' runtime library call like write() in a signal-
handler is a bug.

#include <unistd.h>
#include <sys/time.h>
#include <sys/file.h>
#include <time.h>
#include <signal.h>

void alrm(int sig)
write(2, "timeout\n", 8);

int main(int argc, char* argv[])
struct sigaction sa;
struct itimerval tv = {
.it_interval = {.tv_sec = 10, .tv_usec = 0},
.it_value = {.tv_sec = 10, .tv_usec = 0},
struct itimerval otv;

sigaction(SIGALRM, NULL, &sa);
sa.sa_handler = alrm;
sa.sa_flags = 0;
sigaction(SIGALRM, &sa, NULL);

// signal(SIGALRM, alrm);
setitimer(ITIMER_REAL, &tv, &otv);
int fd = open(argv[1], O_RDWR);
if ( fd < 0 )
return 1;
if ( flock(fd, LOCK_EX) < 0 )
return 1;
int ch;
read(0, &ch, 1);
return 0;

Dick Johnson
Penguin : Linux version on an i686 machine (5589.56 BogoMips).
Warning : 98.36% of all statistics are fiction.

The information transmitted in this message is confidential and may be privileged. Any review, retransmission, dissemination, or other use of this information by persons or entities other than the intended recipient is prohibited. If you are not the intended recipient, please notify Analogic Corporation immediately - by replying to this message or by sending an email to DeliveryErrors@xxxxxxxxxxxx - and destroy all copies of this information, including any attachments, without reading or disclosing them.

Thank you.
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at
Please read the FAQ at