]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
syslog: simplify and model after lxc log functions
authorChristian Brauner <christian.brauner@mailbox.org>
Sun, 4 Sep 2016 19:16:12 +0000 (21:16 +0200)
committerChristian Brauner <christian.brauner@mailbox.org>
Sun, 4 Sep 2016 21:37:16 +0000 (23:37 +0200)
- add lxc_syslog_priority_to_string()
- add lxc_syslog_priority_to_int()
- remove syslog_facility struct
- add lxc.syslog to lxc_getconfig struct
- adapt config_syslog() callback

Signed-off-by: Christian Brauner <christian.brauner@mailbox.org>
src/lxc/conf.c
src/lxc/conf.h
src/lxc/confile.c
src/lxc/log.c
src/lxc/log.h

index 4ece410e9decfea8d37ea8d8f2cf9324fd96640d..22e2e29e5a4417c0dc167ca6cdafc0e599306a67 100644 (file)
@@ -4168,6 +4168,7 @@ void lxc_conf_free(struct lxc_conf *conf)
        free(conf->init_cmd);
        free(conf->unexpanded_config);
        free(conf->pty_names);
+       free(conf->syslog);
        lxc_clear_config_network(conf);
        free(conf->lsm_aa_profile);
        free(conf->lsm_se_context);
index 2593ce511794bf81eded4d0d4e853fcd84d1c412..69a72ea50bb31806168fb03b68432307ba139f19 100644 (file)
@@ -378,6 +378,10 @@ struct lxc_conf {
 
        /* indicator if the container will be destroyed on shutdown */
        int ephemeral;
+
+       /* The facility to pass to syslog. Let's users establish as what type of
+        * program liblxc is supposed to write to the syslog. */
+       char *syslog;
 };
 
 #ifdef HAVE_TLS
index fac919defc78a3c7cff95892a2c0e100ecbfd1a8..9ad05e5882b2ede12242381265eaa2495a61de98 100644 (file)
@@ -273,23 +273,6 @@ static const struct signame signames[] = {
 #endif
 };
 
-struct syslog_facility {
-       const char *name;
-       int facility;
-};
-
-static const struct syslog_facility syslog_facilities[] = {
-       { "daemon",     LOG_DAEMON },
-       { "local0",     LOG_LOCAL0 },
-       { "local1",     LOG_LOCAL1 },
-       { "local2",     LOG_LOCAL2 },
-       { "local3",     LOG_LOCAL3 },
-       { "local4",     LOG_LOCAL4 },
-       { "local5",     LOG_LOCAL5 },
-       { "local6",     LOG_LOCAL6 },
-       { "local7",     LOG_LOCAL7 },
-};
-
 static const size_t config_size = sizeof(config)/sizeof(struct lxc_config_t);
 
 extern struct lxc_config_t *lxc_getconfig(const char *key)
@@ -2027,8 +2010,8 @@ int lxc_config_read(const char *file, struct lxc_conf *conf, bool from_include)
        }
 
        /* Catch only the top level config file name in the structure */
-       if( ! conf->rcfile )
-               conf->rcfile = strdup( file );
+       if(!conf->rcfile)
+               conf->rcfile = strdup(file);
 
        return lxc_file_for_each_line(file, parse_line, &c);
 }
@@ -2577,6 +2560,8 @@ int lxc_get_config_item(struct lxc_conf *c, const char *key, char *retv,
                return lxc_get_conf_int(c, retv, inlen, c->init_gid);
        else if (strcmp(key, "lxc.ephemeral") == 0)
                return lxc_get_conf_int(c, retv, inlen, c->ephemeral);
+       else if (strcmp(key, "lxc.syslog") == 0)
+               v = c->syslog;
        else return -1;
 
        if (!v)
@@ -2957,19 +2942,15 @@ static int config_ephemeral(const char *key, const char *value,
 }
 
 static int config_syslog(const char *key, const char *value,
-                           struct lxc_conf *lxc_conf)
+                        struct lxc_conf *lxc_conf)
 {
-       int n;
-       int facility = -1;
-
-       for (n = 0; n < sizeof(syslog_facilities) / sizeof((syslog_facilities)[0]); n++) {
-               if (strcasecmp(syslog_facilities[n].name, value) == 0) {
-                       facility = syslog_facilities[n].facility;
-                       lxc_log_syslog(facility);
-                       return 0;
-               }
+       int facility;
+       facility = lxc_syslog_priority_to_int(value);
+       if (facility == -EINVAL) {
+               ERROR("Wrong value for lxc.syslog");
+               return -1;
        }
 
-       ERROR("Wrong value for lxc.syslog");
-       return -1;
+       lxc_log_syslog(facility);
+       return config_string_item(&lxc_conf->syslog, value);
 }
index 7fed3c65aa8aaae9389cb1d16e405f67d91da874..cab77f24c2e5512866b305f630f6841d1fe5003a 100644 (file)
@@ -108,8 +108,9 @@ static int log_append_syslog(const struct lxc_log_appender *appender,
        }
 
        syslog(lxc_log_priority_to_syslog(event->priority),
-               "%s %s - %s:%s:%d - %s" ,
+               "%s%s %s - %s:%s:%d - %s" ,
                log_vmname ? log_vmname : "",
+               log_vmname ? ":" : "",
                event->category,
                event->locinfo->file, event->locinfo->func,
                event->locinfo->line,
index 8906c234d5ff8f1d01a9aaeb77605d4404bd3c9c..7a366009169e7e7ec7be882071e01ab60581a3fa 100644 (file)
 
 #include "config.h"
 
+#include <errno.h>
 #include <stdarg.h>
 #include <stdio.h>
 #include <sys/time.h>
 #include <string.h>
 #include <strings.h>
 #include <stdbool.h>
+#include <syslog.h>
 
 #include "conf.h"
 
@@ -53,7 +55,7 @@
 #define ATTR_UNUSED
 #endif
 
-/* predefined priorities. */
+/* predefined lxc log priorities. */
 enum lxc_loglevel {
        LXC_LOG_PRIORITY_TRACE,
        LXC_LOG_PRIORITY_DEBUG,
@@ -151,6 +153,24 @@ static inline const char* lxc_log_priority_to_string(int priority)
                return "NOTSET";
        }
 }
+
+static inline const char* lxc_syslog_priority_to_string(int priority)
+{
+       switch (priority) {
+       case LOG_DAEMON: return "daemon";
+       case LOG_LOCAL0: return "local0";
+       case LOG_LOCAL1: return "local1";
+       case LOG_LOCAL2: return "local2";
+       case LOG_LOCAL3: return "local3";
+       case LOG_LOCAL4: return "local4";
+       case LOG_LOCAL5: return "local5";
+       case LOG_LOCAL6: return "local6";
+       case LOG_LOCAL7: return "local7";
+       default:
+               return "NOTSET";
+       }
+}
+
 /*
  * converts a literal priority to an int
  */
@@ -169,6 +189,21 @@ static inline int lxc_log_priority_to_int(const char* name)
        return LXC_LOG_PRIORITY_NOTSET;
 }
 
+static inline int lxc_syslog_priority_to_int(const char* name)
+{
+       if (!strcasecmp("daemon", name)) return LOG_DAEMON;
+       if (!strcasecmp("local0", name)) return LOG_LOCAL0;
+       if (!strcasecmp("local1", name)) return LOG_LOCAL1;
+       if (!strcasecmp("local2", name)) return LOG_LOCAL2;
+       if (!strcasecmp("local3", name)) return LOG_LOCAL3;
+       if (!strcasecmp("local4", name)) return LOG_LOCAL4;
+       if (!strcasecmp("local5", name)) return LOG_LOCAL5;
+       if (!strcasecmp("local6", name)) return LOG_LOCAL6;
+       if (!strcasecmp("local7", name)) return LOG_LOCAL7;
+
+       return -EINVAL;
+}
+
 static inline void
 __lxc_log_append(const struct lxc_log_appender *appender,
                struct lxc_log_event* event)