]>
Commit | Line | Data |
---|---|---|
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 | */ | |
19 | #include "lxc/lxclock.h" | |
20 | #include "config.h" | |
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 | ||
31 | static void test_two_locks(void) | |
32 | { | |
33 | struct lxc_lock *l; | |
34 | pid_t pid; | |
35 | int ret, status; | |
36 | int p[2]; | |
37 | char c; | |
38 | ||
39 | if (pipe(p) < 0) | |
40 | exit(EXIT_FAILURE); | |
41 | ||
42 | if ((pid = fork()) < 0) | |
43 | exit(EXIT_FAILURE); | |
44 | ||
45 | if (pid == 0) { | |
46 | if (read(p[0], &c, 1) < 0) { | |
47 | perror("read"); | |
48 | exit(EXIT_FAILURE); | |
49 | } | |
50 | ||
51 | l = lxc_newlock("/tmp", "lxctest-sem"); | |
52 | if (!l) { | |
53 | fprintf(stderr, "%d: child: failed to create lock\n", __LINE__); | |
54 | exit(EXIT_FAILURE); | |
55 | } | |
56 | ||
57 | if (lxclock(l, 0) < 0) { | |
58 | fprintf(stderr, "%d: child: failed to grab lock\n", __LINE__); | |
59 | exit(EXIT_FAILURE); | |
60 | } | |
61 | ||
62 | fprintf(stderr, "%d: child: grabbed lock\n", __LINE__); | |
63 | exit(EXIT_SUCCESS); | |
64 | } | |
65 | ||
66 | l = lxc_newlock("/tmp", "lxctest-sem"); | |
67 | if (!l) { | |
68 | fprintf(stderr, "%d: failed to create lock\n", __LINE__); | |
69 | exit(EXIT_FAILURE); | |
70 | } | |
71 | ||
72 | if (lxclock(l, 0) < 0) { | |
73 | fprintf(stderr, "%d; failed to get lock\n", __LINE__); | |
74 | exit(EXIT_FAILURE); | |
75 | } | |
76 | ||
77 | if (write(p[1], "a", 1) < 0) { | |
78 | perror("write"); | |
79 | exit(EXIT_FAILURE); | |
80 | } | |
81 | ||
82 | sleep(3); | |
83 | ||
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)); | |
89 | if (WEXITSTATUS(status) != 0) | |
90 | exit(EXIT_FAILURE); | |
91 | } else | |
92 | printf("%d did not exit normally\n", pid); | |
93 | return; | |
94 | } else if (ret < 0) { | |
95 | perror("waitpid"); | |
96 | exit(EXIT_FAILURE); | |
97 | } | |
98 | ||
99 | kill(pid, SIGKILL); | |
100 | wait(&status); | |
101 | close(p[1]); | |
102 | close(p[0]); | |
103 | lxcunlock(l); | |
104 | lxc_putlock(l); | |
105 | } | |
106 | ||
107 | int main(int argc, char *argv[]) | |
108 | { | |
109 | int ret; | |
110 | struct lxc_lock *lock; | |
111 | ||
112 | lock = lxc_newlock(NULL, NULL); | |
113 | if (!lock) { | |
114 | fprintf(stderr, "%d: failed to get unnamed lock\n", __LINE__); | |
115 | exit(EXIT_FAILURE); | |
116 | } | |
117 | ||
118 | ret = lxclock(lock, 0); | |
119 | if (ret) { | |
120 | fprintf(stderr, "%d: failed to take unnamed lock (%d)\n", __LINE__, ret); | |
121 | exit(EXIT_FAILURE); | |
122 | } | |
123 | ||
124 | ret = lxcunlock(lock); | |
125 | if (ret) { | |
126 | fprintf(stderr, "%d: failed to put unnamed lock (%d)\n", __LINE__, ret); | |
127 | exit(EXIT_FAILURE); | |
128 | } | |
129 | lxc_putlock(lock); | |
130 | ||
131 | lock = lxc_newlock("/var/lib/lxc", mycontainername); | |
132 | if (!lock) { | |
133 | fprintf(stderr, "%d: failed to get lock\n", __LINE__); | |
134 | exit(EXIT_FAILURE); | |
135 | } | |
136 | ||
137 | struct stat sb; | |
138 | char *pathname = RUNTIME_PATH "/lxc/lock/var/lib/lxc/"; | |
139 | ||
140 | ret = stat(pathname, &sb); | |
141 | if (ret != 0) { | |
142 | fprintf(stderr, "%d: filename %s not created\n", __LINE__, | |
143 | pathname); | |
144 | exit(EXIT_FAILURE); | |
145 | } | |
146 | lxc_putlock(lock); | |
147 | ||
148 | test_two_locks(); | |
149 | ||
150 | fprintf(stderr, "all tests passed\n"); | |
151 | ||
152 | exit(ret); | |
153 | } |