]> git.proxmox.com Git - mirror_lxc.git/blobdiff - src/lxc/log.c
Centralize hook names
[mirror_lxc.git] / src / lxc / log.c
index df1b87761acbcca5b0c2b9e0597507107ab0a0de..d5822c32bbbee5cb62201ccdbe652fa77da4a672 100644 (file)
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-#define _GNU_SOURCE
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE 1
+#endif
 #define __STDC_FORMAT_MACROS /* Required for PRIu64 to work. */
-#include <stdint.h>
-#include <stdio.h>
 #include <errno.h>
+#include <fcntl.h>
 #include <inttypes.h>
 #include <limits.h>
-#include <unistd.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <string.h>
 #include <pthread.h>
-
-#include <syslog.h>
+#include <stdint.h>
 #include <stdio.h>
-
-#include <fcntl.h>
 #include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <syslog.h>
+#include <unistd.h>
 
-#include "log.h"
 #include "caps.h"
-#include "utils.h"
+#include "config.h"
+#include "file_utils.h"
+#include "log.h"
 #include "lxccontainer.h"
+#include "utils.h"
 
 #ifndef HAVE_STRLCPY
 #include "include/strlcpy.h"
@@ -121,14 +122,20 @@ static char *lxc_log_get_va_msg(struct lxc_log_event *event)
                return NULL;
 
        va_copy(args, *event->vap);
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wformat-nonliteral"
        len = vsnprintf(NULL, 0, event->fmt, args) + 1;
+#pragma GCC diagnostic pop
        va_end(args);
 
        msg = malloc(len * sizeof(char));
        if (!msg)
                return NULL;
 
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wformat-nonliteral"
        rc = vsnprintf(msg, len, event->fmt, *event->vap);
+#pragma GCC diagnostic pop
        if (rc == -1 || rc >= len) {
                free(msg);
                return NULL;
@@ -182,7 +189,10 @@ static int log_append_stderr(const struct lxc_log_appender *appender,
                log_container_name ? ": " : "");
        fprintf(stderr, "%s: %s: %d ", event->locinfo->file,
                event->locinfo->func, event->locinfo->line);
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wformat-nonliteral"
        vfprintf(stderr, event->fmt, *event->vap);
+#pragma GCC diagnostic pop
        fprintf(stderr, "\n");
 
        return 0;
@@ -258,13 +268,13 @@ static int lxc_unix_epoch_to_utc(char *buf, size_t bufsize, const struct timespe
        /* Transform hours to seconds. */
        h_in_s = hours * 3600;
 
-       /* Calculate minutes by substracting the seconds for all days in the
+       /* Calculate minutes by subtracting the seconds for all days in the
         * epoch and for all hours in the epoch and divide by the number of
         * minutes in an hour.
         */
        minutes = (time->tv_sec - d_in_s - h_in_s) / 60;
 
-       /* Calculate the seconds by substracting the seconds for all days in the
+       /* Calculate the seconds by subtracting the seconds for all days in the
         * epoch, hours in the epoch and minutes in the epoch.
         */
        seconds = (time->tv_sec - d_in_s - h_in_s - (minutes * 60));
@@ -296,7 +306,7 @@ static int lxc_unix_epoch_to_utc(char *buf, size_t bufsize, const struct timespe
  * think you are, you __will__ cause trouble using them.
  * (As a short example how this can cause trouble: LXD uses forkstart to fork
  * off a new process that runs the container. At the same time the go runtime
- * LXD relies on does its own multi-threading thing which we can't controll. The
+ * LXD relies on does its own multi-threading thing which we can't control. The
  * fork()ing + threading then seems to mess with the locking states in these
  * time functions causing deadlocks.)
  * The current solution is to be good old unix people and use the Epoch as our
@@ -347,8 +357,11 @@ static int log_append_logfile(const struct lxc_log_appender *appender,
        if (n < 0)
                return n;
 
-       if ((size_t)n < (sizeof(buffer) - 1)) {
+       if ((size_t)n < STRARRAYLEN(buffer)) {
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wformat-nonliteral"
                ret = vsnprintf(buffer + n, sizeof(buffer) - n, event->fmt, *event->vap);
+#pragma GCC diagnostic pop
                if (ret < 0)
                        return 0;
 
@@ -356,16 +369,11 @@ static int log_append_logfile(const struct lxc_log_appender *appender,
        }
 
        if ((size_t)n >= sizeof(buffer))
-               n = sizeof(buffer) - 1;
+               n = STRARRAYLEN(buffer);
 
        buffer[n] = '\n';
 
-again:
-       ret = write(fd_to_use, buffer, n + 1);
-       if (ret < 0 && errno == EINTR)
-               goto again;
-
-       return ret;
+       return lxc_write_nointr(fd_to_use, buffer, n + 1);
 }
 
 #if HAVE_DLOG
@@ -488,7 +496,7 @@ static int build_dir(const char *name)
 
                ret = lxc_unpriv(mkdir(n, 0755));
                if (ret && errno != EEXIST) {
-                       SYSERROR("Failed to create directory %s", n);
+                       SYSERROR("Failed to create directory \"%s\"", n);
                        free(n);
                        return -1;
                }
@@ -506,7 +514,7 @@ static int log_open(const char *name)
        int fd;
        int newfd;
 
-       fd = lxc_unpriv(open(name, O_CREAT | O_WRONLY | O_APPEND | O_CLOEXEC, 0666));
+       fd = lxc_unpriv(open(name, O_CREAT | O_WRONLY | O_APPEND | O_CLOEXEC, 0660));
        if (fd < 0) {
                SYSERROR("Failed to open log file \"%s\"", name);
                return -1;
@@ -640,7 +648,7 @@ static int _lxc_log_set_file(const char *name, const char *lxcpath, int create_d
 /*
  * lxc_log_init:
  * Called from lxc front-end programs (like lxc-create, lxc-start) to
- * initalize the log defaults.
+ * initialize the log defaults.
  */
 int lxc_log_init(struct lxc_log *log)
 {
@@ -835,7 +843,7 @@ inline const char *lxc_log_get_file(void)
 
 inline void lxc_log_set_prefix(const char *prefix)
 {
-       /* We don't care if thte prefix is truncated. */
+       /* We don't care if the prefix is truncated. */
        (void)strlcpy(log_prefix, prefix, sizeof(log_prefix));
 }