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
33 #include <lxc/lxccontainer.h>
40 #ifdef MUTEX_DEBUGGING
44 #define MAX_STACKDEPTH 25
46 lxc_log_define(lxclock
, lxc
);
48 #ifdef MUTEX_DEBUGGING
49 static pthread_mutex_t thread_mutex
= PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
;
51 static inline void dump_stacktrace(void)
53 void *array
[MAX_STACKDEPTH
];
58 size
= backtrace(array
, MAX_STACKDEPTH
);
59 strings
= backtrace_symbols(array
, size
);
61 /* Using fprintf here as our logging module is not thread safe. */
62 fprintf(stderr
, "\tObtained %zu stack frames\n", size
);
64 for (i
= 0; i
< size
; i
++)
65 fprintf(stderr
, "\t\t%s\n", strings
[i
]);
70 static pthread_mutex_t thread_mutex
= PTHREAD_MUTEX_INITIALIZER
;
72 static inline void dump_stacktrace(void) {;}
75 static void lock_mutex(pthread_mutex_t
*l
)
79 ret
= pthread_mutex_lock(l
);
81 SYSERROR("Failed to acquire mutex");
87 static void unlock_mutex(pthread_mutex_t
*l
)
91 ret
= pthread_mutex_unlock(l
);
93 SYSERROR("Failed to release mutex");
99 static char *lxclock_name(const char *p
, const char *n
)
106 * "/run" + "/lxc/lock/$lxcpath/$lxcname + '\0' if root
108 * $XDG_RUNTIME_DIR + "/lxc/lock/$lxcpath/$lxcname + '\0' if non-root
111 /* length of "/lxc/lock/" + $lxcpath + "/" + "." + $lxcname + '\0' */
112 len
= STRLITERALLEN("/lxc/lock/") + strlen(n
) + strlen(p
) + 3;
114 rundir
= get_rundir();
118 len
+= strlen(rundir
);
126 ret
= snprintf(dest
, len
, "%s/lxc/lock/%s", rundir
, p
);
127 if (ret
< 0 || (size_t)ret
>= len
) {
133 ret
= mkdir_p(dest
, 0755);
140 ret
= snprintf(dest
, len
, "%s/lxc/lock/%s/.%s", rundir
, p
, n
);
142 if (ret
< 0 || (size_t)ret
>= len
) {
150 static sem_t
*lxc_new_unnamed_sem(void)
155 s
= malloc(sizeof(*s
));
159 ret
= sem_init(s
, 0, 1);
168 struct lxc_lock
*lxc_newlock(const char *lxcpath
, const char *name
)
172 l
= malloc(sizeof(*l
));
177 l
->type
= LXC_LOCK_ANON_SEM
;
178 l
->u
.sem
= lxc_new_unnamed_sem();
187 l
->type
= LXC_LOCK_FLOCK
;
188 l
->u
.f
.fname
= lxclock_name(lxcpath
, name
);
201 int lxclock(struct lxc_lock
*l
, int timeout
)
204 int ret
= -1, saved_errno
= errno
;
207 case LXC_LOCK_ANON_SEM
:
209 ret
= sem_wait(l
->u
.sem
);
215 ret
= clock_gettime(CLOCK_REALTIME
, &ts
);
221 ts
.tv_sec
+= timeout
;
222 ret
= sem_timedwait(l
->u
.sem
, &ts
);
231 ERROR("Timeouts are not supported with file locks");
236 ERROR("No filename set for file lock");
240 if (l
->u
.f
.fd
== -1) {
241 l
->u
.f
.fd
= open(l
->u
.f
.fname
, O_CREAT
| O_RDWR
| O_NOFOLLOW
| O_CLOEXEC
| O_NOCTTY
, S_IWUSR
| S_IRUSR
);
242 if (l
->u
.f
.fd
== -1) {
243 SYSERROR("Failed to open \"%s\"", l
->u
.f
.fname
);
249 memset(&lk
, 0, sizeof(struct flock
));
252 lk
.l_whence
= SEEK_SET
;
254 ret
= fcntl(l
->u
.f
.fd
, F_OFD_SETLKW
, &lk
);
257 ret
= flock(l
->u
.f
.fd
, LOCK_EX
);
269 int lxcunlock(struct lxc_lock
*l
)
272 int ret
= 0, saved_errno
= errno
;
275 case LXC_LOCK_ANON_SEM
:
279 ret
= sem_post(l
->u
.sem
);
285 if (l
->u
.f
.fd
!= -1) {
286 memset(&lk
, 0, sizeof(struct flock
));
289 lk
.l_whence
= SEEK_SET
;
291 ret
= fcntl(l
->u
.f
.fd
, F_OFD_SETLK
, &lk
);
294 ret
= flock(l
->u
.f
.fd
, LOCK_EX
| LOCK_NB
);
312 * lxc_putlock() is only called when a container_new() fails,
313 * or during container_put(), which is already guaranteed to
314 * only be done by one task.
315 * So the only exclusion we need to provide here is for regular
316 * thread safety (i.e. file descriptor table changes).
318 void lxc_putlock(struct lxc_lock
*l
)
324 case LXC_LOCK_ANON_SEM
:
326 sem_destroy(l
->u
.sem
);
333 if (l
->u
.f
.fd
!= -1) {
347 void process_lock(void)
349 lock_mutex(&thread_mutex
);
352 void process_unlock(void)
354 unlock_mutex(&thread_mutex
);
357 int container_mem_lock(struct lxc_container
*c
)
359 return lxclock(c
->privlock
, 0);
362 void container_mem_unlock(struct lxc_container
*c
)
364 lxcunlock(c
->privlock
);
367 int container_disk_lock(struct lxc_container
*c
)
371 ret
= lxclock(c
->privlock
, 0);
375 ret
= lxclock(c
->slock
, 0);
377 lxcunlock(c
->privlock
);
384 void container_disk_unlock(struct lxc_container
*c
)
387 lxcunlock(c
->privlock
);