clsync
Loading...
Searching...
No Matches
cgroup.c
Go to the documentation of this file.
1/*
2 clsync - file tree sync utility based on inotify/kqueue/bsm
3
4 Copyright (C) 2014 Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
5
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "common.h"
21#include "error.h"
22#include <libcgroup.h>
23
24static struct cgroup *cgroup = NULL;
25
27{
28 debug ( 2, "cgroup_name == \"%s\"", ctx_p->cg_groupname );
29 SAFE ( cgroup_init(), return -1; );
30 SAFE ( ( cgroup = cgroup_new_cgroup ( ctx_p->cg_groupname ) ) == NULL, return -1; );
31 return 0;
32}
33
35{
36 int rc;
37 char *allowed_devices[] = CG_ALLOWED_DEVICES, **allowed_device_i;
38 /*
39 * Unfortunately, libcgroup doesn't allow multiple values for one key, and cgroups doesn't allow multiple devices for one set. So I was been have to write this hack. It adds character '/' to start of "devices.allow" for every new entry. So libclsync thinks that it's different keys, "/sys/fs/cgroup/devices/clsync/123/devices.allow" == "/sys/fs/cgroup/devices/clsync/123//devices.allow".
40 */
41 char control_name_buf[BUFSIZ + BUFSIZ] = {[0 ... BUFSIZ - 1] = '/', 'd', 'e', 'v', 'i', 'c', 'e', 's', '.', 'a', 'l', 'l', 'o', 'w'}, *control_name = &control_name_buf[BUFSIZ];
42 debug ( 2, "" );
43 struct cgroup_controller *cgc;
44 SAFE ( ( cgc = cgroup_add_controller ( cgroup, "devices" ) ) == NULL, return -1; );
45 debug ( 8, "Deny device: \"a\"" );
46 SAFE ( cgroup_add_value_string ( cgc, "devices.deny", "a" ), return -1; );
47 allowed_device_i = allowed_devices;
48
49 while ( *allowed_device_i != NULL ) {
50 critical_on ( control_name < control_name_buf );
51 debug ( 8, "Allow device: \"%s\" (\"%s\" = \"%s\")", *allowed_device_i, control_name, *allowed_device_i );
52 SAFE ( cgroup_add_value_string ( cgc, control_name, *allowed_device_i ), return -1; );
53 control_name--;
54 allowed_device_i++;
55 }
56
57 if ( ( rc = cgroup_create_cgroup ( cgroup, 1 ) ) ) {
58 error ( "Got error while cgroup_create_cgroup(): %s", cgroup_strerror ( rc ) );
59 return -1;
60 }
61
62 return 0;
63}
64
66{
67 int rc;
68 debug ( 2, "" );
69
70 if ( ( rc = cgroup_attach_task_pid ( cgroup, ctx_p->pid ) ) ) {
71 error ( "Got error while cgroup_attach_task_pid(): %s", cgroup_strerror ( rc ) );
72 return -1;
73 }
74
75 return 0;
76}
77
79{
80 ( void ) ctx_p;
81 debug ( 2, "" );
82 error_on ( cgroup_delete_cgroup_ext ( cgroup, CGFLAG_DELETE_IGNORE_MIGRATION | CGFLAG_DELETE_RECURSIVE ) );
83 cgroup_free ( &cgroup );
84 debug ( 15, "end" );
85 return 0;
86}
87
int clsync_cgroup_attach(ctx_t *ctx_p)
Definition cgroup.c:65
int clsync_cgroup_init(ctx_t *ctx_p)
Definition cgroup.c:26
__extension__ int clsync_cgroup_forbid_extra_devices()
Definition cgroup.c:34
static struct cgroup * cgroup
Definition cgroup.c:24
int clsync_cgroup_deinit(ctx_t *ctx_p)
Definition cgroup.c:78
#define CG_ALLOWED_DEVICES
#define BUFSIZ
#define error(...)
Definition error.h:36
#define debug(debug_level,...)
Definition error.h:50
#define critical_on(cond)
Definition error.h:33
#define error_on(cond)
Definition error.h:37
#define SAFE(code, onfail)
Definition macros.h:56
ctx_t * ctx_p
Definition mon_kqueue.c:85
Definition ctx.h:315
pid_t pid
Definition ctx.h:318