Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/netbsd-7]: src Pull up following revision(s) (requested by christos in t...



details:   https://anonhg.NetBSD.org/src/rev/d1c2e47a2823
branches:  netbsd-7
changeset: 799836:d1c2e47a2823
user:      martin <martin%NetBSD.org@localhost>
date:      Sun Mar 06 18:05:08 2016 +0000

description:
Pull up following revision(s) (requested by christos in ticket #1123):
        external/bsd/blacklist/lib/bl.c: revision 1.27
        lib/Makefile: revision 1.237
        external/bsd/blacklist/lib/Makefile: revision 1.4
        external/bsd/blacklist/lib/Makefile: revision 1.5
Move blacklistd later now that it depends on libpthread (From Rin Okuyama)
typo
Add a mutex to prevent races during initialization code from multiple threads.
Found in named.

diffstat:

 external/bsd/blacklist/lib/Makefile |   5 +++-
 external/bsd/blacklist/lib/bl.c     |  45 +++++++++++++++++++++++++++++++-----
 lib/Makefile                        |   4 +-
 3 files changed, 44 insertions(+), 10 deletions(-)

diffs (185 lines):

diff -r 3b343f66d5af -r d1c2e47a2823 external/bsd/blacklist/lib/Makefile
--- a/external/bsd/blacklist/lib/Makefile       Sun Mar 06 18:01:48 2016 +0000
+++ b/external/bsd/blacklist/lib/Makefile       Sun Mar 06 18:05:08 2016 +0000
@@ -1,7 +1,10 @@
-# $NetBSD: Makefile,v 1.3.2.2 2015/04/30 06:07:34 riz Exp $
+# $NetBSD: Makefile,v 1.3.2.3 2016/03/06 18:05:08 martin Exp $
 
 USE_SHLIBDIR=   yes
 
+CPPFLAGS+=-D_REENTRANT
+DPADD+=${LIBPTHREAD}
+LDADD+=-lpthread
 LIB=blacklist
 SRCS=bl.c blacklist.c
 MAN=libblacklist.3
diff -r 3b343f66d5af -r d1c2e47a2823 external/bsd/blacklist/lib/bl.c
--- a/external/bsd/blacklist/lib/bl.c   Sun Mar 06 18:01:48 2016 +0000
+++ b/external/bsd/blacklist/lib/bl.c   Sun Mar 06 18:05:08 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: bl.c,v 1.24.2.3 2015/06/02 20:32:44 snj Exp $  */
+/*     $NetBSD: bl.c,v 1.24.2.4 2016/03/06 18:05:08 martin Exp $       */
 
 /*-
  * Copyright (c) 2014 The NetBSD Foundation, Inc.
@@ -33,7 +33,7 @@
 #endif
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: bl.c,v 1.24.2.3 2015/06/02 20:32:44 snj Exp $");
+__RCSID("$NetBSD: bl.c,v 1.24.2.4 2016/03/06 18:05:08 martin Exp $");
 
 #include <sys/param.h>
 #include <sys/types.h>
@@ -53,6 +53,9 @@
 #include <errno.h>
 #include <stdarg.h>
 #include <netinet/in.h>
+#ifdef _REENTRANT
+#include <pthread.h>
+#endif
 
 #include "bl.h"
 
@@ -66,6 +69,16 @@
 } bl_message_t;
 
 struct blacklist {
+#ifdef _REENTRANT
+       pthread_mutex_t b_mutex;
+# define BL_INIT(b)    pthread_mutex_init(&b->b_mutex, NULL)
+# define BL_LOCK(b)    pthread_mutex_lock(&b->b_mutex)
+# define BL_UNLOCK(b)  pthread_mutex_unlock(&b->b_mutex)
+#else
+# define BL_INIT(b)    do {} while(/*CONSTCOND*/0)
+# define BL_LOCK(b)    BL_INIT(b)
+# define BL_UNLOCK(b)  BL_INIT(b)
+#endif
        int b_fd;
        int b_connected;
        struct sockaddr_un b_sun;
@@ -88,13 +101,17 @@
 }
 
 static void
-bl_reset(bl_t b)
+bl_reset(bl_t b, bool locked)
 {
        int serrno = errno;
+       if (!locked)
+               BL_LOCK(b);
        close(b->b_fd);
        errno = serrno;
        b->b_fd = -1;
        b->b_connected = -1;
+       if (!locked)
+               BL_UNLOCK(b);
 }
 
 static void
@@ -129,12 +146,15 @@
 #define SOCK_NOSIGPIPE 0
 #endif
 
+       BL_LOCK(b);
+
        if (b->b_fd == -1) {
                b->b_fd = socket(PF_LOCAL,
                    SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK|SOCK_NOSIGPIPE, 0);
                if (b->b_fd == -1) {
                        bl_log(b->b_fun, LOG_ERR, "%s: socket failed (%m)",
                            __func__);
+                       BL_UNLOCK(b);
                        return -1;
                }
 #if SOCK_CLOEXEC == 0
@@ -153,9 +173,16 @@
 #endif
        }
 
-       if (bl_isconnected(b))
+       if (bl_isconnected(b)) {
+               BL_UNLOCK(b);
                return 0;
+       }
 
+       /*
+        * We try to connect anyway even when we are a server to verify
+        * that no other server is listening to the socket. If we succeed
+        * to connect and we are a server, someone else owns it.
+        */
        rv = connect(b->b_fd, (const void *)sun, (socklen_t)sizeof(*sun));
        if (rv == 0) {
                if (srv) {
@@ -177,6 +204,7 @@
                                    __func__, sun->sun_path);
                                b->b_connected = 1;
                        }
+                       BL_UNLOCK(b);
                        return -1;
                }
                bl_log(b->b_fun, LOG_DEBUG, "Connected to blacklist server",
@@ -237,9 +265,11 @@
        }
 #endif
 
+       BL_UNLOCK(b);
        return 0;
 out:
-       bl_reset(b);
+       bl_reset(b, true);
+       BL_UNLOCK(b);
        return -1;
 }
 
@@ -252,6 +282,7 @@
        b->b_fun = fun == NULL ? vsyslog : fun;
        b->b_fd = -1;
        b->b_connected = -1;
+       BL_INIT(b);
 
        memset(&b->b_sun, 0, sizeof(b->b_sun));
        b->b_sun.sun_family = AF_LOCAL;
@@ -272,7 +303,7 @@
 void
 bl_destroy(bl_t b)
 {
-       bl_reset(b);
+       bl_reset(b, false);
        free(b);
 }
 
@@ -377,7 +408,7 @@
                return -1;
 
        if ((sendmsg(b->b_fd, &msg, 0) == -1) && tried++ < NTRIES) {
-               bl_reset(b);
+               bl_reset(b, false);
                goto again;
        }
        return tried >= NTRIES ? -1 : 0;
diff -r 3b343f66d5af -r d1c2e47a2823 lib/Makefile
--- a/lib/Makefile      Sun Mar 06 18:01:48 2016 +0000
+++ b/lib/Makefile      Sun Mar 06 18:05:08 2016 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: Makefile,v 1.215.2.2 2015/04/30 06:07:34 riz Exp $
+#      $NetBSD: Makefile,v 1.215.2.3 2016/03/06 18:05:09 martin Exp $
 #      from: @(#)Makefile      5.25.1.1 (Berkeley) 5/7/91
 
 .include <bsd.own.mk>
@@ -51,7 +51,6 @@
 .endif
 
 SUBDIR+=       ../external/bsd/am-utils/lib
-SUBDIR+=       ../external/bsd/blacklist/lib
 
 SUBDIR+=       ../external/bsd/flex/lib
 SUBDIR+=       ../external/bsd/tre/lib
@@ -96,6 +95,7 @@
 SUBDIR+=       ../crypto/external/bsd/netpgp/lib/verify # depends on libz
 .endif
 
+SUBDIR+=       ../external/bsd/blacklist/lib           # depends on libpthread
 SUBDIR+=       ../external/bsd/elftoolchain/lib/libdwarf # depends on libelf
 SUBDIR+=       ../external/mit/lua/lib # depends on libm
 SUBDIR+=       libcurses       # depends on libterminfo



Home | Main Index | Thread Index | Old Index