gnupg_reopen_std - descriptor state problem

Marcin Gryszkalis mg at fork.pl
Thu Sep 27 19:56:39 CEST 2018


On Friday, September 7, 2018 4:58:04 AM CEST, NIIBE Yutaka wrote:
> I wonder if changes like following make sense to see if fd is valid or
> not.  My theory is that fcntl is cheaper/simpler operation then fstat,
> and might be better in this case.

I tested your patch:
 - in case of manually closing stdin, ie. gpg2 --version <&-

 44239 gpg2     CALL  fcntl(0,F_GETFD,<invalid>0xffffeb88)
 44239 gpg2     RET   fcntl -1 errno 9 Bad file descriptor
 44239 gpg2     CALL  openat(AT_FDCWD,0x4abb8b,0<O_RDONLY>,<unused>0)
 44239 gpg2     NAMI  "/dev/null"
 44239 gpg2     RET   openat 0
 44239 gpg2     CALL  fcntl(0x1,F_GETFD,0)
 44239 gpg2     RET   fcntl 0
 44239 gpg2     CALL  fcntl(0x2,F_GETFD,0)
 44239 gpg2     RET   fcntl 0

 - when calling from my app fcntl always returns 0 - and it's good. 
I prepared patch version that uses both tests (fcntl and fstat) and it 
behaves 
as expected (when called from the perl app):

 42591 gpg2     CALL  fcntl(0,F_GETFD,<invalid>0xffffe720)
 42591 gpg2     RET   fcntl 0 <=== fcntl is right
 42591 gpg2     CALL  fstat(0,0x7fffffffdf40)
 42591 gpg2     RET   fstat -1 errno 9 Bad file descriptor <=== fstat is 
wrong :)
 42591 gpg2     CALL  close(0)
 42591 gpg2     RET   close 0
 42591 gpg2     CALL  openat(AT_FDCWD,0x4abc1b,0<O_RDONLY>,<unused>0)
 42591 gpg2     NAMI  "/dev/null"
 42591 gpg2     RET   openat 0
 42591 gpg2     CALL  fcntl(0x1,F_GETFD,0)
 42591 gpg2     RET   fcntl 0
 42591 gpg2     CALL  fcntl(0x2,F_GETFD,0)
 42591 gpg2     RET   fcntl 0

So I guess you could merge the patch.
Thanks for help with this unusual case :)

best regards
-- 
Marcin Gryszkalis, PGP 0xA5DBEEC7 http://fork.pl/gpg.txt




More information about the Gnupg-devel mailing list