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

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

Version: ~ [ linux-5.14-rc3 ] ~ [ linux-5.13.5 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.53 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.135 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.198 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.240 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.276 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.276 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ 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 ] ~

Diff markup

Differences between /tools/cgroup/cgroup_event_listener.c (Version linux-5.14-rc3) and /tools/cgroup/cgroup_event_listener.c (Version linux-5.5.19)


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