From: "Stephen C. Tweedie" <sct@redhat.com>
To: David Ford <david+cert@blue-labs.org>
Cc: linux-kernel@vger.kernel.org, Stephen Tweedie <sct@redhat.com>,
Matthew Wilcox <matthew@wil.cx>
Subject: Re: broken flock()
Date: Tue, 2 Jul 2002 16:01:18 +0100 [thread overview]
Message-ID: <20020702160118.C4711@redhat.com> (raw)
In-Reply-To: <3D1C96C3.9000500@blue-labs.org>; from david+cert@blue-labs.org on Fri, Jun 28, 2002 at 01:02:59PM -0400
[-- Attachment #1: Type: text/plain, Size: 1372 bytes --]
Hi,
On Fri, Jun 28, 2002 at 01:02:59PM -0400, David Ford wrote:
> NOTE: Linux appears to have broken flock() again. Unless
> the bug is fixed before sendmail 8.13 is shipped,
> 8.13 will change the default locking method to
> fcntl() for Linux kernel 2.4 and later. You may
> want to do this in 8.12 by compiling with
> -DHASFLOCK=0. Be sure to update other sendmail
> related programs to match locking techniques.
> Is it really broken or is sendmail smoking crack like when they said
> that itimers in Linux didn't work?
It really is broken, and sendmail triggers it (at least their
commercial binaries do). I've already been talking to willy about the
problem.
The trouble is the accounting: if one process opens a fd and then
fork()s, it is possible for the lock to be taken in the parent and
released in the child (or vice versa) --- unless there's an explicit
flock(LOCK_UN), then the lock will be released implicitly when the
last reference to the fd is closed.
When this happens, we get the lock count incremented in one task and
decremented in another. That can wrap the lock count backwards to -1
(or rather ~0UL), which causes the locks rlimit check to think we've
exceeded the lock quota and new lock requests will fail. It's easy to
reproduce this: try the attached prog. It produces an erroneous
ENOLCK due to the bug.
Cheers,
Stephen
[-- Attachment #2: locklim.c --]
[-- Type: text/plain, Size: 980 bytes --]
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/file.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
int lock_file(int fd, int l_options) {
int ret;
ret = flock(fd, l_options);
if (ret)
perror("flock error");
return ret;
}
int main() {
int fd;
char *filename = "/tmp/lockf1";
pid_t pid;
int syncpipe[2];
char c;
pipe(syncpipe);
fd = open(filename, O_CREAT | O_RDWR, 0666);
if(fd < 0) {
perror("parent could not open file");
exit(1);
}
pid = fork();
if(pid < 0) {
perror("fork failed");
exit(1);
}
if (pid) {
lock_file(fd, LOCK_EX);
if (close(fd))
perror("parent: error closing file");
write(syncpipe[1], &c, 1);
} else {
/* Wait until the parent has taken the lock */
read(syncpipe[0], &c, 1);
lock_file(fd, LOCK_UN);
lock_file(fd, LOCK_EX);
if(close(fd))
perror("child: error closing file");
exit(0);
}
wait(NULL);
return 0;
}
next prev parent reply other threads:[~2002-07-02 14:59 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2002-06-28 17:02 broken flock() David Ford
2002-07-02 15:01 ` Stephen C. Tweedie [this message]
-- strict thread matches above, loose matches on Subject: below --
2002-07-02 15:44 Matthew Wilcox
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=20020702160118.C4711@redhat.com \
--to=sct@redhat.com \
--cc=david+cert@blue-labs.org \
--cc=linux-kernel@vger.kernel.org \
--cc=matthew@wil.cx \
/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