From: Ian Campbell Subject: Re: [PATCH v2] libvchan: tidy up usages of fcntl in select-type sample application Date: Thu, 31 Oct 2013 21:47:27 +0000 Message-ID: <1383256047.5436.150.camel@dagon.hellion.org.uk> References: <1383119525-26033-26-git-send-email-mattjd@gmail.com> <1383191093-15751-1-git-send-email-mattjd@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: In-Reply-To: <1383191093-15751-1-git-send-email-mattjd@gmail.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: Matthew Daley Cc: Andrew Cooper , Daniel De Graaf , Stefano Stabellini , Ian Jackson , xen-devel@lists.xen.org List-Id: xen-devel@lists.xenproject.org On Thu, 2013-10-31 at 16:44 +1300, Matthew Daley wrote: > Namely, don't overwrite all the other flags when twiddling O_NONBLOCK, > and add basic error handling. > > Coverity-ID: 1055041 > Signed-off-by: Matthew Daley > --- > v2: Adjust other fcntl usage further down as well. Use F_GETFL to modify > only the O_NONBLOCK flag. Both suggested by Daniel De Graaf. > Improve commit message. > > tools/libvchan/node-select.c | 16 +++++++++++++--- > 1 file changed, 13 insertions(+), 3 deletions(-) > > diff --git a/tools/libvchan/node-select.c b/tools/libvchan/node-select.c > index 6c6c19e..acd9bae 100644 > --- a/tools/libvchan/node-select.c > +++ b/tools/libvchan/node-select.c > @@ -90,6 +90,7 @@ int main(int argc, char **argv) > { > int ret; > int libxenvchan_fd; > + int flags; > if (argc < 4 || argv[3][0] != '/') > usage(argv); > if (!strcmp(argv[1], "server")) { > @@ -105,8 +106,13 @@ int main(int argc, char **argv) > exit(1); > } > > - fcntl(0, F_SETFL, O_NONBLOCK); > - fcntl(1, F_SETFL, O_NONBLOCK); > + if ((flags = fcntl(0, F_GETFL)) == -1 || > + fcntl(0, F_SETFL, flags | O_NONBLOCK) == -1 || > + (flags = fcntl(1, F_GETFL)) == -1 || > + fcntl(1, F_SETFL, flags | O_NONBLOCK) == -1) { > + perror("fcntl"); > + exit(1); > + } I'm sure this is correct but it feels like it could be written in a more straightforward/readable way as a series of ifs rather than bundling them all together. Since you do the same thing twice to two file descs it might even be a candidate for a small helper function? > > libxenvchan_fd = libxenvchan_fd_for_select(ctrl); > for (;;) { > @@ -153,7 +159,11 @@ int main(int argc, char **argv) > stdout_wr(); > } > if (!libxenvchan_is_open(ctrl)) { > - fcntl(1, F_SETFL, 0); > + if ((flags = fcntl(1, F_GETFL)) == -1 || > + fcntl(1, F_SETFL, flags & ~O_NONBLOCK) == -1) { > + perror("fcntl"); > + exit(1); > + } > while (outsiz) > stdout_wr(); > return 0;