libcgroup_rs 0.1.0

C FFI libcgroup
Documentation
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
//! # Globals Handler
//!
//! ### Manual
//!
//! [libcg site](http://libcg.sourceforge.net/html)
//!

#[allow(unused_imports)]
use log::{debug,info};


/// Structure describing one or more control groups.
#[allow(non_camel_case_types)]
pub enum cgroup {}


/// Structure describing a controller attached to one struct cgroup, including parameters of the group and their values.
#[allow(non_camel_case_types)]
pub enum cgroup_controller {}


#[allow(non_camel_case_types)]
#[repr(C)]
#[derive(Copy)]
pub enum cgroup_file_type{
    FILE,
    DIR,
    OTHER
}

impl Clone for cgroup_file_type {
    fn clone(&self) -> Self {
        *self
    }
}


#[repr(C)]
#[derive(Copy)]
pub struct CGroupFileInfo {
    pub c_type: cgroup_file_type,
    pub path: *const libc::c_char,
    pub parent: *const libc::c_char,
    pub full_path: *const libc::c_char,
    pub depth: libc::c_short,
}

impl Default for CGroupFileInfo {
    fn default() -> Self {
        unsafe { std::mem::zeroed() }
    }
}


impl Clone for CGroupFileInfo {
    fn clone(&self) -> Self {
        *self
    }
}




#[repr(C)]
#[derive(Copy)]
pub struct CGroupStat {
    pub name: [libc::c_char; libc::FILENAME_MAX as usize],
    pub value: [libc::c_char; libc::FILENAME_MAX as usize],
}

impl CGroupStat{
    pub fn get_name(&self)->String{
        unsafe {
            std::ffi::CStr::from_ptr(self.name.as_ptr())
                .to_string_lossy()
                .into_owned()
                .to_string()
        }
    }

    pub fn get_value(&self)->String{
        unsafe {
            std::ffi::CStr::from_ptr(self.value.as_ptr())
                .to_string_lossy()
                .into_owned()
                .to_string()
        }
    }
}

impl Clone for CGroupStat {
    fn clone(&self) -> Self {
        *self
    }
}

impl Default for CGroupStat {
    fn default() -> Self {
        unsafe { std::mem::zeroed() }
    }
}




#[repr(C)]
#[derive(Copy)]
pub struct CGroupMountPoint{
    pub name: [libc::c_char; libc::FILENAME_MAX as usize],
    pub path: [libc::c_char; libc::FILENAME_MAX as usize],
}

impl CGroupMountPoint{
    pub fn get_name(&self)->String{
        unsafe {
            std::ffi::CStr::from_ptr(self.name.as_ptr())
                .to_string_lossy()
                .into_owned()
                .to_string()
        }
    }

    pub fn get_path(&self)->String{
        unsafe {
            std::ffi::CStr::from_ptr(self.path.as_ptr())
                .to_string_lossy()
                .into_owned()
                .to_string()
        }
    }
}

impl Clone for CGroupMountPoint {
    fn clone(&self) -> Self {
        *self
    }
}

impl Default for CGroupMountPoint {
    fn default() -> Self {
        unsafe { std::mem::zeroed() }
    }
}




#[repr(C)]
#[derive(Copy)]
pub struct CGroupControllerData {
    pub name: [libc::c_char; libc::FILENAME_MAX as usize],
    pub hierarchy: libc::c_int,
    pub num_cgroups: libc::c_int,
    pub enabled: libc::c_int,
}

impl CGroupControllerData{
    pub fn get_name(&self)->String{
        unsafe {
            std::ffi::CStr::from_ptr(self.name.as_ptr())
                .to_string_lossy()
                .into_owned()
                .to_string()
        }
    }

    pub fn get_hierarchy(&self)->u32{
        self.hierarchy as u32
    }

    pub fn get_num_cgroups(&self)->u32{
        self.num_cgroups as u32
    }

    pub fn get_enabled(&self)->u32{
        self.enabled as u32
    }
}


impl Clone for CGroupControllerData {
    fn clone(&self) -> Self {
        *self
    }
}

impl Default for CGroupControllerData {
    fn default() -> Self {
        unsafe { std::mem::zeroed() }
    }
}







extern "C" {



    // 1.Initialize
    pub fn cgroup_init()->libc::c_int;
    pub fn cgroup_get_subsys_mount_point(ctrl:*const libc::c_char,mount_point:*const *const libc::c_char)->libc::c_int;



    // 2.Group Manipulation API
    pub fn cgroup_new_cgroup(name:*const libc::c_char)->*mut cgroup;
    pub fn cgroup_get_cgroup(cg:*mut cgroup)->libc::c_int;

    pub fn cgroup_add_controller(cg:*mut cgroup,name:*const libc::c_char)->*mut cgroup_controller;
    pub fn cgroup_get_controller(cg:*mut cgroup,name:*const libc::c_char)->*mut cgroup_controller;
    pub fn cgroup_free(cg:*const *const cgroup);
    pub fn cgroup_free_controllers(cg:*mut cgroup);

    pub fn cgroup_create_cgroup(cg:*mut cgroup,ignore_ownership:libc::c_int)->libc::c_int;
    pub fn cgroup_create_cgroup_from_parent(cg:*mut cgroup,ignore_ownership:libc::c_int)->libc::c_int;
    pub fn cgroup_modify_cgroup(cg:*mut cgroup)->libc::c_int;
    pub fn cgroup_delete_cgroup(cg:*mut cgroup,ignore_migration:libc::c_int)->libc::c_int;
    pub fn cgroup_delete_cgroup_ext(cg:*mut cgroup,flags:libc::c_int)->libc::c_int;

    pub fn cgroup_copy_cgroup(dst:*mut cgroup,src:*mut cgroup)->libc::c_int;
    pub fn cgroup_compare_cgroup(cg_a:*mut cgroup,cg_b:*mut cgroup)->libc::c_int;
    pub fn cgroup_compare_controllers(cg_ctrl_a:*mut cgroup_controller,cg_ctrl_b:*mut cgroup_controller)->libc::c_int;
    pub fn cgroup_set_uid_gid(
        cg:*mut cgroup,
        tasks_uid: libc::uid_t,
        tasks_gid: libc::gid_t,
        ctrl_uid: libc::uid_t,
        ctrl_gid: libc::gid_t
    )->libc::c_int;
    pub fn cgroup_get_uid_gid(
        cg:*mut cgroup,
        tasks_uid: *const libc::uid_t,
        tasks_gid: *const libc::gid_t,
        ctrl_uid: *const libc::uid_t,
        ctrl_gid: *const libc::gid_t
    )->libc::c_int;



    pub fn cgroup_add_value_string(
        cg_ctrl:*mut cgroup_controller,
        name:*const libc::c_char,
        value:*const libc::c_char
    )->libc::c_int;

    pub fn cgroup_add_value_int64(
        cg_ctrl:*mut cgroup_controller,
        name:*const libc::c_char,
        value:libc::c_longlong
    )->libc::c_int;

    pub fn cgroup_add_value_uint64(
        cg_ctrl:*mut cgroup_controller,
        name:*const libc::c_char,
        value:libc::c_ulonglong
    )->libc::c_int;

    pub fn cgroup_add_value_bool(
        cg_ctrl:*mut cgroup_controller,
        name:*const libc::c_char,
        value:bool
    )->libc::c_int;

    pub fn cgroup_get_value_string(
        cg_ctrl:*mut cgroup_controller,
        name:*const libc::c_char,
        value:*const *const libc::c_char
    )->libc::c_int;

    pub fn cgroup_get_value_int64(
        cg_ctrl:*mut cgroup_controller,
        name:*const libc::c_char,
        value:*mut libc::c_longlong
    )->libc::c_int;

    pub fn cgroup_get_value_uint64(
        cg_ctrl:*mut cgroup_controller,
        name:*const libc::c_char,
        value:*mut libc::c_ulonglong
    )->libc::c_int;

    pub fn cgroup_get_value_bool(
        cg_ctrl:*mut cgroup_controller,
        name:*const libc::c_char,
        value:*mut bool
    )->libc::c_int;

    pub fn cgroup_set_value_string(
        cg_ctrl:*mut cgroup_controller,
        name:*const libc::c_char,
        value:*const libc::c_char
    )->libc::c_int;

    pub fn cgroup_set_value_int64(
        cg_ctrl:*mut cgroup_controller,
        name:*const libc::c_char,
        value:libc::c_longlong
    )->libc::c_int;

    pub fn cgroup_set_value_uint64(
        cg_ctrl:*mut cgroup_controller,
        name:*const libc::c_char,
        value:libc::c_ulonglong
    )->libc::c_int;

    pub fn cgroup_set_value_bool(
        cg_ctrl:*mut cgroup_controller,
        name:*const libc::c_char,
        value:bool
    )->libc::c_int;

    pub fn cgroup_get_value_name_count(cg_ctrl:*mut cgroup_controller)->libc::c_int;
    pub fn cgroup_get_value_name(cg_ctrl:*mut cgroup_controller,idx:libc::c_int)->*mut libc::c_char;



    // 3.Iterators
    pub fn cgroup_walk_tree_begin(
        ctrl: *const libc::c_char,
        base_path: *const libc::c_char,
        depth: libc::c_int,
        handle: *const *const libc::c_void,
        info: *mut CGroupFileInfo,
        base_level: *mut libc::c_int
    )->libc::c_int;
    pub fn cgroup_walk_tree_next(
        handle: *const *const libc::c_void,
        info: *mut CGroupFileInfo,
        base_level: *mut libc::c_int
    )->libc::c_int;
    pub fn cgroup_walk_tree_end(handle: *const *const libc::c_void)->libc::c_int;
    pub fn cgroup_walk_tree_set_flags(handle: *const *const libc::c_void,flags:libc::c_int)->libc::c_int;






    pub fn cgroup_read_stats_begin(
        ctrl_name:*const libc::c_char,
        path_name:*const libc::c_char,
        handle: *const *const libc::c_void,
        info: *mut CGroupStat
    )-> libc::c_int;

    pub fn cgroup_read_stats_next(handle: *const *const libc::c_void,
                                  info: *mut CGroupStat)
                                -> libc::c_int;
    pub fn cgroup_read_stats_end(handle: *const *const libc::c_void) -> libc::c_int;


    pub fn cgroup_get_task_begin(
        cg_name:*const libc::c_char,
        ctrl_name:*const libc::c_char,
        handle: *const *const libc::c_void,
        info: *mut libc::pid_t
    )-> libc::c_int;

    pub fn cgroup_get_task_next(handle: *const *const libc::c_void,
                                      info: *mut libc::pid_t)
                                      -> libc::c_int;
    pub fn cgroup_get_task_end(handle: *const *const libc::c_void) -> libc::c_int;



    pub fn cgroup_get_controller_begin(handle: *const *const libc::c_void,
                                           info: *mut CGroupMountPoint)
                                           -> libc::c_int;
    pub fn cgroup_get_controller_next(handle: *const *const libc::c_void,
                                          info: *mut CGroupMountPoint)
                                          -> libc::c_int;
    pub fn cgroup_get_controller_end(handle: *const *const libc::c_void) -> libc::c_int;



    pub fn cgroup_get_all_controller_begin(handle: *const *const libc::c_void,
                                           info: *mut CGroupControllerData)
                                           -> libc::c_int;
    pub fn cgroup_get_all_controller_next(handle: *const *const libc::c_void,
                                          info: *mut CGroupControllerData)
                                          -> libc::c_int;
    pub fn cgroup_get_all_controller_end(handle: *const *const libc::c_void) -> libc::c_int;


    // 4. Manipulation with Tasks
    pub fn cgroup_attach_task(cg:*mut cgroup)->libc::c_int;
    pub fn cgroup_attach_task_pid(cg:*mut cgroup,pid:libc::pid_t)->libc::c_int;
    pub fn cgroup_get_current_controller_path(
        pid:libc::pid_t,
        ctrl:*const libc::c_char,
        current_path:*mut *mut libc::c_char
    )->libc::c_int;


    // 5. Configuration
    pub fn cgroup_config_load_config(pathname:*const libc::c_char)->libc::c_int;
    pub fn cgroup_unload_cgroups()->libc::c_int;


    // 6.Error handling
    pub fn cgroup_get_last_errno()->libc::c_int;
    pub fn cgroup_strerror(code:libc::c_int)->*const libc::c_char;



}