2 * cgroup_event_listener.c - Simple listener of cgroup events
4 * Copyright (C) Kirill A. Shutemov <kirill@shutemov.name>
17 #include <sys/eventfd.h>
19 #define USAGE_STR "Usage: cgroup_event_listener <path-to-control-file> <args>"
21 int main(int argc
, char **argv
)
25 int event_control
= -1;
26 char event_control_path
[PATH_MAX
];
31 errx(1, "%s", USAGE_STR
);
33 cfd
= open(argv
[1], O_RDONLY
);
35 err(1, "Cannot open %s", argv
[1]);
37 ret
= snprintf(event_control_path
, PATH_MAX
, "%s/cgroup.event_control",
40 errx(1, "Path to cgroup.event_control is too long");
42 event_control
= open(event_control_path
, O_WRONLY
);
43 if (event_control
== -1)
44 err(1, "Cannot open %s", event_control_path
);
48 err(1, "eventfd() failed");
50 ret
= snprintf(line
, LINE_MAX
, "%d %d %s", efd
, cfd
, argv
[2]);
52 errx(1, "Arguments string is too long");
54 ret
= write(event_control
, line
, strlen(line
) + 1);
56 err(1, "Cannot write to cgroup.event_control");
61 ret
= read(efd
, &result
, sizeof(result
));
65 err(1, "Cannot read from eventfd");
67 assert(ret
== sizeof(result
));
69 ret
= access(event_control_path
, W_OK
);
70 if ((ret
== -1) && (errno
== ENOENT
)) {
71 puts("The cgroup seems to have removed.");
76 err(1, "cgroup.event_control is not accessible any more");
78 printf("%s %s: crossed\n", argv
[1], argv
[2]);