This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Ping Re: [PATCH] Make bindresvport() function to multithread-safe
- From: Peng Haitao <penght at cn dot fujitsu dot com>
- To: libc-alpha at sourceware dot org
- Date: Thu, 13 Sep 2012 13:59:41 +0800
- Subject: Ping Re: [PATCH] Make bindresvport() function to multithread-safe
- References: <4F3DED0D.7050504@cn.fujitsu.com>
On 02/17/2012 02:00 PM, Peng Haitao wrote:
>
> 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.
>
Ping.
--
Best Regards,
Peng
> 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)
>
--
Best Regards,
Peng