]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/lxclock.h
lxclock: Replace named sempahore with flock
[mirror_lxc.git] / src / lxc / lxclock.h
1 /* liblxcapi
2 *
3 * Copyright © 2012 Serge Hallyn <serge.hallyn@ubuntu.com>.
4 * Copyright © 2012 Canonical Ltd.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2, as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include <fcntl.h> /* For O_* constants */
21 #include <sys/stat.h> /* For mode constants */
22 #include <sys/file.h>
23 #include <semaphore.h>
24 #include <string.h>
25 #include <time.h>
26
27 #define LXC_LOCK_ANON_SEM 1
28 #define LXC_LOCK_FLOCK 2
29 struct lxc_lock {
30 short type;
31 union {
32 sem_t *sem; // an anonymous semaphore
33 struct {
34 int fd; // fd on which a lock is held (if not -1)
35 char *fname;
36 } f;
37 } u;
38 };
39
40 /*
41 * lxc_newlock: Create a new (unlocked) lock.
42 *
43 * if name is not given, create an unnamed semaphore. We use these
44 * to protect against racing threads.
45 * Note that an unnamed sem was malloced by us and needs to be freed.
46 *
47 * sem is initialized to value of 1
48 * A sem_t * which can be passed to lxclock() and lxcunlock()
49 * will be placed in l->u.sem
50 *
51 * If lxcpath and name are given (both must be given if either is
52 * given) then a lockfile is created, $lxcpath/$lxcname/locks/$name.
53 * We use that to protect the containers as represented on disk.
54 * lxc_newlock() for the named lock only allocates the pathname in
55 * memory so we can quickly open+lock it at lxclock.
56 * l->u.f.fname will container the malloc'ed name (which must be
57 * freed when the container is freed), and u.f.fd = -1.
58 *
59 * return lxclock on success, NULL on failure.
60 */
61 extern struct lxc_lock *lxc_newlock(const char *lxcpath, const char *name);
62
63 /*
64 * lxclock: take an existing lock. If timeout is 0, wait
65 * indefinately. Otherwise use given timeout.
66 * return 0 if we got the lock, -2 on failure to set timeout, or -1
67 * otherwise in which case errno will be set by sem_wait()).
68 *
69 * Note that timeout is (currently?) only supported for privlock, not
70 * for slock. Since currently there is not a single use of the timeout
71 * (except in the test case) I may remove the support for it in sem as
72 * well.
73 */
74 extern int lxclock(struct lxc_lock *lock, int timeout);
75
76 /*
77 * lxcunlock: unlock given sem. Return 0 on success, or -2 if we did not
78 * have the lock. Otherwise returns -1 with errno saved from flock
79 * or sem_post function.
80 */
81 extern int lxcunlock(struct lxc_lock *lock);
82
83 extern void lxc_putlock(struct lxc_lock *l);