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