cgroup_add_task(), cgroup_set_mem_limit*(): More careful error detection
[compctl.git] / cgroup.c
blobab09b65a4d93a0fb8d93a60943d6b292b42aec01
1 #include <limits.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <sys/mount.h>
6 #include <sys/stat.h>
7 #include <unistd.h>
9 #include "cgroup.h"
10 #include "common.h"
12 #if SWAP_LIMIT
13 #define MEMSW ".memsw"
14 #else
15 #define MEMSW
16 #endif
18 void (*cgroup_perror)(const char *s) = perror;
19 static const char cgroupfs[] = "/sys/fs/cgroup/";
21 int
22 cgroup_setup(const char *chier, const char *controllers)
24 char chierdir[PATH_MAX];
25 snprintf(chierdir, sizeof(chierdir), "%s%s", cgroupfs, chier);
26 if (access(chierdir, F_OK) >= 0)
27 return 0;
29 if (mount("none", cgroupfs, "tmpfs", 0, NULL) < 0) {
30 cgroup_perror(cgroupfs);
31 return -1;
33 if (mkdir(chierdir, 0777) < 0) {
34 cgroup_perror(chierdir);
35 return -1;
37 if (mount("none", chierdir, "cgroup", 0, controllers) < 0) {
38 cgroup_perror(chierdir);
39 return -1;
41 return 1;
44 int
45 cgroup_create(const char *chier, const char *cgroup)
47 char cgroupdir[PATH_MAX];
48 snprintf(cgroupdir, sizeof(cgroupdir), "%s%s/%s", cgroupfs, chier, cgroup);
49 if (access(cgroupdir, F_OK) >= 0)
50 return 0;
51 if (mkdir(cgroupdir, 0777) < 0) {
52 cgroup_perror(cgroupdir);
53 return -1;
55 return 1;
59 int
60 cgroup_add_task(const char *chier, const char *cgroup, pid_t pid)
62 char tasksfile[PATH_MAX];
63 snprintf(tasksfile, sizeof(tasksfile), "%s%s/%s/tasks", cgroupfs, chier, cgroup);
64 FILE *tasks = fopen(tasksfile, "a");
65 if (!tasks) {
66 cgroup_perror(tasksfile);
67 return -1;
69 if (fprintf(tasks, "%d\n", pid) < 0) {
70 cgroup_perror(tasksfile);
71 fclose(tasks);
72 return -1;
74 if (fclose(tasks) < 0) {
75 cgroup_perror(tasksfile);
76 return -1;
78 return 1;
81 int
82 cgroup_is_task_in_cgroup(const char *chier, const char *cgroup, pid_t pid)
84 char tasksfile[PATH_MAX];
85 snprintf(tasksfile, sizeof(tasksfile), "%s%s/%s/tasks", cgroupfs, chier, cgroup);
86 FILE *tasks = fopen(tasksfile, "r");
87 if (!tasks) {
88 cgroup_perror(tasksfile);
89 return -1;
92 bool exists = false;
93 char line[128];
94 while (fgets(line, sizeof(line), tasks)) {
95 if (atoi(line) == pid) {
96 exists = true;
97 break;
100 fclose(tasks);
101 return exists;
105 cgroup_task_list(const char *chier, const char *cgroup, pid_t **tasklist)
107 char tasksfile[PATH_MAX];
108 snprintf(tasksfile, sizeof(tasksfile), "%s%s/%s/tasks", cgroupfs, chier, cgroup);
109 FILE *tasks = fopen(tasksfile, "r");
110 if (!tasks) {
111 cgroup_perror(tasksfile);
112 return -1;
115 int ntasks = 0;
116 *tasklist = NULL;
117 char line[128];
118 while (fgets(line, sizeof(line), tasks)) {
119 if (!(ntasks % 32))
120 *tasklist = realloc(*tasklist, (ntasks + 32) * sizeof(*tasklist));
121 (*tasklist)[ntasks++] = atoi(line);
123 fclose(tasks);
124 return ntasks;
128 size_t
129 cgroup_get_mem_limit(const char *chier, const char *cgroup)
131 char limitfile[PATH_MAX];
132 snprintf(limitfile, sizeof(limitfile), "%s%s/%s/memory"MEMSW".limit_in_bytes", cgroupfs, chier, cgroup);
133 FILE *limit = fopen(limitfile, "r");
134 if (!limit) {
135 cgroup_perror(limitfile);
136 return -1;
138 size_t nlimit = 0;
139 fscanf(limit, "%zu", &nlimit);
140 fclose(limit);
141 return nlimit;
145 cgroup_set_mem_limit_do(const char *chier, const char *cgroup, size_t nlimit, char *memsw)
147 char limitfile[PATH_MAX];
148 snprintf(limitfile, sizeof(limitfile), "%s%s/%s/memory%s.limit_in_bytes", cgroupfs, chier, cgroup, memsw);
149 FILE *limit = fopen(limitfile, "w");
150 if (!limit) {
151 cgroup_perror(limitfile);
152 return -1;
154 if (fprintf(limit, "%zu\n", nlimit) < 0) {
155 cgroup_perror(limitfile);
156 fclose(limit);
157 return -1;
159 if (fclose(limit) < 0) {
160 cgroup_perror(limitfile);
161 return -1;
163 return 1;
167 cgroup_set_mem_limit(const char *chier, const char *cgroup, size_t nlimit)
169 /* We need to set both the "normal" and memsw limits, normal first,
170 * since normal <= memsw must hold. */
171 int ret = cgroup_set_mem_limit_do(chier, cgroup, nlimit, "");
172 #if SWAP_LIMIT
173 if (ret >= 0) {
174 int ret2 = cgroup_set_mem_limit_do(chier, cgroup, nlimit, MEMSW);
175 ret = ret2 < 0 ? ret2 : (ret || ret2);
177 #endif
178 return ret;
181 size_t
182 cgroup_get_mem_usage(const char *chier, const char *cgroup)
184 char usagefile[PATH_MAX];
185 snprintf(usagefile, sizeof(usagefile), "%s%s/%s/memory"MEMSW".usage_in_bytes", cgroupfs, chier, cgroup);
186 FILE *usage = fopen(usagefile, "r");
187 if (!usage) {
188 cgroup_perror(usagefile);
189 return -1;
191 size_t nusage = 0;
192 fscanf(usage, "%zu", &nusage);
193 fclose(usage);
194 return nusage;