]>
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 | */ | |
e49c56d6 CB |
19 | |
20 | #include "config.h" | |
21 | ||
948955a2 | 22 | #include <lxc/lxccontainer.h> |
72d0e1cb SG |
23 | |
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 | #include <errno.h> | |
31 | ||
32 | #define MYNAME "lxctest1" | |
33 | ||
787c3ebe | 34 | static int create_container(void) |
72d0e1cb SG |
35 | { |
36 | int status, ret; | |
37 | pid_t pid = fork(); | |
38 | ||
39 | if (pid < 0) { | |
40 | perror("fork"); | |
41 | return -1; | |
42 | } | |
b4edca7c | 43 | |
72d0e1cb | 44 | if (pid == 0) { |
258611dd CB |
45 | execlp("lxc-create", "lxc-create", "-t", "busybox", "-n", MYNAME, NULL); |
46 | exit(EXIT_FAILURE); | |
72d0e1cb | 47 | } |
b4edca7c | 48 | |
72d0e1cb SG |
49 | again: |
50 | ret = waitpid(pid, &status, 0); | |
51 | if (ret == -1) { | |
21e624d9 | 52 | if (errno == EINTR) |
72d0e1cb | 53 | goto again; |
b4edca7c | 54 | |
72d0e1cb SG |
55 | perror("waitpid"); |
56 | return -1; | |
57 | } | |
b4edca7c | 58 | |
72d0e1cb SG |
59 | if (ret != pid) |
60 | goto again; | |
b4edca7c | 61 | |
72d0e1cb SG |
62 | if (!WIFEXITED(status)) { // did not exit normally |
63 | fprintf(stderr, "%d: lxc-create exited abnormally\n", __LINE__); | |
64 | return -1; | |
65 | } | |
b4edca7c | 66 | |
72d0e1cb SG |
67 | return WEXITSTATUS(status); |
68 | } | |
69 | ||
70 | int main(int argc, char *argv[]) | |
71 | { | |
72 | struct lxc_container *c; | |
73 | int ret = 1; | |
74 | ||
afeecbba | 75 | if ((c = lxc_container_new(MYNAME, NULL)) == NULL) { |
72d0e1cb SG |
76 | fprintf(stderr, "%d: error opening lxc_container %s\n", __LINE__, MYNAME); |
77 | ret = 1; | |
78 | goto out; | |
79 | } | |
80 | ||
81 | if (c->is_defined(c)) { | |
82 | fprintf(stderr, "%d: %s thought it was defined\n", __LINE__, MYNAME); | |
83 | goto out; | |
84 | } | |
85 | ||
787c3ebe SH |
86 | if (create_container()) { |
87 | fprintf(stderr, "%d: failed to create a container\n", __LINE__); | |
72d0e1cb SG |
88 | goto out; |
89 | } | |
90 | ||
91 | if (!c->is_defined(c)) { | |
92 | fprintf(stderr, "%d: %s thought it was not defined\n", __LINE__, MYNAME); | |
93 | goto out; | |
94 | } | |
95 | ||
96 | c->load_config(c, NULL); | |
97 | unlink("/tmp/lxctest1"); | |
98 | if (!c->save_config(c, "/tmp/lxctest1")) { | |
99 | fprintf(stderr, "%d: failed writing config file /tmp/lxctest1\n", __LINE__); | |
100 | goto out; | |
101 | } | |
b4edca7c | 102 | |
e29bf450 | 103 | rename(LXCPATH "/" MYNAME "/config", LXCPATH "/" MYNAME "/config.bak"); |
72d0e1cb SG |
104 | if (!c->save_config(c, NULL)) { |
105 | fprintf(stderr, "%d: failed writing config file\n", __LINE__); | |
106 | goto out; | |
107 | } | |
108 | ||
068b3d04 SG |
109 | if (!c->destroy(c)) { |
110 | fprintf(stderr, "%d: error deleting %s\n", __LINE__, MYNAME); | |
111 | goto out; | |
112 | } | |
113 | ||
114 | if (c->is_defined(c)) { | |
115 | fprintf(stderr, "%d: %s thought it was defined\n", __LINE__, MYNAME); | |
116 | goto out; | |
117 | } | |
118 | ||
72d0e1cb SG |
119 | fprintf(stderr, "all lxc_container tests passed for %s\n", c->name); |
120 | ret = 0; | |
b4edca7c | 121 | |
72d0e1cb SG |
122 | out: |
123 | lxc_container_put(c); | |
124 | exit(ret); | |
125 | } |