]> git.proxmox.com Git - mirror_lxc.git/blob - src/tests/locktests.c
Merge pull request #596 from lazy404/master
[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 #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(1);
41 if ((pid = fork()) < 0)
42 exit(1);
43 if (pid == 0) {
44 if (read(p[0], &c, 1) < 0) {
45 perror("read");
46 exit(1);
47 }
48 l = lxc_newlock("/tmp", "lxctest-sem");
49 if (!l) {
50 fprintf(stderr, "%d: child: failed to create lock\n", __LINE__);
51 exit(1);
52 }
53 if (lxclock(l, 0) < 0) {
54 fprintf(stderr, "%d: child: failed to grab lock\n", __LINE__);
55 exit(1);
56 }
57 fprintf(stderr, "%d: child: grabbed lock\n", __LINE__);
58 exit(0);
59 }
60 l = lxc_newlock("/tmp", "lxctest-sem");
61 if (!l) {
62 fprintf(stderr, "%d: failed to create lock\n", __LINE__);
63 exit(1);
64 }
65 if (lxclock(l, 0) < 0) {
66 fprintf(stderr, "%d; failed to get lock\n", __LINE__);
67 exit(1);
68 }
69 if (write(p[1], "a", 1) < 0) {
70 perror("write");
71 exit(1);
72 }
73 sleep(3);
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,
78 WEXITSTATUS(status));
79 if (WEXITSTATUS(status) == 0)
80 exit(1);
81 } else
82 printf("%d did not exit normally\n", pid);
83 return;
84 } else if (ret < 0) {
85 perror("waitpid");
86 exit(1);
87 }
88 kill(pid, SIGKILL);
89 wait(&status);
90 close(p[1]);
91 close(p[0]);
92 lxcunlock(l);
93 lxc_putlock(l);
94 }
95
96 int main(int argc, char *argv[])
97 {
98 int ret;
99 struct lxc_lock *lock;
100
101 lock = lxc_newlock(NULL, NULL);
102 if (!lock) {
103 fprintf(stderr, "%d: failed to get unnamed lock\n", __LINE__);
104 exit(1);
105 }
106 ret = lxclock(lock, 0);
107 if (ret) {
108 fprintf(stderr, "%d: failed to take unnamed lock (%d)\n", __LINE__, ret);
109 exit(1);
110 }
111
112 ret = lxcunlock(lock);
113 if (ret) {
114 fprintf(stderr, "%d: failed to put unnamed lock (%d)\n", __LINE__, ret);
115 exit(1);
116 }
117 lxc_putlock(lock);
118
119 lock = lxc_newlock("/var/lib/lxc", mycontainername);
120 if (!lock) {
121 fprintf(stderr, "%d: failed to get lock\n", __LINE__);
122 exit(1);
123 }
124 struct stat sb;
125 char *pathname = RUNTIME_PATH "/lxc/lock/var/lib/lxc/";
126 ret = stat(pathname, &sb);
127 if (ret != 0) {
128 fprintf(stderr, "%d: filename %s not created\n", __LINE__,
129 pathname);
130 exit(1);
131 }
132 lxc_putlock(lock);
133
134 test_two_locks();
135
136 fprintf(stderr, "all tests passed\n");
137
138 exit(ret);
139 }