]> git.proxmox.com Git - mirror_lxc.git/blob - src/tests/console.c
Fix tests on Android
[mirror_lxc.git] / src / tests / console.c
1 /* liblxcapi
2 *
3 * Copyright © 2013 Oracle.
4 *
5 * Authors:
6 * Dwight Engen <dwight.engen@oracle.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2, as
10 * published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
22 #include <lxc/lxccontainer.h>
23
24 #include <errno.h>
25 #include <unistd.h>
26 #include <stdio.h>
27
28 #define TTYCNT 4
29 #define TTYCNT_STR "4"
30 #define TSTNAME "lxcconsoletest"
31 #define MAXCONSOLES 512
32
33 #define TSTERR(fmt, ...) do { \
34 fprintf(stderr, "%s:%d " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__); \
35 } while (0)
36
37 static void test_console_close_all(int ttyfd[MAXCONSOLES],
38 int masterfd[MAXCONSOLES])
39 {
40 int i;
41
42 for (i = 0; i < MAXCONSOLES; i++) {
43 if (masterfd[i] != -1) {
44 close(masterfd[i]);
45 masterfd[i] = -1;
46 }
47 if (ttyfd[i] != -1) {
48 close(ttyfd[i]);
49 ttyfd[i] = -1;
50 }
51 }
52 }
53
54 static int test_console_running_container(struct lxc_container *c)
55 {
56 int nrconsoles, i, ret = -1;
57 int ttynum [MAXCONSOLES];
58 int ttyfd [MAXCONSOLES];
59 int masterfd[MAXCONSOLES];
60
61 for (i = 0; i < MAXCONSOLES; i++)
62 ttynum[i] = ttyfd[i] = masterfd[i] = -1;
63
64 ttynum[0] = 1;
65 ret = c->console_getfd(c, &ttynum[0], &masterfd[0]);
66 if (ret < 0) {
67 TSTERR("console allocate failed");
68 goto err1;
69 }
70 ttyfd[0] = ret;
71 if (ttynum[0] != 1) {
72 TSTERR("console allocate got bad ttynum %d", ttynum[0]);
73 goto err2;
74 }
75
76 /* attempt to alloc same ttynum */
77 ret = c->console_getfd(c, &ttynum[0], &masterfd[1]);
78 if (ret != -1) {
79 TSTERR("console allocate should fail for allocated ttynum %d", ttynum[0]);
80 goto err2;
81 }
82 close(masterfd[0]); masterfd[0] = -1;
83 close(ttyfd[0]); ttyfd[0] = -1;
84
85 /* ensure we can allocate all consoles, we do this a few times to
86 * show that the closes are freeing up the allocated slots
87 */
88 for (i = 0; i < 10; i++) {
89 for (nrconsoles = 0; nrconsoles < MAXCONSOLES; nrconsoles++) {
90 ret = c->console_getfd(c, &ttynum[nrconsoles], &masterfd[nrconsoles]);
91 if (ret < 0)
92 break;
93 ttyfd[nrconsoles] = ret;
94 }
95 if (nrconsoles != TTYCNT) {
96 TSTERR("didn't allocate all consoles %d != %d", nrconsoles, TTYCNT);
97 goto err2;
98 }
99 test_console_close_all(ttyfd, masterfd);
100 }
101 ret = 0;
102
103 err2:
104 test_console_close_all(ttyfd, masterfd);
105 err1:
106 return ret;
107 }
108
109 /* test_container: test console function
110 *
111 * @lxcpath : the lxcpath in which to create the container
112 * @group : name of the container group or NULL for default "lxc"
113 * @name : name of the container
114 * @template : template to use when creating the container
115 */
116 static int test_console(const char *lxcpath,
117 const char *group, const char *name,
118 const char *template)
119 {
120 int ret;
121 struct lxc_container *c = NULL;
122
123 if (lxcpath) {
124 ret = mkdir(lxcpath, 0755);
125 if (ret < 0 && errno != EEXIST) {
126 TSTERR("failed to mkdir %s %s", lxcpath, strerror(errno));
127 goto out1;
128 }
129 }
130 ret = -1;
131
132 if ((c = lxc_container_new(name, lxcpath)) == NULL) {
133 TSTERR("instantiating container %s", name);
134 goto out1;
135 }
136 if (c->is_defined(c)) {
137 c->stop(c);
138 c->destroy(c);
139 c = lxc_container_new(name, lxcpath);
140 }
141 if (!c->createl(c, template, NULL, NULL, 0, NULL)) {
142 TSTERR("creating container %s", name);
143 goto out2;
144 }
145 c->load_config(c, NULL);
146 c->set_config_item(c, "lxc.tty", TTYCNT_STR);
147 c->save_config(c, NULL);
148 c->want_daemonize(c);
149 if (!c->startl(c, 0, NULL)) {
150 TSTERR("starting container %s", name);
151 goto out3;
152 }
153
154 ret = test_console_running_container(c);
155
156 c->stop(c);
157 out3:
158 c->destroy(c);
159 out2:
160 lxc_container_put(c);
161 out1:
162 return ret;
163 }
164
165 int main(int argc, char *argv[])
166 {
167 int ret;
168 ret = test_console(NULL, NULL, TSTNAME, "busybox");
169 if (ret < 0)
170 goto err1;
171
172 ret = test_console("/var/lib/lxctest2", NULL, TSTNAME, "busybox");
173 if (ret < 0)
174 goto err1;
175 printf("All tests passed\n");
176 err1:
177 return ret;
178 }