This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Make bindresvport() function to multithread-safe


bindresvport() uses two static variables port and startport which are not
protected. It is not safe when in multithread circumstance.

bindresvport() select a port number from the range 512 to 1023, when in
multithread circumstance, the port may be 1024. So the static variables will be
protected.

Signed-off-by: Peng Haitao <penght@cn.fujitsu.com>
---
 ChangeLog           |    4 ++++
 sunrpc/bindrsvprt.c |   10 ++++++++++
 2 files changed, 14 insertions(+), 0 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 58d02d6..42bfe3a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2012-02-16  Peng Haitao  <penght@cn.fujitsu.com>
+
+	* sunrpc/bindrsvprt.c: Add lock to protect static variable.
+
 2012-02-16  Richard Henderson  <rth@redhat.com>
 
 	* sysdeps/s390/s390-32/crti.S, sysdeps/s390/s390-32/crtn.S: New files.
diff --git a/sunrpc/bindrsvprt.c b/sunrpc/bindrsvprt.c
index d493c9f..f2fdefb 100644
--- a/sunrpc/bindrsvprt.c
+++ b/sunrpc/bindrsvprt.c
@@ -35,6 +35,12 @@
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
+#include <bits/libc-lock.h>
+
+/*
+ * Locks the static variables in this file.
+ */
+__libc_lock_define_initialized (static, lock);
 
 /*
  * Bind a socket to a privileged IP port
@@ -64,6 +70,8 @@ bindresvport (int sd, struct sockaddr_in *sin)
       return -1;
     }
 
+  __libc_lock_lock (lock);
+
   if (port == 0)
     {
       port = (__getpid () % NPORTS) + STARTPORT;
@@ -94,6 +102,8 @@ bindresvport (int sd, struct sockaddr_in *sin)
       goto again;
     }
 
+  __libc_lock_unlock (lock);
+
   return res;
 }
 libc_hidden_def (bindresvport)
-- 
1.7.1

-- 
Best Regards,
Peng


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]