2 * cgroup_event_listener.c - Simple listener of cgroup events
4 * Copyright (C) Kirill A. Shutemov <kirill@shutemov.name>
16 #include <sys/eventfd.h>
18 #define USAGE_STR "Usage: cgroup_event_listener <path-to-control-file> <args>\n"
20 int main(int argc
, char **argv
)
24 int event_control
= -1;
25 char event_control_path
[PATH_MAX
];
30 fputs(USAGE_STR
, stderr
);
34 cfd
= open(argv
[1], O_RDONLY
);
36 fprintf(stderr
, "Cannot open %s: %s\n", argv
[1],
41 ret
= snprintf(event_control_path
, PATH_MAX
, "%s/cgroup.event_control",
43 if (ret
>= PATH_MAX
) {
44 fputs("Path to cgroup.event_control is too long\n", stderr
);
48 event_control
= open(event_control_path
, O_WRONLY
);
49 if (event_control
== -1) {
50 fprintf(stderr
, "Cannot open %s: %s\n", event_control_path
,
57 perror("eventfd() failed");
61 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
);
67 ret
= write(event_control
, line
, strlen(line
) + 1);
69 perror("Cannot write to cgroup.event_control");
76 ret
= read(efd
, &result
, sizeof(result
));
80 perror("Cannot read from eventfd");
83 assert(ret
== sizeof(result
));
85 ret
= access(event_control_path
, W_OK
);
86 if ((ret
== -1) && (errno
== ENOENT
)) {
87 puts("The cgroup seems to have removed.");
93 perror("cgroup.event_control "
94 "is not accessible any more");
98 printf("%s %s: crossed\n", argv
[1], argv
[2]);
104 if (event_control
>= 0)
105 close(event_control
);