-
-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathcgroup.c
209 lines (150 loc) · 5.37 KB
/
cgroup.c
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
/*
* SNOOPY COMMAND LOGGER
*
* Copyright (c) 2022 Bostjan Skufca Jese <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/*
* Includes order: from local to global
*/
#include "cgroup.h"
#include "snoopy.h"
#include "util/file-snoopy.h"
#include "util/string-snoopy.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
/*
* Local defines
*/
#define PROC_PID_CGROUP_PATH_SIZE_MAX 32
static int doesCgroupEntryContainController (char const * const cgroupEntry, char const * const controllerName);
int snoopy_datasource_cgroup (char * const resultBuf, size_t resultBufSize, char const * const arg)
{
int myPid;
char procPidCgroupFilePath[PROC_PID_CGROUP_PATH_SIZE_MAX];
char * procPidCgroupContent = NULL;
char * cgroupEntry = NULL;
int retMsgLen;
// Verify the argument
if (0 == strcmp(arg, "")) {
snprintf(resultBuf, resultBufSize, "Missing cgroup selection argument");
return SNOOPY_DATASOURCE_FAILURE;
}
// Generate /proc/PID/cgroup path
myPid = getpid();
snprintf(procPidCgroupFilePath, PROC_PID_CGROUP_PATH_SIZE_MAX, "/proc/%d/cgroup", myPid);
// Get the cgroup info content
if (snoopy_util_file_getSmallTextFileContent(procPidCgroupFilePath, &procPidCgroupContent) < 0) {
snprintf(resultBuf, resultBufSize, "Unable to read file %s, reason: %s", procPidCgroupFilePath, procPidCgroupContent);
free(procPidCgroupContent);
return SNOOPY_DATASOURCE_FAILURE;
}
// Search
if (SNOOPY_TRUE == snoopy_util_string_containsOnlyDigits(arg)) {
// Search mode: ^MYNUMBER:.*
size_t searchStringLen;
char * searchString = NULL;
searchStringLen = strlen(arg) + 2; // +1 for the ':' and +1 for the '\0'
searchString = malloc(searchStringLen);
snprintf(searchString, searchStringLen, "%s:", arg);
cgroupEntry = snoopy_util_string_findLineStartingWith(procPidCgroupContent, searchString);
free(searchString);
if (cgroupEntry) {
snoopy_util_string_nullTerminateLine(cgroupEntry);
}
} else {
// Search mode: ^[^:]+:MYTEXT:
// This search mangles the procPidCgroupContent, but it leaves a null-terminated
// cgroupEntry ready for subsequent use.
char * nextEntry = NULL;
cgroupEntry = strtok_r(procPidCgroupContent, "\n", &nextEntry);
while (cgroupEntry) {
if (SNOOPY_TRUE == doesCgroupEntryContainController(cgroupEntry, arg)) {
break;
}
cgroupEntry = strtok_r(NULL, "\n", &nextEntry);
}
}
// Not found?
if (NULL == cgroupEntry) {
free(procPidCgroupContent);
return snprintf(resultBuf, resultBufSize, "%s", "(none)");
}
// Found
retMsgLen = snprintf(resultBuf, resultBufSize, "%s", cgroupEntry);
free(procPidCgroupContent);
return retMsgLen;
}
static int doesCgroupEntryContainController (char const * const cgroupEntry, char const * const controllerName)
{
const char * firstColon;
const char * controllerList;
char * secondColon;
// Find relevant colon characters to isolate the controller list
firstColon = strchr(cgroupEntry, ':');
if (!firstColon) {
return SNOOPY_ERROR;
}
controllerList = firstColon+1;
secondColon = strchr(controllerList, ':');
if (!secondColon) {
return SNOOPY_ERROR;
}
// If controller list is empty, we can't match then
if (controllerList == secondColon) {
return SNOOPY_FALSE;
}
// Change second colon to \0, to enable subsequent search
*secondColon = '\0';
// Check whole literal match
if (0 == strcmp(controllerList, controllerName)) {
*secondColon = ':';
return SNOOPY_TRUE;
}
// Is there even a ',' character in controller list - if not, cut the execution short
if (!strchr(controllerList, ',')) {
*secondColon = ':';
return SNOOPY_FALSE;
}
// Parse the comma-separated list of controllers
const char * tokenPtr = controllerList;
while (tokenPtr) {
char * commaPtr = strchr(tokenPtr, ',');
if (commaPtr) {
*commaPtr = '\0';
}
// Token matches
if (0 == strcmp(tokenPtr, controllerName)) {
if (commaPtr) *commaPtr = ',';
*secondColon = ':';
return SNOOPY_TRUE;
}
// Last iteration
if (commaPtr) {
// Not found, still stuff to search, so search on
*commaPtr = ',';
tokenPtr = commaPtr + 1;
} else {
// Last iteration
tokenPtr = NULL;
break;
}
}
*secondColon = ':';
return SNOOPY_FALSE;
}