]>
git.proxmox.com Git - mirror_lxc.git/blob - src/tests/console_log.c
3 * Copyright © 2017 Christian Brauner <christian.brauner@ubuntu.com>.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2, as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30 #include <sys/types.h>
33 #include <lxc/lxccontainer.h>
38 int main(int argc
, char *argv
[])
41 struct stat st_log_file
;
42 struct lxc_container
*c
;
43 struct lxc_console_log log
;
44 bool do_unlink
= false;
45 int fret
= EXIT_FAILURE
;
47 c
= lxc_container_new("console-log", NULL
);
49 lxc_error("%s", "Failed to create container \"console-log\"");
53 if (c
->is_defined(c
)) {
54 lxc_error("%s\n", "Container \"console-log\" is defined");
58 /* Set console ringbuffer size. */
59 if (!c
->set_config_item(c
, "lxc.console.buffer.size", "4096")) {
60 lxc_error("%s\n", "Failed to set config item \"lxc.console.buffer.size\"");
64 /* Set console log file. */
65 if (!c
->set_config_item(c
, "lxc.console.logfile", "/tmp/console-log.log")) {
66 lxc_error("%s\n", "Failed to set config item \"lxc.console.logfile\"");
70 if (!c
->createl(c
, "busybox", NULL
, NULL
, 0, NULL
)) {
71 lxc_error("%s\n", "Failed to create busybox container \"console-log\"");
75 if (!c
->is_defined(c
)) {
76 lxc_error("%s\n", "Container \"console-log\" is not defined");
82 if (!c
->load_config(c
, NULL
)) {
83 lxc_error("%s\n", "Failed to load config for container \"console-log\"");
87 if (!c
->want_daemonize(c
, true)) {
88 lxc_error("%s\n", "Failed to mark container \"console-log\" daemonized");
92 if (!c
->startl(c
, 0, NULL
)) {
93 lxc_error("%s\n", "Failed to start container \"console-log\" daemonized");
97 /* Leave some time for the container to write something to the log. */
100 /* Retrieve the contents of the ringbuffer. */
102 log
.read_max
= &(uint64_t){0};
105 ret
= c
->console_log(c
, &log
);
107 lxc_error("%s - Failed to retrieve console log \n", strerror(-ret
));
110 lxc_debug("Retrieved %" PRIu64
111 " bytes from console log. Contents are \"%s\"\n",
112 *log
.read_max
, log
.data
);
116 /* Leave another two seconds to ensure boot is finished. */
119 /* Clear the console ringbuffer. */
120 log
.read_max
= &(uint64_t){0};
123 ret
= c
->console_log(c
, &log
);
125 if (ret
!= -ENODATA
) {
126 lxc_error("%s - Failed to retrieve console log\n", strerror(-ret
));
132 lxc_error("%s\n", "Failed to stop container \"console-log\"");
138 if (!c
->load_config(c
, NULL
)) {
139 lxc_error("%s\n", "Failed to load config for container \"console-log\"");
143 if (!c
->startl(c
, 0, NULL
)) {
144 lxc_error("%s\n", "Failed to start container \"console-log\" daemonized");
145 goto on_error_destroy
;
148 /* Leave some time for the container to write something to the log. */
151 ret
= stat("/tmp/console-log.log", &st_log_file
);
153 lxc_error("%s - Failed to stat on-disk logfile\n", strerror(errno
));
157 /* Turn on rotation for the console log file. */
158 if (!c
->set_config_item(c
, "lxc.console.rotate", "1")) {
159 lxc_error("%s\n", "Failed to set config item \"lxc.console.rotate\"");
164 lxc_error("%s\n", "Failed to stop container \"console-log\"");
168 if (!c
->startl(c
, 0, NULL
)) {
169 lxc_error("%s\n", "Failed to start container \"console-log\" daemonized");
170 goto on_error_destroy
;
173 /* Leave some time for the container to write something to the log. */
179 if (c
->is_running(c
) && !c
->stop(c
))
180 lxc_error("%s\n", "Failed to stop container \"console-log\"");
184 lxc_error("%s\n", "Failed to destroy container \"console-log\"");
187 lxc_container_put(c
);
189 ret
= unlink("/tmp/console-log.log");
191 lxc_error("%s - Failed to remove container log file\n",
194 ret
= unlink("/tmp/console-log.log.1");
196 lxc_error("%s - Failed to remove container log file\n",