Re: [PATCH] tools/counter: close fd when exit

From: Andy Shevchenko
Date: Tue Sep 03 2024 - 19:19:55 EST


Tue, Sep 03, 2024 at 04:01:21PM +0800, zhangjiao2 kirjoitti:
> From: zhang jiao <zhangjiao2@xxxxxxxxxxxxxxxxxxxx>
>
> close fd when exit the program

Please, mind English grammar and punctuation.
Also this doesn't state what the problem is.

...

> if (ret == -1) {
> fprintf(stderr, "Error adding watches[%d]: %s\n", i,
> strerror(errno));
> + close(fd);

Since fd is not used in the messaging it's better to close it before printing
anything. Ditto for other cases.

...

> if (ret != sizeof(event_data)) {
> fprintf(stderr, "Failed to read event data\n");
> + close(fd);

> return -EIO;

Side note: This will return garbage to the userspace. Should be

return EIO;

> }

--
With Best Regards,
Andy Shevchenko