]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/lxclock.h
Merge pull request #3204 from brauner/switch_to_spdx
[mirror_lxc.git] / src / lxc / lxclock.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #ifndef __LXC_LXCLOCK_H
4 #define __LXC_LXCLOCK_H
5
6 #include <fcntl.h>
7 #include <semaphore.h>
8 #include <string.h>
9 #include <sys/file.h>
10 #include <sys/stat.h>
11 #include <time.h>
12 #include <unistd.h>
13
14 #ifndef F_OFD_GETLK
15 #define F_OFD_GETLK 36
16 #endif
17
18 #ifndef F_OFD_SETLK
19 #define F_OFD_SETLK 37
20 #endif
21
22 #ifndef F_OFD_SETLKW
23 #define F_OFD_SETLKW 38
24 #endif
25
26 #define LXC_LOCK_ANON_SEM 1 /*!< Anonymous semaphore lock */
27 #define LXC_LOCK_FLOCK 2 /*!< flock(2) lock */
28
29 /* private */
30 /*!
31 * LXC Lock
32 */
33 struct lxc_lock {
34 short type; /*!< Lock type */
35
36 union {
37 sem_t *sem; /*!< Anonymous semaphore (LXC_LOCK_ANON_SEM) */
38 /*! LXC_LOCK_FLOCK details */
39 struct {
40 int fd; /*!< fd on which a lock is held (if not -1) */
41 char *fname; /*!< Name of lock */
42 } f;
43 } u; /*!< Container for lock type elements */
44 };
45
46 /*!
47 * \brief Create a new (unlocked) lock.
48 *
49 * \param lxcpath lxcpath lock should relate to.
50 * \param name Name for lock.
51 *
52 * \return Newly-allocated lxclock on success, \c NULL on failure.
53
54 * \note If \p name is not given, create an unnamed semaphore
55 * (used to protect against racing threads).
56 *
57 * \note Note that an unnamed sem was malloced by us and needs to be freed.
58 *
59 * \internal \ref sem is initialized to a value of \c 1.
60 * A 'sem_t *' which can be passed to \ref lxclock() and \ref lxcunlock()
61 * will be placed in \c l->u.sem.
62 *
63 * If \ref lxcpath and \ref name are given (both must be given if either is
64 * given) then a lockfile is created as \c /run/lxc/lock/$lxcpath/.$name if root,
65 * or \c $XDG_RUNTIME_DIR/lxc/lock/$lxcpath/.$name if non-root.
66 * The lock is used to protect the containers on-disk representation.
67 *
68 * \internal This function allocates the pathname for the given lock in memory
69 * such that it can be can quickly opened and locked by \ref lxclock().
70 * \c l->u.f.fname will contain the malloc'ed name (which must be
71 * freed when the container is freed), and \c u.f.fd = -1.
72 *
73 */
74 extern struct lxc_lock *lxc_newlock(const char *lxcpath, const char *name);
75
76 /*!
77 * \brief Take an existing lock.
78 *
79 * \param lock Lock to operate on.
80 * \param timeout Seconds to wait to take lock (\c 0 signifies an
81 * indefinite wait).
82 *
83 * \return \c 0 if lock obtained, \c -2 on failure to set timeout,
84 * or \c -1 on any other error (\c errno will be set by \c sem_wait(3)
85 * or \c fcntl(2)).
86 *
87 * \note \p timeout is (currently?) only supported for privlock, not
88 * for slock. Since currently there is not a single use of the timeout
89 * (except in the test case) I may remove the support for it in sem as
90 * well.
91 */
92 extern int lxclock(struct lxc_lock *lock, int timeout);
93
94 /*!
95 * \brief Unlock specified lock previously locked using \ref lxclock().
96 *
97 * \param lock \ref lxc_lock.
98 *
99 * \return \c 0 on success, \c -2 if provided lock was not already held,
100 * otherwise \c -1 with \c errno saved from \c fcntl(2) or sem_post function.
101 */
102 extern int lxcunlock(struct lxc_lock *lock);
103
104 /*!
105 * \brief Free a lock created by \ref lxc_newlock().
106 *
107 * \param lock Lock.
108 */
109 extern void lxc_putlock(struct lxc_lock *lock);
110
111 /*!
112 * \brief Lock the current process.
113 */
114 extern void process_lock(void);
115
116 /*!
117 * \brief Unlock the current process.
118 */
119 extern void process_unlock(void);
120
121 struct lxc_container;
122
123 /*!
124 * \brief Lock the containers memory.
125 *
126 * \param c Container.
127 *
128 * \return As for \ref lxclock().
129 */
130 extern int container_mem_lock(struct lxc_container *c);
131
132 /*!
133 * \brief Unlock the containers memory.
134 *
135 * \param c Container.
136 */
137 extern void container_mem_unlock(struct lxc_container *c);
138
139 /*!
140 * \brief Lock the containers disk data.
141 *
142 * \param c Container.
143 *
144 * \return \c 0 on success, or an \ref lxclock() error return
145 * values on error.
146 */
147 extern int container_disk_lock(struct lxc_container *c);
148
149 /*!
150 * \brief Unlock the containers disk data.
151 *
152 * \param c Container.
153 *
154 */
155 extern void container_disk_unlock(struct lxc_container *c);
156
157 #endif