]>
git.proxmox.com Git - mirror_lxc.git/blob - src/tests/console.c
3 * Copyright © 2013 Oracle.
6 * Dwight Engen <dwight.engen@oracle.com>
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.
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.
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.
22 #include <lxc/lxccontainer.h>
31 #define TTYCNT_STR "4"
32 #define TSTNAME "lxcconsoletest"
33 #define MAXCONSOLES 512
35 #define TSTERR(fmt, ...) do { \
36 fprintf(stderr, "%s:%d " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__); \
39 static void test_console_close_all(int ttyfd
[MAXCONSOLES
],
40 int ptxfd
[MAXCONSOLES
])
44 for (i
= 0; i
< MAXCONSOLES
; i
++) {
57 static int test_console_running_container(struct lxc_container
*c
)
59 int nrconsoles
, i
, ret
= -1;
60 int ttynum
[MAXCONSOLES
];
61 int ttyfd
[MAXCONSOLES
];
62 int ptxfd
[MAXCONSOLES
];
64 for (i
= 0; i
< MAXCONSOLES
; i
++)
65 ttynum
[i
] = ttyfd
[i
] = ptxfd
[i
] = -1;
69 ret
= c
->console_getfd(c
, &ttynum
[0], &ptxfd
[0]);
71 TSTERR("console allocate failed");
77 TSTERR("console allocate got bad ttynum %d", ttynum
[0]);
81 /* attempt to alloc same ttynum */
82 ret
= c
->console_getfd(c
, &ttynum
[0], &ptxfd
[1]);
84 TSTERR("console allocate should fail for allocated ttynum %d", ttynum
[0]);
87 close(ptxfd
[0]); ptxfd
[0] = -1;
88 close(ttyfd
[0]); ttyfd
[0] = -1;
90 /* ensure we can allocate all consoles, we do this a few times to
91 * show that the closes are freeing up the allocated slots
93 for (i
= 0; i
< 10; i
++) {
94 for (nrconsoles
= 0; nrconsoles
< MAXCONSOLES
; nrconsoles
++) {
95 ret
= c
->console_getfd(c
, &ttynum
[nrconsoles
], &ptxfd
[nrconsoles
]);
98 ttyfd
[nrconsoles
] = ret
;
101 if (nrconsoles
!= TTYCNT
) {
102 TSTERR("didn't allocate all consoles %d != %d", nrconsoles
, TTYCNT
);
106 test_console_close_all(ttyfd
, ptxfd
);
112 test_console_close_all(ttyfd
, ptxfd
);
118 /* test_container: test console function
120 * @lxcpath : the lxcpath in which to create the container
121 * @group : name of the container group or NULL for default "lxc"
122 * @name : name of the container
123 * @template : template to use when creating the container
125 static int test_console(const char *lxcpath
,
126 const char *group
, const char *name
,
127 const char *template)
130 struct lxc_container
*c
= NULL
;
133 ret
= mkdir(lxcpath
, 0755);
134 if (ret
< 0 && errno
!= EEXIST
) {
135 TSTERR("failed to mkdir %s %s", lxcpath
, strerror(errno
));
141 if ((c
= lxc_container_new(name
, lxcpath
)) == NULL
) {
142 TSTERR("instantiating container %s", name
);
146 if (c
->is_defined(c
)) {
149 c
= lxc_container_new(name
, lxcpath
);
152 if (!c
->createl(c
, template, NULL
, NULL
, 0, NULL
)) {
153 TSTERR("creating container %s", name
);
157 c
->load_config(c
, NULL
);
158 c
->set_config_item(c
, "lxc.tty.max", TTYCNT_STR
);
159 c
->set_config_item(c
, "lxc.pty.max", "1024");
160 c
->save_config(c
, NULL
);
161 c
->want_daemonize(c
, true);
163 if (!c
->startl(c
, 0, NULL
)) {
164 TSTERR("starting container %s", name
);
168 ret
= test_console_running_container(c
);
176 lxc_container_put(c
);
182 int main(int argc
, char *argv
[])
186 ret
= test_console(NULL
, NULL
, TSTNAME
, "busybox");
190 ret
= test_console("/var/lib/lxctest2", NULL
, TSTNAME
, "busybox");
194 printf("All tests passed\n");