Skip to content

Commit 799105d

Browse files
gthelenhtejun
authored andcommitted
cgroups: fix cgroup_event_listener error handling
The error handling in cgroup_event_listener.c did not correctly deal with either an error opening either <control_file> or cgroup.event_control. Due to an uninitialized variable the program exit code was undefined if either of these opens failed. This patch simplifies and corrects cgroup_event_listener.c error handling by: 1. using err*() rather than printf(),exit() 2. depending on process exit to close open files With this patch failures always return non-zero error. Signed-off-by: Greg Thelen <gthelen@google.com> Acked-by: Li Zefan <lizefan@huawei.com> Signed-off-by: Tejun Heo <tj@kernel.org>
1 parent 92e015b commit 799105d

1 file changed

Lines changed: 22 additions & 50 deletions

File tree

‎tools/cgroup/cgroup_event_listener.c‎

Lines changed: 22 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
#include <assert.h>
8+
#include <err.h>
89
#include <errno.h>
910
#include <fcntl.h>
1011
#include <libgen.h>
@@ -15,7 +16,7 @@
1516

1617
#include <sys/eventfd.h>
1718

18-
#define USAGE_STR "Usage: cgroup_event_listener <path-to-control-file> <args>\n"
19+
#define USAGE_STR "Usage: cgroup_event_listener <path-to-control-file> <args>"
1920

2021
int main(int argc, char **argv)
2122
{
@@ -26,49 +27,33 @@ int main(int argc, char **argv)
2627
char line[LINE_MAX];
2728
int ret;
2829

29-
if (argc != 3) {
30-
fputs(USAGE_STR, stderr);
31-
return 1;
32-
}
30+
if (argc != 3)
31+
errx(1, "%s", USAGE_STR);
3332

3433
cfd = open(argv[1], O_RDONLY);
35-
if (cfd == -1) {
36-
fprintf(stderr, "Cannot open %s: %s\n", argv[1],
37-
strerror(errno));
38-
goto out;
39-
}
34+
if (cfd == -1)
35+
err(1, "Cannot open %s", argv[1]);
4036

4137
ret = snprintf(event_control_path, PATH_MAX, "%s/cgroup.event_control",
4238
dirname(argv[1]));
43-
if (ret >= PATH_MAX) {
44-
fputs("Path to cgroup.event_control is too long\n", stderr);
45-
goto out;
46-
}
39+
if (ret >= PATH_MAX)
40+
errx(1, "Path to cgroup.event_control is too long");
4741

4842
event_control = open(event_control_path, O_WRONLY);
49-
if (event_control == -1) {
50-
fprintf(stderr, "Cannot open %s: %s\n", event_control_path,
51-
strerror(errno));
52-
goto out;
53-
}
43+
if (event_control == -1)
44+
err(1, "Cannot open %s", event_control_path);
5445

5546
efd = eventfd(0, 0);
56-
if (efd == -1) {
57-
perror("eventfd() failed");
58-
goto out;
59-
}
47+
if (efd == -1)
48+
err(1, "eventfd() failed");
6049

6150
ret = snprintf(line, LINE_MAX, "%d %d %s", efd, cfd, argv[2]);
62-
if (ret >= LINE_MAX) {
63-
fputs("Arguments string is too long\n", stderr);
64-
goto out;
65-
}
51+
if (ret >= LINE_MAX)
52+
errx(1, "Arguments string is too long");
6653

6754
ret = write(event_control, line, strlen(line) + 1);
68-
if (ret == -1) {
69-
perror("Cannot write to cgroup.event_control");
70-
goto out;
71-
}
55+
if (ret == -1)
56+
err(1, "Cannot write to cgroup.event_control");
7257

7358
while (1) {
7459
uint64_t result;
@@ -77,34 +62,21 @@ int main(int argc, char **argv)
7762
if (ret == -1) {
7863
if (errno == EINTR)
7964
continue;
80-
perror("Cannot read from eventfd");
81-
break;
65+
err(1, "Cannot read from eventfd");
8266
}
8367
assert(ret == sizeof(result));
8468

8569
ret = access(event_control_path, W_OK);
8670
if ((ret == -1) && (errno == ENOENT)) {
87-
puts("The cgroup seems to have removed.");
88-
ret = 0;
89-
break;
90-
}
91-
92-
if (ret == -1) {
93-
perror("cgroup.event_control "
94-
"is not accessible any more");
71+
puts("The cgroup seems to have removed.");
9572
break;
9673
}
9774

75+
if (ret == -1)
76+
err(1, "cgroup.event_control is not accessible any more");
77+
9878
printf("%s %s: crossed\n", argv[1], argv[2]);
9979
}
10080

101-
out:
102-
if (efd >= 0)
103-
close(efd);
104-
if (event_control >= 0)
105-
close(event_control);
106-
if (cfd >= 0)
107-
close(cfd);
108-
109-
return (ret != 0);
81+
return 0;
11082
}

0 commit comments

Comments
 (0)