]> git.proxmox.com Git - mirror_lxc.git/blame - src/liblxc/lock.c
Added C++ compatibility, change to libtool, improve monitoring
[mirror_lxc.git] / src / liblxc / lock.c
CommitLineData
5e97c3fc 1/*
2 * lxc: linux Container library
3 *
4 * (C) Copyright IBM Corp. 2007, 2008
5 *
6 * Authors:
7 * Daniel Lezcano <dlezcano at fr.ibm.com>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
24#define _GNU_SOURCE
25#include <stdio.h>
26#undef _GNU_SOURCE
27#include <errno.h>
28#include <unistd.h>
29#include <stdlib.h>
30#include <sys/file.h>
31#include <lxc.h>
32
33int lxc_get_lock(const char *name)
34{
35 char *lock;
36 int fd, ret;
37
38 asprintf(&lock, LXCPATH "/%s", name);
39 fd = open(lock, O_RDONLY|O_DIRECTORY, S_IRUSR|S_IWUSR);
40 if (fd < 0) {
41 ret = -errno;
42 goto out;
43 }
44
c2cc9f0a 45 fcntl(fd, F_SETFD, FD_CLOEXEC);
46
5e97c3fc 47 if (flock(fd, LOCK_EX|LOCK_NB)) {
48 ret = errno == EWOULDBLOCK ? 0 : -errno;
49 close(fd);
50 goto out;
51 }
52
53 ret = fd;
54out:
55 free(lock);
56 return ret;
57}
58
59void lxc_put_lock(int lock)
60{
61 flock(lock, LOCK_UN);
62 close(lock);
63}