* [PATCH] tools/counter: close fd when exit
@ 2024-09-03 8:01 zhangjiao2
2024-09-03 23:19 ` Andy Shevchenko
0 siblings, 1 reply; 3+ messages in thread
From: zhangjiao2 @ 2024-09-03 8:01 UTC (permalink / raw)
To: wbg; +Cc: linux-iio, linux-kernel, zhang jiao
From: zhang jiao <zhangjiao2@cmss.chinamobile.com>
close fd when exit the program
Signed-off-by: zhang jiao <zhangjiao2@cmss.chinamobile.com>
---
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) {
fprintf(stderr, "Error adding watches[%d]: %s\n", i,
strerror(errno));
+ close(fd);
return 1;
}
}
ret = ioctl(fd, COUNTER_ENABLE_EVENTS_IOCTL);
if (ret == -1) {
perror("Error enabling events");
+ close(fd);
return 1;
}
@@ -70,11 +72,13 @@ int main(void)
ret = read(fd, event_data, sizeof(event_data));
if (ret == -1) {
perror("Failed to read event data");
+ close(fd);
return 1;
}
if (ret != sizeof(event_data)) {
fprintf(stderr, "Failed to read event data\n");
+ close(fd);
return -EIO;
}
@@ -88,5 +92,6 @@ int main(void)
strerror(event_data[1].status));
}
+ close(fd);
return 0;
}
--
2.33.0
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH] tools/counter: close fd when exit
2024-09-03 8:01 [PATCH] tools/counter: close fd when exit zhangjiao2
@ 2024-09-03 23:19 ` Andy Shevchenko
2024-09-08 20:08 ` David Laight
0 siblings, 1 reply; 3+ messages in thread
From: Andy Shevchenko @ 2024-09-03 23:19 UTC (permalink / raw)
To: zhangjiao2; +Cc: wbg, linux-iio, linux-kernel
Tue, Sep 03, 2024 at 04:01:21PM +0800, zhangjiao2 kirjoitti:
> From: zhang jiao <zhangjiao2@cmss.chinamobile.com>
>
> 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
^ permalink raw reply [flat|nested] 3+ messages in thread* RE: [PATCH] tools/counter: close fd when exit
2024-09-03 23:19 ` Andy Shevchenko
@ 2024-09-08 20:08 ` David Laight
0 siblings, 0 replies; 3+ messages in thread
From: David Laight @ 2024-09-08 20:08 UTC (permalink / raw)
To: 'Andy Shevchenko', zhangjiao2; +Cc: wbg, linux-iio, linux-kernel
From: Andy Shevchenko
> Sent: 04 September 2024 00:20
>
> Tue, Sep 03, 2024 at 04:01:21PM +0800, zhangjiao2 kirjoitti:
> > From: zhang jiao <zhangjiao2@cmss.chinamobile.com>
> >
> > close fd when exit the program
>
> Please, mind English grammar and punctuation.
> Also this doesn't state what the problem is.
After all, when the program exits the kernel closes all the files.
> > 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.
Unless close() decides to change errno...
>
> ...
>
> > 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;
If that is the return value from main() it is equally invalid.
Valid return values are 0..255 but 128..255 are likely to be
interpreted as 'killed by a signal'.
So really you only get 0..127.
Mostly used as 0 => success and 1 => failure.
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2024-09-08 20:09 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-09-03 8:01 [PATCH] tools/counter: close fd when exit zhangjiao2
2024-09-03 23:19 ` Andy Shevchenko
2024-09-08 20:08 ` David Laight
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®