From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp48.i.mail.ru (smtp48.i.mail.ru [94.100.177.108]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id BBE324696C3 for ; Fri, 10 Apr 2020 15:20:28 +0300 (MSK) Date: Fri, 10 Apr 2020 15:20:27 +0300 From: Alexander Turenko Message-ID: <20200410122027.r7vxyjsy6lgmuclf@tkn_work_nb> References: <20200410091824.GR3072@uranus> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20200410091824.GR3072@uranus> Subject: Re: [Tarantool-patches] [PATCH 06/13] popen: decouple logger fd from stderr List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Cyrill Gorcunov Cc: tarantool-patches@dev.tarantool.org > > + /* > > + * 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) {