X-Authentication-Warning: delorie.com: mail set sender to djgpp-bounces using -f X-Recipient: djgpp AT delorie DOT com Message-ID: <571E8009.3020503@gmx.de> Date: Mon, 25 Apr 2016 22:37:29 +0200 From: "Juan Manuel Guerrero (juan DOT guerrero AT gmx DOT de) [via djgpp AT delorie DOT com]" User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; de; rv:1.9.2.13) Gecko/20101206 SUSE/3.1.7 Thunderbird/3.1.7 MIME-Version: 1.0 To: djgpp AT delorie DOT com Subject: Re: Small adjustments of fcntl to increase posix compliance. References: <57169220 DOT 7070408 AT gmx DOT de> In-Reply-To: <57169220.7070408@gmx.de> Content-Type: text/plain; charset=ISO-8859-15; format=flowed Content-Transfer-Encoding: 7bit X-Provags-ID: V03:K0:vrVy0I52A5NWSPUpWQlYRcI9XwpWTKkVZxSF3LL5jEwG01xAMQJ JAfA6pLGQZ75b1o8wiaPD3MrjF/dCSd8b9gMJzghHc4ZJMhhSR89iifWYhw5oJ1A//moHah 3cLC82p5A122O+kuBumwFA7IUrb7MJ+4p2M57pHVBzKP6HG+YzqypjYHsVDGYpHFeEFcOQN klt+T6DmQWlWTPOImcnIQ== X-UI-Out-Filterresults: notjunk:1;V01:K0:Po1sQOunqFU=:46ifrKXsYWJT1CXuthgl5v mv9Bk2VRZEaoSvjz9xZ+MBDhWCG4vyaQBzOgiF8s7edXbAI7KoRtswfDc92q2qInAa66cptmJ JsLydf6ytTKrc1sx+KZIIXutBBGUZedAR0C2Y/EUE5oUkavGTZ+R053+Tsz8srEYiWxVT6VSs c/nvRFvBqO5yBSdJA0177UVbQ5uP9KeT3S3/QB6dmd+G4yS9BE2u58s7jm53ylO1yO4z54/Lf 32e7uXop1Z90zlmZHWORFsLTA8CznANxQr6SgWS17hjoX1O/QShN5U94kCP4v9vhyXnByFC/q C0HLeKdjyG8IaSeUw9hGVipdBbMy0FeuOA/MwVzEodTMbESJXuZRbngLzgCgXxxct4Go/OEA9 nbp6nQ/KrwEf/yjDVpeIrIdmUmSwmSFpHlvPrpgm01IaBj/444kncC9Nwx2A4vrlS14D44VCD 5tj2S1htp5oNDfZHYK5As2nsx39pfGg+DDbgmpbJ6S1CIAXnvrqzMTb4vGiYfbW0GQ6/OZ+ki wLx42MreL2bfVbEAThLeNpVU7brYLTUQGuhoGZ1TBqyaT+esLE62L4sS+NA1kaQkjO57oxGAo 3hLq6LfxBCvWJHQqoXHX0TrezUSaC/I2apvmDDSBlc/5FvttLg/qYUGpaRIfBmNsxj4NvZ32H cpRKzAzdVpzunEgU+sHGE6cZmxhoWmz2k9ejgi8GU5iK6g5kWqqXDSeBynI4acOhPToPvt0aF nz/TBPF4IEBkQhcrKr6PcTPrU+q6t3MrD9b1F+L3C9QvAvKZ7YFc47ooV3cRJr6lVFeznMQjA rPfxpDy Reply-To: djgpp AT delorie DOT com Am 19.04.2016 22:16, schrieb Juan Manuel Guerrero (juan DOT guerrero AT gmx DOT de) [via djgpp AT delorie DOT com]: > While I was porting some GNU programs lately I have observed that certain checks > for fcntl() fail. According to http://pubs.opengroup.org/onlinepubs/009695399/functions/fcntl.html > and to the check, fcntl shall return -1 and set errno to EBADF if the file > descriptor is invalid, this means closed or negative file descriptor. Invalid > descriptor produced by F_DUPFD shall set errno to EINVAL and not to EBADF as it > uses to be. Invalid commands shall set errno to EINVAL and not to ENOSYS as it > uses to be. For the case of unsupported commands by DJGPP like socket specific > commands F_GETOWN and F_SETOWN I still set errno to ENOSYS. I have also added > a small test program that shall verify the new behaviour. > > As usual suggestions, objections and comments are welcome. If I do not get a > response in a reasonable periode of time I will commit the change below. OFYI, I have applied the patch below. Regards, Juan M. Guerrero Index: djgpp/include/fcntl.h =================================================================== RCS file: /cvs/djgpp/djgpp/include/fcntl.h,v retrieving revision 1.11 diff -U 5 -r1.11 fcntl.h --- djgpp/include/fcntl.h 2 May 2015 07:31:45 -0000 1.11 +++ djgpp/include/fcntl.h 25 Apr 2016 20:22:37 -0000 @@ -1,5 +1,6 @@ +/* Copyright (C) 2016 DJ Delorie, see COPYING.DJ for details */ /* Copyright (C) 2012 DJ Delorie, see COPYING.DJ for details */ /* Copyright (C) 2009 DJ Delorie, see COPYING.DJ for details */ /* Copyright (C) 2003 DJ Delorie, see COPYING.DJ for details */ /* Copyright (C) 2001 DJ Delorie, see COPYING.DJ for details */ /* Copyright (C) 1998 DJ Delorie, see COPYING.DJ for details */ @@ -31,10 +32,12 @@ #define F_SETLK 7 #define F_SETLKW 8 #define F_GETLK64 9 #define F_SETLK64 10 #define F_SETLKW64 11 +#define F_GETOWN 12 +#define F_SETOWN 13 #define F_UNLCK 0 #define F_RDLCK 1 #define F_WRLCK 2 Index: djgpp/src/docs/kb/wc206.txi =================================================================== RCS file: /cvs/djgpp/djgpp/src/docs/kb/wc206.txi,v retrieving revision 1.1 diff -U 5 -r1.1 wc206.txi --- djgpp/src/docs/kb/wc206.txi 19 Apr 2016 20:29:35 -0000 1.1 +++ djgpp/src/docs/kb/wc206.txi 25 Apr 2016 20:22:37 -0000 @@ -8,5 +8,11 @@ If @var{existing_handle} is not a valid open file descriptor or if @var{new_handle} is out of the allowed range for file descriptors the call of @code{dup2} will fail and @code{errno} will be set to @code{EBADF}. With this adjustment, the @acronym{Posix} compliance of the @code{dup2} implementation is increased. + +@findex fcntl AT r{, and POSIX.1-2001 compliance} +If the passed file descriptors are not valid for the @code{F_DUPFD} command then +the call of @code{fcntl} will fail and @code{errno} will be set to @code{EINVAL}. +With this adjustment, the @acronym{Posix} compliance of the @code{fcntl} implementation +is increased. Index: djgpp/src/libc/posix/fcntl/fcntl.c =================================================================== RCS file: /cvs/djgpp/djgpp/src/libc/posix/fcntl/fcntl.c,v retrieving revision 1.12 diff -U 5 -r1.12 fcntl.c --- djgpp/src/libc/posix/fcntl/fcntl.c 25 Apr 2016 20:21:11 -0000 1.12 +++ djgpp/src/libc/posix/fcntl/fcntl.c 25 Apr 2016 20:22:38 -0000 @@ -268,11 +268,14 @@ int errno_save; /* Verify the descriptor is valid by retrieving the handle's device info word. */ if (dev_info == -1) + { + errno = EBADF; return dev_info; + } /* Allow a fd to override with a FSEXT. */ func = __FSEXT_get_function(fd); if (func) @@ -305,17 +308,17 @@ } if (tofd >= open_max) { - errno = EMFILE; + errno = EINVAL; return -1; } - errno = errno_save; - return dup2(fd, tofd); + errno = (tofd = dup2(fd, tofd)) == -1 ? EINVAL : errno_save; + return tofd; } case F_GETFD: { @@ -536,12 +539,20 @@ ret = _fcntl_lk64(fd, cmd, lock_r64); return ret; } - } + + + case F_GETOWN: + case F_SETOWN: + { + errno = ENOSYS; + return -1; + } + } /* In case fcntl is called with an unrecognized command. */ - errno = ENOSYS; + errno = EINVAL; return -1; } Index: djgpp/src/libc/posix/fcntl/fcntl.txh =================================================================== RCS file: /cvs/djgpp/djgpp/src/libc/posix/fcntl/fcntl.txh,v retrieving revision 1.11 diff -U 5 -r1.11 fcntl.txh --- djgpp/src/libc/posix/fcntl/fcntl.txh 26 Mar 2003 19:42:27 -0000 1.11 +++ djgpp/src/libc/posix/fcntl/fcntl.txh 25 Apr 2016 20:22:38 -0000 @@ -152,23 +152,25 @@ @subheading Return Value If an invalid or unsupported value is passed in @var{cmd}, or @var{fd} is an invalid file handle, the function returns -1 and sets @code{errno} to the appropriate value. Unsupported values of @var{cmd} cause -@code{ENOSYS} to be stored in @code{errno}. If @var{cmd} is -@code{F_DUPFD}, the function returns the new descriptor or -1 in case of -a failure. +@code{ENOSYS} to be stored in @code{errno}. Invalid values of @var{cmd} +cause @code{EINVAL} to be stored in @code{errno}. If @var{cmd} is @code{F_DUPFD}, +the function returns the new descriptor or -1 in case of a failure and sets +@code{errno} to @code{EINVAL}. If @var{fd} is an invalid file handle, +the function sets @code{errno} to @code{EBADF}. Lock requests which specify the open file's current @code{EOF} position as the value of @var{l_start} and zero as the @var{l_len} value will fail, returning -1 with @code{errno} set to @code{EACCES}. @subheading Portability @port-note posix Contrary to Posix requirement, the handle returned by @code{F_DUPFD} shares the @code{FD_CLOEXEC} flag with @var{fd} (unless they are on different sides of the 20-handle mark), since DOS/Windows only maintain a single set of bits for all the handles associated with the same call to @code{open}. -@portability !ansi, posix +@portability !ansi, posix-1003.1-2001 @subheading Example @example Index: djgpp/tests/libc/posix/fcntl/makefile =================================================================== RCS file: /cvs/djgpp/djgpp/tests/libc/posix/fcntl/makefile,v retrieving revision 1.3 diff -U 5 -r1.3 makefile --- djgpp/tests/libc/posix/fcntl/makefile 1 Feb 2001 19:24:45 -0000 1.3 +++ djgpp/tests/libc/posix/fcntl/makefile 25 Apr 2016 20:22:38 -0000 @@ -1,9 +1,10 @@ TOP=../.. SRC += binpr.c SRC += bt.c +SRC += fcntl.c SRC += fcntl3gb.c SRC += inherit.c SRC += makef3gb.c SRC += open.c SRC += tfcntl.c diff -aprNU7 djgpp.orig/tests/libc/posix/fcntl/fcntl.c djgpp/tests/libc/posix/fcntl/fcntl.c --- djgpp.orig/tests/libc/posix/fcntl/fcntl.c 1969-12-31 18:50:15 -0509 +++ djgpp/tests/libc/posix/fcntl/fcntl.c 2016-04-25 19:22:12 -0509 @@ -0,0 +1,48 @@ +#include +#include +#include +#include +#include +#include + + +int +main (void) +{ + int result = 0; + int bad_fd = INT_MAX; + struct rlimit rlim; + + + if (getrlimit(RLIMIT_NOFILE, &rlim) == 0 + && 0 <= rlim.rlim_cur && rlim.rlim_cur <= INT_MAX) + bad_fd = rlim.rlim_cur; + + if (fcntl(0, F_DUPFD, -1) != -1) result |= 1; + if (errno != EINVAL) result |= 2; + + if (fcntl(0, F_DUPFD, bad_fd) != -1) result |= 4; + if (errno != EINVAL) result |= 8; + + close (0); + if (fcntl(0, F_DUPFD, STDERR_FILENO + 1) != -1) result |= 16; + if (errno != EBADF) result |= 32; + + { + int fd; + fd = open(".", O_RDONLY); + if (fd == -1) + result |= 64; + else if (fcntl(fd, F_DUPFD, STDERR_FILENO + 1) == -1) + result |= 128; + + close (fd); + } + + if (result) + printf("fcntl check failed with result = %d\n", result); + else + printf("fcntl check passed.\n"); + + return result; +}