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

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

Version: ~ [ linux-5.1-rc5 ] ~ [ linux-5.0.7 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.34 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.111 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.168 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.178 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.138 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.65 ] ~ [ 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.39.4 ] ~ [ linux-2.6.38.8 ] ~ [ linux-2.6.37.6 ] ~ [ linux-2.6.36.4 ] ~ [ linux-2.6.35.14 ] ~ [ linux-2.6.34.15 ] ~ [ linux-2.6.33.20 ] ~ [ 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 // SPDX-License-Identifier: GPL-2.0
  2 /*
  3  * cgroup_event_listener.c - Simple listener of cgroup events
  4  *
  5  * Copyright (C) Kirill A. Shutemov <kirill@shutemov.name>
  6  */
  7 
  8 #include <assert.h>
  9 #include <err.h>
 10 #include <errno.h>
 11 #include <fcntl.h>
 12 #include <libgen.h>
 13 #include <limits.h>
 14 #include <stdio.h>
 15 #include <string.h>
 16 #include <unistd.h>
 17 
 18 #include <sys/eventfd.h>
 19 
 20 #define USAGE_STR "Usage: cgroup_event_listener <path-to-control-file> <args>"
 21 
 22 int main(int argc, char **argv)
 23 {
 24         int efd = -1;
 25         int cfd = -1;
 26         int event_control = -1;
 27         char event_control_path[PATH_MAX];
 28         char line[LINE_MAX];
 29         int ret;
 30 
 31         if (argc != 3)
 32                 errx(1, "%s", USAGE_STR);
 33 
 34         cfd = open(argv[1], O_RDONLY);
 35         if (cfd == -1)
 36                 err(1, "Cannot open %s", argv[1]);
 37 
 38         ret = snprintf(event_control_path, PATH_MAX, "%s/cgroup.event_control",
 39                         dirname(argv[1]));
 40         if (ret >= PATH_MAX)
 41                 errx(1, "Path to cgroup.event_control is too long");
 42 
 43         event_control = open(event_control_path, O_WRONLY);
 44         if (event_control == -1)
 45                 err(1, "Cannot open %s", event_control_path);
 46 
 47         efd = eventfd(0, 0);
 48         if (efd == -1)
 49                 err(1, "eventfd() failed");
 50 
 51         ret = snprintf(line, LINE_MAX, "%d %d %s", efd, cfd, argv[2]);
 52         if (ret >= LINE_MAX)
 53                 errx(1, "Arguments string is too long");
 54 
 55         ret = write(event_control, line, strlen(line) + 1);
 56         if (ret == -1)
 57                 err(1, "Cannot write to cgroup.event_control");
 58 
 59         while (1) {
 60                 uint64_t result;
 61 
 62                 ret = read(efd, &result, sizeof(result));
 63                 if (ret == -1) {
 64                         if (errno == EINTR)
 65                                 continue;
 66                         err(1, "Cannot read from eventfd");
 67                 }
 68                 assert(ret == sizeof(result));
 69 
 70                 ret = access(event_control_path, W_OK);
 71                 if ((ret == -1) && (errno == ENOENT)) {
 72                         puts("The cgroup seems to have removed.");
 73                         break;
 74                 }
 75 
 76                 if (ret == -1)
 77                         err(1, "cgroup.event_control is not accessible any more");
 78 
 79                 printf("%s %s: crossed\n", argv[1], argv[2]);
 80         }
 81 
 82         return 0;
 83 }
 84 

~ [ 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