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