[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Xen-devel] [PATCH v2] libvchan: tidy up usages of fcntl in select-type sample application



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 <mattjd@xxxxxxxxx>
> ---
> 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;



_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxx
http://lists.xen.org/xen-devel


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.