This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
[PATCH v3] Make bindresvport() function to multithread-safe
- From: Peng Haitao <penght at cn dot fujitsu dot com>
- To: vapier at gentoo dot org
- Cc: libc-alpha at sourceware dot org
- Date: Mon, 24 Sep 2012 14:38:06 +0800
- Subject: [PATCH v3] Make bindresvport() function to multithread-safe
bindresvport() uses a static variable port which is 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 variable
will be added __thread.
Signed-off-by: Peng Haitao <penght@cn.fujitsu.com>
---
ChangeLog | 5 +++++
sunrpc/bindrsvprt.c | 11 ++++++++---
2 files changed, 13 insertions(+), 3 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index a04e104..8e5c5bc 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2012-09-24 Peng Haitao <penght@cn.fujitsu.com>
+
+ * sunrpc/bindrsvprt.c: Add __thread when define static's port,
+ getpid() is changed to gettid().
+
2012-09-21 Joseph Myers <joseph@codesourcery.com>
* libio/iopopen.c [_IO_HAVE_SYS_WAIT]: Make code unconditional.
diff --git a/sunrpc/bindrsvprt.c b/sunrpc/bindrsvprt.c
index d493c9f..f48c7f2 100644
--- a/sunrpc/bindrsvprt.c
+++ b/sunrpc/bindrsvprt.c
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2010, Oracle America, Inc.
+ * Copyright (c) 2010-2012, Oracle America, Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
@@ -42,7 +42,7 @@
int
bindresvport (int sd, struct sockaddr_in *sin)
{
- static short port;
+ static __thread short port;
struct sockaddr_in myaddr;
int i;
@@ -66,7 +66,12 @@ bindresvport (int sd, struct sockaddr_in *sin)
if (port == 0)
{
- port = (__getpid () % NPORTS) + STARTPORT;
+#ifdef INTERNAL_SYSCALL
+ INTERNAL_SYSCALL_DECL (err);
+ port = (INTERNAL_SYSCALL (gettid, err, 0) % NPORTS) + STARTPORT;
+#else
+ port = (INLINE_SYSCALL (gettid, 0) % NPORTS) + STARTPORT;
+#endif
}
/* Initialize to make gcc happy. */
--
1.7.11.4