]>
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>
29 #define TTYCNT_STR "4"
30 #define TSTNAME "lxcconsoletest"
31 #define MAXCONSOLES 512
33 #define TSTERR(fmt, ...) do { \
34 fprintf(stderr, "%s:%d " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__); \
37 static void test_console_close_all(int ttyfd
[MAXCONSOLES
],
38 int masterfd
[MAXCONSOLES
])
42 for (i
= 0; i
< MAXCONSOLES
; i
++) {
43 if (masterfd
[i
] != -1) {
54 static int test_console_running_container(struct lxc_container
*c
)
56 int nrconsoles
, i
, ret
= -1;
57 int ttynum
[MAXCONSOLES
];
58 int ttyfd
[MAXCONSOLES
];
59 int masterfd
[MAXCONSOLES
];
61 for (i
= 0; i
< MAXCONSOLES
; i
++)
62 ttynum
[i
] = ttyfd
[i
] = masterfd
[i
] = -1;
65 ret
= c
->console_getfd(c
, &ttynum
[0], &masterfd
[0]);
67 TSTERR("console allocate failed");
72 TSTERR("console allocate got bad ttynum %d", ttynum
[0]);
76 /* attempt to alloc same ttynum */
77 ret
= c
->console_getfd(c
, &ttynum
[0], &masterfd
[1]);
79 TSTERR("console allocate should fail for allocated ttynum %d", ttynum
[0]);
82 close(masterfd
[0]); masterfd
[0] = -1;
83 close(ttyfd
[0]); ttyfd
[0] = -1;
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
88 for (i
= 0; i
< 10; i
++) {
89 for (nrconsoles
= 0; nrconsoles
< MAXCONSOLES
; nrconsoles
++) {
90 ret
= c
->console_getfd(c
, &ttynum
[nrconsoles
], &masterfd
[nrconsoles
]);
93 ttyfd
[nrconsoles
] = ret
;
95 if (nrconsoles
!= TTYCNT
) {
96 TSTERR("didn't allocate all consoles %d != %d", nrconsoles
, TTYCNT
);
99 test_console_close_all(ttyfd
, masterfd
);
104 test_console_close_all(ttyfd
, masterfd
);
109 /* test_container: test console function
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
116 static int test_console(const char *lxcpath
,
117 const char *group
, const char *name
,
118 const char *template)
121 struct lxc_container
*c
= NULL
;
124 ret
= mkdir(lxcpath
, 0755);
125 if (ret
< 0 && errno
!= EEXIST
) {
126 TSTERR("failed to mkdir %s %s", lxcpath
, strerror(errno
));
132 if ((c
= lxc_container_new(name
, lxcpath
)) == NULL
) {
133 TSTERR("instantiating container %s", name
);
136 if (c
->is_defined(c
)) {
139 c
= lxc_container_new(name
, lxcpath
);
141 if (!c
->createl(c
, template, NULL
, NULL
, 0, NULL
)) {
142 TSTERR("creating container %s", name
);
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
);
154 ret
= test_console_running_container(c
);
160 lxc_container_put(c
);
165 int main(int argc
, char *argv
[])
168 ret
= test_console(NULL
, NULL
, TSTNAME
, "busybox");
172 ret
= test_console("/var/lib/lxctest2", NULL
, TSTNAME
, "busybox");
175 printf("All tests passed\n");