-
Notifications
You must be signed in to change notification settings - Fork 10
/
lscgroup.c
807 lines (702 loc) · 22 KB
/
lscgroup.c
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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
/* lscgroup.c - list cgroups
*
* Copyright (C) 2012 FUJITSU LIMITED
* Author: Yu Yongming <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "defs.h"
#define PATHNAME_MAX 200
#define ROOT_MAX 64
#define CG_CONTROLLER_MAX 20
#define CG_HIER_MAX 20
#define LIST_CER 0x1
#define LIST_ADD 0x2
#define CGROUP_MEMBER_OFFSET_INIT(X,Y,Z) (cgroup_offset_table.X=MEMBER_OFFSET(Y,Z))
#define CGROUP_OFFSET(X) (OFFSET_verify(cgroup_offset_table.X, (char *)__FUNCTION__, __FILE__, __LINE__, #X))
/*
*struct declaration
*/
struct cgroup_offset_table {
long cgroupfs_root_root_list;
long cgroupfs_root_top_cgroup;
long cgroupfs_root_subsys_list;
long cgroupfs_root_actual_subsys_bits;
long cgroupfs_root_name;
long cgroup_sibling;
long cgroup_children;
long cgroup_dentry;
long cgroup_subsys_name;
long cgroup_subsys_sibling;
};
struct cgroup_group_spec {
char path[PATHNAME_MAX];
char *controllers[CG_CONTROLLER_MAX];
};
/*
*function declaration
*/
int _init(void);
int _fini(void);
void cmd_lscgroup(void);
char *help_lscgroup[];
static void cgroup_init(void);
static void get_controller_name(ulong);
static void get_cgroup_name(ulong, char *);
static void parse_top_cgroup(ulong, char *);
static void print_all_cgroups(void);
static int in_cgroupfs_root(char *, struct cgroup_group_spec *);
static ulong locate_top_cgroup(struct cgroup_group_spec *);
static void parse_path(char *, char **);
static ulong locate_cgroup(ulong, ulong, char *);
static void standarlize_path(char *);
static void cgroup_elem(struct cgroup_group_spec *);
static void check_addr(ulong, ulong, char *, char *);
static ulong addr_to_cgroup(ulong, char *);
static void cgroup_addr(char *);
static void cgroup_list_cgroups(char **, struct cgroup_group_spec **, int);
static int parse_cgroup_spec(struct cgroup_group_spec **, char *);
static void free_cgroup_spec(char **, struct cgroup_group_spec **);
/*
* global data
*/
static char controller_name[PATHNAME_MAX];
static int authen = FALSE;
char *spe;
static struct command_table_entry command_table[] = {
{"lscgroup", cmd_lscgroup, help_lscgroup, 0},
{NULL},
};
static struct cgroup_offset_table cgroup_offset_table = { 0 };
char *help_lscgroup[] = {
"lscgroup",
"list all cgroups",
"[[<controllers>:<path>] [...]] [[<address>][...]]",
" The command list all present cgroups and their addresses, chosen cgroups",
" and their addresses or cgroup according to input address. When no parameter",
" is specified, the command list all cgroups which are present. When parameter",
" controller/path is specified, the command list subcgroups of cgroup specified",
" by parameter <controllers>:<path>, controllers in <controllers>:<path> can be",
" a comma separated controller list. When the input is hexadecimal address, the",
" command display the cgroup whose address equals to the specified parameter.",
"\nEXAMPLE",
" display all cgroups:\n",
" %s>lscgroup",
" CGROUP CONTROLLER:PATH",
" ffff8801188f8030 blkio:/",
" ffff8801188d3200 blkio:/libvirt",
" ffff88011a724600 blkio:/libvirt/lxc",
" ffff88011adac600 blkio:/libvirt/qemu",
" ffff8801151f6030 net_cls:/",
" ffff88011522a030 freezer:/",
" ffff8801158f9200 freezer:/libvirt",
" ffff88011a724800 freezer:/libvirt/lxc",
" ffff88011adac800 freezer:/libvirt/qemu",
" ffff880115264030 devices:/",
" ffff880115846e00 devices:/libvirt",
" ffff88011a724a00 devices:/libvirt/lxc",
" ffff88011adaca00 devices:/libvirt/qemu",
" ffff88011794c030 memory:/",
" ffff88011738ca00 memory:/libvirt",
" ffff88011a724c00 memory:/libvirt/lxc",
" ffff88011adacc00 memory:/libvirt/qemu",
" ffff880115b20030 cpuacct:/",
" ffff8801191ed600 cpuacct:/libvirt",
" ffff88011a08d200 cpuacct:/libvirt/lxc",
" ffff8801188d3e00 cpuacct:/libvirt/qemu",
" ffff88011923a030 cpu:/",
" ffff880115846600 cpu:/libvirt",
" ffff88011906e400 cpu:/libvirt/lxc",
" ffff880117be6400 cpu:/libvirt/qemu",
" ffff8801173f2030 cpuset:/",
" ffff8801179e6200 cpuset:/libvirt",
" ffff88011a08d000 cpuset:/libvirt/lxc",
" ffff880119fe8000 cpuset:/libvirt/qemu",
" ",
" display chosen cgroups:\n",
" %s>lscgroup cpu:/libvirt cpu:/",
" CGROUP CONTROLLER:PATH",
" ffff880115846600 cpu:/libvirt",
" ffff88011906e400 cpu:/libvirt/lxc",
" ffff880117be6400 cpu:/libvirt/qemu",
" ffff88011923a030 cpu:/",
" ffff880115846600 cpu:/libvirt",
" ffff88011906e400 cpu:/libvirt/lxc",
" ffff880117be6400 cpu:/libvirt/qemu",
" ",
" display cgroup according to input address:\n",
" %s>lscgroup ffff880119fe8000 ffff880115846600",
" CGROUP CONTROLLER:PATH",
" ffff880119fe8000 cpuset:/libvirt/qemu",
" ffff880115846600 cpu:/libvirt",
NULL
};
int
_init(void)
{
register_extension(command_table);
return 1;
}
int
_fini(void)
{
return 1;
}
static void
cgroup_init(void)
{
CGROUP_MEMBER_OFFSET_INIT(cgroupfs_root_root_list, "cgroupfs_root",
"root_list");
CGROUP_MEMBER_OFFSET_INIT(cgroupfs_root_top_cgroup, "cgroupfs_root",
"top_cgroup");
CGROUP_MEMBER_OFFSET_INIT(cgroupfs_root_subsys_list, "cgroupfs_root",
"subsys_list");
CGROUP_MEMBER_OFFSET_INIT(cgroupfs_root_actual_subsys_bits,
"cgroupfs_root", "actual_subsys_bits");
CGROUP_MEMBER_OFFSET_INIT(cgroupfs_root_name, "cgroupfs_root",
"name");
CGROUP_MEMBER_OFFSET_INIT(cgroup_sibling, "cgroup", "sibling");
CGROUP_MEMBER_OFFSET_INIT(cgroup_children, "cgroup", "children");
CGROUP_MEMBER_OFFSET_INIT(cgroup_dentry, "cgroup", "dentry");
CGROUP_MEMBER_OFFSET_INIT(cgroup_subsys_name, "cgroup_subsys", "name");
CGROUP_MEMBER_OFFSET_INIT(cgroup_subsys_sibling, "cgroup_subsys",
"sibling");
}
static void
get_controller_name(ulong cgroupfs_root)
{
char name[PATHNAME_MAX];
ulong subsys, next, subsys_list, name_buf, name_addr;
int len;
BZERO(name, PATHNAME_MAX);
BZERO(controller_name, PATHNAME_MAX);
readmem(cgroupfs_root + CGROUP_OFFSET(cgroupfs_root_actual_subsys_bits),
KVADDR, &subsys, sizeof(ulong), "cgroupfs_root_actual_subsys",
FAULT_ON_ERROR);
if(subsys == 0) {
readmem(cgroupfs_root + CGROUP_OFFSET(cgroupfs_root_name),
KVADDR, &name, ROOT_MAX, "cgroupfs_root_name",
FAULT_ON_ERROR);
strcat(controller_name, name);
return ;
}
subsys_list = cgroupfs_root + CGROUP_OFFSET(cgroupfs_root_subsys_list);
readmem(subsys_list, KVADDR, &next, sizeof(ulong),
"cgroupfs_root_subsys_list", FAULT_ON_ERROR);
do {
name_buf = next - CGROUP_OFFSET(cgroup_subsys_sibling)
+ CGROUP_OFFSET(cgroup_subsys_name);
readmem(name_buf, KVADDR, &name_addr, sizeof(ulong),
"cgroup_subsys_name", FAULT_ON_ERROR);
readmem(name_addr, KVADDR, name, sizeof(name),
"char* name", FAULT_ON_ERROR);
strcat(controller_name, name);
strcat(controller_name, ",");
readmem(next, KVADDR, &next, sizeof(ulong),
"list_head", FAULT_ON_ERROR);
} while(next != subsys_list);
len = strlen(controller_name);
controller_name[len-1] = '\0';
}
static void
get_cgroup_name(ulong cgroup, char *name)
{
ulong dentry, name_addr;
char *dentry_buf;
int len;
readmem(cgroup + CGROUP_OFFSET(cgroup_dentry), KVADDR,
&dentry, sizeof(ulong), "cgroup dentry",
FAULT_ON_ERROR);
dentry_buf = GETBUF(SIZE(dentry));
readmem(dentry, KVADDR, dentry_buf, SIZE(dentry),
"dentry", FAULT_ON_ERROR);
len = UINT(dentry_buf + OFFSET(dentry_d_name) + OFFSET(qstr_len));
name_addr = ULONG(dentry_buf + OFFSET(dentry_d_name) + OFFSET(qstr_name));
readmem(name_addr, KVADDR, name, len, "qstr name", FAULT_ON_ERROR);
FREEBUF(dentry_buf);
}
static void
parse_top_cgroup(ulong top_cgroup, char *path)
{
int len;
char tmp_buf[PATHNAME_MAX];
ulong list_head[2], next, child;
BZERO(tmp_buf, PATHNAME_MAX);
get_cgroup_name(top_cgroup, tmp_buf);
if (strlen(path) > 1)
strncat(path, "/", 1);
strncat(path, tmp_buf, strlen(tmp_buf));
fprintf(fp, "%lx %s:%s\n", top_cgroup, controller_name, path);
child = top_cgroup + CGROUP_OFFSET(cgroup_children);
readmem(child, KVADDR, list_head, sizeof(ulong) * 2,
"cgroup children", FAULT_ON_ERROR);
if ((list_head[0] == child) && (list_head[1] == child))
return;
next = list_head[0];
while (next != child) {
top_cgroup = next - CGROUP_OFFSET(cgroup_sibling);
readmem(top_cgroup + CGROUP_OFFSET(cgroup_sibling) +
OFFSET(list_head_next), KVADDR, &next, sizeof(ulong),
"cgroup siblings", FAULT_ON_ERROR);
len = strlen(path);
parse_top_cgroup(top_cgroup, path);
path[len] = '\0';
}
}
static void
print_all_cgroups(void)
{
struct syment *roots;
ulong top_cgroup;
ulong list_head[2], next, cgroupfs_root;;
char path_name[PATHNAME_MAX];
BZERO(path_name, PATHNAME_MAX);
if (!(roots = symbol_search("roots")))
error(FATAL, "roots symbol does not exist?\n");
readmem(roots->value, KVADDR, list_head, sizeof(ulong) * 2,
"list_head", FAULT_ON_ERROR);
if ((list_head[0] == roots->value) && (list_head[1] == roots->value)) {
fprintf(fp, "no active cgroup hierarchy in the kernel.");
return;
}
next = list_head[0];
while (next != roots->value) {
cgroupfs_root = next - CGROUP_OFFSET(cgroupfs_root_root_list);
get_controller_name(cgroupfs_root);
top_cgroup = cgroupfs_root +
CGROUP_OFFSET(cgroupfs_root_top_cgroup);
/* print infomation of cgroup and subcgroup */
parse_top_cgroup(top_cgroup, path_name);
readmem(next + OFFSET(list_head_next), KVADDR, &next,
sizeof(ulong), "list_head next", FAULT_ON_ERROR);
BZERO(path_name, PATHNAME_MAX);
BZERO(controller_name, PATHNAME_MAX);
}
}
static int
in_cgroupfs_root(char *tmp_name, struct cgroup_group_spec *elem)
{
char *name;
int i = 0;
name = strtok(tmp_name, ",");
while(name != NULL) {
while(elem->controllers[i] != NULL && i < CG_CONTROLLER_MAX) {
if(STREQ(name, elem->controllers[i]))
return 1;
i++;
}
name = strtok(NULL, ",");
i = 0;
}
return 0;
}
static ulong
locate_top_cgroup(struct cgroup_group_spec *elem)
{
struct syment *roots;
ulong top_cgroup;
ulong list_head[2], next, cgroupfs_root;
char tmp_name[PATHNAME_MAX];
if (!(roots = symbol_search("roots")))
error(FATAL, "roots symbol does not exist?");
readmem(roots->value, KVADDR, list_head, sizeof(ulong)*2,
"list_head", FAULT_ON_ERROR);
if ((list_head[0] == roots->value) && (list_head[1] == roots->value))
return 0;
next = list_head[0];
while (next != roots->value) {
/* begin from first root */
cgroupfs_root = next - CGROUP_OFFSET(cgroupfs_root_root_list);
get_controller_name(cgroupfs_root);
strcpy(tmp_name, controller_name);
if (in_cgroupfs_root(tmp_name, elem)) {
top_cgroup = cgroupfs_root +
CGROUP_OFFSET(cgroupfs_root_top_cgroup);
return top_cgroup;
}
readmem(next + OFFSET(list_head_next), KVADDR, &next,
sizeof(ulong), "list_head", FAULT_ON_ERROR);
BZERO(controller_name, PATHNAME_MAX);
}
return 0;
}
/*
* function parse_path and extracts the input path before the first '/' as
* relatively path which will be used to compare with path name of cgroup.
* In addition, it ignores the unnecessary '/' in the path
*/
static void
parse_path(char *path, char **elem_path)
{
char *token;
token = *elem_path;
if (token[0] == '/') {
*path++ = *token++;
} else {
while (*token) {
if (*token == '/' && *(token + 1) == '/') {
token++;
} else if (*token == '/' && *(token + 1) != '/') {
token++;
break;
} else {
*path++ = *token++;
}
}
}
*elem_path = token;
*path = '\0';
}
static ulong
locate_cgroup(ulong current, ulong prev_addr, char *elem_path)
{
char tmp_buf[PATHNAME_MAX];
char path[PATHNAME_MAX];
char *tmp_path = elem_path;
ulong child, sibling, child_offset, sibling_offset;
if (*elem_path == '\0')
return prev_addr;
BZERO(tmp_buf, PATHNAME_MAX);
get_cgroup_name(current, tmp_buf);
parse_path(path, &elem_path);
readmem(current + CGROUP_OFFSET(cgroup_children), KVADDR, &child,
sizeof(ulong), "cgroup children", FAULT_ON_ERROR);
readmem(current + CGROUP_OFFSET(cgroup_sibling), KVADDR, &sibling,
sizeof(ulong), "cgroup sibling", FAULT_ON_ERROR);
child_offset = CGROUP_OFFSET(cgroup_children);
sibling_offset = CGROUP_OFFSET(cgroup_sibling);
/* parse the children if temp=path_name, do change elem_path */
if (STREQ(tmp_buf, path)) {
if ((child - child_offset != current))
return locate_cgroup(child - sibling_offset,
current, elem_path);
else if (*elem_path == '\0')
return current;
} else {
/* parse the sibling if temp !=path_name, do not chage elem_path */
if (((sibling - child_offset) != prev_addr) &&
((sibling - sibling_offset) != current)) {
elem_path = tmp_path;
return locate_cgroup(sibling - sibling_offset,
prev_addr, elem_path);
}
}
return 0;
}
/*
* make the input path begin with character '/' and end without character '/'
* to cope with the situation that the input path may be like "/xxxxx",
* "xxxxx", "xxxxx/", or "/xxxxx/"
*/
static void
standarlize_path(char *path)
{
int len;
len = strlen(path) - 1;
if (path[len] == '/')
path[len] = '\0';
if (path[0] != '/') {
len = strlen(path);
path[len + 1] = '\0';
while (len) {
path[len] = path[len - 1];
len--;
}
path[0] = '/';
}
}
static void
mani_path(char *path)
{
int len;
standarlize_path(path);
if (STREQ(path, "/")) {
*path = '\0';
return ;
}
len = strlen(path);
while (path[len] != '/') {
path[len] = '\0';
len--;
}
path[len] = '\0';
path[0] = '/';
}
static void
cgroup_elem(struct cgroup_group_spec *list_elem)
{
ulong top_cgroup;
ulong cgroup;
char path_name[PATHNAME_MAX];
char buf1[BUFSIZE];
top_cgroup = locate_top_cgroup(list_elem);
if (top_cgroup == 0) {
fprintf(fp, "%s %s\n",
mkstring(buf1, VADDR_PRLEN, CENTER, "no cgroup"),
spe);
return ;
}
strcpy(path_name, list_elem->path);
standarlize_path(path_name);
cgroup = locate_cgroup(top_cgroup, top_cgroup, path_name);
if (cgroup == 0) {
fprintf(fp, "%s %s\n",
mkstring(buf1, VADDR_PRLEN, CENTER, "no cgroup"),
spe);
return ;
}
mani_path(list_elem->path);
parse_top_cgroup(cgroup, list_elem->path);
}
static void
check_addr(ulong top_cgroup, ulong addr_num, char *path, char *full_path)
{
int len;
char tmp_buf[PATHNAME_MAX];
ulong list_head[2], next, child;
BZERO(tmp_buf, PATHNAME_MAX);
get_cgroup_name(top_cgroup, tmp_buf);
if (strlen(path) > 1)
strncat(path, "/", 1);
strncat(path, tmp_buf, strlen(tmp_buf));
if (top_cgroup == addr_num) {
strcpy(full_path, path);
authen = TRUE;
return ;
}
child = top_cgroup + CGROUP_OFFSET(cgroup_children);
readmem(child, KVADDR, list_head, sizeof(ulong) * 2,
"cgroup children", FAULT_ON_ERROR);
next = list_head[0];
while (next != child) {
top_cgroup = next - CGROUP_OFFSET(cgroup_sibling);
readmem(top_cgroup + CGROUP_OFFSET(cgroup_sibling) +
OFFSET(list_head_next), KVADDR, &next, sizeof(ulong),
"cgroup siblings", FAULT_ON_ERROR);
len = strlen(path);
check_addr(top_cgroup, addr_num, path, full_path);
path[len] = '\0';
}
}
static ulong
addr_to_cgroup(ulong addr_num, char *path)
{
struct syment *roots;
ulong top_cgroup;
ulong list_head[2], next, cgroupfs_root;
char path_name[PATHNAME_MAX];
BZERO(path_name, PATHNAME_MAX);
if (!(roots = symbol_search("roots")))
error(FATAL, "roots symbol does not exist?");
readmem(roots->value, KVADDR, list_head, sizeof(ulong)*2,
"list_head", FAULT_ON_ERROR);
next = list_head[0];
while (next != roots->value) {
/* begin from first root */
cgroupfs_root = next - CGROUP_OFFSET(cgroupfs_root_root_list);
get_controller_name(cgroupfs_root);
top_cgroup = cgroupfs_root +
CGROUP_OFFSET(cgroupfs_root_top_cgroup);
check_addr(top_cgroup, addr_num, path_name, path);
if (authen) {
authen = FALSE;
return TRUE;
}
readmem(next + OFFSET(list_head_next), KVADDR, &next,
sizeof(ulong), "list_head", FAULT_ON_ERROR);
BZERO(path_name, PATHNAME_MAX);
BZERO(controller_name, PATHNAME_MAX);
}
return FALSE;
}
static void
cgroup_addr(char *addr)
{
ulong addr_num;
char path[PATHNAME_MAX];
char buf1[BUFSIZE];
BZERO(path, PATHNAME_MAX);
addr_num = stol (addr, FAULT_ON_ERROR, NULL);
if (!addr_to_cgroup(addr_num, path)) {
fprintf(fp, "%s can not find cgroup with address %s\n",
mkstring(buf1, VADDR_PRLEN, CENTER, addr), addr);
return ;
}
fprintf(fp, "%lx %s:%s\n", addr_num, controller_name, path);
}
static void
cgroup_list_cgroups(char **addr, struct cgroup_group_spec *cgroup_list[],
int flags)
{
int i,j;
char buf1[BUFSIZE];
i = j = 0;
cgroup_init();
if(cgroup_offset_table.cgroupfs_root_root_list == -1) {
fprintf(fp, "cgroup does not exist, check kernel version.");
return ;
}
fprintf(fp, "%s CONTROLLER:PATH\n",
mkstring(buf1, VADDR_PRLEN, CENTER, "CGROUP"));
if (flags == 0 ) {
/* list all the cgroups */
print_all_cgroups();
}
if (flags & LIST_CER) {
/* list the specified cgroups */
while ((cgroup_list[i]->path != NULL)) {
cgroup_elem(cgroup_list[i]);
i++;
}
}
if (flags & LIST_ADD) {
/* list specified addr cgroups */
while ((addr[j] != NULL)) {
cgroup_addr(addr[j]);
j++;
}
}
}
static int
parse_cgroup_spec(struct cgroup_group_spec **list, char *optarg)
{
struct cgroup_group_spec *ptr;
int i, j;
char *controller, *path, *temp;
ptr = *list;
for (i = 0; i < CG_HIER_MAX; i++, ptr++) {
if (!list[i])
break;
}
if (i == CG_HIER_MAX) {
fprintf(fp, "Max allowed hierarchies %d reached\n",
CG_HIER_MAX);
return -1;
}
controller = strtok(optarg, ":");
if (!controller)
return -1;
path = strtok(NULL, ":");
if (!path)
return -1;
list[i] = (struct cgroup_group_spec *)GETBUF(sizeof(**list));
j = 0;
do {
if (j == 0)
temp = strtok(controller, ",");
else
temp = strtok(NULL, ",");
if (temp) {
list[i]->controllers[j] = strdup(temp);
if (!list[i]->controllers[j]) {
FREEBUF(list[i]);
fprintf(fp, "%s\n", strerror(errno));
return -1;
}
}
j++;
} while (temp && (j < (CG_CONTROLLER_MAX - 1)));
strncpy(list[i]->path, path, strlen(path));
return 0;
}
static int
parse_addr_spec(char **addr,char *optarg)
{
int i;
for(i = 0; i < CG_HIER_MAX; i++)
if (!addr[i])
break;
if (i == CG_HIER_MAX) {
fprintf(fp, "Max allowed hierarchies %d reached\n",
CG_HIER_MAX);
return -1;
}
addr[i] = strdup(optarg);
if (!addr[i]) {
fprintf(fp, "%s\n",strerror(errno));
return -1;
}
return 0;
}
static void
free_cgroup_spec(char **addr, struct cgroup_group_spec **list)
{
int i,j;
for (i = 0; i < CG_HIER_MAX; i++) {
if (list[i]) {
for(j = 0; j< CG_CONTROLLER_MAX; j++)
if (list[i]->controllers[j])
free(list[i]->controllers[j]);
FREEBUF(list[i]);
}
else {
break;
}
}
for (i =0; i< CG_HIER_MAX; i++) {
if (addr[i])
free(addr[i]);
else
break;
}
}
void
cmd_lscgroup(void)
{
int c, ret, flags;
char *addr[CG_HIER_MAX];
struct cgroup_group_spec *cgroup_list[CG_HIER_MAX];
ret = flags = 0;
BZERO(cgroup_list, sizeof(cgroup_list));
BZERO(addr, sizeof(addr));
while ((c = getopt(argcnt, args, "")) != EOF) {
switch(c) {
default:
cmd_usage(pc->curcmd, SYNOPSIS);
return ;
}
}
if (argerrs)
cmd_usage(pc->curcmd, SYNOPSIS);
while (optind < argcnt) {
spe = strdup(args[optind]);
if (args[optind]) {
if (hexadecimal(args[optind], 0)) {
ret = parse_addr_spec(addr, args[optind]);
if (ret) {
fprintf(fp, "%s: cgroup controller "
"and path parsing failed(%s)\n",
args[0], args[optind]);
free_cgroup_spec(addr, cgroup_list);
return ;
}
}
else {
ret = parse_cgroup_spec(cgroup_list,
args[optind]);
if (ret) {
fprintf(fp, "%s: cgroup controller "
"and path parsing failed(%s)\n",
args[0], args[optind]);
free_cgroup_spec(addr, cgroup_list);
return ;
}
}
}
optind++;
}
if (addr[0] != NULL)
flags |= LIST_ADD;
if (cgroup_list[0] != NULL)
flags |= LIST_CER;
cgroup_list_cgroups(addr, cgroup_list, flags);
free_cgroup_spec(addr, cgroup_list);
}