The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Alex Riesen <raa.lkml@gmail.com>
To: "linux-os (Dick Johnson)" <linux-os@analogic.com>
Cc: Trond Myklebust <trond.myklebust@fys.uio.no>,
	boi@boi.at, Linux kernel <linux-kernel@vger.kernel.org>
Subject: Re: blocking file lock functions (lockf,flock,fcntl) do not return after timer signal
Date: Wed, 12 Oct 2005 23:15:31 +0200	[thread overview]
Message-ID: <20051012211531.GA4068@steel.home> (raw)
In-Reply-To: <Pine.LNX.4.61.0510121112060.4302@chaos.analogic.com>

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;
}


  parent reply	other threads:[~2005-10-12 21:15 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-10-12  7:54 blocking file lock functions (lockf,flock,fcntl) do not return after timer signal "Dieter Müller (BOI GmbH)"
2005-10-12 12:48 ` Alex Riesen
2005-10-12 13:09   ` linux-os (Dick Johnson)
2005-10-12 14:39   ` Trond Myklebust
2005-10-12 15:10     ` Alex Riesen
2005-10-12 15:20       ` linux-os (Dick Johnson)
2005-10-12 15:37         ` Michael Kerrisk
2005-10-12 15:43           ` linux-os (Dick Johnson)
2005-10-12 16:05             ` Michael Kerrisk
2005-10-12 16:06             ` Mark Lord
2005-10-12 21:15         ` Alex Riesen [this message]
2005-10-12 16:36       ` Trond Myklebust

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20051012211531.GA4068@steel.home \
    --to=raa.lkml@gmail.com \
    --cc=boi@boi.at \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-os@analogic.com \
    --cc=trond.myklebust@fys.uio.no \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox