summaryrefslogtreecommitdiff
path: root/src/cgroups.cpp
blob: eafa1e3cb8ab3044e67fc16a10a410dd095d5f81 (plain)
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
#include <config.h>

#include <glib.h>
#include <glib/gi18n.h>

#include "procman.h"
#include "util.h"

gboolean
cgroups_enabled(void)
{
    static gboolean initialized = FALSE;
    static gboolean has_cgroups;

    if (!initialized) {
        initialized = TRUE;
        has_cgroups = g_file_test("/proc/cgroups", G_FILE_TEST_EXISTS);
    }

    return has_cgroups;
}

void
append_cgroup_name(char *line, gchar **current_cgroup_name)
{
    gchar *controller, *path, *tmp, *path_plus_space;
    int paren_offset, off, tmp_size;

    controller = g_strstr_len(line, -1, ":") + 1;
    if (!controller)
        return;

    path = g_strstr_len(controller, -1, ":") + 1;
    if (!path)
        return;

    *(path - 1) = '\0';
    g_strdelimit(controller, ",", '/');

    if ((std::strcmp(path, "/") == 0) || (std::strncmp(controller, "name=", 5) == 0))
        return;

    if (*current_cgroup_name == NULL) {
        *current_cgroup_name = g_strdup_printf("%s (%s)", path, controller);
        return;
    }

    /* add a space to the end of the path string */
    path_plus_space = g_strdup_printf("%s ", path);

    if ((tmp = g_strstr_len(*current_cgroup_name, -1, path_plus_space))) {
        tmp_size = strlen(*current_cgroup_name) + strlen(controller) + 1;
        paren_offset = g_strstr_len(tmp + strlen(path), -1, ")") - *current_cgroup_name;
        *(*current_cgroup_name + paren_offset) = '\0';
        tmp = (gchar *)g_strnfill(tmp_size, '\0');
        off = g_strlcat(tmp, *current_cgroup_name, tmp_size);
        *(tmp + off) = '/';
        off++;
        off += g_strlcat(tmp + off, controller, tmp_size);
        *(tmp + off) = ')';
        off++;
        g_strlcat(tmp + off, *current_cgroup_name + paren_offset + 1, tmp_size);
    } else
        tmp = g_strdup_printf("%s, %s(%s)", *current_cgroup_name, path_plus_space, controller);

    g_free(path_plus_space);
    g_free(*current_cgroup_name);
    *current_cgroup_name = tmp;
}

int
check_cgroup_changed(gchar *line, gchar *current_cgroup_set)
{
    /* check if line is contained in current_cgroup_set */
    gchar *controller, *path, *tmp, *found, *close_paren, *open_paren;
    int ret = 0;

    controller = g_strstr_len(line, -1, ":") + 1;
    if (!controller)
        return 1;

    path = g_strstr_len(controller, -1, ":") + 1;
    if (!path)
        return 1;

    *(path - 1) = '\0';

    if (std::strncmp(controller, "name=", 5) == 0)
        goto out;

    /* if there are multiple controllers just report string has changed */
    if (g_strstr_len(controller, -1, ",")) {
        ret = 1;
        goto out;
    }

    if (!current_cgroup_set) {
        if (std::strcmp(path, "/") != 0)
            ret = 1;
        goto out;
    }

    /* special case for root cgroup */
    tmp = current_cgroup_set;
    if (std::strcmp(path, "/") == 0) {
        while ((found = g_strstr_len(tmp, -1, controller))) {
            close_paren = g_strstr_len(found, -1, ")");
            open_paren = g_strstr_len(found, -1, "(");
            if (close_paren) {
                if (!open_paren || (close_paren < open_paren)) {
                    ret = 1;
                    goto out;
                }
            }
            tmp = found + strlen(controller);
        }
        goto out;
    }

    tmp = current_cgroup_set;
    while ((found = g_strstr_len(tmp, -1, path))) {
            found = found + strlen(path);
            close_paren = g_strstr_len(found, -1, ")");
            if (*found == ' ') {
                if (g_strstr_len(found + 1, close_paren - found, controller))
                    goto out;
            }
            tmp = close_paren + 1;
    }
    ret = 1;
out:
    *(path - 1) = ':';
    return ret;
}

void
get_process_cgroup_info(ProcInfo *info)
{
    gchar *path;
    gchar *cgroup_name = NULL;
    int cgroups_changed = 0;
    gchar *text;
    char **lines;
    int i;

    if (!cgroups_enabled())
        return;

    /* read out of /proc/pid/cgroup */
    path = g_strdup_printf("/proc/%d/cgroup", info->pid);
    if (!path)
        return;
    if(!g_file_get_contents(path, &text, NULL, NULL))
        goto out;
    lines = g_strsplit(text, "\n", -1);
    g_free(text);
    if (!lines)
        goto out;

    for (i = 0; lines[i] != NULL; i++) {
        if (lines[i][0] == '\0')
            continue;
        if (check_cgroup_changed(lines[i], info->cgroup_name)) {
            cgroups_changed = 1;
            break;
        }
    }

    if (cgroups_changed) {
        for (i = 0; lines[i] != NULL; i++) {
            if (lines[i][0] == '\0')
                continue;
            append_cgroup_name(lines[i], &cgroup_name);
        }
        if (info->cgroup_name)
            g_free(info->cgroup_name);
        if (!cgroup_name)
            info->cgroup_name = g_strdup("");
        else
            info->cgroup_name = cgroup_name;
    }

    g_strfreev(lines);
out:
    g_free(path);
}