[PATCH 16/46] dup2: Use dup3 if arch does not have the dup2 syscall

Rich Felker dalias at aerifal.cx
Thu Nov 15 22:13:39 UTC 2012


On Tue, Nov 13, 2012 at 11:31:25AM +0000, Markos Chandras wrote:
> From: Markos Chandras <markos.chandras at imgtec.com>
> 
> Signed-off-by: Markos Chandras <markos.chandras at imgtec.com>
> ---
>  libc/sysdeps/linux/common/dup2.c |    8 +++++++-
>  1 files changed, 7 insertions(+), 1 deletions(-)
> 
> diff --git a/libc/sysdeps/linux/common/dup2.c b/libc/sysdeps/linux/common/dup2.c
> index 006f06b..e58105d 100644
> --- a/libc/sysdeps/linux/common/dup2.c
> +++ b/libc/sysdeps/linux/common/dup2.c
> @@ -10,6 +10,12 @@
>  #include <sys/syscall.h>
>  #include <unistd.h>
>  
> -
> +#if defined(__NR_dup3) && ! defined(__NR_dup2)
> +int dup2(int old, int newfd)
> +{
> +	return dup3(old, newfd, 0);
> +}
> +#else
>  _syscall2(int, dup2, int, oldfd, int, newfd)
> +#endif
>  libc_hidden_def(dup2)

This is not valid. dup3 explicitly fails if old==newfd, but dup2 is
required to work in this case. However you can't just return success
in that case, because old might not be valid. You need to perform
fcntl or something to determine if the fd is valid, and return an
error if it's not.

Rich


More information about the uClibc mailing list