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
pub use self::result::{CGroupError, Result};
use self::result::check_return;
mod result;
pub use self::iter::*;
mod iter;
pub use self::init::init;
mod init;

extern crate libcgroup_sys as ffi;
extern crate libc;

use std::ffi::{CStr, CString};

pub struct CGroup {
    cgroup: *const ffi::cgroup,
}

pub struct CGroupController {
    controller: *const ffi::cgroup_controller,
}

pub struct CGroupUidGid {
    pub tasks_uid: u32,
    pub tasks_gid: u32,
    pub control_uid: u32,
    pub control_gid: u32,
}

pub struct CGroupPermissions {
    pub control_dperm: u32,
    pub control_fperm: u32,
    pub task_fperm: u32,
}

impl Drop for CGroup {
    fn drop(&mut self) {
        unsafe {
            ffi::cgroup_free(&self.cgroup);
        }
    }
}

pub enum Compare {
    Equal,
    NotEqual,
    ControllersNotEqual,
}

impl CGroup {
    pub fn new<S>(name: S) -> Result<CGroup>
        where S: Into<String>
    {
        init();
        let cg = CGroup {
            cgroup: unsafe { ffi::cgroup_new_cgroup(CString::new(name.into()).unwrap().as_ptr()) },
        };
        if cg.cgroup.is_null() {
            return check_return(ffi::ECGFAIL, cg);
        }
        Ok(cg)
    }

    pub fn add_controller<S>(&self, controller: S) -> Result<CGroupController>
        where S: Into<String>
    {
        let ctrlr = unsafe {
            ffi::cgroup_add_controller(self.cgroup,
                                       CString::new(controller.into()).unwrap().as_ptr())
        };
        let cgroup_controller = CGroupController { controller: ctrlr };
        if ctrlr.is_null() {
            return check_return(ffi::ECGFAIL, cgroup_controller);
        }
        Ok(cgroup_controller)
    }

    pub fn get_controller<S>(&self, controller: S) -> Result<CGroupController>
        where S: Into<String>
    {
        let ctrlr = unsafe {
            ffi::cgroup_get_controller(self.cgroup,
                                       CString::new(controller.into()).unwrap().as_ptr())
        };
        let cgroup_controller = CGroupController { controller: ctrlr };
        if ctrlr.is_null() {
            return check_return(ffi::ECGFAIL, cgroup_controller);
        }
        Ok(cgroup_controller)
    }

    pub fn create(&self) -> Result<()> {
        let ret = unsafe { ffi::cgroup_create_cgroup(self.cgroup, 0) };
        check_return(ret, ())
    }

    pub fn create_from_parent(&self) -> Result<()> {
        let ret = unsafe { ffi::cgroup_create_cgroup_from_parent(self.cgroup, 0) };
        check_return(ret, ())
    }

    pub fn modify(&self) -> Result<()> {
        let ret = unsafe { ffi::cgroup_modify_cgroup(self.cgroup) };
        check_return(ret, ())
    }

    pub fn delete(&self) -> Result<()> {
        let ret = unsafe { ffi::cgroup_delete_cgroup(self.cgroup, 0) };
        check_return(ret, ())
    }

    pub fn get(&self) -> Result<()> {
        let ret = unsafe { ffi::cgroup_get_cgroup(self.cgroup) };
        check_return(ret, ())
    }

    pub fn copy(&self, dest: &CGroup) -> Result<()> {
        let ret = unsafe { ffi::cgroup_copy_cgroup(self.cgroup, dest.cgroup) };
        check_return(ret, ())
    }

    pub fn compare(&self, other: &CGroup) -> Compare {
        match unsafe { ffi::cgroup_compare_cgroup(self.cgroup, other.cgroup) } {
            ffi::ECGCONTROLLERNOTEQUAL => Compare::ControllersNotEqual,
            ffi::ECGROUPNOTEQUAL => Compare::NotEqual,
            0 => Compare::Equal,
            x => panic!("Invalid return: {}", x),
        }
    }

    pub fn set_uid_gid(&self, uid_gid: CGroupUidGid) -> Result<()> {
        let ret = unsafe {
            ffi::cgroup_set_uid_gid(self.cgroup,
                                    uid_gid.tasks_uid,
                                    uid_gid.tasks_gid,
                                    uid_gid.control_uid,
                                    uid_gid.control_gid)
        };
        check_return(ret, ())
    }

    pub fn get_uid_gid(&self) -> Result<CGroupUidGid> {
        let (mut tasks_uid, mut tasks_gid, mut control_uid, mut control_gid) =
            (0u32, 0u32, 0u32, 0u32);
        let ret = unsafe {
            ffi::cgroup_get_uid_gid(self.cgroup,
                                    &mut tasks_uid,
                                    &mut tasks_gid,
                                    &mut control_uid,
                                    &mut control_gid)
        };
        check_return(ret,
                     CGroupUidGid {
                         tasks_uid: tasks_uid,
                         tasks_gid: tasks_gid,
                         control_uid: control_uid,
                         control_gid: control_gid,
                     })
    }

    pub fn set_permissions(&self, perms: CGroupPermissions) {
        unsafe {
            ffi::cgroup_set_permissions(self.cgroup,
                                        perms.control_dperm,
                                        perms.control_fperm,
                                        perms.task_fperm)
        };
    }
}

impl CGroupController {
    pub fn compare(&self, other: &CGroupController) -> i32 {
        unsafe { ffi::cgroup_compare_controllers(self.controller, other.controller) }
    }

    pub fn add_value_string<S>(&self, name: S, value: S) -> Result<()>
        where S: Into<String>
    {
        let ret = unsafe {
            ffi::cgroup_add_value_string(self.controller,
                                         CString::new(name.into()).unwrap().as_ptr(),
                                         CString::new(value.into()).unwrap().as_ptr())
        };
        check_return(ret, ())
    }

    pub fn add_value_int64<S>(&self, name: S, value: i64) -> Result<()>
        where S: Into<String>
    {
        let ret = unsafe {
            ffi::cgroup_add_value_int64(self.controller,
                                        CString::new(name.into()).unwrap().as_ptr(),
                                        value)
        };
        check_return(ret, ())
    }

    pub fn add_value_uint64<S>(&self, name: S, value: u64) -> Result<()>
        where S: Into<String>
    {
        let ret = unsafe {
            ffi::cgroup_add_value_uint64(self.controller,
                                         CString::new(name.into()).unwrap().as_ptr(),
                                         value)
        };
        check_return(ret, ())
    }

    pub fn add_value_bool<S>(&self, name: S, value: bool) -> Result<()>
        where S: Into<String>
    {
        let ret = unsafe {
            ffi::cgroup_add_value_bool(self.controller,
                                       CString::new(name.into()).unwrap().as_ptr(),
                                       value)
        };
        check_return(ret, ())
    }

    pub fn get_value_string<S>(&self, name: S) -> Result<String>
        where S: Into<String>
    {
        let value: *const libc::c_char = std::ptr::null();
        let ret = unsafe {
            ffi::cgroup_get_value_string(self.controller,
                                         CString::new(name.into()).unwrap().as_ptr(),
                                         &value as *const *const libc::c_char)
        };
        check_return(ret,
                     unsafe { CStr::from_ptr(value).to_string_lossy().into_owned() })
    }

    pub fn get_value_int64<S>(&self, name: S) -> Result<i64>
        where S: Into<String>
    {
        let value = 0i64;
        let ret = unsafe {
            ffi::cgroup_get_value_int64(self.controller,
                                        CString::new(name.into()).unwrap().as_ptr(),
                                        &value)
        };
        check_return(ret, value)
    }

    pub fn get_value_uint64<S>(&self, name: S) -> Result<u64>
        where S: Into<String>
    {
        let value = 0u64;
        let ret = unsafe {
            ffi::cgroup_get_value_uint64(self.controller,
                                         CString::new(name.into()).unwrap().as_ptr(),
                                         &value)
        };
        check_return(ret, value)
    }

    pub fn get_value_bool<S>(&self, name: S) -> Result<bool>
        where S: Into<String>
    {
        let value = false;
        let ret = unsafe {
            ffi::cgroup_get_value_bool(self.controller,
                                       CString::new(name.into()).unwrap().as_ptr(),
                                       &value)
        };
        check_return(ret, value)
    }

    pub fn set_value_string<S>(&self, name: S, value: S) -> Result<*const CGroupController>
        where S: Into<String>
    {
        let ret = unsafe {
            ffi::cgroup_set_value_string(self.controller,
                                         CString::new(name.into()).unwrap().as_ptr(),
                                         CString::new(value.into()).unwrap().as_ptr())
        };
        check_return(ret, self)
    }

    pub fn set_value_int64<S>(&self, name: S, value: i64) -> Result<*const CGroupController>
        where S: Into<String>
    {
        let ret = unsafe {
            ffi::cgroup_set_value_int64(self.controller,
                                        CString::new(name.into()).unwrap().as_ptr(),
                                        value)
        };
        check_return(ret, self)
    }

    pub fn set_value_uint64<S>(&self, name: S, value: u64) -> Result<*const CGroupController>
        where S: Into<String>
    {
        let ret = unsafe {
            ffi::cgroup_set_value_uint64(self.controller,
                                         CString::new(name.into()).unwrap().as_ptr(),
                                         value)
        };
        check_return(ret, self)
    }

    pub fn set_value_bool<S>(&self, name: S, value: bool) -> Result<*const CGroupController>
        where S: Into<String>
    {
        let ret = unsafe {
            ffi::cgroup_set_value_bool(self.controller,
                                       CString::new(name.into()).unwrap().as_ptr(),
                                       value)
        };
        check_return(ret, self)
    }
}