]>
git.proxmox.com Git - mirror_lxc.git/blob - src/tests/locktests.c
3 * Copyright © 2012 Serge Hallyn <serge.hallyn@ubuntu.com>.
4 * Copyright © 2012 Canonical Ltd.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2, as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "lxc/lxclock.h"
24 #include <sys/types.h>
28 #define mycontainername "lxctest.sem"
29 #define TIMEOUT_SECS 3
31 static void test_two_locks(void)
41 if ((pid
= fork()) < 0)
44 if (read(p
[0], &c
, 1) < 0) {
48 l
= lxc_newlock("/tmp", "lxctest-sem");
50 fprintf(stderr
, "%d: child: failed to create lock\n", __LINE__
);
53 if (lxclock(l
, 0) < 0) {
54 fprintf(stderr
, "%d: child: failed to grab lock\n", __LINE__
);
57 fprintf(stderr
, "%d: child: grabbed lock\n", __LINE__
);
60 l
= lxc_newlock("/tmp", "lxctest-sem");
62 fprintf(stderr
, "%d: failed to create lock\n", __LINE__
);
65 if (lxclock(l
, 0) < 0) {
66 fprintf(stderr
, "%d; failed to get lock\n", __LINE__
);
69 if (write(p
[1], "a", 1) < 0) {
74 ret
= waitpid(pid
, &status
, WNOHANG
);
75 if (ret
== pid
) { // task exited
76 if (WIFEXITED(status
)) {
77 printf("%d exited normally with exit code %d\n", pid
,
79 if (WEXITSTATUS(status
) == 0)
82 printf("%d did not exit normally\n", pid
);
96 int main(int argc
, char *argv
[])
99 struct lxc_lock
*lock
;
101 lock
= lxc_newlock(NULL
, NULL
);
103 fprintf(stderr
, "%d: failed to get unnamed lock\n", __LINE__
);
106 ret
= lxclock(lock
, 0);
108 fprintf(stderr
, "%d: failed to take unnamed lock (%d)\n", __LINE__
, ret
);
112 ret
= lxcunlock(lock
);
114 fprintf(stderr
, "%d: failed to put unnamed lock (%d)\n", __LINE__
, ret
);
119 lock
= lxc_newlock("/var/lib/lxc", mycontainername
);
121 fprintf(stderr
, "%d: failed to get lock\n", __LINE__
);
125 char *pathname
= RUNTIME_PATH
"/lxc/lock/var/lib/lxc/";
126 ret
= stat(pathname
, &sb
);
128 fprintf(stderr
, "%d: filename %s not created\n", __LINE__
,
136 fprintf(stderr
, "all tests passed\n");