Skip to content

Commit 02a2f00

Browse files
Taeungborkmann
authored andcommitted
samples/bpf: Check the error of write() and read()
test_task_rename() and test_urandom_read() can be failed during write() and read(), So check the result of them. Reviewed-by: David Laight <David.Laight@ACULAB.COM> Signed-off-by: Taeung Song <treeze.taeung@gmail.com> Acked-by: David S. Miller <davem@davemloft.net> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
1 parent 492b7e8 commit 02a2f00

1 file changed

Lines changed: 15 additions & 4 deletions

File tree

‎samples/bpf/test_overhead_user.c‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77
#define _GNU_SOURCE
88
#include <sched.h>
9+
#include <errno.h>
910
#include <stdio.h>
1011
#include <sys/types.h>
1112
#include <asm/unistd.h>
@@ -44,8 +45,13 @@ static void test_task_rename(int cpu)
4445
exit(1);
4546
}
4647
start_time = time_get_ns();
47-
for (i = 0; i < MAX_CNT; i++)
48-
write(fd, buf, sizeof(buf));
48+
for (i = 0; i < MAX_CNT; i++) {
49+
if (write(fd, buf, sizeof(buf)) < 0) {
50+
printf("task rename failed: %s\n", strerror(errno));
51+
close(fd);
52+
return;
53+
}
54+
}
4955
printf("task_rename:%d: %lld events per sec\n",
5056
cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
5157
close(fd);
@@ -63,8 +69,13 @@ static void test_urandom_read(int cpu)
6369
exit(1);
6470
}
6571
start_time = time_get_ns();
66-
for (i = 0; i < MAX_CNT; i++)
67-
read(fd, buf, sizeof(buf));
72+
for (i = 0; i < MAX_CNT; i++) {
73+
if (read(fd, buf, sizeof(buf)) < 0) {
74+
printf("failed to read from /dev/urandom: %s\n", strerror(errno));
75+
close(fd);
76+
return;
77+
}
78+
}
6879
printf("urandom_read:%d: %lld events per sec\n",
6980
cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
7081
close(fd);

0 commit comments

Comments
 (0)