~ubuntu-branches/ubuntu/maverick/libcgroup/maverick-proposed

« back to all changes in this revision

Viewing changes to src/config.c

  • Committer: Bazaar Package Importer
  • Author(s): Dustin Kirkland
  • Date: 2009-08-26 11:29:17 UTC
  • Revision ID: james.westby@ubuntu.com-20090826112917-402ews2uj6v350d2
Tags: upstream-0.34
ImportĀ upstreamĀ versionĀ 0.34

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright IBM Corporation. 2007
 
3
 *
 
4
 * Authors:     Balbir Singh <balbir@linux.vnet.ibm.com>
 
5
 *              Dhaval Giani <dhaval@linux.vnet.ibm.com>
 
6
 *
 
7
 * This program is free software; you can redistribute it and/or modify it
 
8
 * under the terms of version 2.1 of the GNU Lesser General Public License
 
9
 * as published by the Free Software Foundation.
 
10
 *
 
11
 * This program is distributed in the hope that it would be useful, but
 
12
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
14
 *
 
15
 * TODOs:
 
16
 *      1. Implement our own hashing scheme
 
17
 *      2. Add namespace support
 
18
 *      3. Add support for parsing cgroup filesystem and creating a
 
19
 *         config out of it.
 
20
 *
 
21
 * Code initiated and designed by Balbir Singh. All faults are most likely
 
22
 * his mistake.
 
23
 *
 
24
 * Cleanup and changes to use the "official" structures and functions made
 
25
 * by Dhaval Giani. All faults will still be Balbir's mistake :)
 
26
 */
 
27
 
 
28
#include <assert.h>
 
29
#include <dirent.h>
 
30
#include <errno.h>
 
31
#include <grp.h>
 
32
#include <libcgroup.h>
 
33
#include <libcgroup-internal.h>
 
34
#include <limits.h>
 
35
#include <pwd.h>
 
36
#include <pthread.h>
 
37
#include <search.h>
 
38
#include <stdio.h>
 
39
#include <stdlib.h>
 
40
#include <string.h>
 
41
#include <unistd.h>
 
42
 
 
43
#include <sys/mount.h>
 
44
#include <sys/stat.h>
 
45
#include <sys/types.h>
 
46
 
 
47
#define MAX_CGROUPS 1024
 
48
 
 
49
extern FILE *yyin;
 
50
extern int yyparse(void);
 
51
 
 
52
/*
 
53
 * The basic global data structures.
 
54
 *
 
55
 * config_mount_table -> Where what controller is mounted
 
56
 * table_index -> Where in the table are we.
 
57
 * config_table_lock -> Serializing access to config_mount_table.
 
58
 * cgroup_table -> Which cgroups have to be created.
 
59
 * cgroup_table_index -> Where in the cgroup_table we are.
 
60
 */
 
61
static struct cg_mount_table_s config_mount_table[CG_CONTROLLER_MAX];
 
62
static int config_table_index;
 
63
static pthread_rwlock_t config_table_lock = PTHREAD_RWLOCK_INITIALIZER;
 
64
static struct cgroup config_cgroup_table[MAX_CGROUPS];
 
65
int cgroup_table_index;
 
66
 
 
67
/*
 
68
 * Needed for the type while mounting cgroupfs.
 
69
 */
 
70
static const char cgroup_filesystem[] = "cgroup";
 
71
 
 
72
/*
 
73
 * NOTE: All these functions return 1 on success
 
74
 * and not 0 as is the library convention
 
75
 */
 
76
 
 
77
/*
 
78
 * This call just sets the name of the cgroup. It will
 
79
 * always be called in the end, because the parser will
 
80
 * work bottom up.
 
81
 */
 
82
int cgroup_config_insert_cgroup(char *cg_name)
 
83
{
 
84
        struct cgroup *config_cgroup =
 
85
                        &config_cgroup_table[cgroup_table_index];
 
86
 
 
87
        strncpy(config_cgroup->name, cg_name, FILENAME_MAX);
 
88
        /*
 
89
         * Since this will be the last part to be parsed.
 
90
         */
 
91
        cgroup_table_index++;
 
92
        free(cg_name);
 
93
        return 1;
 
94
}
 
95
 
 
96
/*
 
97
 * This function sets the various controller's control
 
98
 * files. It will always append values for cgroup_table_index
 
99
 * entry in the cgroup_table. The index is incremented in
 
100
 * cgroup_config_insert_cgroup
 
101
 */
 
102
int cgroup_config_parse_controller_options(char *controller, char *name_value)
 
103
{
 
104
        char *buffer = NULL;
 
105
        char *name, *value;
 
106
        struct cgroup_controller *cgc;
 
107
        int error;
 
108
        struct cgroup *config_cgroup =
 
109
                &config_cgroup_table[cgroup_table_index];
 
110
        char *nm_pairs, *nv_buf;
 
111
 
 
112
        cgroup_dbg("Adding controller %s, value %s\n", controller, name_value);
 
113
        cgc = cgroup_add_controller(config_cgroup, controller);
 
114
 
 
115
        if (!cgc)
 
116
                goto parse_error;
 
117
 
 
118
        /*
 
119
         * Did we just specify the controller to create the correct
 
120
         * set of directories, without setting any values?
 
121
         */
 
122
        if (!name_value)
 
123
                goto done;
 
124
 
 
125
        nm_pairs = strtok_r(name_value, ":", &nv_buf);
 
126
        cgroup_dbg("[1] name value pair being processed is %s\n", nm_pairs);
 
127
 
 
128
        name = strtok_r(nm_pairs, " ", &buffer);
 
129
 
 
130
        if (!name)
 
131
                goto parse_error;
 
132
 
 
133
        value = strtok_r(NULL, " ", &buffer);
 
134
 
 
135
        if (!value)
 
136
                goto parse_error;
 
137
 
 
138
        cgroup_dbg("name is %s, value is %s\n", name, value);
 
139
        error = cgroup_add_value_string(cgc, name, value);
 
140
 
 
141
        if (error)
 
142
                goto parse_error;
 
143
 
 
144
        while ((nm_pairs = strtok_r(NULL, ":", &nv_buf))) {
 
145
                cgroup_dbg("[2] name value pair being processed is %s\n",
 
146
                        nm_pairs);
 
147
                name = strtok_r(nm_pairs, " ", &buffer);
 
148
 
 
149
                if (!name)
 
150
                        goto parse_error;
 
151
 
 
152
                value = strtok_r(NULL, " ", &buffer);
 
153
 
 
154
                if (!value)
 
155
                        goto parse_error;
 
156
 
 
157
                cgroup_dbg("name is %s, value is %s\n", name, value);
 
158
                error = cgroup_add_value_string(cgc, name, value);
 
159
 
 
160
                if (error)
 
161
                        goto parse_error;
 
162
        }
 
163
 
 
164
done:
 
165
        free(controller);
 
166
        free(name_value);
 
167
        return 1;
 
168
 
 
169
parse_error:
 
170
        free(controller);
 
171
        free(name_value);
 
172
        cgroup_delete_cgroup(config_cgroup, 1);
 
173
        cgroup_table_index--;
 
174
        return 0;
 
175
}
 
176
 
 
177
/*
 
178
 * Sets the tasks file's uid and gid
 
179
 */
 
180
int cgroup_config_group_task_perm(char *perm_type, char *value)
 
181
{
 
182
        struct passwd *pw, *pw_buffer;
 
183
        struct group *group, *group_buffer;
 
184
        int error;
 
185
        long val = atoi(value);
 
186
        char buffer[CGROUP_BUFFER_LEN];
 
187
        struct cgroup *config_cgroup =
 
188
                        &config_cgroup_table[cgroup_table_index];
 
189
 
 
190
        if (!strcmp(perm_type, "uid")) {
 
191
                if (!val) {
 
192
                        pw = (struct passwd *) malloc(sizeof(struct passwd));
 
193
 
 
194
                        if (!pw)
 
195
                                goto group_task_error;
 
196
 
 
197
                        error = getpwnam_r(value, pw, buffer, CGROUP_BUFFER_LEN,
 
198
                                                                &pw_buffer);
 
199
                        if (pw_buffer == NULL) {
 
200
                                free(pw);
 
201
                                goto group_task_error;
 
202
                        }
 
203
 
 
204
                        val = pw->pw_uid;
 
205
                        free(pw);
 
206
                }
 
207
                config_cgroup->tasks_uid = val;
 
208
        }
 
209
 
 
210
        if (!strcmp(perm_type, "gid")) {
 
211
                if (!val) {
 
212
                        group = (struct group *) malloc(sizeof(struct group));
 
213
 
 
214
                        if (!group)
 
215
                                goto group_task_error;
 
216
 
 
217
                        error = getgrnam_r(value, group, buffer,
 
218
                                        CGROUP_BUFFER_LEN, &group_buffer);
 
219
 
 
220
                        if (group_buffer == NULL) {
 
221
                                free(group);
 
222
                                goto group_task_error;
 
223
                        }
 
224
 
 
225
                        val = group->gr_gid;
 
226
                        free(group);
 
227
                }
 
228
                config_cgroup->tasks_gid = val;
 
229
        }
 
230
 
 
231
        free(perm_type);
 
232
        free(value);
 
233
        return 1;
 
234
 
 
235
group_task_error:
 
236
        free(perm_type);
 
237
        free(value);
 
238
        cgroup_delete_cgroup(config_cgroup, 1);
 
239
        cgroup_table_index--;
 
240
        return 0;
 
241
}
 
242
 
 
243
/*
 
244
 * Set the control file's uid/gid
 
245
 */
 
246
int cgroup_config_group_admin_perm(char *perm_type, char *value)
 
247
{
 
248
        struct passwd *pw, *pw_buffer;
 
249
        struct group *group, *group_buffer;
 
250
        int error;
 
251
        struct cgroup *config_cgroup =
 
252
                                &config_cgroup_table[cgroup_table_index];
 
253
        long val = atoi(value);
 
254
        char buffer[CGROUP_BUFFER_LEN];
 
255
 
 
256
        if (!strcmp(perm_type, "uid")) {
 
257
                if (!val) {
 
258
                        pw = (struct passwd *) malloc(sizeof(struct passwd));
 
259
 
 
260
                        if (!pw)
 
261
                                goto admin_error;
 
262
 
 
263
                        error = getpwnam_r(value, pw, buffer, CGROUP_BUFFER_LEN,
 
264
                                                                &pw_buffer);
 
265
                        if (pw_buffer == NULL) {
 
266
                                free(pw);
 
267
                                goto admin_error;
 
268
                        }
 
269
 
 
270
                        val = pw->pw_uid;
 
271
                        free(pw);
 
272
                }
 
273
                config_cgroup->control_uid = val;
 
274
        }
 
275
 
 
276
        if (!strcmp(perm_type, "gid")) {
 
277
                if (!val) {
 
278
                        group = (struct group *) malloc(sizeof(struct group));
 
279
 
 
280
                        if (!group)
 
281
                                goto admin_error;
 
282
 
 
283
                        error = getgrnam_r(value, group, buffer,
 
284
                                        CGROUP_BUFFER_LEN, &group_buffer);
 
285
 
 
286
                        if (group_buffer == NULL) {
 
287
                                free(group);
 
288
                                goto admin_error;
 
289
                        }
 
290
 
 
291
                        val = group->gr_gid;
 
292
                        free(group);
 
293
                }
 
294
                config_cgroup->control_gid = val;
 
295
        }
 
296
 
 
297
        free(perm_type);
 
298
        free(value);
 
299
        return 1;
 
300
 
 
301
admin_error:
 
302
        free(perm_type);
 
303
        free(value);
 
304
        cgroup_delete_cgroup(config_cgroup, 1);
 
305
        cgroup_table_index--;
 
306
        return 0;
 
307
}
 
308
 
 
309
/*
 
310
 * The moment we have found the controller's information
 
311
 * insert it into the config_mount_table.
 
312
 */
 
313
int cgroup_config_insert_into_mount_table(char *name, char *mount_point)
 
314
{
 
315
        int i;
 
316
 
 
317
        if (config_table_index >= CG_CONTROLLER_MAX)
 
318
                return 0;
 
319
 
 
320
        pthread_rwlock_wrlock(&config_table_lock);
 
321
 
 
322
        /*
 
323
         * Merge controller names with the same mount point
 
324
         */
 
325
        for (i = 0; i < config_table_index; i++) {
 
326
                if (strcmp(config_mount_table[i].path, mount_point) == 0) {
 
327
                        char *cname = config_mount_table[i].name;
 
328
                        strncat(cname, ",", FILENAME_MAX - strlen(cname) - 1);
 
329
                        strncat(cname, name,
 
330
                                        FILENAME_MAX - strlen(cname) - 1);
 
331
                        goto done;
 
332
                }
 
333
        }
 
334
 
 
335
        strcpy(config_mount_table[config_table_index].name, name);
 
336
        strcpy(config_mount_table[config_table_index].path, mount_point);
 
337
        config_table_index++;
 
338
done:
 
339
        pthread_rwlock_unlock(&config_table_lock);
 
340
        free(name);
 
341
        free(mount_point);
 
342
        return 1;
 
343
}
 
344
 
 
345
/*
 
346
 * Cleanup all the data from the config_mount_table
 
347
 */
 
348
void cgroup_config_cleanup_mount_table(void)
 
349
{
 
350
        memset(&config_mount_table, 0,
 
351
                        sizeof(struct cg_mount_table_s) * CG_CONTROLLER_MAX);
 
352
}
 
353
 
 
354
/*
 
355
 * Start mounting the mount table.
 
356
 */
 
357
int cgroup_config_mount_fs()
 
358
{
 
359
        int ret;
 
360
        struct stat buff;
 
361
        int i;
 
362
 
 
363
        for (i = 0; i < config_table_index; i++) {
 
364
                struct cg_mount_table_s *curr = &(config_mount_table[i]);
 
365
 
 
366
                ret = stat(curr->path, &buff);
 
367
                
 
368
                if (ret < 0 && errno != ENOENT) {
 
369
                        last_errno = errno;
 
370
                        return ECGOTHER;
 
371
                }
 
372
 
 
373
                if (errno == ENOENT) {
 
374
                        ret = cg_mkdir_p(curr->path);
 
375
                        if (ret)
 
376
                                return ret;
 
377
                } else if (!S_ISDIR(buff.st_mode)) {
 
378
                        errno = ENOTDIR;
 
379
                        last_errno = errno;
 
380
                        return ECGOTHER;
 
381
                }
 
382
 
 
383
                ret = mount(cgroup_filesystem, curr->path, cgroup_filesystem,
 
384
                                                                0, curr->name);
 
385
 
 
386
                if (ret < 0)
 
387
                        return ECGMOUNTFAIL;
 
388
        }
 
389
        return 0;
 
390
}
 
391
 
 
392
/*
 
393
 * Actually create the groups once the parsing has been finished.
 
394
 */
 
395
int cgroup_config_create_groups()
 
396
{
 
397
        int error = 0;
 
398
        int i;
 
399
 
 
400
        for (i = 0; i < cgroup_table_index; i++) {
 
401
                struct cgroup *cgroup = &config_cgroup_table[i];
 
402
                error = cgroup_create_cgroup(cgroup, 0);
 
403
                cgroup_dbg("creating group %s, error %d\n", cgroup->name,
 
404
                        error);
 
405
                if (error)
 
406
                        return error;
 
407
        }
 
408
        return error;
 
409
}
 
410
 
 
411
/*
 
412
 * Destroy the cgroups
 
413
 */
 
414
int cgroup_config_destroy_groups(void)
 
415
{
 
416
        int error = 0;
 
417
        int i;
 
418
 
 
419
        for (i = 0; i < cgroup_table_index; i++) {
 
420
                struct cgroup *cgroup = &config_cgroup_table[i];
 
421
                error = cgroup_delete_cgroup(cgroup, 0);
 
422
                if (error)
 
423
                        return error;
 
424
        }
 
425
        return error;
 
426
}
 
427
 
 
428
/*
 
429
 * Unmount the controllers
 
430
 */
 
431
int cgroup_config_unmount_controllers(void)
 
432
{
 
433
        int i;
 
434
        int error;
 
435
 
 
436
        for (i = 0; i < config_table_index; i++) {
 
437
                /*
 
438
                 * We ignore failures and ensure that all mounted
 
439
                 * containers are unmounted
 
440
                 */
 
441
                error = umount(config_mount_table[i].path);
 
442
                if (error < 0)
 
443
                        cgroup_dbg("Unmount failed\n");
 
444
                error = rmdir(config_mount_table[i].path);
 
445
                if (error < 0)
 
446
                        cgroup_dbg("rmdir failed\n");
 
447
        }
 
448
 
 
449
        return 0;
 
450
}
 
451
 
 
452
/*
 
453
 * The main function which does all the setup of the data structures
 
454
 * and finally creates the cgroups
 
455
 */
 
456
int cgroup_config_load_config(const char *pathname)
 
457
{
 
458
        int error;
 
459
        yyin = fopen(pathname, "r");
 
460
 
 
461
        if (!yyin) {
 
462
                cgroup_dbg("Failed to open file %s\n", pathname);
 
463
                last_errno = errno;
 
464
                return ECGOTHER;
 
465
        }
 
466
 
 
467
        if (yyparse() != 0) {
 
468
                cgroup_dbg("Failed to parse file %s\n", pathname);
 
469
                fclose(yyin);
 
470
                return ECGROUPPARSEFAIL;
 
471
        }
 
472
 
 
473
        error = cgroup_config_mount_fs();
 
474
        if (error)
 
475
                goto err_mnt;
 
476
 
 
477
        error = cgroup_init();
 
478
        if (error)
 
479
                goto err_mnt;
 
480
 
 
481
        error = cgroup_config_create_groups();
 
482
        cgroup_dbg("creating all cgroups now, error=%d\n", error);
 
483
        if (error)
 
484
                goto err_grp;
 
485
 
 
486
        fclose(yyin);
 
487
        return 0;
 
488
err_grp:
 
489
        cgroup_config_destroy_groups();
 
490
err_mnt:
 
491
        cgroup_config_unmount_controllers();
 
492
        fclose(yyin);
 
493
        return error;
 
494
}
 
495
 
 
496
static int cgroup_cleanup_cgroup_controller_files(struct cgroup_file_info info,
 
497
                                                char *root_path, char *ctrl)
 
498
{
 
499
        void *task_handle;
 
500
        pid_t pid;
 
501
        char *rel_path = NULL;
 
502
        int error;
 
503
        int ret;
 
504
 
 
505
 
 
506
        rel_path = info.full_path + strlen(root_path) - 1;
 
507
 
 
508
        if (!strncmp(rel_path, "/", strlen(rel_path)))
 
509
                return 0;
 
510
 
 
511
        error = cgroup_get_task_begin(rel_path, ctrl,
 
512
                                        &task_handle, &pid);
 
513
 
 
514
        if (error && error != ECGEOF)
 
515
                return error;
 
516
 
 
517
        while (error != ECGEOF) {
 
518
                ret = cgroup_attach_task_pid(NULL, pid);
 
519
 
 
520
                if (ret) {
 
521
                        cgroup_get_task_end(&task_handle);
 
522
                        return ret;
 
523
                }
 
524
                error = cgroup_get_task_next(&task_handle, &pid);
 
525
 
 
526
                if (error && error != ECGEOF) {
 
527
                        cgroup_get_task_end(&task_handle);
 
528
                        return error;
 
529
                }
 
530
        }
 
531
 
 
532
        cgroup_get_task_end(&task_handle);
 
533
 
 
534
        error = rmdir(info.full_path);
 
535
        if (error) {
 
536
                last_errno = errno;
 
537
                return ECGOTHER;
 
538
        }
 
539
 
 
540
        return 0;
 
541
}
 
542
 
 
543
static int cgroup_config_unload_controller(struct cgroup_mount_point mount_info)
 
544
{
 
545
        struct cgroup_file_info info;
 
546
        void *tree_handle;
 
547
        int lvl;
 
548
        int ret = 0, error;
 
549
        char *root_path = NULL;
 
550
 
 
551
        error = cgroup_walk_tree_begin(mount_info.name, "/", 0, &tree_handle,
 
552
                                                        &info, &lvl);
 
553
 
 
554
        if (error && error != ECGEOF)
 
555
                return error;
 
556
 
 
557
        root_path = strdup(info.full_path);
 
558
 
 
559
        if (!root_path) {
 
560
                cgroup_walk_tree_end(&tree_handle);
 
561
                last_errno = errno;
 
562
                return ECGOTHER;
 
563
        }
 
564
 
 
565
        ret = cgroup_walk_tree_set_flags(&tree_handle,
 
566
                                CGROUP_WALK_TYPE_POST_DIR);
 
567
 
 
568
        if (ret) {
 
569
                cgroup_walk_tree_end(&tree_handle);
 
570
                goto out_error;
 
571
        }
 
572
 
 
573
        while (error != ECGEOF) {
 
574
                if (info.type == CGROUP_FILE_TYPE_DIR) {
 
575
                        ret = cgroup_cleanup_cgroup_controller_files(info,
 
576
                                                root_path, mount_info.name);
 
577
 
 
578
                        if (ret) {
 
579
                                cgroup_walk_tree_end(&tree_handle);
 
580
                                goto out_error;
 
581
                        }
 
582
                }
 
583
 
 
584
                error = cgroup_walk_tree_next(0, &tree_handle, &info, lvl);
 
585
 
 
586
                if (error && error != ECGEOF) {
 
587
                        ret = error;
 
588
                        cgroup_walk_tree_end(&tree_handle);
 
589
                        goto out_error;
 
590
                }
 
591
        }
 
592
        cgroup_walk_tree_end(&tree_handle);
 
593
        error = umount(mount_info.path);
 
594
 
 
595
        if (error) {
 
596
                last_errno = errno;
 
597
                ret = ECGOTHER;
 
598
                goto out_error;
 
599
        }
 
600
 
 
601
        error = rmdir(mount_info.path);
 
602
 
 
603
        if (error) {
 
604
                last_errno = errno;
 
605
                ret = ECGOTHER;
 
606
        }
 
607
 
 
608
out_error:
 
609
        free(root_path);
 
610
        return ret;
 
611
}
 
612
 
 
613
int cgroup_unload_cgroups(void)
 
614
{
 
615
        int error = 0;
 
616
        void *ctrl_handle;
 
617
        int ret = 0;
 
618
        char *curr_path = NULL;
 
619
        struct cgroup_mount_point info;
 
620
 
 
621
        error = cgroup_init();
 
622
 
 
623
        if (error) {
 
624
                ret = error;
 
625
                goto out_error;
 
626
        }
 
627
 
 
628
        error = cgroup_get_controller_begin(&ctrl_handle, &info);
 
629
 
 
630
 
 
631
        if (error && error != ECGEOF) {
 
632
                ret = error;
 
633
                goto out_error;
 
634
        }
 
635
 
 
636
        while (error != ECGEOF) {
 
637
                if (!curr_path || strcmp(info.path, curr_path) != 0) {
 
638
                        if (curr_path)
 
639
                                free(curr_path);
 
640
 
 
641
                        curr_path = strdup(info.path);
 
642
                        if (!curr_path)
 
643
                                goto out_errno;
 
644
 
 
645
                        ret = cgroup_config_unload_controller(info);
 
646
 
 
647
                        if (ret)
 
648
                                goto out_error;
 
649
                }
 
650
 
 
651
                error = cgroup_get_controller_next(&ctrl_handle, &info);
 
652
 
 
653
                if (error && error != ECGEOF) {
 
654
                        ret = error;
 
655
                        goto out_error;
 
656
                }
 
657
        }
 
658
 
 
659
out_error:
 
660
        if (curr_path)
 
661
                free(curr_path);
 
662
        cgroup_get_controller_end(&ctrl_handle);
 
663
        return ret;
 
664
 
 
665
out_errno:
 
666
        last_errno = errno;
 
667
        cgroup_get_controller_end(&ctrl_handle);
 
668
        return ECGOTHER;
 
669
}