chiark / gitweb /
Let config_parse open file where applicable
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 16 Jul 2014 22:27:12 +0000 (18:27 -0400)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 16 Jul 2014 22:47:20 +0000 (18:47 -0400)
Special care is needed so that we get an error message if the
file failed to parse, but not when it is missing. To avoid duplicating
the same error check in every caller, add an additional 'warn' boolean
to tell config_parse whether a message should be issued.
This makes things both shorter and more robust wrt. to error reporting.

20 files changed:
src/bootchart/bootchart.c
src/core/load-dropin.c
src/core/load-fragment.c
src/core/main.c
src/dbus1-generator/dbus1-generator.c
src/journal-remote/journal-remote.c
src/journal-remote/journal-upload.c
src/journal/coredump.c
src/journal/journald-server.c
src/login/logind.c
src/network/networkd-netdev.c
src/network/networkd-network.c
src/resolve/resolved-manager.c
src/shared/conf-parser.c
src/shared/conf-parser.h
src/shared/install.c
src/shared/sleep-config.c
src/timesync/timesyncd.c
src/tty-ask-password-agent/tty-ask-password-agent.c
src/udev/net/link-config.c

index 8e849da59c8a8fc666052b5fd42b5a89eaf11638..cbfc28d2f137925e82b2f1ba867ebcf4bf64e57c 100644 (file)
@@ -124,17 +124,11 @@ static void parse_conf(void) {
                 { "Bootchart", "ControlGroup",     config_parse_bool,   0, &arg_show_cgroup },
                 { NULL, NULL, NULL, 0, NULL }
         };
-        _cleanup_fclose_ FILE *f;
-        int r;
-
-        f = fopen(BOOTCHART_CONF, "re");
-        if (!f)
-                return;
 
-        r = config_parse(NULL, BOOTCHART_CONF, f,
-                         NULL, config_item_table_lookup, items, true, false, NULL);
-        if (r < 0)
-                log_warning("Failed to parse configuration file: %s", strerror(-r));
+        config_parse(NULL, BOOTCHART_CONF, NULL,
+                     NULL,
+                     config_item_table_lookup, items,
+                     true, false, true, NULL);
 
         if (init != NULL)
                 strscpy(arg_init_path, sizeof(arg_init_path), init);
index 66547cf4bcd8dfd97633d587a889cfc281f7b656..21c991526c7fdb804c3427e18eb7c6574cf01765 100644 (file)
@@ -186,8 +186,9 @@ int unit_load_dropin(Unit *u) {
 
         STRV_FOREACH(f, u->dropin_paths) {
                 config_parse(u->id, *f, NULL,
-                             UNIT_VTABLE(u)->sections, config_item_perf_lookup,
-                             load_fragment_gperf_lookup, false, false, u);
+                             UNIT_VTABLE(u)->sections,
+                             config_item_perf_lookup, load_fragment_gperf_lookup,
+                             false, false, false, u);
         }
 
         u->dropin_mtime = now(CLOCK_REALTIME);
index 54010b804ef211bafb01ddf3d38040600690d6ea..81f137946cdc6822b4f334cc17292eb4bdbda533 100644 (file)
@@ -3368,9 +3368,10 @@ static int load_from_path(Unit *u, const char *path) {
                 u->load_state = UNIT_LOADED;
 
                 /* Now, parse the file contents */
-                r = config_parse(u->id, filename, f, UNIT_VTABLE(u)->sections,
-                                 config_item_perf_lookup,
-                                 load_fragment_gperf_lookup, false, true, u);
+                r = config_parse(u->id, filename, f,
+                                 UNIT_VTABLE(u)->sections,
+                                 config_item_perf_lookup, load_fragment_gperf_lookup,
+                                 false, true, false, u);
                 if (r < 0)
                         return r;
         }
index d1fb265df1371a159bb8ed914ed5c56b715371cc..f9ee297afaecdf59463e46baad7ee47c76bf8539 100644 (file)
@@ -682,23 +682,13 @@ static int parse_config_file(void) {
                 {}
         };
 
-        _cleanup_fclose_ FILE *f;
         const char *fn;
-        int r;
 
         fn = arg_running_as == SYSTEMD_SYSTEM ? PKGSYSCONFDIR "/system.conf" : PKGSYSCONFDIR "/user.conf";
-        f = fopen(fn, "re");
-        if (!f) {
-                if (errno == ENOENT)
-                        return 0;
-
-                log_warning("Failed to open configuration file '%s': %m", fn);
-                return 0;
-        }
-
-        r = config_parse(NULL, fn, f, "Manager\0", config_item_table_lookup, items, false, false, NULL);
-        if (r < 0)
-                log_warning("Failed to parse configuration file: %s", strerror(-r));
+        config_parse(NULL, fn, NULL,
+                     "Manager\0",
+                     config_item_table_lookup, items,
+                     false, false, true, NULL);
 
         return 0;
 }
index ba2953014d73aa790ee687ff6ebd65e46e44eaa8..dcfceecf6356e0ef26b00fa3c93303db7fe3e602 100644 (file)
@@ -163,27 +163,17 @@ static int add_dbus(const char *path, const char *fname, const char *type) {
                 { "D-BUS Service", "SystemdService", config_parse_string, 0, &service },
         };
 
-        _cleanup_fclose_ FILE *f = NULL;
-        _cleanup_free_ char *p = NULL;
+        char *p;
         int r;
 
         assert(path);
         assert(fname);
 
-        p = strjoin(path, "/", fname, NULL);
-        if (!p)
-                return log_oom();
-
-        f = fopen(p, "re");
-        if (!f) {
-                if (errno == -ENOENT)
-                        return 0;
-
-                log_error("Failed to read %s: %m", p);
-                return -errno;
-        }
-
-        r = config_parse(NULL, p, f, "D-BUS Service\0", config_item_table_lookup, table, true, false, NULL);
+        p = strappenda3(path, "/", fname);
+        r = config_parse(NULL, p, NULL,
+                         "D-BUS Service\0",
+                         config_item_table_lookup, table,
+                         true, false, true, NULL);
         if (r < 0)
                 return r;
 
index 08de9d03ae90e2a9ae01fa47768dd955a6346b08..37ad9029fd008b39771b80a6ceb80f225a64abe8 100644 (file)
@@ -1122,16 +1122,11 @@ static int parse_config(void) {
                 { "Remote",  "ServerCertificateFile",  config_parse_path,             0, &arg_cert       },
                 { "Remote",  "TrustedCertificateFile", config_parse_path,             0, &arg_trust      },
                 {}};
-        int r;
-
-        r = config_parse(NULL, PKGSYSCONFDIR "/journal-remote.conf", NULL,
-                         "Remote\0",
-                         config_item_table_lookup, items,
-                         false, false, NULL);
-        if (r < 0)
-                log_error("Failed to parse configuration file: %s", strerror(-r));
 
-        return r;
+        return config_parse(NULL, PKGSYSCONFDIR "/journal-remote.conf", NULL,
+                            "Remote\0",
+                            config_item_table_lookup, items,
+                            false, false, true, NULL);
 }
 
 static void help(void) {
index a381ec59bf6834fd07132923c1dbb94e7adc5bbd..72396bf205a22192d32b56ba0f708d0f503573b1 100644 (file)
@@ -495,16 +495,11 @@ static int parse_config(void) {
                 { "Upload",  "ServerCertificateFile",  config_parse_path,   0, &arg_cert   },
                 { "Upload",  "TrustedCertificateFile", config_parse_path,   0, &arg_trust  },
                 {}};
-        int r;
-
-        r = config_parse(NULL, PKGSYSCONFDIR "/journal-upload.conf", NULL,
-                         "Upload\0",
-                         config_item_table_lookup, items,
-                         false, false, NULL);
-        if (r < 0)
-                log_error("Failed to parse configuration file: %s", strerror(-r));
 
-        return r;
+        return config_parse(NULL, PKGSYSCONFDIR "/journal-upload.conf", NULL,
+                            "Upload\0",
+                            config_item_table_lookup, items,
+                            false, false, true, NULL);
 }
 
 static void help(void) {
index 4ac1a41299af2e12292574c7c0b799f53df31580..182c2b1bad1e0f31d211fa99160067c00b23bc41 100644 (file)
@@ -114,16 +114,10 @@ static int parse_config(void) {
                 {}
         };
 
-        return config_parse(
-                        NULL,
-                        "/etc/systemd/coredump.conf",
-                        NULL,
-                        "Coredump\0",
-                        config_item_table_lookup,
-                        items,
-                        false,
-                        false,
-                        NULL);
+        return config_parse(NULL, "/etc/systemd/coredump.conf", NULL,
+                            "Coredump\0",
+                            config_item_table_lookup, items,
+                            false, false, true, NULL);
 }
 
 static int fix_acl(int fd, uid_t uid) {
index 097af24c5bff458ee58254278ae72168878698ae..4ea9d43c0dc1ce60369323d3cb1aa245d979453f 100644 (file)
@@ -1331,27 +1331,12 @@ static int server_parse_proc_cmdline(Server *s) {
 }
 
 static int server_parse_config_file(Server *s) {
-        static const char fn[] = "/etc/systemd/journald.conf";
-        _cleanup_fclose_ FILE *f = NULL;
-        int r;
-
         assert(s);
 
-        f = fopen(fn, "re");
-        if (!f) {
-                if (errno == ENOENT)
-                        return 0;
-
-                log_warning("Failed to open configuration file %s: %m", fn);
-                return -errno;
-        }
-
-        r = config_parse(NULL, fn, f, "Journal\0", config_item_perf_lookup,
-                         journald_gperf_lookup, false, false, s);
-        if (r < 0)
-                log_warning("Failed to parse configuration file: %s", strerror(-r));
-
-        return r;
+        return config_parse(NULL, "/etc/systemd/journald.conf", NULL,
+                            "Journal\0",
+                            config_item_perf_lookup, journald_gperf_lookup,
+                            false, false, true, s);
 }
 
 static int server_dispatch_sync(sd_event_source *es, usec_t t, void *userdata) {
index a1881840e10329ab06942dcef3537772c9f62bff..ec5529db1a3868430d989c440bcab0cd0e68c560 100644 (file)
@@ -1097,27 +1097,12 @@ int manager_run(Manager *m) {
 }
 
 static int manager_parse_config_file(Manager *m) {
-        static const char fn[] = "/etc/systemd/logind.conf";
-        _cleanup_fclose_ FILE *f = NULL;
-        int r;
-
         assert(m);
 
-        f = fopen(fn, "re");
-        if (!f) {
-                if (errno == ENOENT)
-                        return 0;
-
-                log_warning("Failed to open configuration file %s: %m", fn);
-                return -errno;
-        }
-
-        r = config_parse(NULL, fn, f, "Login\0", config_item_perf_lookup,
-                         logind_gperf_lookup, false, false, m);
-        if (r < 0)
-                log_warning("Failed to parse configuration file: %s", strerror(-r));
-
-        return r;
+        return config_parse(NULL, "/etc/systemd/logind.conf", NULL,
+                            "Login\0",
+                            config_item_perf_lookup, logind_gperf_lookup,
+                            false, false, true, m);
 }
 
 int main(int argc, char *argv[]) {
index 7d12af3938e2552bd7699c9ba4254782aa07ba93..9974913f49b364abbc5e405eed20b0d3a7d68d35 100644 (file)
@@ -526,11 +526,9 @@ static int netdev_load_one(Manager *manager, const char *filename) {
         r = config_parse(NULL, filename, file,
                          "Match\0NetDev\0VLAN\0MACVLAN\0VXLAN\0Tunnel\0Peer\0Tun\0Tap\0Bond\0",
                          config_item_perf_lookup, network_netdev_gperf_lookup,
-                         false, false, netdev);
-        if (r < 0) {
-                log_warning("Could not parse config file %s: %s", filename, strerror(-r));
+                         false, false, true, netdev);
+        if (r < 0)
                 return r;
-        }
 
         /* skip out early if configuration does not match the environment */
         if (net_match_config(NULL, NULL, NULL, NULL, NULL,
index 252c9a0ab0bd0db9d7fbb0bc504a82d82ba154fb..700577fccfa5be2d8bfd2e3ac1f0509e35e51028 100644 (file)
@@ -96,11 +96,9 @@ static int network_load_one(Manager *manager, const char *filename) {
         r = config_parse(NULL, filename, file,
                          "Match\0Network\0Address\0Route\0DHCP\0DHCPv4\0",
                          config_item_perf_lookup, network_network_gperf_lookup,
-                         false, false, network);
-        if (r < 0) {
-                log_warning("Could not parse config file %s: %s", filename, strerror(-r));
+                         false, false, true, network);
+        if (r < 0)
                 return r;
-        }
 
         LIST_PREPEND(networks, manager->networks, network);
 
index 3d2979dbbe196922eba809f3dcad72f6f1b592f8..ab504d0a758f73033255b106a22f4dbb4103a54c 100644 (file)
@@ -373,18 +373,12 @@ int config_parse_dnsv(
 }
 
 int manager_parse_config_file(Manager *m) {
-        int r;
-
         assert(m);
 
-        r = config_parse(NULL, "/etc/systemd/resolved.conf", NULL,
-                         "Resolve\0",
-                         config_item_perf_lookup, resolved_gperf_lookup,
-                         false, false, m);
-        if (r < 0)
-                log_warning("Failed to parse configuration file: %s", strerror(-r));
-
-        return 0;
+        return config_parse(NULL, "/etc/systemd/resolved.conf", NULL,
+                            "Resolve\0",
+                            config_item_perf_lookup, resolved_gperf_lookup,
+                            false, false, true, m);
 }
 
 int manager_new(Manager **ret) {
index 1f40986649bad25722694cebd49984469df1c840..0be7226871218e0a38f687ad59d6a55ca6fc9dc2 100644 (file)
@@ -245,7 +245,7 @@ static int parse_line(const char* unit,
                 if (!fn)
                         return -ENOMEM;
 
-                return config_parse(unit, fn, NULL, sections, lookup, table, relaxed, false, userdata);
+                return config_parse(unit, fn, NULL, sections, lookup, table, relaxed, false, false, userdata);
         }
 
         if (*l == '[') {
@@ -326,6 +326,7 @@ int config_parse(const char *unit,
                  const void *table,
                  bool relaxed,
                  bool allow_include,
+                 bool warn,
                  void *userdata) {
 
         _cleanup_free_ char *section = NULL, *continuation = NULL;
@@ -340,7 +341,11 @@ int config_parse(const char *unit,
         if (!f) {
                 f = ours = fopen(filename, "re");
                 if (!f) {
-                        log_full(errno == ENOENT ? LOG_DEBUG : LOG_ERR, "Failed to open configuration file '%s': %m", filename);
+                        /* Only log on request, except for ENOENT,
+                         * since we return 0 to the caller. */
+                        if (warn || errno == ENOENT)
+                                log_full(errno == ENOENT ? LOG_DEBUG : LOG_ERR,
+                                         "Failed to open configuration file '%s': %m", filename);
                         return errno == ENOENT ? 0 : -errno;
                 }
         }
@@ -363,8 +368,11 @@ int config_parse(const char *unit,
 
                 if (continuation) {
                         c = strappend(continuation, l);
-                        if (!c)
+                        if (!c) {
+                                if (warn)
+                                        log_oom();
                                 return -ENOMEM;
+                        }
 
                         free(continuation);
                         continuation = NULL;
@@ -386,8 +394,11 @@ int config_parse(const char *unit,
                                 continuation = c;
                         else {
                                 continuation = strdup(l);
-                                if (!continuation)
+                                if (!continuation) {
+                                        if (warn)
+                                                log_oom();
                                         return -ENOMEM;
+                                }
                         }
 
                         continue;
@@ -408,8 +419,12 @@ int config_parse(const char *unit,
                                userdata);
                 free(c);
 
-                if (r < 0)
+                if (r < 0) {
+                        if (warn)
+                                log_warning("Failed to parse file '%s': %s",
+                                            filename, strerror(-r));
                         return r;
+                }
         }
 
         return 0;
index 3e2c78480574059b838a41cb80f966d0179fadf8..ea7b710dece722b5cecb1ed90c809a579029fa86 100644 (file)
@@ -89,6 +89,7 @@ int config_parse(const char *unit,
                  const void *table,
                  bool relaxed,
                  bool allow_include,
+                 bool warn,
                  void *userdata);
 
 /* Generic parsers */
index a080d8f328af9ae4acbd3041362f6f5d5b2985c2..bc2a3779719c39f5f5b0c98a556af5a523b4b638 100644 (file)
@@ -1076,7 +1076,10 @@ static int unit_file_load(
                 return -ENOMEM;
         }
 
-        r = config_parse(NULL, path, f, NULL, config_item_table_lookup, items, true, true, info);
+        r = config_parse(NULL, path, f,
+                         NULL,
+                         config_item_table_lookup, items,
+                         true, true, false, info);
         if (r < 0)
                 return r;
 
index 4b2b0fe1003c154dfd1c5a0d16bc254934e7b50b..16a488d56b982537385c3588cc62f2bbc0a06e9f 100644 (file)
@@ -48,19 +48,9 @@ int parse_sleep_config(const char *verb, char ***_modes, char ***_states) {
                 {}
         };
 
-        int r;
-        _cleanup_fclose_ FILE *f;
-
-        f = fopen(PKGSYSCONFDIR "/sleep.conf", "re");
-        if (!f)
-                log_full(errno == ENOENT ? LOG_DEBUG: LOG_WARNING,
-                         "Failed to open configuration file " PKGSYSCONFDIR "/sleep.conf: %m");
-        else {
-                r = config_parse(NULL, PKGSYSCONFDIR "/sleep.conf", f, "Sleep\0",
-                                 config_item_table_lookup, items, false, false, NULL);
-                if (r < 0)
-                        log_warning("Failed to parse configuration file: %s", strerror(-r));
-        }
+        config_parse(NULL, PKGSYSCONFDIR "/sleep.conf", NULL,
+                     "Sleep\0",
+                     config_item_table_lookup, items, false, false, true, NULL);
 
         if (streq(verb, "suspend")) {
                 /* empty by default */
index 67a434adeec3e210dd8e840a6e54eb2bc0a29c8b..e4e3aaecdfbc1128cf7f450b7b56bbce664a76f9 100644 (file)
@@ -1095,27 +1095,10 @@ int config_parse_servers(
 }
 
 static int manager_parse_config_file(Manager *m) {
-        static const char fn[] = "/etc/systemd/timesyncd.conf";
-        _cleanup_fclose_ FILE *f = NULL;
-        int r;
-
-        assert(m);
-
-        f = fopen(fn, "re");
-        if (!f) {
-                if (errno == ENOENT)
-                        return 0;
-
-                log_warning("Failed to open configuration file %s: %m", fn);
-                return -errno;
-        }
-
-        r = config_parse(NULL, fn, f, "Time\0", config_item_perf_lookup,
-                         timesyncd_gperf_lookup, false, false, m);
-        if (r < 0)
-                log_warning("Failed to parse configuration file: %s", strerror(-r));
-
-        return r;
+        return config_parse(NULL, "/etc/systemd/timesyncd.conf", NULL,
+                            "Time\0",
+                            config_item_perf_lookup, timesyncd_gperf_lookup,
+                            false, false, true, m);
 }
 
 static bool network_is_online(void) {
index 0398a9d814ecae58d961f1cdc851ec4a8fe64b94..5852f711730e47e0145252c175e8fcbf873b7aea 100644 (file)
@@ -258,25 +258,16 @@ static int parse_password(const char *filename, char **wall) {
                 { NULL, NULL, NULL, 0, NULL }
         };
 
-        FILE *f;
         int r;
 
         assert(filename);
 
-        f = fopen(filename, "re");
-        if (!f) {
-                if (errno == ENOENT)
-                        return 0;
-
-                log_error("open(%s): %m", filename);
-                return -errno;
-        }
-
-        r = config_parse(NULL, filename, f, NULL, config_item_table_lookup, items, true, false, NULL);
-        if (r < 0) {
-                log_error("Failed to parse password file %s: %s", filename, strerror(-r));
-                goto finish;
-        }
+        r = config_parse(NULL, filename, NULL,
+                         NULL,
+                         config_item_table_lookup, items,
+                         true, false, true, NULL);
+        if (r < 0)
+                return r;
 
         if (!socket_name) {
                 log_error("Invalid password file %s", filename);
@@ -414,8 +405,6 @@ static int parse_password(const char *filename, char **wall) {
         }
 
 finish:
-        fclose(f);
-
         safe_close(socket_fd);
 
         free(packet);
index fe916a4326755e7fc0be49d2c89d77db6e831533..73243fa107b1fe87ebcdc2e8254e9d8cbd62e6fb 100644 (file)
@@ -177,11 +177,10 @@ static int load_link(link_config_ctx *ctx, const char *filename) {
         r = config_parse(NULL, filename, file,
                          "Match\0Link\0Ethernet\0",
                          config_item_perf_lookup, link_config_gperf_lookup,
-                         false, false, link);
-        if (r < 0) {
-                log_warning("Could not parse config file %s: %s", filename, strerror(-r));
+                         false, false, true, link);
+        if (r < 0)
                 return r;
-        else
+        else
                 log_debug("Parsed configuration file %s", filename);
 
         link->filename = strdup(filename);