* [PATCH v2] tools/counter: Close fd when exit
@ 2024-09-04 1:42 zhangjiao2
2024-09-04 7:35 ` Andy Shevchenko
2024-09-29 10:02 ` William Breathitt Gray
0 siblings, 2 replies; 3+ messages in thread
From: zhangjiao2 @ 2024-09-04 1:42 UTC (permalink / raw)
To: andy.shevchenko; +Cc: wbg, linux-iio, linux-kernel, zhang jiao
From: zhang jiao <zhangjiao2@cmss.chinamobile.com>
Since fd is not used in the messaging it's better to
close it before printing anything. Ditto for other cases.
Signed-off-by: zhang jiao <zhangjiao2@cmss.chinamobile.com>
---
v1->v2:
Close fd before fprintf.
tools/counter/counter_example.c | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/tools/counter/counter_example.c b/tools/counter/counter_example.c
index be55287b950f..37569208c235 100644
--- a/tools/counter/counter_example.c
+++ b/tools/counter/counter_example.c
@@ -57,12 +57,14 @@ int main(void)
if (ret == -1) {
+ close(fd);
fprintf(stderr, "Error adding watches[%d]: %s\n", i,
strerror(errno));
return 1;
}
}
ret = ioctl(fd, COUNTER_ENABLE_EVENTS_IOCTL);
if (ret == -1) {
+ close(fd);
perror("Error enabling events");
return 1;
}
@@ -70,11 +72,13 @@ int main(void)
ret = read(fd, event_data, sizeof(event_data));
if (ret == -1) {
+ close(fd);
perror("Failed to read event data");
return 1;
}
if (ret != sizeof(event_data)) {
+ close(fd);
fprintf(stderr, "Failed to read event data\n");
return -EIO;
}
@@ -88,5 +92,6 @@ int main(void)
strerror(event_data[1].status));
}
+ close(fd);
return 0;
}
--
2.33.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH v2] tools/counter: Close fd when exit
2024-09-04 1:42 [PATCH v2] tools/counter: Close fd when exit zhangjiao2
@ 2024-09-04 7:35 ` Andy Shevchenko
2024-09-29 10:02 ` William Breathitt Gray
1 sibling, 0 replies; 3+ messages in thread
From: Andy Shevchenko @ 2024-09-04 7:35 UTC (permalink / raw)
To: zhangjiao2; +Cc: wbg, linux-iio, linux-kernel
On Wed, Sep 4, 2024 at 4:42 AM zhangjiao2
<zhangjiao2@cmss.chinamobile.com> wrote:
> Since fd is not used in the messaging it's better to
> close it before printing anything.
It was my comment which you addressed. Thanks for that.
But it does not explain what the problem is.
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH v2] tools/counter: Close fd when exit
2024-09-04 1:42 [PATCH v2] tools/counter: Close fd when exit zhangjiao2
2024-09-04 7:35 ` Andy Shevchenko
@ 2024-09-29 10:02 ` William Breathitt Gray
1 sibling, 0 replies; 3+ messages in thread
From: William Breathitt Gray @ 2024-09-29 10:02 UTC (permalink / raw)
To: zhangjiao2; +Cc: andy.shevchenko, linux-iio, linux-kernel, David Laight
[-- Attachment #1: Type: text/plain, Size: 949 bytes --]
On Wed, Sep 04, 2024 at 09:42:53AM +0800, zhangjiao2 wrote:
> From: zhang jiao <zhangjiao2@cmss.chinamobile.com>
>
> Since fd is not used in the messaging it's better to
> close it before printing anything. Ditto for other cases.
>
> Signed-off-by: zhang jiao <zhangjiao2@cmss.chinamobile.com>
Hello zhang,
I appreciate you for submitting this patch. Before I can accept it, I
need to understand the reason for it.
Previously counter_example.c did call close() before returning, but as
David Laight pointed out, we removed it for being redundant when the
kernel closes file descriptors on exit, as well as possibly changing
errno before perror() and strerror() are called.
Is this patch made to address a particular bug you have discovered? I
would like to document the rationale for this change in the commit
message so we properly understand the reason for calling close() here.
Sincerely,
William Breathitt Gray
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 228 bytes --]
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2024-09-29 10:03 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-09-04 1:42 [PATCH v2] tools/counter: Close fd when exit zhangjiao2
2024-09-04 7:35 ` Andy Shevchenko
2024-09-29 10:02 ` William Breathitt Gray
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox