diff options
author | Lennart Poettering <lennart@poettering.net> | 2017-12-11 19:50:30 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2017-12-14 10:42:25 +0100 |
commit | 0d5366733428b657e1b5b7700b461e878e00b578 (patch) | |
tree | e2bcdb9f85e01c79221a9f4797e11fae60166c48 /src/resolve | |
parent | Merge pull request #7625 from thom311/th/const-strlen (diff) | |
download | systemd-0d5366733428b657e1b5b7700b461e878e00b578.tar.xz systemd-0d5366733428b657e1b5b7700b461e878e00b578.zip |
tree-wide: use __fsetlocking() instead of fxyz_unlocked()
Let's replace usage of fputc_unlocked() and friends by __fsetlocking(f,
FSETLOCKING_BYCALLER). This turns off locking for the entire FILE*,
instead of doing individual per-call decision whether to use normal
calls or _unlocked() calls.
This has various benefits:
1. It's easier to read and easier not to forget
2. It's more comprehensive, as fprintf() and friends are covered too
(as these functions have no _unlocked() counterpart)
3. Philosophically, it's a bit more correct, because it's more a
property of the file handle really whether we ever pass it on to another
thread, not of the operations we then apply to it.
This patch reworks all pieces of codes that so far used fxyz_unlocked()
calls to use __fsetlocking() instead. It also reworks all places that
use open_memstream(), i.e. use stdio FILE* for string manipulations.
Note that this in some way a revert of 4b61c8751135c58be043d86b9fef4c8ec7aadf18.
Diffstat (limited to 'src/resolve')
-rw-r--r-- | src/resolve/resolved-link.c | 32 | ||||
-rw-r--r-- | src/resolve/resolved-manager.c | 3 | ||||
-rw-r--r-- | src/resolve/resolved-resolv-conf.c | 54 |
3 files changed, 50 insertions, 39 deletions
diff --git a/src/resolve/resolved-link.c b/src/resolve/resolved-link.c index ed7bdc848a..e3e50eca53 100644 --- a/src/resolve/resolved-link.c +++ b/src/resolve/resolved-link.c @@ -19,6 +19,7 @@ ***/ #include <net/if.h> +#include <stdio_ext.h> #include "sd-network.h" @@ -1100,7 +1101,10 @@ int link_save_user(Link *l) { if (r < 0) goto fail; - fputs_unlocked("# This is private data. Do not parse.\n", f); + (void) __fsetlocking(f, FSETLOCKING_BYCALLER); + (void) fchmod(fileno(f), 0644); + + fputs("# This is private data. Do not parse.\n", f); v = resolve_support_to_string(l->llmnr_support); if (v) @@ -1117,11 +1121,11 @@ int link_save_user(Link *l) { if (l->dns_servers) { DnsServer *server; - fputs_unlocked("SERVERS=", f); + fputs("SERVERS=", f); LIST_FOREACH(servers, server, l->dns_servers) { if (server != l->dns_servers) - fputc_unlocked(' ', f); + fputc(' ', f); v = dns_server_string(server); if (!v) { @@ -1129,26 +1133,26 @@ int link_save_user(Link *l) { goto fail; } - fputs_unlocked(v, f); + fputs(v, f); } - fputc_unlocked('\n', f); + fputc('\n', f); } if (l->search_domains) { DnsSearchDomain *domain; - fputs_unlocked("DOMAINS=", f); + fputs("DOMAINS=", f); LIST_FOREACH(domains, domain, l->search_domains) { if (domain != l->search_domains) - fputc_unlocked(' ', f); + fputc(' ', f); if (domain->route_only) - fputc_unlocked('~', f); + fputc('~', f); - fputs_unlocked(DNS_SEARCH_DOMAIN_NAME(domain), f); + fputs(DNS_SEARCH_DOMAIN_NAME(domain), f); } - fputc_unlocked('\n', f); + fputc('\n', f); } if (!set_isempty(l->dnssec_negative_trust_anchors)) { @@ -1156,16 +1160,16 @@ int link_save_user(Link *l) { Iterator i; char *nta; - fputs_unlocked("NTAS=", f); + fputs("NTAS=", f); SET_FOREACH(nta, l->dnssec_negative_trust_anchors, i) { if (space) - fputc_unlocked(' ', f); + fputc(' ', f); - fputs_unlocked(nta, f); + fputs(nta, f); space = true; } - fputc_unlocked('\n', f); + fputc('\n', f); } r = fflush_and_check(f); diff --git a/src/resolve/resolved-manager.c b/src/resolve/resolved-manager.c index 983e6c091a..2dbf432df9 100644 --- a/src/resolve/resolved-manager.c +++ b/src/resolve/resolved-manager.c @@ -20,6 +20,7 @@ #include <netinet/in.h> #include <poll.h> +#include <stdio_ext.h> #include <sys/ioctl.h> #if HAVE_LIBIDN2 @@ -535,6 +536,8 @@ static int manager_sigusr1(sd_event_source *s, const struct signalfd_siginfo *si if (!f) return log_oom(); + (void) __fsetlocking(f, FSETLOCKING_BYCALLER); + LIST_FOREACH(scopes, scope, m->dns_scopes) dns_scope_dump(scope, f); diff --git a/src/resolve/resolved-resolv-conf.c b/src/resolve/resolved-resolv-conf.c index 4ea7490ac9..bad04d6a29 100644 --- a/src/resolve/resolved-resolv-conf.c +++ b/src/resolve/resolved-resolv-conf.c @@ -19,6 +19,7 @@ ***/ #include <resolv.h> +#include <stdio_ext.h> #include "alloc-util.h" #include "dns-domain.h" @@ -201,7 +202,7 @@ static void write_resolv_conf_server(DnsServer *s, FILE *f, unsigned *count) { } if (*count == MAXNS) - fputs_unlocked("# Too many DNS servers configured, the following entries may be ignored.\n", f); + fputs("# Too many DNS servers configured, the following entries may be ignored.\n", f); (*count)++; fprintf(f, "nameserver %s\n", dns_server_string(s)); @@ -217,43 +218,43 @@ static void write_resolv_conf_search( assert(domains); assert(f); - fputs_unlocked("search", f); + fputs("search", f); ORDERED_SET_FOREACH(domain, domains, i) { if (++count > MAXDNSRCH) { - fputs_unlocked("\n# Too many search domains configured, remaining ones ignored.", f); + fputs("\n# Too many search domains configured, remaining ones ignored.", f); break; } length += strlen(domain) + 1; if (length > 256) { - fputs_unlocked("\n# Total length of all search domains is too long, remaining ones ignored.", f); + fputs("\n# Total length of all search domains is too long, remaining ones ignored.", f); break; } - fputc_unlocked(' ', f); - fputs_unlocked(domain, f); + fputc(' ', f); + fputs(domain, f); } - fputs_unlocked("\n", f); + fputs("\n", f); } static int write_uplink_resolv_conf_contents(FILE *f, OrderedSet *dns, OrderedSet *domains) { Iterator i; - fputs_unlocked("# This file is managed by man:systemd-resolved(8). Do not edit.\n" - "#\n" - "# This is a dynamic resolv.conf file for connecting local clients directly to\n" - "# all known uplink DNS servers. This file lists all configured search domains.\n" - "#\n" - "# Third party programs must not access this file directly, but only through the\n" - "# symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a different way,\n" - "# replace this symlink by a static file or a different symlink.\n" - "#\n" - "# See man:systemd-resolved.service(8) for details about the supported modes of\n" - "# operation for /etc/resolv.conf.\n" - "\n", f); + fputs("# This file is managed by man:systemd-resolved(8). Do not edit.\n" + "#\n" + "# This is a dynamic resolv.conf file for connecting local clients directly to\n" + "# all known uplink DNS servers. This file lists all configured search domains.\n" + "#\n" + "# Third party programs must not access this file directly, but only through the\n" + "# symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a different way,\n" + "# replace this symlink by a static file or a different symlink.\n" + "#\n" + "# See man:systemd-resolved.service(8) for details about the supported modes of\n" + "# operation for /etc/resolv.conf.\n" + "\n", f); if (ordered_set_isempty(dns)) - fputs_unlocked("# No DNS servers known.\n", f); + fputs("# No DNS servers known.\n", f); else { unsigned count = 0; DnsServer *s; @@ -296,10 +297,8 @@ static int write_stub_resolv_conf_contents(FILE *f, OrderedSet *dns, OrderedSet int manager_write_resolv_conf(Manager *m) { _cleanup_ordered_set_free_ OrderedSet *dns = NULL, *domains = NULL; - _cleanup_free_ char *temp_path_uplink = NULL; - _cleanup_free_ char *temp_path_stub = NULL; - _cleanup_fclose_ FILE *f_uplink = NULL; - _cleanup_fclose_ FILE *f_stub = NULL; + _cleanup_free_ char *temp_path_uplink = NULL, *temp_path_stub = NULL; + _cleanup_fclose_ FILE *f_uplink = NULL, *f_stub = NULL; int r; assert(m); @@ -319,10 +318,15 @@ int manager_write_resolv_conf(Manager *m) { r = fopen_temporary_label(PRIVATE_UPLINK_RESOLV_CONF, PRIVATE_UPLINK_RESOLV_CONF, &f_uplink, &temp_path_uplink); if (r < 0) return log_warning_errno(r, "Failed to open private resolv.conf file for writing: %m"); + + (void) __fsetlocking(f_uplink, FSETLOCKING_BYCALLER); + (void) fchmod(fileno(f_uplink), 0644); + r = fopen_temporary_label(PRIVATE_STUB_RESOLV_CONF, PRIVATE_STUB_RESOLV_CONF, &f_stub, &temp_path_stub); if (r < 0) return log_warning_errno(r, "Failed to open private stub-resolv.conf file for writing: %m"); - (void) fchmod(fileno(f_uplink), 0644); + + (void) __fsetlocking(f_stub, FSETLOCKING_BYCALLER); (void) fchmod(fileno(f_stub), 0644); r = write_uplink_resolv_conf_contents(f_uplink, dns, domains); |