3 * Copyright © 2012 Serge Hallyn <serge.hallyn@ubuntu.com>.
4 * Copyright © 2012 Canonical Ltd.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
31 #include <lxc/lxccontainer.h>
37 #ifdef MUTEX_DEBUGGING
41 #define MAX_STACKDEPTH 25
43 lxc_log_define(lxclock
, lxc
);
45 #ifdef MUTEX_DEBUGGING
46 static pthread_mutex_t thread_mutex
= PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
;
48 static inline void dump_stacktrace(void)
50 void *array
[MAX_STACKDEPTH
];
55 size
= backtrace(array
, MAX_STACKDEPTH
);
56 strings
= backtrace_symbols(array
, size
);
58 /* Using fprintf here as our logging module is not thread safe. */
59 fprintf(stderr
, "\tObtained %zu stack frames\n", size
);
61 for (i
= 0; i
< size
; i
++)
62 fprintf(stderr
, "\t\t%s\n", strings
[i
]);
67 static pthread_mutex_t thread_mutex
= PTHREAD_MUTEX_INITIALIZER
;
69 static inline void dump_stacktrace(void) {;}
72 static void lock_mutex(pthread_mutex_t
*l
)
76 ret
= pthread_mutex_lock(l
);
78 fprintf(stderr
, "%s - Failed acquire mutex", strerror(ret
));
84 static void unlock_mutex(pthread_mutex_t
*l
)
88 ret
= pthread_mutex_unlock(l
);
90 fprintf(stderr
, "%s - Failed to release mutex", strerror(ret
));
96 static char *lxclock_name(const char *p
, const char *n
)
104 * "/run" + "/lxc/lock/$lxcpath/$lxcname + '\0' if root
106 * $XDG_RUNTIME_DIR + "/lxc/lock/$lxcpath/$lxcname + '\0' if non-root
109 /* length of "/lxc/lock/" + $lxcpath + "/" + "." + $lxcname + '\0' */
110 len
= strlen("/lxc/lock/") + strlen(n
) + strlen(p
) + 3;
111 rundir
= get_rundir();
114 len
+= strlen(rundir
);
116 if ((dest
= malloc(len
)) == NULL
) {
121 ret
= snprintf(dest
, len
, "%s/lxc/lock/%s", rundir
, p
);
122 if (ret
< 0 || ret
>= len
) {
127 ret
= mkdir_p(dest
, 0755);
134 ret
= snprintf(dest
, len
, "%s/lxc/lock/%s/.%s", rundir
, p
, n
);
136 if (ret
< 0 || ret
>= len
) {
143 static sem_t
*lxc_new_unnamed_sem(void)
148 s
= malloc(sizeof(*s
));
151 ret
= sem_init(s
, 0, 1);
159 struct lxc_lock
*lxc_newlock(const char *lxcpath
, const char *name
)
163 l
= malloc(sizeof(*l
));
168 l
->type
= LXC_LOCK_ANON_SEM
;
169 l
->u
.sem
= lxc_new_unnamed_sem();
177 l
->type
= LXC_LOCK_FLOCK
;
178 l
->u
.f
.fname
= lxclock_name(lxcpath
, name
);
190 int lxclock(struct lxc_lock
*l
, int timeout
)
192 int ret
= -1, saved_errno
= errno
;
196 case LXC_LOCK_ANON_SEM
:
198 ret
= sem_wait(l
->u
.sem
);
203 if (clock_gettime(CLOCK_REALTIME
, &ts
) == -1) {
207 ts
.tv_sec
+= timeout
;
208 ret
= sem_timedwait(l
->u
.sem
, &ts
);
216 ERROR("Error: timeout not supported with flock");
220 ERROR("Error: filename not set for flock");
223 if (l
->u
.f
.fd
== -1) {
224 l
->u
.f
.fd
= open(l
->u
.f
.fname
, O_CREAT
| O_RDWR
| O_NOFOLLOW
| O_CLOEXEC
| O_NOCTTY
, S_IWUSR
| S_IRUSR
);
225 if (l
->u
.f
.fd
== -1) {
226 ERROR("Error opening %s", l
->u
.f
.fname
);
231 memset(&lk
, 0, sizeof(struct flock
));
233 lk
.l_whence
= SEEK_SET
;
234 ret
= fcntl(l
->u
.f
.fd
, F_OFD_SETLKW
, &lk
);
237 ret
= flock(l
->u
.f
.fd
, LOCK_EX
);
248 int lxcunlock(struct lxc_lock
*l
)
250 int ret
= 0, saved_errno
= errno
;
254 case LXC_LOCK_ANON_SEM
:
258 ret
= sem_post(l
->u
.sem
);
263 if (l
->u
.f
.fd
!= -1) {
264 memset(&lk
, 0, sizeof(struct flock
));
266 lk
.l_whence
= SEEK_SET
;
267 ret
= fcntl(l
->u
.f
.fd
, F_OFD_SETLK
, &lk
);
270 ret
= flock(l
->u
.f
.fd
, LOCK_EX
| LOCK_NB
);
285 * lxc_putlock() is only called when a container_new() fails,
286 * or during container_put(), which is already guaranteed to
287 * only be done by one task.
288 * So the only exclusion we need to provide here is for regular
289 * thread safety (i.e. file descriptor table changes).
291 void lxc_putlock(struct lxc_lock
*l
)
296 case LXC_LOCK_ANON_SEM
:
298 sem_destroy(l
->u
.sem
);
304 if (l
->u
.f
.fd
!= -1) {
315 void process_lock(void)
317 lock_mutex(&thread_mutex
);
320 void process_unlock(void)
322 unlock_mutex(&thread_mutex
);
325 int container_mem_lock(struct lxc_container
*c
)
327 return lxclock(c
->privlock
, 0);
330 void container_mem_unlock(struct lxc_container
*c
)
332 lxcunlock(c
->privlock
);
335 int container_disk_lock(struct lxc_container
*c
)
339 if ((ret
= lxclock(c
->privlock
, 0)))
341 if ((ret
= lxclock(c
->slock
, 0))) {
342 lxcunlock(c
->privlock
);
348 void container_disk_unlock(struct lxc_container
*c
)
351 lxcunlock(c
->privlock
);