]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/lxclock.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #ifndef __LXC_LXCLOCK_H
4 #define __LXC_LXCLOCK_H
17 #define F_OFD_GETLK 36
21 #define F_OFD_SETLK 37
25 #define F_OFD_SETLKW 38
28 #define LXC_LOCK_ANON_SEM 1 /*!< Anonymous semaphore lock */
29 #define LXC_LOCK_FLOCK 2 /*!< flock(2) lock */
36 short type
; /*!< Lock type */
39 sem_t
*sem
; /*!< Anonymous semaphore (LXC_LOCK_ANON_SEM) */
40 /*! LXC_LOCK_FLOCK details */
42 int fd
; /*!< fd on which a lock is held (if not -1) */
43 char *fname
; /*!< Name of lock */
45 } u
; /*!< Container for lock type elements */
49 * \brief Create a new (unlocked) lock.
51 * \param lxcpath lxcpath lock should relate to.
52 * \param name Name for lock.
54 * \return Newly-allocated lxclock on success, \c NULL on failure.
56 * \note If \p name is not given, create an unnamed semaphore
57 * (used to protect against racing threads).
59 * \note Note that an unnamed sem was malloced by us and needs to be freed.
61 * \internal \ref sem is initialized to a value of \c 1.
62 * A 'sem_t *' which can be passed to \ref lxclock() and \ref lxcunlock()
63 * will be placed in \c l->u.sem.
65 * If \ref lxcpath and \ref name are given (both must be given if either is
66 * given) then a lockfile is created as \c /run/lxc/lock/$lxcpath/.$name if root,
67 * or \c $XDG_RUNTIME_DIR/lxc/lock/$lxcpath/.$name if non-root.
68 * The lock is used to protect the containers on-disk representation.
70 * \internal This function allocates the pathname for the given lock in memory
71 * such that it can be can quickly opened and locked by \ref lxclock().
72 * \c l->u.f.fname will contain the malloc'ed name (which must be
73 * freed when the container is freed), and \c u.f.fd = -1.
76 __hidden
extern struct lxc_lock
*lxc_newlock(const char *lxcpath
, const char *name
);
79 * \brief Take an existing lock.
81 * \param lock Lock to operate on.
82 * \param timeout Seconds to wait to take lock (\c 0 signifies an
85 * \return \c 0 if lock obtained, \c -2 on failure to set timeout,
86 * or \c -1 on any other error (\c errno will be set by \c sem_wait(3)
89 * \note \p timeout is (currently?) only supported for privlock, not
90 * for slock. Since currently there is not a single use of the timeout
91 * (except in the test case) I may remove the support for it in sem as
94 __hidden
extern int lxclock(struct lxc_lock
*lock
, int timeout
);
97 * \brief Unlock specified lock previously locked using \ref lxclock().
99 * \param lock \ref lxc_lock.
101 * \return \c 0 on success, \c -2 if provided lock was not already held,
102 * otherwise \c -1 with \c errno saved from \c fcntl(2) or sem_post function.
104 __hidden
extern int lxcunlock(struct lxc_lock
*lock
);
107 * \brief Free a lock created by \ref lxc_newlock().
111 __hidden
extern void lxc_putlock(struct lxc_lock
*lock
);
114 * \brief Lock the current process.
116 __hidden
extern void process_lock(void);
119 * \brief Unlock the current process.
121 __hidden
extern void process_unlock(void);
123 struct lxc_container
;
126 * \brief Lock the containers memory.
128 * \param c Container.
130 * \return As for \ref lxclock().
132 __hidden
extern int container_mem_lock(struct lxc_container
*c
);
135 * \brief Unlock the containers memory.
137 * \param c Container.
139 __hidden
extern void container_mem_unlock(struct lxc_container
*c
);
142 * \brief Lock the containers disk data.
144 * \param c Container.
146 * \return \c 0 on success, or an \ref lxclock() error return
149 __hidden
extern int container_disk_lock(struct lxc_container
*c
);
152 * \brief Unlock the containers disk data.
154 * \param c Container.
157 __hidden
extern void container_disk_unlock(struct lxc_container
*c
);