[sheepdog] [PATCH] sheep: lock against concurrent access to the store directory
Liu Yuan
namei.unix at gmail.com
Fri May 18 04:17:32 CEST 2012
On 05/17/2012 11:40 PM, Christoph Hellwig wrote:
> This patch uses lockf on a specially create lock file in the base directory
> to detect early on if another sheep is using this directory and abort with
> a useful error message.
>
> Signed-off-by: Christoph Hellwig <hch at lst.de>
>
> diff --git a/sheep/store.c b/sheep/store.c
> index 5d433f3..e93daef 100644
> --- a/sheep/store.c
> +++ b/sheep/store.c
> @@ -672,11 +672,40 @@ again:
> return 0;
> }
>
> +#define LOCK_PATH "/lock"
> +
> int init_base_path(const char *d)
> {
> int new = 0;
> + int ret;
> + int fd;
> + char *lock_path;
> +
> + ret = init_path(d, &new);
> + if (ret)
> + return ret;
> +
> + lock_path = zalloc(strlen(d) + strlen(LOCK_PATH) + 1);
> + sprintf(lock_path, "%s" LOCK_PATH, d);
> +
> + fd = open(lock_path, O_WRONLY|O_CREAT, def_fmode);
> + if (fd < 0) {
> + eprintf("failed to open lock file %s (%s)\n",
> + lock_path, strerror(errno));
> + return -1;
> + }
> +
> + if (lockf(fd, F_TLOCK, 1) < 0) {
> + if (errno == EACCES || errno == EAGAIN) {
> + eprintf("another sheep daemon is using %s\n", d);
> + } else {
> + eprintf("unable to get base dir lock (%s)\n",
> + strerror(errno));
> + }
> + return -1;
> + }
>
> - return init_path(d, &new);
> + return 0;
> }
>
Should free(lock_path), or use stack to allocate the space.
Thanks,
Yuan
> #define OBJ_PATH "/obj/"
More information about the sheepdog
mailing list