]> git.proxmox.com Git - mirror_lxc.git/blob - src/tests/console_log.c
Merge pull request #3800 from evverx/gh3796
[mirror_lxc.git] / src / tests / console_log.c
1 /* liblxcapi
2 *
3 * Copyright © 2017 Christian Brauner <christian.brauner@ubuntu.com>.
4 *
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.
8 *
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.
13 *
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.
17 */
18
19 #define __STDC_FORMAT_MACROS
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <inttypes.h>
23 #include <signal.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 #include <sys/wait.h>
31
32 #include <lxc/lxccontainer.h>
33
34 #include "lxctest.h"
35 #include "utils.h"
36
37 int main(int argc, char *argv[])
38 {
39 int ret;
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;
45
46 c = lxc_container_new("console-log", NULL);
47 if (!c) {
48 lxc_error("%s", "Failed to create container \"console-log\"");
49 exit(fret);
50 }
51
52 if (c->is_defined(c)) {
53 lxc_error("%s\n", "Container \"console-log\" is defined");
54 goto on_error_put;
55 }
56
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\"");
60 goto on_error_put;
61 }
62
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\"");
66 goto on_error_put;
67 }
68
69 if (!c->createl(c, "busybox", NULL, NULL, 0, NULL)) {
70 lxc_error("%s\n", "Failed to create busybox container \"console-log\"");
71 goto on_error_put;
72 }
73
74 if (!c->is_defined(c)) {
75 lxc_error("%s\n", "Container \"console-log\" is not defined");
76 goto on_error_put;
77 }
78
79 c->clear_config(c);
80
81 if (!c->load_config(c, NULL)) {
82 lxc_error("%s\n", "Failed to load config for container \"console-log\"");
83 goto on_error_stop;
84 }
85
86 if (!c->want_daemonize(c, true)) {
87 lxc_error("%s\n", "Failed to mark container \"console-log\" daemonized");
88 goto on_error_stop;
89 }
90
91 if (!c->startl(c, 0, NULL)) {
92 lxc_error("%s\n", "Failed to start container \"console-log\" daemonized");
93 goto on_error_stop;
94 }
95
96 /* Leave some time for the container to write something to the log. */
97 sleep(2);
98
99 /* Retrieve the contents of the ringbuffer. */
100 log.clear = false;
101 log.read_max = &(uint64_t){0};
102 log.read = true;
103
104 ret = c->console_log(c, &log);
105 if (ret < 0) {
106 lxc_error("%s - Failed to retrieve console log \n", strerror(-ret));
107 goto on_error_stop;
108 } else {
109 lxc_debug("Retrieved %" PRIu64
110 " bytes from console log. Contents are \"%s\"\n",
111 *log.read_max, log.data);
112 free(log.data);
113 }
114
115 /* Leave another two seconds to ensure boot is finished. */
116 sleep(2);
117
118 /* Clear the console ringbuffer. */
119 log.read_max = &(uint64_t){0};
120 log.read = false;
121 log.clear = true;
122 ret = c->console_log(c, &log);
123 if (ret < 0) {
124 if (ret != -ENODATA) {
125 lxc_error("%s - Failed to retrieve console log\n", strerror(-ret));
126 goto on_error_stop;
127 }
128 }
129
130 if (!c->stop(c)) {
131 lxc_error("%s\n", "Failed to stop container \"console-log\"");
132 goto on_error_stop;
133 }
134
135 c->clear_config(c);
136
137 if (!c->load_config(c, NULL)) {
138 lxc_error("%s\n", "Failed to load config for container \"console-log\"");
139 goto on_error_stop;
140 }
141
142 if (!c->startl(c, 0, NULL)) {
143 lxc_error("%s\n", "Failed to start container \"console-log\" daemonized");
144 goto on_error_destroy;
145 }
146
147 /* Leave some time for the container to write something to the log. */
148 sleep(2);
149
150 ret = stat("/tmp/console-log.log", &st_log_file);
151 if (ret < 0) {
152 lxc_error("%s - Failed to stat on-disk logfile\n", strerror(errno));
153 goto on_error_stop;
154 }
155
156 /* Turn on rotation for the console log file. */
157 if (!c->set_config_item(c, "lxc.console.rotate", "1")) {
158 lxc_error("%s\n", "Failed to set config item \"lxc.console.rotate\"");
159 goto on_error_put;
160 }
161
162 if (!c->stop(c)) {
163 lxc_error("%s\n", "Failed to stop container \"console-log\"");
164 goto on_error_stop;
165 }
166
167 if (!c->startl(c, 0, NULL)) {
168 lxc_error("%s\n", "Failed to start container \"console-log\" daemonized");
169 goto on_error_destroy;
170 }
171
172 /* Leave some time for the container to write something to the log. */
173 sleep(2);
174
175 fret = 0;
176
177 on_error_stop:
178 if (c->is_running(c) && !c->stop(c))
179 lxc_error("%s\n", "Failed to stop container \"console-log\"");
180
181 on_error_destroy:
182 if (!c->destroy(c))
183 lxc_error("%s\n", "Failed to destroy container \"console-log\"");
184
185 on_error_put:
186 lxc_container_put(c);
187 if (do_unlink) {
188 ret = unlink("/tmp/console-log.log");
189 if (ret < 0)
190 lxc_error("%s - Failed to remove container log file\n",
191 strerror(errno));
192
193 ret = unlink("/tmp/console-log.log.1");
194 if (ret < 0)
195 lxc_error("%s - Failed to remove container log file\n",
196 strerror(errno));
197 }
198 exit(fret);
199 }