[Tarantool-patches] [PATCH 06/13] popen: decouple logger fd from stderr

Alexander Turenko alexander.turenko at tarantool.org
Fri Apr 10 15:20:27 MSK 2020


> > +	/*
> > +	 * We will call dup() in a loop until it will return
> > +	 * fd > STDERR_FILENO. The array `discarded_fds` stores
> > +	 * intermediate fds to close them after all dup() calls.
> > +	 */
> > +	static_assert(STDERR_FILENO + 1 == 3,
> > +		      "Unexpected STDERR_FILENO constant");
> 
> We already have (in popen_new)
> 
> 	static_assert(STDIN_FILENO == 0 &&
> 		      STDOUT_FILENO == 1 &&
> 		      STDERR_FILENO == 2,
> 		      "stdin/out/err are not posix compatible");
> 
> no need for this again.
> 
> > +	int discarded_fds[STDERR_FILENO + 1] = {-1, -1, -1};
> 
> And here we could
> 
> 	int discarded_fds[POPEN_FLAG_FD_STDEND_BIT]
> 
> the POPEN_FLAG_FD_STDEND_BIT constant introduced exactly for that.

Okay, thanks!

> 
> > +
> > +	for (size_t i = 0; i < lengthof(discarded_fds) + 1; ++i) {
> > +		int new_fd = dup(fd);
> > +		if (new_fd < 0) {
> > +			save_errno = errno;
> > +			break;
> > +		}
> > +
> > +		/* Found wanted fd. */
> > +		if (new_fd > STDERR_FILENO) {
> > +			res = new_fd;
> > +			break;
> > +		}
> > +
> > +		/* Save to close then. */
> > +		assert(i < lengthof(discarded_fds));
> > +		discarded_fds[i] = new_fd;
> > +	}
> > +
> > +	/* Close all intermediate fds (if any). */
> > +	for (size_t i = 0; i < lengthof(discarded_fds); ++i)
> > +		if (discarded_fds[i] >= 0)
> > +			close(discarded_fds[i]);
> 
> Wrap for() with {} please.

No problem.

Changed:

diff --git a/src/lib/core/popen.c b/src/lib/core/popen.c
index b8ce77494..ef2f8e2aa 100644
--- a/src/lib/core/popen.c
+++ b/src/lib/core/popen.c
@@ -90,9 +90,7 @@ dup_not_std_streams(int fd)
         * fd > STDERR_FILENO. The array `discarded_fds` stores
         * intermediate fds to close them after all dup() calls.
         */
-       static_assert(STDERR_FILENO + 1 == 3,
-                     "Unexpected STDERR_FILENO constant");
-       int discarded_fds[STDERR_FILENO + 1] = {-1, -1, -1};
+       int discarded_fds[POPEN_FLAG_FD_STDEND_BIT] = {-1, -1, -1};
 
        for (size_t i = 0; i < lengthof(discarded_fds) + 1; ++i) {
                int new_fd = dup(fd);
@@ -113,9 +111,10 @@ dup_not_std_streams(int fd)
        }
 
        /* Close all intermediate fds (if any). */
-       for (size_t i = 0; i < lengthof(discarded_fds); ++i)
+       for (size_t i = 0; i < lengthof(discarded_fds); ++i) {
                if (discarded_fds[i] >= 0)
                        close(discarded_fds[i]);
+       }
 
        /* Report an error if it occurs. */
        if (res < 0) {


More information about the Tarantool-patches mailing list