All of lore.kernel.org
 help / color / mirror / Atom feed
From: yumkam@gmail.com (Yuriy M. Kaminskiy)
To: util-linux@vger.kernel.org
Subject: Re: fsck -l: fix racing between unlock/unlink and open
Date: Tue, 19 Apr 2016 17:58:11 +0300	[thread overview]
Message-ID: <m3potlk5mk.fsf@gmail.com> (raw)
In-Reply-To: 20160418102238.54gvnoxnsq7z76uv@ws.net.home

Karel Zak <kzak@redhat.com> writes:

> On Fri, Apr 08, 2016 at 12:59:29AM +0300, Yuriy M. Kaminskiy wrote:
>> Probably, there are better solutions, but I prefer KISS.
>
>> From fcb97ddcbc336bc8860828c47cdaf21c7b1ca655 Mon Sep 17 00:00:00 2001
>> From: "Yuriy M. Kaminskiy" <yumkam@gmail.com>
>> Date: Fri, 8 Apr 2016 00:38:56 +0300
>> Subject: [PATCH] fsck: fix racing between unlock/unlink and open
>> 
>> Process A	Process B	Process C
>> open()
>> [creates file]
>> lock()
>> [succeed]
>> 		open()
>> 		[open existing]
>> 		lock()...
>> running()
>> close()
>> 		[...succeed]
>> unlink()
>> 		running()
>> 				open()
>> 				[creates file] {BAD!}
>> 				lock()
>> 				[succeed] {BAD!}
>> 				running() {BAD!}
>> 		close()
>> 
>
> It would be better to check if the file still exist on the filesystem
> with the same inode number, if not then re-open. It means that on race
> you will create a new lock file. (You have to unlock after unlink.)
> See http://stackoverflow.com/questions/17708885/flock-removing-locked-file-without-race-condition
>
> Simple example:
>
> #include <stdio.h>
> #include <sys/file.h>
> #include <sys/types.h>
> #include <sys/stat.h>
> #include <fcntl.h>
> #include <unistd.h>
> #include <errno.h>
>
> int main(int argc, char **argv)
> {
> 	const char *lockpath = "test.lock";
> 	int fd;
> 	struct stat st_lock, st_file;
>
> 	while (1) {
> 		fd = open(lockpath, O_RDONLY|O_CREAT|O_CLOEXEC,
> 				    S_IWUSR|S_IRUSR|S_IRGRP|S_IROTH);
> 		fstat(fd, &st_lock);
> 		flock(fd, LOCK_EX);
>
> 		errno = 0;
> 		if (stat(lockpath, &st_file) == 0 && st_file.st_ino == st_lock.st_ino)
> 			break;

Requires stable inode numbers (well, probably acceptable assumption for {/var,}/run).

> 		if (errno)
> 			fprintf(stderr, "%d: stat failed: %m\n", getpid());
> 		else
> 			fprintf(stderr, "%d: ignore %lu (fs has:%lu)\n", getpid(), st_lock.st_ino, st_file.st_ino);
> 		close(fd);
> 	}
>
> 	fprintf(stderr, "%d: -->locked ino: %lu\n", getpid(), st_lock.st_ino);
> 	sleep(10);
> 	unlink(lockpath);
> 	close(fd);
> 	return 0;
> }
>
>
> See untested fsck patch below. Comments?

I still prefer KISS. (And I bet it will eat more resources than just
leaving those empty files around.)
(But either way, bug will be fixed, so I do as you like).

[...]


  reply	other threads:[~2016-04-19 14:58 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-04-07 21:59 fsck -l: fix racing between unlock/unlink and open Yuriy M. Kaminskiy
2016-04-18 10:22 ` Karel Zak
2016-04-19 14:58   ` Yuriy M. Kaminskiy [this message]
2016-04-20 10:13   ` Nir Soffer
2016-04-22  9:09 ` Karel Zak

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=m3potlk5mk.fsf@gmail.com \
    --to=yumkam@gmail.com \
    --cc=util-linux@vger.kernel.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.