]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
daemon/network: unix socket pre-bind handling
authorTomas Krizek <tomas.krizek@nic.cz>
Fri, 10 Jan 2020 15:42:52 +0000 (16:42 +0100)
committerTomas Krizek <tomas.krizek@nic.cz>
Fri, 17 Jan 2020 12:39:35 +0000 (13:39 +0100)
Unlink the unix address in case the file exists, e.g. from previous
unclean run.
Also attempt to create the directory for unix socket in case it doesn't
exist.

daemon/network.c

index d41436fba3332f448426f75e53405cf813150076..08dd8dde077f1470ba17ac2725cee2f1dbe71ead 100644 (file)
@@ -22,6 +22,7 @@
 #include "daemon/worker.h"
 
 #include <assert.h>
+#include <libgen.h>
 #include <sys/un.h>
 #include <unistd.h>
 
@@ -263,6 +264,14 @@ static int open_endpoint(struct network *net, struct endpoint *ep,
        }
 
        if (sa) {
+               if (sa->sa_family == AF_UNIX) {
+                       struct sockaddr_un *sun = (struct sockaddr_un*)sa;
+                       char *dirc = strdup(sun->sun_path);
+                       char *dname = dirname(dirc);
+                       (void)unlink(sun->sun_path);  /** Attempt to unlink if socket path exists. */
+                       (void)mkdir(dname, S_IRWXU|S_IRWXG);  /** Attempt to create dir. */
+                       free(dirc);
+               }
                ep->fd = io_bind(sa, ep->flags.sock_type, &ep->flags);
                if (ep->fd < 0) return ep->fd;
        }