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

From: Alex Riesen
Date: Wed Oct 12 2005 - 16:16:23 EST


linux-os (Dick Johnson), Wed, Oct 12, 2005 17:20:26 +0200:
> >>>> 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
> >
> > Doesn't look so. I'd expect "flock: EWOULDBLOCK" and "sleeping" after
> > the first timeout.

It's EINTR, btw.

linux-os (Dick Johnson), Wed, Oct 12, 2005 17:20:26 +0200:
> 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.

Ahh yes, of course. signal(2) places a syscall-restarting handler in glibc.
My bad, sorry.

For the last time:

// everything works as expected, flock returns with EINTR in the
// second instance of the program.
#include <unistd.h>
#include <sys/time.h>
#include <sys/file.h>
#include <stdio.h>
#include <signal.h>
#include <errno.h>

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

int main(int argc, char* argv[])
{
struct itimerval tv = {
.it_interval = {.tv_sec = 10, .tv_usec = 0},
.it_value = {.tv_sec = 10, .tv_usec = 0},
};
struct sigaction sa = { .sa_handler = alrm, .sa_flags = 0 };
sigaction(SIGALRM, &sa, NULL);
setitimer(ITIMER_REAL, &tv, NULL);
int fd = open(argv[1], O_RDWR);
if ( fd < 0 ) {
perror(argv[1]);
return 1;
}
printf("locking...\n");
if ( flock(fd, LOCK_EX) < 0 ) {
perror("flock");
return 1;
}
printf("sleeping...\n");
int ch;
while ( read(0, &ch, 1) < 0 && EINTR == errno )
;
close(fd);
return 0;
}

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/