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