~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/cgroup/cgroup_event_listener.c

Version: ~ [ linux-5.6-rc3 ] ~ [ linux-5.5.6 ] ~ [ linux-5.4.22 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.106 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.171 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.214 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.214 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.82 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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 <err.h>
  9 #include <errno.h>
 10 #include <fcntl.h>
 11 #include <libgen.h>
 12 #include <limits.h>
 13 #include <stdio.h>
 14 #include <string.h>
 15 #include <unistd.h>
 16 
 17 #include <sys/eventfd.h>
 18 
 19 #define USAGE_STR "Usage: cgroup_event_listener <path-to-control-file> <args>"
 20 
 21 int main(int argc, char **argv)
 22 {
 23         int efd = -1;
 24         int cfd = -1;
 25         int event_control = -1;
 26         char event_control_path[PATH_MAX];
 27         char line[LINE_MAX];
 28         int ret;
 29 
 30         if (argc != 3)
 31                 errx(1, "%s", USAGE_STR);
 32 
 33         cfd = open(argv[1], O_RDONLY);
 34         if (cfd == -1)
 35                 err(1, "Cannot open %s", argv[1]);
 36 
 37         ret = snprintf(event_control_path, PATH_MAX, "%s/cgroup.event_control",
 38                         dirname(argv[1]));
 39         if (ret >= PATH_MAX)
 40                 errx(1, "Path to cgroup.event_control is too long");
 41 
 42         event_control = open(event_control_path, O_WRONLY);
 43         if (event_control == -1)
 44                 err(1, "Cannot open %s", event_control_path);
 45 
 46         efd = eventfd(0, 0);
 47         if (efd == -1)
 48                 err(1, "eventfd() failed");
 49 
 50         ret = snprintf(line, LINE_MAX, "%d %d %s", efd, cfd, argv[2]);
 51         if (ret >= LINE_MAX)
 52                 errx(1, "Arguments string is too long");
 53 
 54         ret = write(event_control, line, strlen(line) + 1);
 55         if (ret == -1)
 56                 err(1, "Cannot write to cgroup.event_control");
 57 
 58         while (1) {
 59                 uint64_t result;
 60 
 61                 ret = read(efd, &result, sizeof(result));
 62                 if (ret == -1) {
 63                         if (errno == EINTR)
 64                                 continue;
 65                         err(1, "Cannot read from eventfd");
 66                 }
 67                 assert(ret == sizeof(result));
 68 
 69                 ret = access(event_control_path, W_OK);
 70                 if ((ret == -1) && (errno == ENOENT)) {
 71                         puts("The cgroup seems to have removed.");
 72                         break;
 73                 }
 74 
 75                 if (ret == -1)
 76                         err(1, "cgroup.event_control is not accessible any more");
 77 
 78                 printf("%s %s: crossed\n", argv[1], argv[2]);
 79         }
 80 
 81         return 0;
 82 }
 83 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp