aboutsummaryrefslogtreecommitdiff
path: root/cgroups.c
blob: c4f3324ea120e76e6dcaf825c9e64d703bcec00c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
/* 
Copyright (c) 2014 Igor Pashev <pashev.igor@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>

#include <fcgiapp.h>
#include <libcgroup.h>

#include "debug.h"


// trim traling slashes, but not if str == "/"
static void
trim_trailing_slashes (char *str)
{
  if (NULL != str)
    {
      size_t len = strlen (str);
      if (len > 1)
        {
          char *last_symbol = (char *) str + len - 1;
          while ('/' == *last_symbol)
            last_symbol--;
          last_symbol[1] = '\0';
        }
    }
}


// Is name in "name1,name2,name3" ?
static bool
controller_is_in_list (const char *controllers, const char *name)
{
  if ((NULL == controllers) || ('\0' == controllers[0])
      || ('*' == controllers[0]))
    return true;

  const char *p = strstr (controllers, name);

  if (NULL == p)
    return false;

  if ((controllers != p) && (',' != p[-1]))
    return false;

  p += strlen (name);
  if ((',' != *p) && ('\0' != *p))
    return false;

  return true;
}


//  "name1,name2" => 2
//  "name1,name2,name3" => 3
static int
count_controllers (const char *controllers)
{
  size_t number_of_controllers = 0;
  size_t controllers_len = strlen (controllers) + 1;
  char *tail = NULL;
  char controllers_copy[controllers_len];
  memcpy (controllers_copy, controllers, controllers_len);

  char *next_controller = strtok_r (controllers_copy, ",", &tail);
  do
    {
      debug ("next controller `%s'", next_controller);
      number_of_controllers++;
      next_controller = strtok_r (NULL, ",", &tail);
    }
  while (NULL != next_controller);
  debug ("number of controllers in `%s': %d", controllers,
         number_of_controllers);
  return number_of_controllers;
}


static bool
group_has_controller (const char *controller, const char *group)
{
  int rc;
  void *handle = NULL;
  int base_level = 0;
  struct cgroup_file_info info;

  rc =
    cgroup_walk_tree_begin (controller, group, 0, &handle, &info,
                            &base_level);

  if (0 == rc)
    {
      cgroup_walk_tree_end (&handle);
      if (CGROUP_FILE_TYPE_DIR == info.type)
        return true;
    }

  return false;
}


static bool
group_exists (const char *controllers, const char *group)
{
  size_t controllers_len = strlen (controllers) + 1;
  char *tail = NULL;
  char controllers_copy[controllers_len];
  memcpy (controllers_copy, controllers, controllers_len);

  char *controller = strtok_r (controllers_copy, ",", &tail);
  do
    {
      debug ("controller `%s'", controller);
      if (!group_has_controller (controller, group))
        return false;
      controller = strtok_r (NULL, ",", &tail);
    }
  while (NULL != controller);
  return true;
}


static bool
group_has_pid (const char *controllers, const char *group, pid_t pid)
{
  bool ret = true;

  char proc_cgroup_path[sizeof ("/proc/2147483647/cgroup")];
  char pid_controllers[FILENAME_MAX];

  snprintf (proc_cgroup_path, sizeof (proc_cgroup_path), "/proc/%d/cgroup",
            pid);
  debug ("reading `%s'", proc_cgroup_path);

  FILE *proc_cgroup_file = fopen (proc_cgroup_path, "r");
  if (NULL != proc_cgroup_file)
    {
      char *next_controller = pid_controllers;
      while (!feof (proc_cgroup_file))
        {
          char cntrls[FILENAME_MAX];
          char path[FILENAME_MAX];
          int rc = fscanf (proc_cgroup_file, "%*d:%[^:]:%s\n", cntrls, path);
          if (2 != rc)
            {
              debug ("fscanf() returned %d != 2", rc);
              continue;
            }

          debug ("pid %d is in `%s:%s'", pid, cntrls, path);
          if (0 == strcmp (path, group))
            {
              if (next_controller != pid_controllers)
                {
                  *next_controller = ',';
                  next_controller++;
                }
              size_t l = strlen (cntrls);
              memcpy (next_controller, cntrls, l);
              next_controller += l;
            }
        }
      fclose (proc_cgroup_file);

      *next_controller = '\0';
      debug ("all pid %d controllers `%s'", pid, pid_controllers);

      // all controllers must be in pid_controllers:
      size_t l = strlen (controllers) + 1;
      char controllers_copy[l];
      char *tail = NULL;
      memcpy (controllers_copy, controllers, l);
      next_controller = strtok_r (controllers_copy, ",", &tail);
      while (NULL != next_controller)
        {
          debug ("checking controller `%s'", next_controller);
          if (!controller_is_in_list (pid_controllers, next_controller))
            {
              ret = false;
              break;
            }
          next_controller = strtok_r (NULL, ",", &tail);
        }
    }
  else
    {
      debug ("fopen() failed: %s ", strerror (errno));
      ret = false;
    }

  debug ("pid %d is%s under `%s' controllers", pid, (ret ? "" : " not"),
         controllers);
  return ret;
}


static void
fcgi_cgroups_list_controllers_by_mountpoint (FCGX_Request * request,
                                             const char *mountpoint)
{
  int rc;
  void *handle = NULL;
  struct cgroup_mount_point controller;
  int contr_count = 0;

  FCGX_PutS ("[", request->out);

  rc = cgroup_get_controller_begin (&handle, &controller);
  debug ("cgroup_get_controller_begin() returned %d", rc);

  while (0 == rc)
    {
      debug ("controller `%s', mount point `%s'", controller.name,
             controller.path);
      if (0 == strcmp (mountpoint, controller.path))
        {
          if (contr_count > 0)
            FCGX_PutS (", ", request->out);
          FCGX_FPrintF (request->out, "\"%s\"", controller.name);
          contr_count++;
        }
      rc = cgroup_get_controller_next (&handle, &controller);
    }
  debug ("exit from loop with %d", rc);

  cgroup_get_controller_end (&handle);

  FCGX_PutS ("]", request->out);
}


static void
fcgi_cgroups_list_groups (FCGX_Request * request, const char *controller,
                          const char *path, const char *mountpoint)
{
  int rc;
  void *handle = NULL;
  int base_level = 0;
  struct cgroup_file_info info;
  int group_count = 0;

  FCGX_PutS ("[", request->out);

  size_t mountpoint_len = strlen (mountpoint);

  rc =
    cgroup_walk_tree_begin (controller, path, 0, &handle, &info, &base_level);
  debug ("cgroup_walk_tree_begin() returned %d", rc);

  while (0 == rc)
    {
      if (CGROUP_FILE_TYPE_DIR == info.type)
        {
          if (group_count > 0)
            FCGX_PutS (", ", request->out);

          const char *rel_path = info.full_path + mountpoint_len;
          while ('/' == *rel_path)
            rel_path++;

          FCGX_FPrintF (request->out, "\"/%s\"", rel_path);

          debug ("full path = `%s', rel path = `%s'", info.full_path,
                 rel_path);

          group_count++;
        }
      rc = cgroup_walk_tree_next (0, &handle, &info, base_level);
    }
  debug ("exit from loop with %d", rc);

  cgroup_walk_tree_end (&handle);

  FCGX_PutS ("]", request->out);
}


static void
fcgi_cgroups_heirarchy (FCGX_Request * request, const char *controller,
                        const char *path, const char *mountpoint)
{
  FCGX_PutS ("{", request->out);

  FCGX_PutS ("controllers: ", request->out);
  fcgi_cgroups_list_controllers_by_mountpoint (request, mountpoint);

  FCGX_PutS (", ", request->out);

  FCGX_PutS ("groups: ", request->out);
  fcgi_cgroups_list_groups (request, controller, path, mountpoint);

  FCGX_PutS ("}", request->out);
}


static void
fcgi_cgroups_list_hierarhies (FCGX_Request * request, const char *controllers,
                              const char *path)
{
  int rc;
  void *handle = NULL;
  struct cgroup_mount_point controller;
  char mountpoint[FILENAME_MAX];        // it's a sizeof cgroup_mount_point.path
  int hier_count = 0;

  FCGX_PutS ("[", request->out);

  mountpoint[0] = '\0';

  debug ("controllers `%s', path `%s'", controllers, path);

  rc = cgroup_get_controller_begin (&handle, &controller);
  debug ("cgroup_get_controller_begin() returned %d", rc);

  while (0 == rc)
    {
      debug ("controller `%s', mount point `%s'", controller.name,
             controller.path);

      bool use_controller =
        controller_is_in_list (controllers, controller.name)
        && group_has_controller (controller.name, path);

      if (use_controller)
        {
          // XXX are controllers sorted by mount point?
          // XXX use stat() and st_dev/st_ino ?
          if (0 != strcmp (mountpoint, controller.path))        // new mount point (hierarchy)
            {
              strcpy (mountpoint, controller.path);
              if (hier_count > 0)
                FCGX_PutS (", ", request->out);
              fcgi_cgroups_heirarchy (request, controller.name, path,
                                      mountpoint);
              hier_count++;
            }
        }
      rc = cgroup_get_controller_next (&handle, &controller);
    }
  debug ("exit from loop with %d", rc);

  cgroup_get_controller_end (&handle);

  FCGX_PutS ("]", request->out);
}


static void
fcgi_cgroups_list_tasks (FCGX_Request * request, const char *controllers,
                         const char *path)
{
  int rc;
  pid_t pid;
  void *handle = NULL;
  int pid_count = 0;

  FCGX_PutS ("[", request->out);

  if (group_exists (controllers, path))
    {
      const char *first_controller;
      char *other_controllers;
      size_t l = strlen (controllers) + 1;
      char cntlrs[l];
      memcpy (cntlrs, controllers, l);
      first_controller = strtok_r (cntlrs, ",", &other_controllers);
      rc = cgroup_get_task_begin (path, first_controller, &handle, &pid);
      debug ("cgroup_get_task_begin() returned %d", rc);

      while (0 == rc)
        {
          if ((NULL == other_controllers)
              || group_has_pid (other_controllers, path, pid))
            {
              if (pid_count > 0)
                FCGX_PutS (", ", request->out);
              FCGX_FPrintF (request->out, "%d", pid);
              pid_count++;
            }
          rc = cgroup_get_task_next (&handle, &pid);
        }
      debug ("exit from loop with %d", rc);

      cgroup_get_task_end (&handle);
    }
  else
    {
      debug ("group `%s:%s' does not exist", controllers, path);
    }

  FCGX_PutS ("]", request->out);
}


static void
fcgi_cgroups_attach_task (FCGX_Request * request, const char *controllers,
                          const char *path, const char *pid_s)
{
  char *p;
  pid_t pid = strtoul (pid_s, &p, 10);

  if ((pid <= 0) || ('\0' != *p))
    {
      debug ("invalid pid: %s", pid_s);
      FCGX_FPrintF (request->out, "{error: \"Invalid pid\"}");
      return;
    }

  size_t controllers_len = strlen (controllers) + 1;
  char controllers_copy[controllers_len];
  memcpy (controllers_copy, controllers, controllers_len);

  size_t number_of_controllers = count_controllers (controllers);
  const char *controllers_list[number_of_controllers + 1];      // + sentinel
  memset (controllers_list, 0, sizeof (controllers_list));

  char *tail = NULL;
  int controller_index = 0;
  char *next_controller = strtok_r (controllers_copy, ",", &tail);
  do
    {
      debug ("controller[%d] = %s", controller_index, next_controller);
      controllers_list[controller_index] = next_controller;
      next_controller = strtok_r (NULL, ",", &tail);
      controller_index++;
    }
  while (NULL != next_controller);

  int rc = cgroup_change_cgroup_path (path, pid, controllers_list);
  if (0 != rc)
    {
      debug ("cgroup_change_cgroup_path() returned %d (%s)", rc,
             cgroup_strerror (rc));
      FCGX_FPrintF (request->out, "{error: \"%s\"}", cgroup_strerror (rc));
    }
  else
    FCGX_FPrintF (request->out, "{}");
}


static void
fcgi_cgroups_action (FCGX_Request * request, const char *controllers,
                     const char *path, const char *action)
{
  size_t l = strlen (action) + 1;

  char act[l];

  memcpy (act, action, l);

  char *arg = strchr (act, '=');
  if (NULL != arg)
    {
      *arg = '\0';
      arg++;
    }

  debug ("action `%s', argument `%s'", act, arg);

  if (0 == strcmp ("list", act))
    fcgi_cgroups_list_hierarhies (request, controllers, path);
  else if (0 == strcmp ("list-tasks", act))
    fcgi_cgroups_list_tasks (request, controllers, path);
  else if (0 == strcmp ("attach-task", act))
    fcgi_cgroups_attach_task (request, controllers, path, arg);
}


void
fcgi_cgroups (FCGX_Request * request, char **uri_tail)
{
  // controllers:path?action
  char *controllers;
  char *path = NULL;
  char *action = NULL;

  while ('/' == **uri_tail)
    (*uri_tail)++;

  char *colon = strchr (*uri_tail, ':');
  if (NULL != colon)
    {
      *colon = '\0';
      path = colon + 1;
      controllers = *uri_tail;
    }
  else
    {
      controllers = "*";
      path = *uri_tail;
    }

  char *ques = strchr (path, '?');
  if (NULL != ques)
    {
      *ques = '\0';
      action = ques + 1;
    }

  trim_trailing_slashes (path);

  debug ("controllers `%s', path `%s', action `%s'", controllers, path,
         action);

  if ((NULL == action) || ('\0' == action[0]))
    fcgi_cgroups_list_hierarhies (request, controllers, path);
  else
    fcgi_cgroups_action (request, controllers, path, action);
}