]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/lxclock.h
commands: add lxc_cmd_state_server()
[mirror_lxc.git] / src / lxc / lxclock.h
1 /*! \file
2 *
3 * liblxcapi
4 *
5 * Copyright © 2012 Serge Hallyn <serge.hallyn@ubuntu.com>.
6 * Copyright © 2012 Canonical Ltd.
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #ifndef __LXC_LXCLOCK_H
24 #define __LXC_LXCLOCK_H
25
26 #include <fcntl.h> /* For O_* constants */
27 #include <sys/stat.h> /* For mode constants */
28 #include <sys/file.h>
29 #include <semaphore.h>
30 #include <string.h>
31 #include <time.h>
32
33 #define LXC_LOCK_ANON_SEM 1 /*!< Anonymous semaphore lock */
34 #define LXC_LOCK_FLOCK 2 /*!< flock(2) lock */
35
36 // private
37 /*!
38 * LXC Lock
39 */
40 struct lxc_lock {
41 short type; //!< Lock type
42
43 union {
44 sem_t *sem; //!< Anonymous semaphore (LXC_LOCK_ANON_SEM)
45 /*! LXC_LOCK_FLOCK details */
46 struct {
47 int fd; //!< fd on which a lock is held (if not -1)
48 char *fname; //!< Name of lock
49 } f;
50 } u; //!< Container for lock type elements
51 };
52
53 /*!
54 * \brief Create a new (unlocked) lock.
55 *
56 * \param lxcpath lxcpath lock should relate to.
57 * \param name Name for lock.
58 *
59 * \return Newly-allocated lxclock on success, \c NULL on failure.
60
61 * \note If \p name is not given, create an unnamed semaphore
62 * (used to protect against racing threads).
63 *
64 * \note Note that an unnamed sem was malloced by us and needs to be freed.
65 *
66 * \internal \ref sem is initialized to a value of \c 1.
67 * A 'sem_t *' which can be passed to \ref lxclock() and \ref lxcunlock()
68 * will be placed in \c l->u.sem.
69 *
70 * If \ref lxcpath and \ref name are given (both must be given if either is
71 * given) then a lockfile is created as \c $lxcpath/$lxcname/locks/$name.
72 * The lock is used to protect the containers on-disk representation.
73 *
74 * \internal This function allocates the pathname for the given lock in memory
75 * such that it can be can quickly opened and locked by \ref lxclock().
76 * \c l->u.f.fname will contain the malloc'ed name (which must be
77 * freed when the container is freed), and \c u.f.fd = -1.
78 *
79 */
80 extern struct lxc_lock *lxc_newlock(const char *lxcpath, const char *name);
81
82 /*!
83 * \brief Take an existing lock.
84 *
85 * \param lock Lock to operate on.
86 * \param timeout Seconds to wait to take lock (\c 0 signifies an
87 * indefinite wait).
88 *
89 * \return \c 0 if lock obtained, \c -2 on failure to set timeout,
90 * or \c -1 on any other error (\c errno will be set by \c sem_wait(3)).
91 *
92 * \note \p timeout is (currently?) only supported for privlock, not
93 * for slock. Since currently there is not a single use of the timeout
94 * (except in the test case) I may remove the support for it in sem as
95 * well.
96 */
97 extern int lxclock(struct lxc_lock *lock, int timeout);
98
99 /*!
100 * \brief Unlock specified lock previously locked using \ref lxclock().
101 *
102 * \param lock \ref lxc_lock.
103 *
104 * \return \c 0 on success, \c -2 if provided lock was not already held,
105 * otherwise \c -1 with \c errno saved from \c flock(2) or sem_post function.
106 */
107 extern int lxcunlock(struct lxc_lock *lock);
108
109 /*!
110 * \brief Free a lock created by \ref lxc_newlock().
111 *
112 * \param lock Lock.
113 */
114 extern void lxc_putlock(struct lxc_lock *lock);
115
116 /*!
117 * \brief Lock the current process.
118 */
119 extern void process_lock(void);
120
121 /*!
122 * \brief Unlock the current process.
123 */
124 extern void process_unlock(void);
125
126 struct lxc_container;
127
128 /*!
129 * \brief Lock the containers memory.
130 *
131 * \param c Container.
132 *
133 * \return As for \ref lxclock().
134 */
135 extern int container_mem_lock(struct lxc_container *c);
136
137 /*!
138 * \brief Unlock the containers memory.
139 *
140 * \param c Container.
141 */
142 extern void container_mem_unlock(struct lxc_container *c);
143
144 /*!
145 * \brief Lock the containers disk data.
146 *
147 * \param c Container.
148 *
149 * \return \c 0 on success, or an \ref lxclock() error return
150 * values on error.
151 */
152 extern int container_disk_lock(struct lxc_container *c);
153
154 /*!
155 * \brief Unlock the containers disk data.
156 */
157 extern void container_disk_unlock(struct lxc_container *c);
158
159 #endif