• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

drakenclimber / libcgroup / 14455586901

14 Apr 2025 08:54PM UTC coverage: 56.146% (-1.0%) from 57.123%
14455586901

push

github

drakenclimber
ftests: Add a test for the memory abstraction layer

Add a test for the memory abstraction layer abstractions.
Currently only supports:
            memory.max <-> memory.limit_in_bytes
            memory.high <-> memory.soft_limit_in_bytes

-----------------------------------------------------------------
Test Results:
        Run Date:                          Apr 14 14:52:47
        Passed:                                  1 test(s)
        Skipped:                                 0 test(s)
        Failed:                                  0 test(s)
-----------------------------------------------------------------
Timing Results:
        Test                              Time (sec)
        --------------------------------------------
        setup                                   0.00
        093-cgxget-memory_settings.py           3.34
        teardown                                0.00
        --------------------------------------------
        Total Run Time                          3.34

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>

5600 of 9974 relevant lines covered (56.15%)

584.12 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

60.59
/src/tools/tools-common.c
1
// SPDX-License-Identifier: LGPL-2.1-only
2
/**
3
 * Copyright Red Hat, Inc. 2009
4
 *
5
 * Author:        Vivek Goyal <vgoyal@redhat.com>
6
 *                Jan Safranek <jsafrane@redhat.com>
7
 */
8

9
/* for asprintf */
10
#define _GNU_SOURCE
11

12
#include "tools-common.h"
13

14
#include <libcgroup.h>
15

16
#include <string.h>
17
#include <stdlib.h>
18
#include <dirent.h>
19
#include <errno.h>
20
#include <stdio.h>
21
#include <pwd.h>
22
#include <grp.h>
23

24
#include <sys/types.h>
25

26
int parse_cgroup_spec(struct cgroup_group_spec **cdptr, char *optarg, int capacity)
374✔
27
{
28
        char *cptr = NULL, *pathptr = NULL, *temp;
374✔
29
        struct cgroup_group_spec *ptr;
30
        int i, j;
31

32
        ptr = *cdptr;
374✔
33

34
        /* Find first free entry inside the cgroup data array */
35
        for (i = 0; i < capacity; i++, ptr++) {
374✔
36
                if (!cdptr[i])
374✔
37
                        break;
374✔
38
        }
39

40
        if (i == capacity) {
374✔
41
                /* No free slot found */
42
                fprintf(stderr, "Max allowed hierarchies %d reached\n", capacity);
×
43
                return -1;
×
44
        }
45

46
        /* Extract list of controllers */
47
        if (optarg[0] == ':') {
374✔
48
                /* No controller was passed in */
49
                cptr = NULL;
42✔
50
                pathptr = strtok(optarg, ":");
42✔
51
        } else {
52
                /* Extract the list of controllers from the user */
53
                cptr = strtok(optarg, ":");
332✔
54
                cgroup_dbg("list of controllers is %s\n", cptr);
332✔
55
                if (!cptr)
332✔
56
                        return -1;
×
57

58
                pathptr = strtok(NULL, ":");
332✔
59
        }
60

61
        cgroup_dbg("cgroup path is %s\n", pathptr);
374✔
62
        if (!pathptr)
374✔
63
                return -1;
×
64

65
        /* instantiate cgroup_data. */
66
        cdptr[i] = calloc(1, sizeof(struct cgroup_group_spec));
374✔
67
        if (!cdptr[i]) {
374✔
68
                fprintf(stderr, "%s\n", strerror(errno));
×
69
                return -1;
×
70
        }
71

72
        if (cptr != NULL) {
374✔
73
                /* Convert list of controllers into an array of strings. */
74
                j = 0;
332✔
75
                do {
76
                        if (j == 0)
738✔
77
                                temp = strtok(cptr, ",");
332✔
78
                        else
79
                                temp = strtok(NULL, ",");
406✔
80

81
                        if (temp) {
738✔
82
                                cdptr[i]->controllers[j] = strdup(temp);
406✔
83
                                if (!cdptr[i]->controllers[j]) {
406✔
84
                                        free(cdptr[i]);
×
85
                                        fprintf(stderr, "%s\n", strerror(errno));
×
86
                                        return -1;
×
87
                                }
88
                        }
89
                        j++;
738✔
90
                } while (temp && j < CG_CONTROLLER_MAX-1);
738✔
91
        }
92

93
        /* Store path to the cgroup */
94
        strncpy(cdptr[i]->path, pathptr, FILENAME_MAX - 1);
374✔
95
        cdptr[i]->path[sizeof(cdptr[i]->path) - 1] = '\0';
374✔
96

97
        return 0;
374✔
98
}
99

100

101
/**
102
 * Free a single cgroup_group_spec structure
103
 * <--->@param cl The structure to free from memory.
104
 */
105
void cgroup_free_group_spec(struct cgroup_group_spec *cl)
342✔
106
{
107
        /* Loop variable */
108
        int i = 0;
342✔
109

110
        /* Make sure our structure is not NULL, first. */
111
        if (!cl) {
342✔
112
                cgroup_dbg("Warning: Attempted to free NULL rule.\n");
×
113
                return;
×
114
        }
115

116
        /* We must free any used controller strings, too. */
117
        for (i = 0; i < CG_CONTROLLER_MAX; i++) {
34,542✔
118
                if (cl->controllers[i])
34,200✔
119
                        free(cl->controllers[i]);
378✔
120
        }
121

122
        free(cl);
342✔
123
}
124

125

126
int cgroup_string_list_init(struct cgroup_string_list *list, int initial_size)
37✔
127
{
128
        if (list == NULL)
37✔
129
                return ECGINVAL;
×
130

131
        list->items = calloc(initial_size, sizeof(char *));
37✔
132
        if (!list->items)
37✔
133
                return ECGFAIL;
×
134

135
        list->count = 0;
37✔
136
        list->size = initial_size;
37✔
137

138
        return 0;
37✔
139
}
140

141
void cgroup_string_list_free(struct cgroup_string_list *list)
35✔
142
{
143
        int i;
144

145
        if (list == NULL)
35✔
146
                return;
×
147

148
        if (list->items == NULL)
35✔
149
                return;
×
150

151
        for (i = 0; i < list->count; i++)
66✔
152
                free(list->items[i]);
31✔
153

154
        free(list->items);
35✔
155
}
156

157
int cgroup_string_list_add_item(struct cgroup_string_list *list, const char *item)
33✔
158
{
159
        if (list == NULL)
33✔
160
                return ECGINVAL;
×
161

162
        if (list->size <= list->count) {
33✔
163
                char **tmp = realloc(list->items, sizeof(char *) * list->size*2);
×
164

165
                if (tmp == NULL)
×
166
                        return ECGFAIL;
×
167
                list->items = tmp;
×
168
                list->size = list->size * 2;
×
169
        }
170

171
        list->items[list->count] = strdup(item);
33✔
172
        if (list->items[list->count] == NULL)
33✔
173
                return ECGFAIL;
×
174
        list->count++;
33✔
175

176
        return 0;
33✔
177
}
178

179
static int _compare_string(const void *a, const void *b)
×
180
{
181
        const char *sa = * (char * const *) a;
×
182
        const char *sb = * (char * const *) b;
×
183

184
        return strcmp(sa, sb);
×
185
}
186

187

188
int cgroup_string_list_add_directory(struct cgroup_string_list *list, char *dirname,
2✔
189
                                     char *program_name)
190
{
191
        int start, ret, count = 0;
2✔
192
        struct dirent *item;
193
        DIR *d;
194

195
        if (list == NULL)
2✔
196
                return ECGINVAL;
×
197

198
        start = list->count;
2✔
199

200
        d = opendir(dirname);
2✔
201
        if (!d) {
2✔
202
                fprintf(stderr, "%s: cannot open %s: %s\n", program_name, dirname,
×
203
                        strerror(errno));
×
204
                exit(1);
×
205
        }
206

207
        do {
208
                errno = 0;
6✔
209
                item = readdir(d);
6✔
210
                if (item) {
6✔
211
                        char *fullpath;
212
                        unsigned char dtype = item->d_type;
4✔
213

214
                        ret = asprintf(&fullpath, "%s/%s", dirname, item->d_name);
4✔
215
                        if (ret < 0) {
4✔
216
                                fprintf(stderr, "%s: out of memory\n", program_name);
×
217
                                exit(1);
×
218
                        }
219

220
                        if (dtype == DT_UNKNOWN) {
4✔
221
                                struct stat st;
222
                                int err = 0;
×
223

224
                                err = stat(fullpath, &st);
×
225
                                if (err != 0) {
×
226
                                        fprintf(stderr, "%s: cannot stat %s: %s\n",
×
227
                                                program_name, fullpath, strerror(errno));
×
228
                                        exit(1);
×
229
                                }
230

231
                                if (st.st_mode & S_IFREG)
×
232
                                        dtype = DT_REG;
×
233

234
                        }
235

236
                        if (dtype == DT_REG || dtype == DT_LNK) {
4✔
237
                                char *file_ext;
238

239
                                /* we are interested in .conf files, skip others */
240
                                file_ext = strstr(item->d_name, ".conf");
×
241
                                if (!file_ext) {
×
242
                                        free(fullpath);
×
243
                                        continue;
×
244
                                }
245

246
                                if (strcmp(file_ext, ".conf") || strlen(item->d_name) == 5) {
×
247
                                        free(fullpath);
×
248
                                        continue;
×
249
                                }
250

251
                                ret = cgroup_string_list_add_item(list, fullpath);
×
252
                                count++;
×
253
                                if (ret) {
×
254
                                        fprintf(stderr, "%s: %s\n", program_name,
×
255
                                                cgroup_strerror(ret));
256
                                        exit(1);
×
257
                                }
258
                        }
259

260
                        free(fullpath);
4✔
261
                }
262
                if (!item && errno) {
6✔
263
                        fprintf(stderr, "%s: cannot read %s: %s\n", program_name, dirname,
×
264
                                strerror(errno));
×
265
                        exit(1);
×
266
                }
267
        } while (item != NULL);
6✔
268
        closedir(d);
2✔
269

270
        /* sort the names found in the directory */
271
        if (count > 0)
2✔
272
                qsort(&list->items[start], count, sizeof(char *), _compare_string);
×
273

274
        return 0;
2✔
275
}
276

277
/* allowed mode strings are octal version: "755" */
278
int parse_mode(char *string, mode_t *pmode, const char *program_name)
8✔
279
{
280
        size_t len;
281
        char *end;
282
        long mode;
283

284
        len = strlen(string);
8✔
285
        if (len < 3 || len > 4)
8✔
286
                goto err;
×
287

288
        errno = 0;
8✔
289
        mode = strtol(string, &end, 8);
8✔
290
        if (errno != 0 || *end != '\0')
8✔
291
                goto err;
×
292
        *pmode = mode;
8✔
293
        return 0;
8✔
294

295
err:
×
296
        *pmode = 0;
×
297
        fprintf(stdout, "%s wrong mode format %s\n", program_name, string);
×
298

299
        return -1;
×
300
}
301

302
int parse_uid_gid(char *string, uid_t *uid, gid_t *gid, const char *program_name)
8✔
303
{
304
        char *grp_string = NULL;
8✔
305
        char *pwd_string = NULL;
8✔
306
        struct passwd *pwd;
307
        struct group *grp;
308

309
        *uid = *gid = NO_UID_GID;
8✔
310

311
        if (string[0] == ':')
8✔
312
                grp_string = strtok(string, ":");
×
313
        else {
314
                pwd_string = strtok(string, ":");
8✔
315
                if (pwd_string != NULL)
8✔
316
                        grp_string = strtok(NULL, ":");
8✔
317
        }
318

319
        if (pwd_string != NULL) {
8✔
320
                pwd = getpwnam(pwd_string);
8✔
321
                if (pwd != NULL) {
8✔
322
                        *uid = pwd->pw_uid;
8✔
323
                } else {
324
                        fprintf(stderr, "%s: can't find uid of user %s.\n", program_name,
×
325
                                pwd_string);
326
                        return -1;
×
327
                }
328
        }
329
        if (grp_string != NULL) {
8✔
330
                grp = getgrnam(grp_string);
8✔
331
                if (grp != NULL) {
8✔
332
                        *gid = grp->gr_gid;
8✔
333
                } else {
334
                        fprintf(stderr, "%s: can't find gid of group %s.\n", program_name,
×
335
                                grp_string);
336
                        return -1;
×
337
                }
338
        }
339

340
        return 0;
8✔
341
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc