Fold priv_write_file() into its only remaining user
authorkrw <krw@openbsd.org>
Tue, 11 Apr 2017 10:40:14 +0000 (10:40 +0000)
committerkrw <krw@openbsd.org>
Tue, 11 Apr 2017 10:40:14 +0000 (10:40 +0000)
priv_write_resolv_conf() and move the latter into kroute.c
with all its priv_ friends.

No intentional functional change.

sbin/dhclient/dhclient.c
sbin/dhclient/dhcpd.h
sbin/dhclient/kroute.c

index a2189c2..beda165 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: dhclient.c,v 1.412 2017/04/10 21:47:44 krw Exp $      */
+/*     $OpenBSD: dhclient.c,v 1.413 2017/04/11 10:40:14 krw Exp $      */
 
 /*
  * Copyright 2004 Henning Brauer <henning@openbsd.org>
@@ -2547,34 +2547,6 @@ apply_ignore_list(char *ignore_list)
        memcpy(config->ignored_options, list, sizeof(config->ignored_options));
 }
 
-void
-priv_write_file(char *path, int flags, mode_t mode,
-    u_int8_t *contents, size_t sz)
-{
-       ssize_t n;
-       int fd;
-
-       fd = open(path, flags, mode);
-       if (fd == -1) {
-               log_warn("Couldn't open '%s'", path);
-               return;
-       }
-
-       n = write(fd, contents, sz);
-       if (n == -1)
-               log_warn("Couldn't write contents to '%s'", path);
-       else if ((size_t)n < sz)
-               log_warnx("Short contents write to '%s' (%zd vs %zu)", path,
-                   n, sz);
-
-       if (fchown(fd, 0, 0) == -1)
-               log_warn("fchown(fd, %d, %d) of '%s' failed", 0, 0, path);
-       if (fchmod(fd, mode) == -1)
-               log_warn("fchmod(fd, 0x%x) of '%s' failed", mode, path);
-
-       close(fd);
-}
-
 void
 set_lease_times(struct client_lease *lease)
 {
@@ -2742,28 +2714,6 @@ write_resolv_conf(u_int8_t *contents, size_t sz)
        flush_unpriv_ibuf("write_resolv_conf");
 }
 
-void
-priv_write_resolv_conf(struct interface_info *ifi, struct imsg *imsg)
-{
-       u_int8_t *contents;
-       size_t sz;
-
-       if (imsg->hdr.len < IMSG_HEADER_SIZE) {
-               log_warnx("short IMSG_WRITE_RESOLV_CONF");
-               return;
-       }
-
-       if (!resolv_conf_priority(ifi))
-               return;
-
-       contents = imsg->data;
-       sz = imsg->hdr.len - IMSG_HEADER_SIZE;
-
-       priv_write_file("/etc/resolv.conf",
-           O_WRONLY | O_CREAT | O_TRUNC,
-           S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH, contents, sz);
-}
-
 /*
  * add_direct_route is the equivalent of
  *
index 9f90520..ea88b3d 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: dhcpd.h,v 1.169 2017/04/08 20:16:04 krw Exp $ */
+/*     $OpenBSD: dhcpd.h,v 1.170 2017/04/11 10:40:14 krw Exp $ */
 
 /*
  * Copyright (c) 2004 Henning Brauer <henning@openbsd.org>
@@ -172,6 +172,7 @@ struct dhcp_timeout {
        void    *arg;
 };
 
+#define        _PATH_RESOLV_CONF       "/etc/resolv.conf"
 #define        _PATH_DHCLIENT_CONF     "/etc/dhclient.conf"
 #define        _PATH_DHCLIENT_DB       "/var/db/dhclient.leases"
 
index 5686aea..543f26b 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: kroute.c,v 1.88 2017/04/09 20:44:13 krw Exp $ */
+/*     $OpenBSD: kroute.c,v 1.89 2017/04/11 10:40:14 krw Exp $ */
 
 /*
  * Copyright 2012 Kenneth R Westerback <krw@openbsd.org>
@@ -18,6 +18,7 @@
 
 #include <sys/ioctl.h>
 #include <sys/socket.h>
+#include <sys/stat.h>
 #include <sys/sysctl.h>
 
 #include <arpa/inet.h>
@@ -30,6 +31,7 @@
 #include <netinet/if_ether.h>
 
 #include <errno.h>
+#include <fcntl.h>
 #include <ifaddrs.h>
 #include <imsg.h>
 #include <limits.h>
@@ -531,6 +533,47 @@ priv_cleanup(struct interface_info *ifi, struct imsg_hup *imsg)
        priv_delete_address(ifi, &dimsg);
 }
 
+/*
+ * priv_write_resolv_conf writes out a new resolv.conf.
+ */
+void
+priv_write_resolv_conf(struct interface_info *ifi, struct imsg *imsg)
+{
+       u_int8_t *contents;
+       ssize_t n;
+       size_t sz;
+       int fd;
+
+
+       if (imsg->hdr.len < IMSG_HEADER_SIZE) {
+               log_warnx("short IMSG_WRITE_RESOLV_CONF");
+               return;
+       }
+
+       if (!resolv_conf_priority(ifi))
+               return;
+
+       contents = imsg->data;
+       sz = imsg->hdr.len - IMSG_HEADER_SIZE;
+
+       fd = open(_PATH_RESOLV_CONF, O_WRONLY | O_CREAT | O_TRUNC,
+           S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
+
+       if (fd == -1) {
+               log_warn("Couldn't open '%s'", _PATH_RESOLV_CONF);
+               return;
+       }
+
+       n = write(fd, contents, sz);
+       if (n == -1)
+               log_warn("Couldn't write contents to '%s'", _PATH_RESOLV_CONF);
+       else if ((size_t)n < sz)
+               log_warnx("Short contents write to '%s' (%zd vs %zu)",
+                   _PATH_RESOLV_CONF, n, sz);
+
+       close(fd);
+}
+
 int
 resolv_conf_priority(struct interface_info *ifi)
 {