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