]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/log.c
log.c: always use dir when lxcpath is not default
[mirror_lxc.git] / src / lxc / log.c
1 /*
2 * lxc: linux Container library
3 *
4 * (C) Copyright IBM Corp. 2007, 2008
5 *
6 * Authors:
7 * Cedric Le Goater <legoater@free.fr>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23 #include <stdio.h>
24 #include <errno.h>
25 #include <limits.h>
26 #include <unistd.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <string.h>
30
31 #define __USE_GNU /* for *_CLOEXEC */
32
33 #include <fcntl.h>
34 #include <stdlib.h>
35
36 #include "log.h"
37 #include "caps.h"
38 #include "utils.h"
39
40 #define LXC_LOG_PREFIX_SIZE 32
41 #define LXC_LOG_BUFFER_SIZE 512
42
43 int lxc_log_fd = -1;
44 static char log_prefix[LXC_LOG_PREFIX_SIZE] = "lxc";
45 static char *log_fname = NULL;
46 /* command line values for logfile or logpriority should always override
47 * values from the configuration file or defaults
48 */
49 static int lxc_logfile_specified = 0;
50 static int lxc_loglevel_specified = 0;
51
52 lxc_log_define(lxc_log, lxc);
53
54 /*---------------------------------------------------------------------------*/
55 static int log_append_stderr(const struct lxc_log_appender *appender,
56 struct lxc_log_event *event)
57 {
58 if (event->priority < LXC_LOG_PRIORITY_ERROR)
59 return 0;
60
61 fprintf(stderr, "%s: ", log_prefix);
62 vfprintf(stderr, event->fmt, *event->vap);
63 fprintf(stderr, "\n");
64 return 0;
65 }
66
67 /*---------------------------------------------------------------------------*/
68 static int log_append_logfile(const struct lxc_log_appender *appender,
69 struct lxc_log_event *event)
70 {
71 char buffer[LXC_LOG_BUFFER_SIZE];
72 int n;
73
74 if (lxc_log_fd == -1)
75 return 0;
76
77 n = snprintf(buffer, sizeof(buffer),
78 "%15s %10ld.%03ld %-8s %s - ",
79 log_prefix,
80 event->timestamp.tv_sec,
81 event->timestamp.tv_usec / 1000,
82 lxc_log_priority_to_string(event->priority),
83 event->category);
84
85 n += vsnprintf(buffer + n, sizeof(buffer) - n, event->fmt,
86 *event->vap);
87
88 if (n >= sizeof(buffer) - 1) {
89 WARN("truncated next event from %d to %zd bytes", n,
90 sizeof(buffer));
91 n = sizeof(buffer) - 1;
92 }
93
94 buffer[n] = '\n';
95
96 return write(lxc_log_fd, buffer, n + 1);
97 }
98
99 static struct lxc_log_appender log_appender_stderr = {
100 .name = "stderr",
101 .append = log_append_stderr,
102 .next = NULL,
103 };
104
105 static struct lxc_log_appender log_appender_logfile = {
106 .name = "logfile",
107 .append = log_append_logfile,
108 .next = NULL,
109 };
110
111 static struct lxc_log_category log_root = {
112 .name = "root",
113 .priority = LXC_LOG_PRIORITY_ERROR,
114 .appender = NULL,
115 .parent = NULL,
116 };
117
118 struct lxc_log_category lxc_log_category_lxc = {
119 .name = "lxc",
120 .priority = LXC_LOG_PRIORITY_ERROR,
121 .appender = &log_appender_stderr,
122 .parent = &log_root
123 };
124
125 /*---------------------------------------------------------------------------*/
126 static int build_dir(const char *name)
127 {
128 char *n = strdup(name); // because we'll be modifying it
129 char *p, *e;
130 int ret;
131
132 if (!n) {
133 ERROR("Out of memory while creating directory '%s'.", name);
134 return -1;
135 }
136
137 e = &n[strlen(n)];
138 for (p = n+1; p < e; p++) {
139 if (*p != '/')
140 continue;
141 *p = '\0';
142 if (access(n, F_OK)) {
143 ret = lxc_unpriv(mkdir(n, 0755));
144 if (ret && errno != -EEXIST) {
145 SYSERROR("failed to create directory '%s'.", n);
146 free(n);
147 return -1;
148 }
149 }
150 *p = '/';
151 }
152 free(n);
153 return 0;
154 }
155
156 /*---------------------------------------------------------------------------*/
157 static int log_open(const char *name)
158 {
159 int fd;
160 int newfd;
161
162 fd = lxc_unpriv(open(name, O_CREAT | O_WRONLY |
163 O_APPEND | O_CLOEXEC, 0666));
164 if (fd == -1) {
165 ERROR("failed to open log file \"%s\" : %s", name,
166 strerror(errno));
167 return -1;
168 }
169
170 if (fd > 2)
171 return fd;
172
173 newfd = fcntl(fd, F_DUPFD_CLOEXEC, 3);
174 if (newfd == -1)
175 ERROR("failed to dup log fd %d : %s", fd, strerror(errno));
176
177 close(fd);
178 return newfd;
179 }
180
181 /*
182 * Build the path to the log file
183 * @name : the name of the container
184 * @lxcpath : the lxcpath to use as a basename or NULL to use LOGPATH
185 * Returns malloced path on sucess, or NULL on failure
186 */
187 static char *build_log_path(const char *name, const char *lxcpath)
188 {
189 char *p;
190 int len, ret, use_dir;
191
192 #if USE_CONFIGPATH_LOGS
193 use_dir = 1;
194 #else
195 use_dir = 0;
196 #endif
197
198 /*
199 * If USE_CONFIGPATH_LOGS is true or lxcpath is given, the resulting
200 * path will be:
201 * '$logpath' + '/' + '$name' + '/' + '$name' + '.log' + '\0'
202 *
203 * If USE_CONFIGPATH_LOGS is false the resulting path will be:
204 * '$logpath' + '/' + '$name' + '.log' + '\0'
205 */
206 len = strlen(name) + 6; /* 6 == '/' + '.log' + '\0' */
207 if (lxcpath)
208 use_dir = 1;
209 else
210 lxcpath = LOGPATH;
211
212 if (use_dir)
213 len += strlen(lxcpath) + 1 + strlen(name) + 1; /* add "/$container_name/" */
214 else
215 len += strlen(lxcpath) + 1;
216 p = malloc(len);
217 if (!p)
218 return p;
219
220 if (use_dir)
221 ret = snprintf(p, len, "%s/%s/%s.log", lxcpath, name, name);
222 else
223 ret = snprintf(p, len, "%s/%s.log", lxcpath, name);
224
225 if (ret < 0 || ret >= len) {
226 free(p);
227 return NULL;
228 }
229 return p;
230 }
231
232 /*
233 * This can be called:
234 * 1. when a program calls lxc_log_init with no logfile parameter (in which
235 * case the default is used). In this case lxc.logfile can override this.
236 * 2. when a program calls lxc_log_init with a logfile parameter. In this
237 * case we don't want lxc.logfile to override this.
238 * 3. When a lxc.logfile entry is found in config file.
239 */
240 static int __lxc_log_set_file(const char *fname, int create_dirs)
241 {
242 if (lxc_log_fd != -1) {
243 // we are overriding the default.
244 close(lxc_log_fd);
245 free(log_fname);
246 }
247
248 #if USE_CONFIGPATH_LOGS
249 // we don't build_dir for the default if the default is
250 // i.e. /var/lib/lxc/$container/$container.log
251 if (create_dirs)
252 #endif
253 if (build_dir(fname)) {
254 ERROR("failed to create dir for log file \"%s\" : %s", fname,
255 strerror(errno));
256 return -1;
257 }
258
259 lxc_log_fd = log_open(fname);
260 if (lxc_log_fd == -1)
261 return -1;
262
263 log_fname = strdup(fname);
264 return 0;
265 }
266
267 static int _lxc_log_set_file(const char *name, const char *lxcpath, int create_dirs)
268 {
269 char *logfile;
270 int ret;
271
272 logfile = build_log_path(name, lxcpath);
273 if (!logfile) {
274 ERROR("could not build log path");
275 return -1;
276 }
277 ret = __lxc_log_set_file(logfile, create_dirs);
278 free(logfile);
279 return ret;
280 }
281
282 extern int lxc_log_init(const char *name, const char *file,
283 const char *priority, const char *prefix, int quiet,
284 const char *lxcpath)
285 {
286 int lxc_priority = LXC_LOG_PRIORITY_ERROR;
287 int ret;
288
289 if (lxc_log_fd != -1) {
290 WARN("lxc_log_init called with log already initialized");
291 return 0;
292 }
293
294 if (priority) {
295 lxc_loglevel_specified = 1;
296 lxc_priority = lxc_log_priority_to_int(priority);
297
298 if (lxc_priority == LXC_LOG_PRIORITY_NOTSET) {
299 ERROR("invalid log priority %s", priority);
300 return -1;
301 }
302 }
303
304 lxc_log_category_lxc.priority = lxc_priority;
305 lxc_log_category_lxc.appender = &log_appender_logfile;
306
307 if (!quiet)
308 lxc_log_category_lxc.appender->next = &log_appender_stderr;
309
310 if (prefix)
311 lxc_log_set_prefix(prefix);
312
313 if (file) {
314 lxc_logfile_specified = 1;
315 if (strcmp(file, "none") == 0)
316 return 0;
317 ret = __lxc_log_set_file(file, 1);
318 } else {
319 ret = -1;
320
321 /* try LOGPATH if lxcpath is the default */
322 if (strcmp(lxcpath, default_lxc_path()) == 0)
323 ret = _lxc_log_set_file(name, NULL, 0);
324
325 /* try in lxcpath */
326 if (ret < 0)
327 ret = _lxc_log_set_file(name, lxcpath, 1);
328
329 /* try LOGPATH in case its writable by the caller */
330 if (ret < 0)
331 ret = _lxc_log_set_file(name, NULL, 0);
332 }
333
334 /*
335 * If !file, that is, if the user did not request this logpath, then
336 * ignore failures and continue logging to console
337 */
338 if (!file && ret != 0) {
339 INFO("Ignoring failure to open default logfile.");
340 ret = 0;
341 }
342
343 return ret;
344 }
345
346 /*
347 * This is called when we read a lxc.loglevel entry in a lxc.conf file. This
348 * happens after processing command line arguments, which override the .conf
349 * settings. So only set the level if previously unset.
350 */
351 extern int lxc_log_set_level(int level)
352 {
353 if (lxc_loglevel_specified)
354 return 0;
355 if (level < 0 || level >= LXC_LOG_PRIORITY_NOTSET) {
356 ERROR("invalid log priority %d", level);
357 return -1;
358 }
359 lxc_log_category_lxc.priority = level;
360 return 0;
361 }
362
363 extern int lxc_log_get_level(void)
364 {
365 if (!lxc_loglevel_specified)
366 return LXC_LOG_PRIORITY_NOTSET;
367 return lxc_log_category_lxc.priority;
368 }
369
370 /*
371 * This is called when we read a lxc.logfile entry in a lxc.conf file. This
372 * happens after processing command line arguments, which override the .conf
373 * settings. So only set the file if previously unset.
374 */
375 extern int lxc_log_set_file(const char *fname)
376 {
377 if (lxc_logfile_specified)
378 return 0;
379 return __lxc_log_set_file(fname, 0);
380 }
381
382 extern const char *lxc_log_get_file(void)
383 {
384 return log_fname;
385 }
386
387 extern void lxc_log_set_prefix(const char *prefix)
388 {
389 strncpy(log_prefix, prefix, sizeof(log_prefix));
390 log_prefix[sizeof(log_prefix) - 1] = 0;
391 }
392
393 extern const char *lxc_log_get_prefix(void)
394 {
395 return log_prefix;
396 }