]>
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.
19 #define __STDC_FORMAT_MACROS
29 #include <sys/types.h>
32 #include <lxc/lxccontainer.h>
37 int main(int argc
, char *argv
[])
40 struct stat st_log_file
;
41 struct lxc_container
*c
;
42 struct lxc_console_log log
;
43 bool do_unlink
= false;
44 int fret
= EXIT_FAILURE
;
46 c
= lxc_container_new("console-log", NULL
);
48 lxc_error("%s", "Failed to create container \"console-log\"");
52 if (c
->is_defined(c
)) {
53 lxc_error("%s\n", "Container \"console-log\" is defined");
57 /* Set console ringbuffer size. */
58 if (!c
->set_config_item(c
, "lxc.console.buffer.size", "4096")) {
59 lxc_error("%s\n", "Failed to set config item \"lxc.console.buffer.size\"");
63 /* Set console log file. */
64 if (!c
->set_config_item(c
, "lxc.console.logfile", "/tmp/console-log.log")) {
65 lxc_error("%s\n", "Failed to set config item \"lxc.console.logfile\"");
69 if (!c
->createl(c
, "busybox", NULL
, NULL
, 0, NULL
)) {
70 lxc_error("%s\n", "Failed to create busybox container \"console-log\"");
74 if (!c
->is_defined(c
)) {
75 lxc_error("%s\n", "Container \"console-log\" is not defined");
81 if (!c
->load_config(c
, NULL
)) {
82 lxc_error("%s\n", "Failed to load config for container \"console-log\"");
86 if (!c
->want_daemonize(c
, true)) {
87 lxc_error("%s\n", "Failed to mark container \"console-log\" daemonized");
91 if (!c
->startl(c
, 0, NULL
)) {
92 lxc_error("%s\n", "Failed to start container \"console-log\" daemonized");
96 /* Leave some time for the container to write something to the log. */
99 /* Retrieve the contents of the ringbuffer. */
101 log
.read_max
= &(uint64_t){0};
104 ret
= c
->console_log(c
, &log
);
106 lxc_error("%s - Failed to retrieve console log \n", strerror(-ret
));
109 lxc_debug("Retrieved %" PRIu64
110 " bytes from console log. Contents are \"%s\"\n",
111 *log
.read_max
, log
.data
);
114 /* Leave another two seconds to ensure boot is finished. */
117 /* Clear the console ringbuffer. */
118 log
.read_max
= &(uint64_t){0};
121 ret
= c
->console_log(c
, &log
);
123 if (ret
!= -ENODATA
) {
124 lxc_error("%s - Failed to retrieve console log\n", strerror(-ret
));
130 lxc_error("%s\n", "Failed to stop container \"console-log\"");
136 if (!c
->load_config(c
, NULL
)) {
137 lxc_error("%s\n", "Failed to load config for container \"console-log\"");
141 if (!c
->startl(c
, 0, NULL
)) {
142 lxc_error("%s\n", "Failed to start container \"console-log\" daemonized");
143 goto on_error_destroy
;
146 /* Leave some time for the container to write something to the log. */
149 ret
= stat("/tmp/console-log.log", &st_log_file
);
151 lxc_error("%s - Failed to stat on-disk logfile\n", strerror(errno
));
155 /* Turn on rotation for the console log file. */
156 if (!c
->set_config_item(c
, "lxc.console.rotate", "1")) {
157 lxc_error("%s\n", "Failed to set config item \"lxc.console.rotate\"");
162 lxc_error("%s\n", "Failed to stop container \"console-log\"");
166 if (!c
->startl(c
, 0, NULL
)) {
167 lxc_error("%s\n", "Failed to start container \"console-log\" daemonized");
168 goto on_error_destroy
;
171 /* Leave some time for the container to write something to the log. */
177 if (c
->is_running(c
) && !c
->stop(c
))
178 lxc_error("%s\n", "Failed to stop container \"console-log\"");
182 lxc_error("%s\n", "Failed to destroy container \"console-log\"");
185 lxc_container_put(c
);
187 ret
= unlink("/tmp/console-log.log");
189 lxc_error("%s - Failed to remove container log file\n",
192 ret
= unlink("/tmp/console-log.log.1");
194 lxc_error("%s - Failed to remove container log file\n",