]>
Commit | Line | Data |
---|---|---|
72d0e1cb SG |
1 | /* liblxcapi |
2 | * | |
3 | * Copyright © 2012 Serge Hallyn <serge.hallyn@ubuntu.com>. | |
4 | * Copyright © 2012 Canonical Ltd. | |
5 | * | |
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. | |
9 | * | |
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. | |
14 | * | |
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. | |
18 | */ | |
f2363e38 | 19 | #include "lxc/lxclock.h" |
061ba5d0 | 20 | #include "config.h" |
72d0e1cb SG |
21 | #include <unistd.h> |
22 | #include <signal.h> | |
23 | #include <stdio.h> | |
24 | #include <sys/types.h> | |
25 | #include <sys/wait.h> | |
26 | #include <stdlib.h> | |
27 | ||
28 | #define mycontainername "lxctest.sem" | |
29 | #define TIMEOUT_SECS 3 | |
30 | ||
0b98289e | 31 | static void test_two_locks(void) |
72d0e1cb | 32 | { |
df271a59 SH |
33 | struct lxc_lock *l; |
34 | pid_t pid; | |
35 | int ret, status; | |
36 | int p[2]; | |
37 | char c; | |
72d0e1cb | 38 | |
df271a59 | 39 | if (pipe(p) < 0) |
72d0e1cb | 40 | exit(1); |
3cc076dc | 41 | |
df271a59 SH |
42 | if ((pid = fork()) < 0) |
43 | exit(1); | |
3cc076dc | 44 | |
df271a59 SH |
45 | if (pid == 0) { |
46 | if (read(p[0], &c, 1) < 0) { | |
47 | perror("read"); | |
48 | exit(1); | |
49 | } | |
3cc076dc | 50 | |
df271a59 SH |
51 | l = lxc_newlock("/tmp", "lxctest-sem"); |
52 | if (!l) { | |
53 | fprintf(stderr, "%d: child: failed to create lock\n", __LINE__); | |
54 | exit(1); | |
55 | } | |
3cc076dc | 56 | |
df271a59 SH |
57 | if (lxclock(l, 0) < 0) { |
58 | fprintf(stderr, "%d: child: failed to grab lock\n", __LINE__); | |
59 | exit(1); | |
60 | } | |
3cc076dc | 61 | |
df271a59 SH |
62 | fprintf(stderr, "%d: child: grabbed lock\n", __LINE__); |
63 | exit(0); | |
72d0e1cb | 64 | } |
3cc076dc | 65 | |
df271a59 SH |
66 | l = lxc_newlock("/tmp", "lxctest-sem"); |
67 | if (!l) { | |
68 | fprintf(stderr, "%d: failed to create lock\n", __LINE__); | |
69 | exit(1); | |
72d0e1cb | 70 | } |
3cc076dc | 71 | |
df271a59 SH |
72 | if (lxclock(l, 0) < 0) { |
73 | fprintf(stderr, "%d; failed to get lock\n", __LINE__); | |
72d0e1cb SG |
74 | exit(1); |
75 | } | |
3cc076dc | 76 | |
9f3d75a0 | 77 | if (write(p[1], "a", 1) < 0) { |
df271a59 SH |
78 | perror("write"); |
79 | exit(1); | |
72d0e1cb | 80 | } |
3cc076dc | 81 | |
df271a59 | 82 | sleep(3); |
3cc076dc | 83 | |
df271a59 SH |
84 | ret = waitpid(pid, &status, WNOHANG); |
85 | if (ret == pid) { // task exited | |
86 | if (WIFEXITED(status)) { | |
87 | printf("%d exited normally with exit code %d\n", pid, | |
88 | WEXITSTATUS(status)); | |
59ecb672 | 89 | if (WEXITSTATUS(status) != 0) |
df271a59 SH |
90 | exit(1); |
91 | } else | |
92 | printf("%d did not exit normally\n", pid); | |
93 | return; | |
94 | } else if (ret < 0) { | |
95 | perror("waitpid"); | |
96 | exit(1); | |
72d0e1cb | 97 | } |
3cc076dc | 98 | |
df271a59 SH |
99 | kill(pid, SIGKILL); |
100 | wait(&status); | |
101 | close(p[1]); | |
102 | close(p[0]); | |
103 | lxcunlock(l); | |
104 | lxc_putlock(l); | |
72d0e1cb SG |
105 | } |
106 | ||
107 | int main(int argc, char *argv[]) | |
108 | { | |
df271a59 SH |
109 | int ret; |
110 | struct lxc_lock *lock; | |
72d0e1cb | 111 | |
df271a59 SH |
112 | lock = lxc_newlock(NULL, NULL); |
113 | if (!lock) { | |
72d0e1cb SG |
114 | fprintf(stderr, "%d: failed to get unnamed lock\n", __LINE__); |
115 | exit(1); | |
df271a59 | 116 | } |
3cc076dc | 117 | |
df271a59 SH |
118 | ret = lxclock(lock, 0); |
119 | if (ret) { | |
72d0e1cb SG |
120 | fprintf(stderr, "%d: failed to take unnamed lock (%d)\n", __LINE__, ret); |
121 | exit(1); | |
df271a59 | 122 | } |
72d0e1cb | 123 | |
df271a59 SH |
124 | ret = lxcunlock(lock); |
125 | if (ret) { | |
72d0e1cb SG |
126 | fprintf(stderr, "%d: failed to put unnamed lock (%d)\n", __LINE__, ret); |
127 | exit(1); | |
df271a59 SH |
128 | } |
129 | lxc_putlock(lock); | |
72d0e1cb | 130 | |
df271a59 | 131 | lock = lxc_newlock("/var/lib/lxc", mycontainername); |
72d0e1cb SG |
132 | if (!lock) { |
133 | fprintf(stderr, "%d: failed to get lock\n", __LINE__); | |
134 | exit(1); | |
135 | } | |
3cc076dc | 136 | |
df271a59 | 137 | struct stat sb; |
72cf81f6 | 138 | char *pathname = RUNTIME_PATH "/lxc/lock/var/lib/lxc/"; |
3cc076dc | 139 | |
df271a59 SH |
140 | ret = stat(pathname, &sb); |
141 | if (ret != 0) { | |
142 | fprintf(stderr, "%d: filename %s not created\n", __LINE__, | |
143 | pathname); | |
144 | exit(1); | |
72d0e1cb | 145 | } |
df271a59 | 146 | lxc_putlock(lock); |
72d0e1cb | 147 | |
df271a59 | 148 | test_two_locks(); |
72d0e1cb | 149 | |
df271a59 | 150 | fprintf(stderr, "all tests passed\n"); |
72d0e1cb | 151 | |
72d0e1cb SG |
152 | exit(ret); |
153 | } |