Is it considered to be ok to not check the return code of close(2) in base?
Konstantin Belousov
kostikbel at gmail.com
Mon Jan 8 18:42:42 UTC 2018
On Mon, Jan 08, 2018 at 10:13:19AM -0800, Conrad Meyer wrote:
> On Mon, Jan 8, 2018 at 10:05 AM, Andrew Duane <aduane at juniper.net> wrote:
> > Of course, my OCD will kick in and say this would need to be something like:
> >
> > #ifdef DEBUG_CLOSE
> > #define close(f) do {if (close(f) < 0) assert(errno != EBADF); } while (0)
> > #endif
> >
> > Have to watch those macro replacements like "if (need_to_close) close(f);". And the close succeeding :-)
>
> Of course, this has turned into nerd sniping. But I'll take my turn.
>
> Better to use the GCC "statement expression" extension so that the
> return value of "close()" can still be used by callers naive to the
> macro.
>
> #define close(f) ({ int __ret; __ret = (close)(f); if (__ret < 0)
> assert(errno != EBADF); __ret; })
You can create a dso which interposes close(3), calls original close()
resolved by dlsym(RTLD_NEXT), and checks for the error, for use with
LD_PRELOAD environment variable. No need to vandali^H^H^H^hack on either
kernel nor userland code.
More information about the freebsd-hackers
mailing list