[systemd-devel] [PATCH 1/2] Report about syntax error in extended format

Oleksii Shevchuk alxchk at gmail.com
Mon Apr 1 00:17:04 PDT 2013


---
 src/shared/conf-parser.c | 86 ++++++++++++++++++++++++++++++------------------
 src/shared/conf-parser.h |  3 ++
 2 files changed, 57 insertions(+), 32 deletions(-)

diff --git a/src/shared/conf-parser.c b/src/shared/conf-parser.c
index c2cf5a6..60a6b35 100644
--- a/src/shared/conf-parser.c
+++ b/src/shared/conf-parser.c
@@ -35,6 +35,28 @@
 #include "set.h"
 #include "exit-status.h"
 
+static int log_syntax(const char * file, unsigned line, bool error, const char *description, ...) {
+        _cleanup_free_ char *buf = NULL;
+
+        va_list arg_descr, arg_copy;
+
+        va_start(arg_descr, description);
+        va_copy(arg_copy, arg_descr);
+        if (vasprintf(&buf, description, arg_copy) < 0) {
+                va_end(arg_descr);
+                return log_oom();
+        }
+        va_end(arg_descr);
+
+        return log_struct(error ? LOG_ERR : LOG_WARNING,
+                          "MESSAGE=[%s:%d]: %s", file, line, buf,
+                          "MESSAGE_ID=%s\n", error ? CONF_PARSER_ERROR : CONF_PARSER_WARNING,
+                          "FILE=%s\n", file,
+                          "LINE=%u\n", line,
+                          "ERROR=%s\n", buf,
+                          NULL);
+}
+
 int config_item_table_lookup(
                 void *table,
                 const char *section,
@@ -145,7 +167,7 @@ static int next_assignment(
 
         /* Warn about unknown non-extension fields. */
         if (!relaxed && !startswith(lvalue, "X-"))
-                log_info("[%s:%u] Unknown lvalue '%s' in section '%s'. Ignoring.", filename, line, lvalue, section);
+                log_syntax(filename, line, false, "Unknown lvalue '%s' in section '%s'", lvalue, section);
 
         return 0;
 }
@@ -199,7 +221,7 @@ static int parse_line(
                 assert(k > 0);
 
                 if (l[k-1] != ']') {
-                        log_error("[%s:%u] Invalid section header.", filename, line);
+                        log_syntax(filename, line, true, "Invalid section header");
                         return -EBADMSG;
                 }
 
@@ -209,8 +231,8 @@ static int parse_line(
 
                 if (sections && !nulstr_contains(sections, n)) {
 
-                        if (!relaxed)
-                                log_info("[%s:%u] Unknown section '%s'. Ignoring.", filename, line, n);
+                        if (! relaxed)
+                                log_syntax(filename, line, false, "Unknown section '%s'. Ignoring.", n);
 
                         free(n);
                         *section = NULL;
@@ -225,14 +247,14 @@ static int parse_line(
         if (sections && !*section) {
 
                 if (!relaxed)
-                        log_info("[%s:%u] Assignment outside of section. Ignoring.", filename, line);
+                        log_syntax(filename, line, false, "Assignment outside of section. Ignoring.");
 
                 return 0;
         }
 
         e = strchr(l, '=');
         if (!e) {
-                log_error("[%s:%u] Missing '='.", filename, line);
+                log_syntax(filename, line, true, "Missing '='.");
                 return -EBADMSG;
         }
 
@@ -274,7 +296,7 @@ int config_parse(
                 f = fopen(filename, "re");
                 if (!f) {
                         r = -errno;
-                        log_error("Failed to open configuration file '%s': %s", filename, strerror(-r));
+                        log_error("Failed to open configuration file '%s': %s", filename, strerror(-errno));
                         goto finish;
                 }
 
@@ -379,7 +401,7 @@ int config_parse_int(
 
         r = safe_atoi(rvalue, i);
         if (r < 0) {
-                log_error("[%s:%u] Failed to parse numeric value, ingoring: %s", filename, line, rvalue);
+                log_syntax(filename, line, true, "Failed to parse numeric value '%s'. Ingoring.", rvalue);
                 return 0;
         }
 
@@ -406,7 +428,7 @@ int config_parse_long(
 
         r = safe_atoli(rvalue, i);
         if (r < 0) {
-                log_error("[%s:%u] Failed to parse numeric value, ignoring: %s", filename, line, rvalue);
+                log_syntax(filename, line, true, "Failed to parse numeric value, ignoring: %s", rvalue);
                 return 0;
         }
 
@@ -433,7 +455,7 @@ int config_parse_uint64(
 
         r = safe_atou64(rvalue, u);
         if (r < 0) {
-                log_error("[%s:%u] Failed to parse numeric value, ignoring: %s", filename, line, rvalue);
+                log_syntax(filename, line, true, "Failed to parse numeric value, ignoring: %s", rvalue);
                 return 0;
         }
 
@@ -460,7 +482,7 @@ int config_parse_unsigned(
 
         r = safe_atou(rvalue, u);
         if (r < 0) {
-                log_error("[%s:%u] Failed to parse numeric value: %s", filename, line, rvalue);
+                log_syntax(filename, line, true, "Failed to parse numeric value: %s", rvalue);
                 return r;
         }
 
@@ -487,7 +509,7 @@ int config_parse_double(
 
         r = safe_atod(rvalue, d);
         if (r < 0) {
-                log_error("[%s:%u] Failed to parse numeric value: %s", filename, line, rvalue);
+                log_syntax(filename, line, true, "Failed to parse numeric value: %s", rvalue);
                 return r;
         }
 
@@ -513,7 +535,7 @@ int config_parse_bytes_size(
         assert(data);
 
         if (parse_bytes(rvalue, &o) < 0 || (off_t) (size_t) o != o) {
-                log_error("[%s:%u] Failed to parse byte value, ignoring: %s", filename, line, rvalue);
+                log_syntax(filename, line, true, "Failed to parse byte value, ignoring: %s", rvalue);
                 return 0;
         }
 
@@ -542,7 +564,7 @@ int config_parse_bytes_off(
         assert_cc(sizeof(off_t) == sizeof(uint64_t));
 
         if (parse_bytes(rvalue, bytes) < 0) {
-                log_error("[%s:%u] Failed to parse bytes value, ignoring: %s", filename, line, rvalue);
+                log_syntax(filename, line, true, "Failed to parse bytes value, ignoring: %s", rvalue);
                 return 0;
         }
 
@@ -568,7 +590,7 @@ int config_parse_bool(
         assert(data);
 
         if ((k = parse_boolean(rvalue)) < 0) {
-                log_error("[%s:%u] Failed to parse boolean value, ignoring: %s", filename, line, rvalue);
+                log_syntax(filename, line, true, "Failed to parse boolean value, ignoring: %s", rvalue);
                 return 0;
         }
 
@@ -598,7 +620,7 @@ int config_parse_tristate(
 
         k = parse_boolean(rvalue);
         if (k < 0) {
-                log_error("[%s:%u] Failed to parse boolean value, ignoring: %s", filename, line, rvalue);
+                log_syntax(filename, line, true, "Failed to parse boolean value, ignoring: %s", rvalue);
                 return 0;
         }
 
@@ -629,7 +651,7 @@ int config_parse_string(
                 return log_oom();
 
         if (!utf8_is_valid(n)) {
-                log_error("[%s:%u] String is not UTF-8 clean, ignoring assignment: %s", filename, line, rvalue);
+                log_syntax(filename, line, true, "[%s:%u] String is not UTF-8 clean, ignoring assignment: %s", rvalue);
                 free(n);
                 return 0;
         }
@@ -664,12 +686,12 @@ int config_parse_path(
         assert(data);
 
         if (!utf8_is_valid(rvalue)) {
-                log_error("[%s:%u] Path is not UTF-8 clean, ignoring assignment: %s", filename, line, rvalue);
+                log_syntax(filename, line, true, "Path is not UTF-8 clean, ignoring assignment: %s", rvalue);
                 return 0;
         }
 
         if (!path_is_absolute(rvalue)) {
-                log_error("[%s:%u] Not an absolute path, ignoring: %s", filename, line, rvalue);
+                log_syntax(filename, line, true, "Not an absolute path, ignoring: %s", rvalue);
                 return 0;
         }
 
@@ -728,7 +750,7 @@ int config_parse_strv(
                         return log_oom();
 
                 if (!utf8_is_valid(n)) {
-                        log_error("[%s:%u] String is not UTF-8 clean, ignoring: %s", filename, line, rvalue);
+                        log_syntax(filename, line, true, "[%s:%u] String is not UTF-8 clean, ignoring: %s", rvalue);
                         continue;
                 }
 
@@ -774,12 +796,12 @@ int config_parse_path_strv(
                         return log_oom();
 
                 if (!utf8_is_valid(n)) {
-                        log_error("[%s:%u] Path is not UTF-8 clean, ignoring assignment: %s", filename, line, rvalue);
+                        log_syntax(filename, line, true, "Path is not UTF-8 clean, ignoring assignment: %s", rvalue);
                         continue;
                 }
 
                 if (!path_is_absolute(n)) {
-                        log_error("[%s:%u] Not an absolute path, ignoring: %s", filename, line, rvalue);
+                        log_syntax(filename, line, true, "Not an absolute path, ignoring: %s", rvalue);
                         continue;
                 }
 
@@ -810,7 +832,7 @@ int config_parse_usec(
         assert(data);
 
         if (parse_usec(rvalue, usec) < 0) {
-                log_error("[%s:%u] Failed to parse time value, ignoring: %s", filename, line, rvalue);
+                log_syntax(filename, line, true, "Failed to parse time value, ignoring: %s", rvalue);
                 return 0;
         }
 
@@ -835,7 +857,7 @@ int config_parse_nsec(
         assert(data);
 
         if (parse_nsec(rvalue, nsec) < 0) {
-                log_error("[%s:%u] Failed to parse time value, ignoring: %s", filename, line, rvalue);
+                log_syntax(filename, line, true, "Failed to parse time value, ignoring: %s", rvalue);
                 return 0;
         }
 
@@ -864,12 +886,12 @@ int config_parse_mode(
         errno = 0;
         l = strtol(rvalue, &x, 8);
         if (!x || x == rvalue || *x || errno) {
-                log_error("[%s:%u] Failed to parse mode value, ignoring: %s", filename, line, rvalue);
+                log_syntax(filename, line, true, "Failed to parse mode value, ignoring: %s", rvalue);
                 return 0;
         }
 
         if (l < 0000 || l > 07777) {
-                log_error("[%s:%u] mode value out of range, ignoring: %s", filename, line, rvalue);
+                log_syntax(filename, line, true, "Mode value out of range, ignoring: %s", rvalue);
                 return 0;
         }
 
@@ -897,7 +919,7 @@ int config_parse_facility(
 
         x = log_facility_unshifted_from_string(rvalue);
         if (x < 0) {
-                log_error("[%s:%u] Failed to parse log facility, ignoring: %s", filename, line, rvalue);
+                log_syntax(filename, line, true, "Failed to parse log facility, ignoring: %s", rvalue);
                 return 0;
         }
 
@@ -926,7 +948,7 @@ int config_parse_level(
 
         x = log_level_from_string(rvalue);
         if (x < 0) {
-                log_error("[%s:%u] Failed to parse log level, ignoring: %s", filename, line, rvalue);
+                log_syntax(filename, line, true, "Failed to parse log level, ignoring: %s", rvalue);
                 return 0;
         }
 
@@ -985,18 +1007,18 @@ int config_parse_set_status(
 
                                 r = set_put(status_set->signal, INT_TO_PTR(val));
                                 if (r < 0) {
-                                        log_error("[%s:%u] Unable to store: %s", filename, line, w);
+                                        log_syntax(filename, line, true, "Unable to store: %s", w);
                                         return r;
                                 }
                         } else {
-                                log_error("[%s:%u] Failed to parse value, ignoring: %s", filename, line, w);
+                                log_syntax(filename, line, true, "Failed to parse value, ignoring: %s", w);
                                 return 0;
                         }
                 } else {
                         free(temp);
 
                         if (val < 0 || val > 255)
-                                log_warning("[%s:%u] Value %d is outside range 0-255, ignoring", filename, line, val);
+                                log_syntax(filename, line, false, "Value %d is outside range 0-255, ignoring", val);
                         else {
                                 r = set_ensure_allocated(&status_set->code, trivial_hash_func, trivial_compare_func);
                                 if (r < 0)
@@ -1004,7 +1026,7 @@ int config_parse_set_status(
 
                                 r = set_put(status_set->code, INT_TO_PTR(val));
                                 if (r < 0) {
-                                        log_error("[%s:%u] Unable to store: %s", filename, line, w);
+                                        log_syntax(filename, line, true, "[%s:%u] Unable to store: %s", w);
                                         return r;
                                 }
                         }
diff --git a/src/shared/conf-parser.h b/src/shared/conf-parser.h
index 9096c60..62c9a33 100644
--- a/src/shared/conf-parser.h
+++ b/src/shared/conf-parser.h
@@ -135,3 +135,6 @@ int config_parse_set_status(const char *filename, unsigned line, const char *sec
                                                                         \
                 return 0;                                               \
         }
+
+#define CONF_PARSER_ERROR "c772d24e9a884cbeb9ea12625c306c01"
+#define CONF_PARSER_WARNING "28777bb2b706464e853a669766c1b82f"
-- 
1.8.1.2



More information about the systemd-devel mailing list