]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/lxclock.h
5 * Copyright © 2012 Serge Hallyn <serge.hallyn@ubuntu.com>.
6 * Copyright © 2012 Canonical Ltd.
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.
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.
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.
22 #include <fcntl.h> /* For O_* constants */
23 #include <sys/stat.h> /* For mode constants */
25 #include <semaphore.h>
29 #define LXC_LOCK_ANON_SEM 1
30 #define LXC_LOCK_FLOCK 2
34 sem_t
*sem
; // an anonymous semaphore
36 int fd
; // fd on which a lock is held (if not -1)
43 * lxc_newlock: Create a new (unlocked) lock.
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.
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
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.
61 * return lxclock on success, NULL on failure.
63 extern struct lxc_lock
*lxc_newlock(const char *lxcpath
, const char *name
);
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()).
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
76 extern int lxclock(struct lxc_lock
*lock
, int timeout
);
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.
83 extern int lxcunlock(struct lxc_lock
*lock
);
85 extern void lxc_putlock(struct lxc_lock
*l
);
87 extern int process_lock(void);
88 extern void process_unlock(void);
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
);