]> git.proxmox.com Git - mirror_lxc.git/blob - src/tests/shutdowntest.c
Merge pull request #4028 from brauner/2021-11-02.fixes
[mirror_lxc.git] / src / tests / shutdowntest.c
1
2 /* liblxcapi
3 *
4 * Copyright © 2012 Serge Hallyn <serge.hallyn@ubuntu.com>.
5 * Copyright © 2012 Canonical Ltd.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2, as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "config.h"
22
23 #include <lxc/lxccontainer.h>
24
25 #include <unistd.h>
26 #include <signal.h>
27 #include <stdio.h>
28 #include <sys/types.h>
29 #include <sys/wait.h>
30 #include <stdlib.h>
31 #include <errno.h>
32
33 #define MYNAME "lxctest1"
34
35 int main(int argc, char *argv[])
36 {
37 struct lxc_container *c;
38 int ret = 1;
39
40 if ((c = lxc_container_new(MYNAME, NULL)) == NULL) {
41 fprintf(stderr, "%d: error opening lxc_container %s\n", __LINE__, MYNAME);
42 ret = 1;
43 goto out;
44 }
45
46 if (c->is_defined(c)) {
47 fprintf(stderr, "%d: %s thought it was defined\n", __LINE__, MYNAME);
48 goto out;
49 }
50
51 if (!c->set_config_item(c, "lxc.net.0.type", "veth")) {
52 fprintf(stderr, "%d: failed to set network type\n", __LINE__);
53 goto out;
54 }
55
56 c->set_config_item(c, "lxc.net.0.link", "lxcbr0");
57 c->set_config_item(c, "lxc.net.0.flags", "up");
58
59 if (!c->createl(c, "busybox", NULL, NULL, 0, NULL)) {
60 fprintf(stderr, "%d: failed to create a container\n", __LINE__);
61 goto out;
62 }
63
64 if (!c->is_defined(c)) {
65 fprintf(stderr, "%d: %s thought it was not defined\n", __LINE__, MYNAME);
66 goto out;
67 }
68
69 c->clear_config(c);
70 c->load_config(c, NULL);
71 c->want_daemonize(c, true);
72
73 if (!c->startl(c, 0, NULL)) {
74 fprintf(stderr, "%d: failed to start %s\n", __LINE__, MYNAME);
75 goto out;
76 }
77
78 /* Wait for init to be ready for SIGPWR */
79 sleep(20);
80
81 if (!c->shutdown(c, 120)) {
82 fprintf(stderr, "%d: failed to shut down %s\n", __LINE__, MYNAME);
83 if (!c->stop(c))
84 fprintf(stderr, "%d: failed to kill %s\n", __LINE__, MYNAME);
85
86 goto out;
87 }
88
89 if (!c->destroy(c)) {
90 fprintf(stderr, "%d: error deleting %s\n", __LINE__, MYNAME);
91 goto out;
92 }
93
94 if (c->is_defined(c)) {
95 fprintf(stderr, "%d: %s thought it was defined\n", __LINE__, MYNAME);
96 goto out;
97 }
98
99 fprintf(stderr, "all lxc_container tests passed for %s\n", c->name);
100 ret = 0;
101
102 out:
103 if (c && c->is_defined(c))
104 c->destroy(c);
105
106 lxc_container_put(c);
107 exit(ret);
108 }