-
Notifications
You must be signed in to change notification settings - Fork 0
/
cielcg.py
executable file
·630 lines (592 loc) · 27.1 KB
/
cielcg.py
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
#!/usr/bin/python
'''
cielcg - alternative cgroup-tools (cgexec) implementation for both cgroup v1 and v2 (cgroup2)
(C) @cielavenir
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
'''
import sys
import os
import argparse
import shutil
import errno
try:
from shutil import chown
except ImportError:
from backports.shutil_chown import chown
cgrpath = cgrver = None
def GetCgroupMount():
global cgrpath, cgrver
if cgrpath:
return (cgrpath, cgrver)
with open('/proc/mounts') as f:
for line in f:
dv, path, typ, opt, x, y = line.rstrip().split()
if typ == 'cgroup':
cgrpath, cgrver = os.path.dirname(path), 1
return (cgrpath, cgrver)
f.seek(0)
for line in f:
dv, path, typ, opt, x, y = line.rstrip().split()
if typ == 'cgroup2':
cgrpath, cgrver = path, 2
return (cgrpath, cgrver)
raise Exception('could not found cgroup path')
def ConvertToInt(val):
try:
return int(val)
except ValueError:
return val
def makedirs(name, mode=0o777, exist_ok=False):
if not exist_ok:
os.makedirs(name,mode=mode)
elif sys.version_info[0]>=3:
os.makedirs(name,mode=mode,exist_ok=exist_ok)
else:
try:
os.makedirs(name,mode=mode)
except OSError as e:
if e.errno!=errno.EEXIST:
raise
def rmdirs(path):
for curDir, dirs, files in os.walk(path, topdown=False):
for dir in dirs:
if not os.path.islink(os.path.join(curDir, dir)):
os.rmdir(os.path.join(curDir, dir))
os.rmdir(path)
def cgexec(logout, argv):
cgrpath, cgrver = GetCgroupMount()
parser = argparse.ArgumentParser(prog='cgexec')
parser.add_argument('-g', action='append', default=[], metavar='<controllers>:<path>', help='Control group which should be added')
parser.add_argument('cmd', nargs=argparse.REMAINDER, help='Command to execute')
args = parser.parse_args(argv)
# print(args.cmd)
if not args.cmd:
sys.stderr.write('command to execute is not specified\n')
return
pid = os.getpid()
if cgrver == 1:
for grp in args.g:
typ, path = grp.split(':')
if typ.startswith('/'):
typ = typ[1:]
if path.startswith('/'):
path = path[1:]
with open(os.path.join(cgrpath,typ,path,'cgroup.procs'), 'a') as f:
f.write('%d\n'%pid)
elif cgrver == 2:
for grp in args.g:
typ, path = grp.split(':')
if typ.startswith('/'):
typ = typ[1:]
if path.startswith('/'):
path = path[1:]
with open(os.path.join(cgrpath,path,'cgroup.procs'), 'a') as f:
f.write('%d\n'%pid)
else:
raise NotImplementedError('unknown cgroup version %d'%cgrver)
os.execvp(args.cmd[0],args.cmd)
def cgset(logout, argv):
def cgr2write(dir, key, val):
if key == 'cpu.cfs_period_us':
if os.path.isfile(os.path.join(dir,'cpu.max')):
with open(os.path.join(dir,'cpu.max'), 'r+') as f:
cont = f.read().split()
cont[1] = val
f.seek(0)
f.write('%s\n'%(' '.join(cont)))
f.truncate(f.tell())
return True
return False
if key == 'cpu.cfs_quota_us':
if os.path.isfile(os.path.join(dir,'cpu.max')):
with open(os.path.join(dir,'cpu.max'), 'r+') as f:
cont = f.read().split()
cont[0] = 'max' if val=='-1' else val
f.seek(0)
f.write('%s\n'%(' '.join(cont)))
f.truncate(f.tell())
return True
return False
if key == 'cpu.rt_period_us':
if os.path.isfile(os.path.join(dir,'cpu.rt.max')):
with open(os.path.join(dir,'cpu.rt.max'), 'r+') as f:
cont = f.read().split()
cont[1] = val
f.seek(0)
f.write('%s\n'%(' '.join(cont)))
f.truncate(f.tell())
return True
return False
if key == 'cpu.rt_runtime_us':
if os.path.isfile(os.path.join(dir,'cpu.rt.max')):
with open(os.path.join(dir,'cpu.rt.max'), 'r+') as f:
cont = f.read().split()
cont[0] = 'max' if val=='-1' else val
f.seek(0)
f.write('%s\n'%(' '.join(cont)))
f.truncate(f.tell())
return True
return False
elif key == 'cpu.shares':
if os.path.isfile(os.path.join(dir,'cpu.weight')):
with open(os.path.join(dir,'cpu.weight'), 'w') as f:
f.write('%d\n'%(int(val) * 100 // 1024))
return True
return False
elif key == 'memory.limit_in_bytes':
if os.path.isfile(os.path.join(dir,'memory.max')):
with open(os.path.join(dir,'memory.max'), 'w') as f:
f.write('%s\n'%val) # todo handle max properly
return True
return False
elif key == 'memory.soft_limit_in_bytes':
if os.path.isfile(os.path.join(dir,'memory.high')):
with open(os.path.join(dir,'memory.high'), 'w') as f:
f.write('%s\n'%val) # todo handle max properly
return True
return False
cgrpath, cgrver = GetCgroupMount()
parser = argparse.ArgumentParser(prog='cgset')
parser.add_argument('-r', '--variable', metavar='<name=value>', action='append', default=[], help='Define parameter to set')
parser.add_argument('--copy-from', metavar='<source_cgroup_path>', help='Control group whose parameters will be copied')
parser.add_argument('path', nargs='+', help='Control group')
args = parser.parse_args(argv)
if args.copy_from is None and not args.variable:
sys.stderr.write('need to parameter value or source\n')
return
if cgrver == 1:
for path in args.path:
if path.startswith('/'):
path = path[1:]
if args.copy_from is not None:
src = args.copy_from
if src.startswith('/'):
src = src[1:]
for typ in [e for e in os.listdir(cgrpath) if not os.path.islink(os.path.join(cgrpath,e))]:
if os.path.isdir(os.path.join(cgrpath,typ,src)) and os.path.isdir(os.path.join(cgrpath,typ,path)):
for ent in os.listdir(os.path.join(cgrpath,typ,src)):
if os.path.isfile(os.path.join(cgrpath,typ,src,ent)):
if ent in ['tasks', 'notify_on_release', 'release_agent']:
continue
if ent.startswith('cgroup.'):
continue
with open(os.path.join(cgrpath,typ,src,ent), 'r') as fin:
try:
with open(os.path.join(cgrpath,typ,path,ent), 'w') as fout:
shutil.copyfileobj(fin, fout)
except OSError as e:
if e.errno!=errno.EINVAL:
raise
for var in args.variable:
key, val = var.split('=',1)
typ = key.split('.')[0]
with open(os.path.join(cgrpath,typ,path,key), 'w') as f:
f.write(val)
elif cgrver == 2:
for path in args.path:
if path.startswith('/'):
path = path[1:]
if args.copy_from is not None:
src = args.copy_from
if src.startswith('/'):
src = src[1:]
for ent in os.listdir(os.path.join(cgrpath,src)):
if os.path.isfile(os.path.join(cgrpath,src,ent)):
if ent.startswith('cgroup.'):
continue
with open(os.path.join(cgrpath,src,ent), 'r') as fin:
try:
with open(os.path.join(cgrpath,path,ent), 'w') as fout:
shutil.copyfileobj(fin, fout)
except OSError as e:
if e.errno!=errno.EINVAL:
raise
for var in args.variable:
key, val = var.split('=',1)
if not cgr2write(os.path.join(cgrpath,path), key, val):
with open(os.path.join(cgrpath,path,key), 'w') as f:
f.write(val)
else:
raise NotImplementedError('unknown cgroup version %d'%cgrver)
def cgget(logout, argv):
def cgr2read(logout, args, dir, key):
if key == 'cpu.cfs_period_us':
if os.path.isfile(os.path.join(dir,'cpu.max')):
with open(os.path.join(dir,'cpu.max'), 'r') as f:
cont = f.read().split()[1]
if not args.values_only:
logout.write('cpu.cfs_period_us: ')
logout.write('%s\n'%cont)
return True
return False
elif key == 'cpu.cfs_quota_us':
if os.path.isfile(os.path.join(dir,'cpu.max')):
with open(os.path.join(dir,'cpu.max'), 'r') as f:
cont = f.read().split()[0]
if not args.values_only:
logout.write('cpu.cfs_quota_us: ')
logout.write('%s\n'%('-1' if cont == 'max' else cont))
return True
return False
elif key == 'cpu.rt_period_us':
if os.path.isfile(os.path.join(dir,'cpu.rt.max')):
with open(os.path.join(dir,'cpu.rt.max'), 'r') as f:
cont = f.read().split()[1]
if not args.values_only:
logout.write('cpu.rt_period_us: ')
logout.write('%s\n'%cont)
return True
return False
elif key == 'cpu.rt_runtime_us':
if os.path.isfile(os.path.join(dir,'cpu.rt.max')):
with open(os.path.join(dir,'cpu.rt.max'), 'r') as f:
cont = f.read().split()[0]
if not args.values_only:
logout.write('cpu.rt_runtime_us: ')
logout.write('%s\n'%('-1' if cont == 'max' else cont))
return True
return False
elif key == 'cpu.shares':
if os.path.isfile(os.path.join(dir,'cpu.weight')):
with open(os.path.join(dir,'cpu.weight'), 'r') as f:
cont = int(f.read())
if not args.values_only:
logout.write('cpu.shares: ')
logout.write('%d\n'%(1024 * cont // 100))
return True
return False
elif key == 'memory.limit_in_bytes':
if os.path.isfile(os.path.join(dir,'memory.max')):
with open(os.path.join(dir,'memory.max'), 'r') as f:
cont = f.read().strip()
if not args.values_only:
logout.write('memory.limit_in_bytes: ')
logout.write('%s\n'%(str((1<<63)-4096) if cont == 'max' else cont))
return True
return False
elif key == 'memory.soft_limit_in_bytes':
if os.path.isfile(os.path.join(dir,'memory.high')):
with open(os.path.join(dir,'memory.high'), 'r') as f:
cont = f.read().strip()
if not args.values_only:
logout.write('memory.soft_limit_in_bytes: ')
logout.write('%s\n'%(str((1<<63)-4096) if cont == 'max' else cont))
return True
return False
cgrpath, cgrver = GetCgroupMount()
parser = argparse.ArgumentParser(prog='cgget')
parser.add_argument('-g', action='append', default=[], metavar='<controllers>(:<path>)', help='Control group which should be added')
parser.add_argument('-r', '--variable', metavar='<name>', action='append', default=[], help='Define parameter to display')
parser.add_argument('-n', action='store_true', help='Do not print headers')
parser.add_argument('-v', '--values-only', action='store_true', help='Print only values, not parameter names')
parser.add_argument('path', nargs='*', help='Control group')
args = parser.parse_args(argv)
paths = []
for path in args.path:
paths.append(path)
typs = []
for grp in args.g:
typpath = grp.split(':')
if len(typpath)==1:
typs.append(typpath[0])
if not typs and cgrver == 1:
typs = [e for e in os.listdir(cgrpath) if not os.path.islink(os.path.join(cgrpath,e))]
if cgrver == 1:
for path in args.path:
if not args.n:
logout.write('%s:\n'%path)
if path.startswith('/'):
path = path[1:]
for typ in typs:
if os.path.isdir(os.path.join(cgrpath,typ,path)):
for ent in sorted(os.listdir(os.path.join(cgrpath,typ,path))):
if not os.path.isfile(os.path.join(cgrpath,typ,path,ent)):
continue
if ent in ['tasks', 'notify_on_release', 'release_agent']:
continue
if ent.startswith('cgroup.'):
continue
if args.variable and ent not in args.variable:
continue
if not args.values_only:
logout.write('%s: '%ent)
if not os.stat(os.path.join(cgrpath,typ,path,ent)).st_mode&0o400:
logout.write('\n')
continue
with open(os.path.join(cgrpath,typ,path,ent), 'r') as f:
shutil.copyfileobj(f, logout)
for grp in args.g:
typpath = grp.split(':')
if len(typpath)==2:
typ, path = typpath
if not args.n:
logout.write('%s:\n'%path)
if path.startswith('/'):
path = path[1:]
if os.path.isdir(os.path.join(cgrpath,typ,path)):
for ent in sorted(os.listdir(os.path.join(cgrpath,typ,path))):
if not os.path.isfile(os.path.join(cgrpath,typ,path,ent)):
continue
if ent in ['tasks', 'notify_on_release', 'release_agent']:
continue
if ent.startswith('cgroup.'):
continue
if args.variable and ent not in args.variable:
continue
if not args.values_only:
logout.write('%s: '%ent)
if not os.stat(os.path.join(cgrpath,typ,path,ent)).st_mode&0o400:
logout.write('\n')
continue
with open(os.path.join(cgrpath,typ,path,ent), 'r') as f:
shutil.copyfileobj(f, logout)
elif cgrver == 2:
for path in args.path:
if not args.n:
logout.write('%s:\n'%path)
if path.startswith('/'):
path = path[1:]
if 1:
if os.path.isdir(os.path.join(cgrpath,path)):
for ent in args.variable:
cgr2read(logout, args, os.path.join(cgrpath,path), ent)
for ent in sorted(os.listdir(os.path.join(cgrpath,path))):
if not os.path.isfile(os.path.join(cgrpath,path,ent)):
continue
if ent.startswith('cgroup.'):
continue
if args.variable and ent not in args.variable:
continue
if not args.values_only:
logout.write('%s: '%ent)
if not os.stat(os.path.join(cgrpath,path,ent)).st_mode&0o400:
logout.write('\n')
continue
with open(os.path.join(cgrpath,path,ent), 'r') as f:
shutil.copyfileobj(f, logout)
for grp in args.g:
typpath = grp.split(':')
if len(typpath)==2:
typ, path = typpath
if not args.n:
logout.write('%s:\n'%path)
if path.startswith('/'):
path = path[1:]
if os.path.isdir(os.path.join(cgrpath,path)):
for ent in args.variable:
cgr2read(logout, args, os.path.join(cgrpath,path), ent)
for ent in sorted(os.listdir(os.path.join(cgrpath,path))):
if not os.path.isfile(os.path.join(cgrpath,path,ent)):
continue
if not ent.startswith(typ+'.'):
continue
if ent.startswith('cgroup.'):
continue
if args.variable and ent not in args.variable:
continue
if not args.values_only:
logout.write('%s: '%ent)
if not os.stat(os.path.join(cgrpath,path,ent)).st_mode&0o400:
logout.write('\n')
continue
with open(os.path.join(cgrpath,path,ent), 'r') as f:
shutil.copyfileobj(f, logout)
else:
raise NotImplementedError('unknown cgroup version %d'%cgrver)
def cgcreate(logout, argv):
cgrpath, cgrver = GetCgroupMount()
parser = argparse.ArgumentParser(prog='cgcreate')
parser.add_argument('-g', action='append', required=True, default=[], metavar='<controllers>:<path>', help='Control group which should be added')
parser.add_argument('-a', metavar='<tuid>:<tgid>', help='Owner of the group and all its files')
parser.add_argument('-t', metavar='<tuid>:<tgid>', help='Owner of the tasks file')
parser.add_argument('-s', '--tperm', metavar='mode', help='Tasks file permissions')
# parser.add_argument('-d', '--dperm', metavar='mode', help='Group directory permissions')
# parser.add_argument('-f', '--fperm', metavar='mode', help='Group file permissions')
args = parser.parse_args(argv)
if cgrver == 1:
for grp in args.g:
typ, path = grp.split(':')
if typ.startswith('/'):
typ = typ[1:]
if path.startswith('/'):
path = path[1:]
makedirs(os.path.join(cgrpath,typ,path),0o755,exist_ok=True)
if args.a is not None:
uid, gid = args.a.split(':')
chown(os.path.join(cgrpath,typ,path), ConvertToInt(uid), ConvertToInt(gid))
if args.t is not None:
uid, gid = args.t.split(':')
chown(os.path.join(cgrpath,typ,path,'tasks'), ConvertToInt(uid), ConvertToInt(gid))
chown(os.path.join(cgrpath,typ,path,'cgroup.procs'), ConvertToInt(uid), ConvertToInt(gid))
if args.tperm is not None:
os.chmod(os.path.join(cgrpath,typ,path,'tasks'), int(args.tperm, 8))
os.chmod(os.path.join(cgrpath,typ,path,'cgroup.procs'), int(args.tperm, 8))
elif cgrver == 2:
for grp in args.g:
typ, path = grp.split(':')
if typ.startswith('/'):
typ = typ[1:]
if path.startswith('/'):
path = path[1:]
makedirs(os.path.join(cgrpath,path),0o755,exist_ok=True)
if args.a is not None:
uid, gid = args.a.split(':')
chown(os.path.join(cgrpath,path), ConvertToInt(uid), ConvertToInt(gid))
if args.t is not None:
uid, gid = args.t.split(':')
chown(os.path.join(cgrpath,path,'cgroup.procs'), ConvertToInt(uid), ConvertToInt(gid))
if args.tperm is not None:
os.chmod(os.path.join(cgrpath,typ,path,'cgroup.procs'), int(args.tperm, 8))
else:
raise NotImplementedError('unknown cgroup version %d'%cgrver)
def cgdelete(logout, argv):
cgrpath, cgrver = GetCgroupMount()
parser = argparse.ArgumentParser(prog='cgdelete')
parser.add_argument('-g', action='append', required=True, default=[], metavar='<controllers>:<path>', help='Control group to be removed')
parser.add_argument('-r', action='store_true', help='Recursively remove all subgroups')
args = parser.parse_args(argv)
if cgrver == 1:
for grp in args.g:
typ, path = grp.split(':')
if typ.startswith('/'):
typ = typ[1:]
if path.startswith('/'):
path = path[1:]
if os.path.isdir(os.path.join(cgrpath,typ,path)):
if args.r:
rmdirs(os.path.join(cgrpath,typ,path))
else:
os.rmdir(os.path.join(cgrpath,typ,path))
elif cgrver == 2:
for grp in args.g:
typ, path = grp.split(':')
if typ.startswith('/'):
typ = typ[1:]
if path.startswith('/'):
path = path[1:]
if os.path.isdir(os.path.join(cgrpath,path)):
if args.r:
rmdirs(os.path.join(cgrpath,path))
else:
os.rmdir(os.path.join(cgrpath,path))
else:
raise NotImplementedError('unknown cgroup version %d'%cgrver)
def cgclassify(logout, argv):
cgrpath, cgrver = GetCgroupMount()
parser = argparse.ArgumentParser(prog='cgclassify')
parser.add_argument('-g', action='append', default=[], metavar='<controllers>:<path>', help='Control group to be used as target')
parser.add_argument('pids', nargs='+', type=int, help='PIDs to set cgroup')
args = parser.parse_args(argv)
if cgrver == 1:
for grp in args.g:
typ, path = grp.split(':')
if typ.startswith('/'):
typ = typ[1:]
if path.startswith('/'):
path = path[1:]
with open(os.path.join(cgrpath,typ,path,'tasks'), 'a') as f:
for pid in args.pids:
f.write('%d\n'%pid)
elif cgrver == 2:
for grp in args.g:
typ, path = grp.split(':')
if typ.startswith('/'):
typ = typ[1:]
if path.startswith('/'):
path = path[1:]
with open(os.path.join(cgrpath,path,'tasks'), 'a') as f:
for pid in args.pids:
f.write('%d\n'%pid)
else:
raise NotImplementedError('unknown cgroup version %d'%cgrver)
def lssubsys(logout, argv):
cgrpath, cgrver = GetCgroupMount()
parser = argparse.ArgumentParser(prog='lssubsys')
args = parser.parse_args(argv)
if cgrver == 1:
for typ in sorted(os.listdir(cgrpath)):
if os.path.isdir(os.path.join(cgrpath,typ)) and not os.path.islink(os.path.join(cgrpath,typ)):
logout.write('%s\n'%typ)
elif cgrver == 2:
pass
else:
raise NotImplementedError('unknown cgroup version %d'%cgrver)
def lscgroup(logout, argv):
cgrpath, cgrver = GetCgroupMount()
parser = argparse.ArgumentParser(prog='lscgroup')
args = parser.parse_args(argv)
if cgrver == 1:
for typ in sorted(os.listdir(cgrpath)):
if os.path.isdir(os.path.join(cgrpath,typ)) and not os.path.islink(os.path.join(cgrpath,typ)):
for curDir, dirs, files in os.walk(os.path.join(cgrpath,typ)):
for dir in dirs:
logout.write('%s:%s\n'%(typ,os.path.join(curDir,dir)[len(os.path.join(cgrpath,typ)):]))
elif cgrver == 2:
for curDir, dirs, files in os.walk(cgrpath):
for dir in dirs:
logout.write(':%s\n'%(os.path.join(curDir,dir)[len(cgrpath):]))
else:
raise NotImplementedError('unknown cgroup version %d'%cgrver)
def cgsnapshot(logout, argv):
raise NotImplementedError()
def cgclear(logout, argv):
raise NotImplementedError()
def cgconfigparser(logout, argv):
raise NotImplementedError()
def cgrulesengd(logout, argv):
raise NotImplementedError()
def main(logout, argv):
appletList = {
'cgexec': cgexec,
'cgset': cgset,
'cgget': cgget,
'cgcreate': cgcreate,
'cgdelete': cgdelete,
'cgclassify': cgclassify,
'lssubsys': lssubsys,
'lscgroup': lscgroup,
# 'cgsnapshot': cgsnapshot,
# 'cgclear': cgclear,
# 'cgconfigparser': cgconfigparser,
# 'cgrulesengd': cgrulesengd,
}
prog = os.path.basename(argv[0]).split('.')[0]
if prog not in appletList:
exe = argv.pop(0)
if not argv:
sys.stderr.write('cielcg - alternative cgroup-tools (cgexec) implementation for both cgroup v1 and v2 (cgroup2)')
sys.stderr.write('use --list to show available subcommands.')
prog = argv[0]
if prog == '--install':
if not os.path.isfile(exe):
raise Exception('exe (%s) does not exist. called by $PATH?'%exe)
for appletname in appletList:
os.symlink(exe, appletname)
return
if prog == '--list':
for appletname in appletList:
logout.write('%s\n'%appletname)
return
fn = appletList.get(prog)
if fn is None:
sys.stderr.write('%s applet is not available\n'%prog)
else:
fn(logout, argv[1:])
if __name__ == '__main__':
main(sys.stdout, list(sys.argv))