dns: rework write_to_netconfig()

The compiler warns when we ignore the return value from write().
And assigning it to an unused variable, causes another warning.
Make some use of it, at least to handle EINTR. All other errors
are still ignored.

While at it, rework the write code to first write to a buffer
in memory.
This commit is contained in:
Thomas Haller
2017-12-18 15:42:06 +01:00
parent ad3bbda8e3
commit 41f608dd91

View File

@@ -378,56 +378,59 @@ run_netconfig (NMDnsManager *self, GError **error, gint *stdin_fd)
} }
static void static void
write_to_netconfig (NMDnsManager *self, gint fd, const char *key, const char *value) netconfig_construct_str (NMDnsManager *self, GString *str, const char *key, const char *value)
{ {
gs_free char *str = NULL; if (value) {
_LOGD ("writing to netconfig: %s='%s'", key, value);
g_string_append_printf (str, "%s='%s'\n", key, value);
}
}
str = g_strdup_printf ("%s='%s'\n", key, value); static void
_LOGD ("writing to netconfig: %s='%s'", key, value); netconfig_construct_strv (NMDnsManager *self, GString *str, const char *key, const char *const*values)
(void) write (fd, str, strlen (str)); {
if (values) {
gs_free char *value = NULL;
value = g_strjoinv (" ", (char **) values);
netconfig_construct_str (self, str, key, value);
}
} }
static SpawnResult static SpawnResult
dispatch_netconfig (NMDnsManager *self, dispatch_netconfig (NMDnsManager *self,
char **searches, const char *const*searches,
char **nameservers, const char *const*nameservers,
const char *nis_domain, const char *nis_domain,
char **nis_servers, const char *const*nis_servers,
GError **error) GError **error)
{ {
char *str;
GPid pid; GPid pid;
gint fd; gint fd;
int status; int status;
gssize l;
nm_auto_free_gstring GString *str = NULL;
pid = run_netconfig (self, error, &fd); pid = run_netconfig (self, error, &fd);
if (pid <= 0) if (pid <= 0)
return SR_NOTFOUND; return SR_NOTFOUND;
str = g_string_new ("");
/* NM is writing already-merged DNS information to netconfig, so it /* NM is writing already-merged DNS information to netconfig, so it
* does not apply to a specific network interface. * does not apply to a specific network interface.
*/ */
write_to_netconfig (self, fd, "INTERFACE", "NetworkManager"); netconfig_construct_str (self, str, "INTERFACE", "NetworkManager");
netconfig_construct_strv (self, str, "DNSSEARCH", searches);
netconfig_construct_strv (self, str, "DNSSERVERS", nameservers);
netconfig_construct_str (self, str, "NISDOMAIN", nis_domain);
netconfig_construct_strv (self, str, "NISSERVERS", nis_servers);
if (searches) { again:
str = g_strjoinv (" ", searches); l = write (fd, str->str, str->len);
write_to_netconfig (self, fd, "DNSSEARCH", str); if (l == -1) {
g_free (str); if (errno == EINTR)
} goto again;
if (nameservers) {
str = g_strjoinv (" ", nameservers);
write_to_netconfig (self, fd, "DNSSERVERS", str);
g_free (str);
}
if (nis_domain)
write_to_netconfig (self, fd, "NISDOMAIN", nis_domain);
if (nis_servers) {
str = g_strjoinv (" ", nis_servers);
write_to_netconfig (self, fd, "NISSERVERS", str);
g_free (str);
} }
nm_close (fd); nm_close (fd);
@@ -1112,8 +1115,12 @@ update_dns (NMDnsManager *self,
result = dispatch_resolvconf (self, searches, nameservers, options, error); result = dispatch_resolvconf (self, searches, nameservers, options, error);
break; break;
case NM_DNS_MANAGER_RESOLV_CONF_MAN_NETCONFIG: case NM_DNS_MANAGER_RESOLV_CONF_MAN_NETCONFIG:
result = dispatch_netconfig (self, searches, nameservers, nis_domain, result = dispatch_netconfig (self,
nis_servers, error); (const char *const*) searches,
(const char *const*) nameservers,
nis_domain,
(const char *const*) nis_servers,
error);
break; break;
default: default:
g_assert_not_reached (); g_assert_not_reached ();