diff options
author | Greg Thelen <gthelen@google.com> | 2013-01-04 16:05:17 -0500 |
---|---|---|
committer | Tejun Heo <tj@kernel.org> | 2013-01-07 12:41:28 -0500 |
commit | 92e015b1cfc24e3cb072385f25171b8599cc7ef3 (patch) | |
tree | 465c64f5b337dcdec22adb6cb4aa82c5fe410bca /tools | |
parent | 12a9d2fef1d35770d3cdc2cd1faabb83c45bc0fa (diff) |
cgroups: move cgroup_event_listener.c to tools/cgroup
Move the cgroup_event_listener.c tool from Documentation into the new
tools/cgroup directory.
This change involves wiring cgroup_event_listener.c into the tools/
make system so that is can be built with:
$ make tools/cgroup
Signed-off-by: Greg Thelen <gthelen@google.com>
Signed-off-by: Tejun Heo <tj@kernel.org>
Diffstat (limited to 'tools')
-rw-r--r-- | tools/Makefile | 19 | ||||
-rw-r--r-- | tools/cgroup/.gitignore | 1 | ||||
-rw-r--r-- | tools/cgroup/Makefile | 11 | ||||
-rw-r--r-- | tools/cgroup/cgroup_event_listener.c | 110 |
4 files changed, 132 insertions, 9 deletions
diff --git a/tools/Makefile b/tools/Makefile index 1f9a529fe544..fc57a28abca1 100644 --- a/tools/Makefile +++ b/tools/Makefile | |||
@@ -3,6 +3,7 @@ include scripts/Makefile.include | |||
3 | help: | 3 | help: |
4 | @echo 'Possible targets:' | 4 | @echo 'Possible targets:' |
5 | @echo '' | 5 | @echo '' |
6 | @echo ' cgroup - cgroup tools' | ||
6 | @echo ' cpupower - a tool for all things x86 CPU power' | 7 | @echo ' cpupower - a tool for all things x86 CPU power' |
7 | @echo ' firewire - the userspace part of nosy, an IEEE-1394 traffic sniffer' | 8 | @echo ' firewire - the userspace part of nosy, an IEEE-1394 traffic sniffer' |
8 | @echo ' lguest - a minimal 32-bit x86 hypervisor' | 9 | @echo ' lguest - a minimal 32-bit x86 hypervisor' |
@@ -33,7 +34,7 @@ help: | |||
33 | cpupower: FORCE | 34 | cpupower: FORCE |
34 | $(call descend,power/$@) | 35 | $(call descend,power/$@) |
35 | 36 | ||
36 | firewire lguest perf usb virtio vm: FORCE | 37 | cgroup firewire lguest perf usb virtio vm: FORCE |
37 | $(call descend,$@) | 38 | $(call descend,$@) |
38 | 39 | ||
39 | selftests: FORCE | 40 | selftests: FORCE |
@@ -45,7 +46,7 @@ turbostat x86_energy_perf_policy: FORCE | |||
45 | cpupower_install: | 46 | cpupower_install: |
46 | $(call descend,power/$(@:_install=),install) | 47 | $(call descend,power/$(@:_install=),install) |
47 | 48 | ||
48 | firewire_install lguest_install perf_install usb_install virtio_install vm_install: | 49 | cgroup_install firewire_install lguest_install perf_install usb_install virtio_install vm_install: |
49 | $(call descend,$(@:_install=),install) | 50 | $(call descend,$(@:_install=),install) |
50 | 51 | ||
51 | selftests_install: | 52 | selftests_install: |
@@ -54,14 +55,14 @@ selftests_install: | |||
54 | turbostat_install x86_energy_perf_policy_install: | 55 | turbostat_install x86_energy_perf_policy_install: |
55 | $(call descend,power/x86/$(@:_install=),install) | 56 | $(call descend,power/x86/$(@:_install=),install) |
56 | 57 | ||
57 | install: cpupower_install firewire_install lguest_install perf_install \ | 58 | install: cgroup_install cpupower_install firewire_install lguest_install \ |
58 | selftests_install turbostat_install usb_install virtio_install \ | 59 | perf_install selftests_install turbostat_install usb_install \ |
59 | vm_install x86_energy_perf_policy_install | 60 | virtio_install vm_install x86_energy_perf_policy_install |
60 | 61 | ||
61 | cpupower_clean: | 62 | cpupower_clean: |
62 | $(call descend,power/cpupower,clean) | 63 | $(call descend,power/cpupower,clean) |
63 | 64 | ||
64 | firewire_clean lguest_clean perf_clean usb_clean virtio_clean vm_clean: | 65 | cgroup_clean firewire_clean lguest_clean perf_clean usb_clean virtio_clean vm_clean: |
65 | $(call descend,$(@:_clean=),clean) | 66 | $(call descend,$(@:_clean=),clean) |
66 | 67 | ||
67 | selftests_clean: | 68 | selftests_clean: |
@@ -70,8 +71,8 @@ selftests_clean: | |||
70 | turbostat_clean x86_energy_perf_policy_clean: | 71 | turbostat_clean x86_energy_perf_policy_clean: |
71 | $(call descend,power/x86/$(@:_clean=),clean) | 72 | $(call descend,power/x86/$(@:_clean=),clean) |
72 | 73 | ||
73 | clean: cpupower_clean firewire_clean lguest_clean perf_clean selftests_clean \ | 74 | clean: cgroup_clean cpupower_clean firewire_clean lguest_clean perf_clean \ |
74 | turbostat_clean usb_clean virtio_clean vm_clean \ | 75 | selftests_clean turbostat_clean usb_clean virtio_clean \ |
75 | x86_energy_perf_policy_clean | 76 | vm_clean x86_energy_perf_policy_clean |
76 | 77 | ||
77 | .PHONY: FORCE | 78 | .PHONY: FORCE |
diff --git a/tools/cgroup/.gitignore b/tools/cgroup/.gitignore new file mode 100644 index 000000000000..633cd9b874f9 --- /dev/null +++ b/tools/cgroup/.gitignore | |||
@@ -0,0 +1 @@ | |||
cgroup_event_listener | |||
diff --git a/tools/cgroup/Makefile b/tools/cgroup/Makefile new file mode 100644 index 000000000000..b4286196b763 --- /dev/null +++ b/tools/cgroup/Makefile | |||
@@ -0,0 +1,11 @@ | |||
1 | # Makefile for cgroup tools | ||
2 | |||
3 | CC = $(CROSS_COMPILE)gcc | ||
4 | CFLAGS = -Wall -Wextra | ||
5 | |||
6 | all: cgroup_event_listener | ||
7 | %: %.c | ||
8 | $(CC) $(CFLAGS) -o $@ $^ | ||
9 | |||
10 | clean: | ||
11 | $(RM) cgroup_event_listener | ||
diff --git a/tools/cgroup/cgroup_event_listener.c b/tools/cgroup/cgroup_event_listener.c new file mode 100644 index 000000000000..3e082f96dc12 --- /dev/null +++ b/tools/cgroup/cgroup_event_listener.c | |||
@@ -0,0 +1,110 @@ | |||
1 | /* | ||
2 | * cgroup_event_listener.c - Simple listener of cgroup events | ||
3 | * | ||
4 | * Copyright (C) Kirill A. Shutemov <kirill@shutemov.name> | ||
5 | */ | ||
6 | |||
7 | #include <assert.h> | ||
8 | #include <errno.h> | ||
9 | #include <fcntl.h> | ||
10 | #include <libgen.h> | ||
11 | #include <limits.h> | ||
12 | #include <stdio.h> | ||
13 | #include <string.h> | ||
14 | #include <unistd.h> | ||
15 | |||
16 | #include <sys/eventfd.h> | ||
17 | |||
18 | #define USAGE_STR "Usage: cgroup_event_listener <path-to-control-file> <args>\n" | ||
19 | |||
20 | int main(int argc, char **argv) | ||
21 | { | ||
22 | int efd = -1; | ||
23 | int cfd = -1; | ||
24 | int event_control = -1; | ||
25 | char event_control_path[PATH_MAX]; | ||
26 | char line[LINE_MAX]; | ||
27 | int ret; | ||
28 | |||
29 | if (argc != 3) { | ||
30 | fputs(USAGE_STR, stderr); | ||
31 | return 1; | ||
32 | } | ||
33 | |||
34 | 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 | } | ||
40 | |||
41 | ret = snprintf(event_control_path, PATH_MAX, "%s/cgroup.event_control", | ||
42 | dirname(argv[1])); | ||
43 | if (ret >= PATH_MAX) { | ||
44 | fputs("Path to cgroup.event_control is too long\n", stderr); | ||
45 | goto out; | ||
46 | } | ||
47 | |||
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, | ||
51 | strerror(errno)); | ||
52 | goto out; | ||
53 | } | ||
54 | |||
55 | efd = eventfd(0, 0); | ||
56 | if (efd == -1) { | ||
57 | perror("eventfd() failed"); | ||
58 | goto out; | ||
59 | } | ||
60 | |||
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); | ||
64 | goto out; | ||
65 | } | ||
66 | |||
67 | ret = write(event_control, line, strlen(line) + 1); | ||
68 | if (ret == -1) { | ||
69 | perror("Cannot write to cgroup.event_control"); | ||
70 | goto out; | ||
71 | } | ||
72 | |||
73 | while (1) { | ||
74 | uint64_t result; | ||
75 | |||
76 | ret = read(efd, &result, sizeof(result)); | ||
77 | if (ret == -1) { | ||
78 | if (errno == EINTR) | ||
79 | continue; | ||
80 | perror("Cannot read from eventfd"); | ||
81 | break; | ||
82 | } | ||
83 | assert(ret == sizeof(result)); | ||
84 | |||
85 | ret = access(event_control_path, W_OK); | ||
86 | 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"); | ||
95 | break; | ||
96 | } | ||
97 | |||
98 | printf("%s %s: crossed\n", argv[1], argv[2]); | ||
99 | } | ||
100 | |||
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); | ||
110 | } | ||