~serge-hallyn/ubuntu/natty/libcgroup/upstart

« back to all changes in this revision

Viewing changes to src/tools/cgsnapshot.c

  • Committer: Bazaar Package Importer
  • Author(s): Jon Bernard
  • Date: 2010-12-15 11:20:42 UTC
  • mfrom: (1.1.12 sid)
  • Revision ID: james.westby@ubuntu.com-20101215112042-ww4izbh92r8e05d2
Tags: 0.37-1
* [59d325] New upstream version 0.37
* [bb3923] Set source format to 3.0 (quilt)
* [084091] Bump Standards-Version to 3.9.1
* [3504c3] Update watchfile to look for bzip2 files
* [41151b] Convert patches to git-buldpackage's patch queue format
  (Closes: #604635)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* " Copyright (C) 2010 Red Hat, Inc. All Rights Reserved.
 
2
 * " Written by Ivana Hutarova Varekova <varekova@redhat.com>
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify it
 
5
 * under the terms of version 2.1 of the GNU Lesser General Public License
 
6
 * as published by the Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it would be useful, but
 
9
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
11
 */
 
12
 
 
13
#include <stdio.h>
 
14
#include <unistd.h>
 
15
#include <stdlib.h>
 
16
#include <string.h>
 
17
#include <getopt.h>
 
18
#include <errno.h>
 
19
 
 
20
#include <sys/types.h>
 
21
#include <sys/stat.h>
 
22
#include <libcgroup.h>
 
23
#include <libcgroup-internal.h>
 
24
#include <pwd.h>
 
25
#include <grp.h>
 
26
 
 
27
enum flag{
 
28
    FL_LIST =           1,
 
29
    FL_SILENT =         2,  /* do-not show any warning/error output */
 
30
    FL_STRICT =         4,  /* don show the variables which are not on
 
31
                            whitelist */
 
32
    FL_OUTPUT =         8,  /* output should be redirect to the given file */
 
33
    FL_BLACK =          16, /* blacklist set */
 
34
    FL_WHITE =          32, /* whitelist set */
 
35
};
 
36
 
 
37
#define BLACKLIST_CONF          "/etc/cgsnapshot_blacklist.conf"
 
38
#define WHITELIST_CONF          "/etc/cgsnapshot_whitelist.conf"
 
39
 
 
40
struct black_list_type {
 
41
        char *name;                     /* variable name */
 
42
        struct black_list_type *next;   /* pointer to the next record */
 
43
};
 
44
 
 
45
struct black_list_type *black_list;
 
46
struct black_list_type *white_list;
 
47
 
 
48
typedef char cont_name_t[FILENAME_MAX];
 
49
 
 
50
int flags;
 
51
FILE *of;
 
52
 
 
53
/*
 
54
 * Display the usage
 
55
 */
 
56
static void usage(int status, const char *program_name)
 
57
{
 
58
        if (status != 0) {
 
59
                fprintf(stderr, "Wrong input parameters,"
 
60
                        " try %s -h' for more information.\n",
 
61
                        program_name);
 
62
        } else {
 
63
                fprintf(stdout, "Usage: %s [-h] [-s] [-b file] "\
 
64
                        "[-w file] [-f output_file] "\
 
65
                        "[controller] [...]\n", program_name);
 
66
                fprintf(stdout, "Generate the configuration file from "\
 
67
                        "the given controllers of control groups\n");
 
68
                fprintf(stdout, "  -b,--blacklist file  Set the blacklist"\
 
69
                        " configuration file (default %s)\n", BLACKLIST_CONF);
 
70
                fprintf(stdout, "  -f,--file            Redirect the output"\
 
71
                        " to output_file\n");
 
72
                fprintf(stdout, "  -h,--help            Display this help\n");
 
73
                fprintf(stdout, "  -s,--silent          Ignore all "\
 
74
                        "warnings\n");
 
75
                fprintf(stdout, "  -t,--strict          Don't show the "\
 
76
                        "variables which are not on the whitelist\n");
 
77
                fprintf(stdout, "  -w,--whitelist file  Set the whitelist"\
 
78
                        " configuration file (don't used by default)\n");
 
79
        }
 
80
}
 
81
 
 
82
/* cache values from blacklist file to the list structure */
 
83
 
 
84
int load_list(char *filename, struct black_list_type **p_list)
 
85
{
 
86
        FILE *fw;
 
87
        int i = 0;
 
88
        int ret;
 
89
        char buf[FILENAME_MAX];
 
90
        char name[FILENAME_MAX];
 
91
 
 
92
        struct black_list_type *start = NULL;
 
93
        struct black_list_type *end = NULL;
 
94
        struct black_list_type *new;
 
95
 
 
96
        fw = fopen(filename, "r");
 
97
        if (fw == NULL) {
 
98
                fprintf(stderr, "ERROR: Failed to open file %s: %s\n",
 
99
                        filename, strerror(errno));
 
100
                *p_list = NULL;
 
101
                return 1;
 
102
        }
 
103
 
 
104
        /* go through the all configuration file and search the line */
 
105
        while (fgets(buf, FILENAME_MAX, fw) != NULL) {
 
106
                buf[FILENAME_MAX-1] = '\0';
 
107
                i = 0;
 
108
 
 
109
                /* if the record start with # then skip it */
 
110
                while ((buf[i] == ' ') || (buf[i] == '\n'))
 
111
                        i++;
 
112
 
 
113
                if ((buf[i] == '#') || (buf[i] == '\0'))
 
114
                        continue;
 
115
 
 
116
                new = (struct black_list_type *)malloc(sizeof
 
117
                        (struct black_list_type));
 
118
                if (new == NULL) {
 
119
                        fprintf(stderr, "ERROR: Memory allocation problem "
 
120
                                "(%s)\n", strerror(errno));
 
121
                        *p_list = NULL;
 
122
                        return 1;
 
123
                }
 
124
 
 
125
                ret = sscanf(buf, "%s", name);
 
126
                new->name = strdup(name);
 
127
 
 
128
                new->next = NULL;
 
129
 
 
130
                /* update the variables list */
 
131
                if (start == NULL) {
 
132
                        start = new;
 
133
                        end = new;
 
134
                } else {
 
135
                        end->next = new;
 
136
                        end = new;
 
137
                }
 
138
        }
 
139
 
 
140
        fclose(fw);
 
141
        *p_list = start;
 
142
        return 0;
 
143
}
 
144
 
 
145
/* free list structure */
 
146
void free_list(struct black_list_type *list)
 
147
{
 
148
        struct black_list_type *now;
 
149
        struct black_list_type *next;
 
150
 
 
151
        now = list;
 
152
        while (now != NULL) {
 
153
                next = now->next;
 
154
                free(now->name);
 
155
                free(now);
 
156
                now = next;
 
157
        }
 
158
        return;
 
159
}
 
160
 
 
161
/* Test whether the variable is on the list
 
162
 * return values are:
 
163
 * 1 ... was found
 
164
 * 0 ... no record was found
 
165
 */
 
166
int is_on_list(char *name, struct black_list_type *list)
 
167
{
 
168
        struct black_list_type *record;
 
169
 
 
170
        record = list;
 
171
        /* go through the list of all values */
 
172
        while (record != NULL) {
 
173
                /* if the variable name is found */
 
174
                if (strcmp(record->name, name) == 0) {
 
175
                        /* return its value */
 
176
                        return 1;
 
177
                }
 
178
                record = record->next;
 
179
        }
 
180
 
 
181
        /* the variable was not found */
 
182
        return 0;
 
183
 
 
184
}
 
185
 
 
186
/* Display permissions record for the given group
 
187
 * defined by path
 
188
 */
 
189
static int display_permissions(const char *path,
 
190
                const char *program_name)
 
191
{
 
192
        int ret;
 
193
        struct stat sba;
 
194
        struct stat sbt;
 
195
        struct passwd *pw;
 
196
        struct group *gr;
 
197
        char tasks_path[FILENAME_MAX];
 
198
 
 
199
        /* admin permissions record */
 
200
        /* get the directory statistic */
 
201
        ret = stat(path, &sba);
 
202
        if (ret) {
 
203
                fprintf(stderr, "ERROR: can't read statistics about %s\n",
 
204
                        path);
 
205
                return -1;
 
206
        }
 
207
 
 
208
        /* tasks permissions record */
 
209
        /* get tasks file statistic */
 
210
        strncpy(tasks_path, path, FILENAME_MAX);
 
211
        tasks_path[FILENAME_MAX-1] = '\0';
 
212
        strncat(tasks_path, "/tasks", FILENAME_MAX);
 
213
        tasks_path[FILENAME_MAX-1] = '\0';
 
214
        ret = stat(tasks_path, &sbt);
 
215
        if (ret) {
 
216
                fprintf(stderr, "ERROR: can't read statistics about %s\n",
 
217
                        tasks_path);
 
218
                return -1;
 
219
        }
 
220
 
 
221
        if ((sba.st_uid) || (sba.st_gid) ||
 
222
                (sbt.st_uid) || (sbt.st_gid)) {
 
223
                /* some uid or gid is nonroot, admin permission
 
224
                   tag is necessery */
 
225
 
 
226
                /* print the header */
 
227
                fprintf(of, "\tperm {\n");
 
228
 
 
229
                /* find out the user and group name */
 
230
                pw = getpwuid(sba.st_uid);
 
231
                if (pw == NULL) {
 
232
                        fprintf(stderr, "ERROR: can't get %d user name\n",
 
233
                                sba.st_uid);
 
234
                        return -1;
 
235
                }
 
236
 
 
237
                gr = getgrgid(sba.st_gid);
 
238
                if (gr == NULL) {
 
239
                        fprintf(stderr, "ERROR: can't get %d group name\n",
 
240
                                sba.st_gid);
 
241
                        return -1;
 
242
                }
 
243
 
 
244
                /* print the admin record */
 
245
                fprintf(of, "\t\tadmin {\n"\
 
246
                        "\t\t\tuid = %s;\n"\
 
247
                        "\t\t\tgid = %s;\n"\
 
248
                        "\t\t}\n", pw->pw_name, gr->gr_name);
 
249
 
 
250
                /* find out the user and group name */
 
251
                pw = getpwuid(sbt.st_uid);
 
252
                if (pw == NULL) {
 
253
                        fprintf(stderr, "ERROR: can't get %d user name\n",
 
254
                                sbt.st_uid);
 
255
                        return -1;
 
256
                }
 
257
 
 
258
                gr = getgrgid(sbt.st_gid);
 
259
                if (gr == NULL) {
 
260
                        fprintf(stderr, "ERROR: can't get %d group name\n",
 
261
                                sbt.st_gid);
 
262
                        return -1;
 
263
                }
 
264
 
 
265
                /* print the task record */
 
266
                fprintf(of, "\t\ttask {\n"\
 
267
                        "\t\t\tuid = %s;\n"\
 
268
                        "\t\t\tgid = %s;\n"\
 
269
                        "\t\t}\n", pw->pw_name, gr->gr_name);
 
270
 
 
271
                fprintf(of, "\t}\n");
 
272
        }
 
273
 
 
274
        return 0;
 
275
}
 
276
 
 
277
/* Displey the control group record:
 
278
 * header
 
279
 *   permission record
 
280
 *   controllers records
 
281
 * tail
 
282
 */
 
283
 
 
284
static int display_cgroup_data(struct cgroup *group,
 
285
                char controller[CG_CONTROLLER_MAX][FILENAME_MAX],
 
286
                const char *group_path, int root_path_len, int first,
 
287
                const char *program_name)
 
288
{
 
289
        int i = 0, j;
 
290
        int bl, wl = 0; /* is on the blacklist/whitelist flag */
 
291
        int nr_var = 0;
 
292
        char *name;
 
293
        char *output_name;
 
294
        struct cgroup_controller *group_controller = NULL;
 
295
        char *value = NULL;
 
296
        char var_path[FILENAME_MAX];
 
297
        int ret = 0;
 
298
        struct stat sb;
 
299
 
 
300
        /* print the  group definition header */
 
301
        fprintf(of, "group %s {\n", group->name);
 
302
 
 
303
        /* display the permission tags */
 
304
        ret = display_permissions(group_path, program_name);
 
305
        if (ret)
 
306
                return ret;
 
307
 
 
308
        /* for all wanted controllers display controllers tag */
 
309
        while (controller[i][0] != '\0') {
 
310
 
 
311
                group_controller = cgroup_get_controller(group, controller[i]);
 
312
                if (group_controller == NULL) {
 
313
                        printf("cannot find controller "\
 
314
                                "'%s' in group '%s'\n",
 
315
                                controller[i], group->name);
 
316
                        i++;
 
317
                        ret = -1;
 
318
                        continue;
 
319
                }
 
320
 
 
321
                /* print the controller header */
 
322
                fprintf(of, "\t%s {\n", controller[i]);
 
323
                i++;
 
324
                nr_var = cgroup_get_value_name_count(group_controller);
 
325
 
 
326
                for (j = 0; j < nr_var; j++) {
 
327
                        name = cgroup_get_value_name(group_controller, j);
 
328
 
 
329
                        /* For the non-root groups cgconfigparser set
 
330
                           permissions of variable files to 777. Thus
 
331
                           It is necessary to test the permissions of
 
332
                           variable files in the root group to find out
 
333
                           whether the variable is writable.
 
334
                         */
 
335
                        strncpy(var_path, group_path, root_path_len);
 
336
                        var_path[root_path_len] = '\0';
 
337
                        strncat(var_path, "/", FILENAME_MAX);
 
338
                        var_path[FILENAME_MAX-1] = '\0';
 
339
                        strncat(var_path, name, FILENAME_MAX);
 
340
                        var_path[FILENAME_MAX-1] = '\0';
 
341
 
 
342
                        /* test whether the  write permissions */
 
343
                        ret = stat(var_path, &sb);
 
344
                        /* freezer.state is not in root group so ret != 0,
 
345
                         * but it should be listed
 
346
                         * device.list should be read to create
 
347
                         * device.allow input
 
348
                         */
 
349
                        if ((ret == 0) && ((sb.st_mode & S_IWUSR) == 0) &&
 
350
                            (strcmp("devices.list", name) != 0)) {
 
351
                                /* variable is not writable */
 
352
                                continue;
 
353
                        }
 
354
 
 
355
                        /* find whether the variable is blacklisted or
 
356
                           whitelisted */
 
357
                        bl = is_on_list(name, black_list);
 
358
                        wl = is_on_list(name, white_list);
 
359
 
 
360
                        /* if it is blacklisted skip it and continue */
 
361
                        if (bl)
 
362
                                continue;
 
363
 
 
364
                        /* if it is not whitelisted and strict tag is used
 
365
                           skip it and continue too */
 
366
                        if ((!wl) && (flags &  FL_STRICT))
 
367
                                continue;
 
368
 
 
369
                        /* if it is not whitelisted and silent tag is not
 
370
                           used write an warning */
 
371
                        if ((!wl) && !(flags &  FL_SILENT) && (first))
 
372
                                fprintf(stderr, "WARNING: variable %s is "\
 
373
                                        "neither blacklisted nor "\
 
374
                                        "whitelisted\n", name);
 
375
 
 
376
                        output_name = name;
 
377
 
 
378
                        /* deal with devices variables:
 
379
                         * - omit devices.deny and device.allow,
 
380
                         * - generate devices.{deny,allow} from
 
381
                         * device.list variable (deny all and then
 
382
                         * all device.list devices */
 
383
                        if ((strcmp("devices.deny", name) == 0) ||
 
384
                                (strcmp("devices.allow", name) == 0)) {
 
385
                                continue;
 
386
                        }
 
387
                        if (strcmp("devices.list", name) == 0) {
 
388
                                output_name = "devices.allow";
 
389
                                fprintf(of,
 
390
                                        "\t\tdevices.deny=\"a *:* rwm\";\n");
 
391
                        }
 
392
 
 
393
                        ret = cgroup_get_value_string(group_controller,
 
394
                                name, &value);
 
395
 
 
396
                        /* variable can not be read */
 
397
                        if (ret != 0) {
 
398
                                ret = 0;
 
399
                                fprintf(stderr, "ERROR: Value of "\
 
400
                                        "variable %s can be read\n",
 
401
                                        name);
 
402
                                goto err;
 
403
                        }
 
404
                        fprintf(of, "\t\t%s=\"%s\";\n", output_name, value);
 
405
                }
 
406
                fprintf(of, "\t}\n");
 
407
        }
 
408
 
 
409
        /* tail of the record */
 
410
        fprintf(of, "}\n\n");
 
411
 
 
412
err:
 
413
        return ret;
 
414
}
 
415
 
 
416
/*
 
417
 * creates the record about the hierarchie which contains
 
418
 * "controller" subsystem
 
419
 */
 
420
static int display_controller_data(
 
421
                char controller[CG_CONTROLLER_MAX][FILENAME_MAX],
 
422
                const char *program_name)
 
423
{
 
424
        int ret;
 
425
        void *handle;
 
426
        int first = 1;
 
427
 
 
428
        struct cgroup_file_info info;
 
429
        int lvl;
 
430
 
 
431
        int prefix_len;
 
432
        char cgroup_name[FILENAME_MAX];
 
433
 
 
434
        struct cgroup *group = NULL;
 
435
 
 
436
        /* start to parse the structure for the first controller -
 
437
           controller[0] attached to hierarchie */
 
438
        ret = cgroup_walk_tree_begin(controller[0], "/", 0,
 
439
                &handle, &info, &lvl);
 
440
        if (ret != 0)
 
441
                return ret;
 
442
 
 
443
        prefix_len = strlen(info.full_path);
 
444
 
 
445
        /* go through all files and directories */
 
446
        while ((ret = cgroup_walk_tree_next(0, &handle, &info, lvl)) == 0) {
 
447
                /* some group starts here */
 
448
                if (info.type == CGROUP_FILE_TYPE_DIR) {
 
449
                        /* parse the group name from full_path*/
 
450
                        strncpy(cgroup_name, &info.full_path[prefix_len],
 
451
                                FILENAME_MAX);
 
452
                        cgroup_name[FILENAME_MAX-1] = '\0';
 
453
 
 
454
 
 
455
                        /* start to grab data about the new group */
 
456
                        group = cgroup_new_cgroup(cgroup_name);
 
457
                        if (group == NULL) {
 
458
                                printf("cannot create group '%s'\n",
 
459
                                        cgroup_name);
 
460
                                return -1;
 
461
                        }
 
462
 
 
463
                        ret = cgroup_get_cgroup(group);
 
464
                        if (ret != 0) {
 
465
                                printf("cannot read group '%s': %s\n",
 
466
                                cgroup_name, cgroup_strerror(ret));
 
467
                                goto err;
 
468
                        }
 
469
 
 
470
                        display_cgroup_data(group, controller, info.full_path,
 
471
                                prefix_len, first, program_name);
 
472
                        first = 0;
 
473
                        cgroup_free(&group);
 
474
                }
 
475
        }
 
476
 
 
477
err:
 
478
        cgroup_walk_tree_end(&handle);
 
479
        if (ret == ECGEOF)
 
480
                ret = 0;
 
481
 
 
482
        return ret;
 
483
 
 
484
}
 
485
 
 
486
static int is_ctlr_on_list(char controllers[CG_CONTROLLER_MAX][FILENAME_MAX],
 
487
                        cont_name_t wanted_conts[FILENAME_MAX])
 
488
{
 
489
        int i = 0;
 
490
        int j = 0;
 
491
 
 
492
        while (controllers[i][0] != '\0') {
 
493
                while (wanted_conts[j][0] != '\0') {
 
494
                        if (strcmp(controllers[i], wanted_conts[j]) == 0)
 
495
                                return 1;
 
496
                        j++;
 
497
                }
 
498
                j = 0;
 
499
                i++;
 
500
        }
 
501
 
 
502
        return 0;
 
503
}
 
504
 
 
505
 
 
506
/* print data about input cont_name controller */
 
507
static int parse_controllers(cont_name_t cont_names[CG_CONTROLLER_MAX],
 
508
        const char *program_name)
 
509
{
 
510
        int ret = 0;
 
511
        void *handle;
 
512
        char path[FILENAME_MAX];
 
513
        struct cgroup_mount_point controller;
 
514
 
 
515
        char controllers[CG_CONTROLLER_MAX][FILENAME_MAX];
 
516
        int max = 0;
 
517
 
 
518
        path[0] = '\0';
 
519
 
 
520
        ret = cgroup_get_controller_begin(&handle, &controller);
 
521
 
 
522
        /* go through the list of controllers/mount point pairs */
 
523
        while (ret == 0) {
 
524
                if (strcmp(path, controller.path) == 0) {
 
525
                        /* if it is still the same mount point */
 
526
                        if (max < CG_CONTROLLER_MAX) {
 
527
                                strncpy(controllers[max],
 
528
                                        controller.name, FILENAME_MAX);
 
529
                                (controllers[max])[FILENAME_MAX-1] = '\0';
 
530
                                max++;
 
531
                        }
 
532
                } else {
 
533
 
 
534
                        /* we got new mount point, print it if needed */
 
535
                        if ((!(flags & FL_LIST) ||
 
536
                                (is_ctlr_on_list(controllers, cont_names)))
 
537
                                && (max != 0)) {
 
538
                                (controllers[max])[0] = '\0';
 
539
                                ret = display_controller_data(
 
540
                                        controllers, program_name);
 
541
                        }
 
542
 
 
543
                        strncpy(controllers[0], controller.name, FILENAME_MAX);
 
544
                        (controllers[0])[FILENAME_MAX-1] = '\0';
 
545
 
 
546
                        strncpy(path, controller.path, FILENAME_MAX);
 
547
                        path[FILENAME_MAX-1] = '\0';
 
548
                        max = 1;
 
549
                }
 
550
 
 
551
                /* the actual controller should not be printed */
 
552
                ret = cgroup_get_controller_next(&handle, &controller);
 
553
        }
 
554
 
 
555
        if (max != 0) {
 
556
                (controllers[max])[0] = '\0';
 
557
                ret = display_controller_data(
 
558
                        controllers, program_name);
 
559
        }
 
560
 
 
561
        cgroup_get_controller_end(&handle);
 
562
        if (ret != ECGEOF)
 
563
                return ret;
 
564
        return 0;
 
565
}
 
566
 
 
567
/* print data about input mount points */
 
568
/* TODO only wanted ones */
 
569
static int parse_mountpoints(cont_name_t cont_names[CG_CONTROLLER_MAX],
 
570
        const char *program_name)
 
571
{
 
572
        int ret;
 
573
        void *handle;
 
574
        struct controller_data info;
 
575
        char *mount_point;
 
576
 
 
577
        /* start mount section */
 
578
        fprintf(of, "mount {\n");
 
579
 
 
580
        /* go through the controller list */
 
581
        ret = cgroup_get_all_controller_begin(&handle, &info);
 
582
        while (ret == 0) {
 
583
 
 
584
                /* the controller attached to some hierarchy */
 
585
                if  (info.hierarchy != 0) {
 
586
                        ret = cgroup_get_subsys_mount_point(info.name,
 
587
                                &mount_point);
 
588
                        if (ret != 0) {
 
589
                                /* the controller is not mounted */
 
590
                                if ((flags &  FL_SILENT) == 0) {
 
591
                                        fprintf(stderr, "ERROR: %s hierarchy "\
 
592
                                                "not mounted\n", info.name);
 
593
                                }
 
594
                        } else
 
595
                                fprintf(of, "\t%s = %s;\n",
 
596
                                        info.name, mount_point);
 
597
                }
 
598
 
 
599
                /* next controller */
 
600
                ret = cgroup_get_all_controller_next(&handle, &info);
 
601
        }
 
602
        if (ret != ECGEOF) {
 
603
                if ((flags &  FL_SILENT) != 0) {
 
604
                        fprintf(stderr,
 
605
                                "E: in get next controller %s\n",
 
606
                                cgroup_strerror(ret));
 
607
                        return ret;
 
608
                }
 
609
        }
 
610
 
 
611
        ret |= cgroup_get_all_controller_end(&handle);
 
612
 
 
613
        /* finish mount section */
 
614
        fprintf(of, "}\n\n");
 
615
        return ret;
 
616
}
 
617
 
 
618
int main(int argc, char *argv[])
 
619
{
 
620
        int ret = 0;
 
621
        int c;
 
622
 
 
623
        int i;
 
624
        int c_number = 0;
 
625
        cont_name_t wanted_cont[CG_CONTROLLER_MAX];
 
626
 
 
627
        char bl_file[FILENAME_MAX];  /* blacklist file name */
 
628
        char wl_file[FILENAME_MAX];  /* whitelist file name */
 
629
 
 
630
        static struct option long_opts[] = {
 
631
                {"help", no_argument, NULL, 'h'},
 
632
                {"silent" , no_argument, NULL, 's'},
 
633
                {"blacklist", required_argument, NULL, 'b'},
 
634
                {"whitelist", required_argument, NULL, 'w'},
 
635
                {"strict", no_argument, NULL, 't'},
 
636
                {"file", required_argument, NULL, 'f'},
 
637
                {0, 0, 0, 0}
 
638
        };
 
639
 
 
640
        for (i = 0; i < CG_CONTROLLER_MAX; i++)
 
641
                wanted_cont[i][0] = '\0';
 
642
        flags = 0;
 
643
 
 
644
        /* parse arguments */
 
645
        while ((c = getopt_long(argc, argv, "hsb:w:tf:", long_opts, NULL))
 
646
                > 0) {
 
647
                switch (c) {
 
648
                case 'h':
 
649
                        usage(0, argv[0]);
 
650
                        return 0;
 
651
                case 's':
 
652
                        flags |= FL_SILENT;
 
653
                        break;
 
654
                case 'b':
 
655
                        flags |= FL_BLACK;
 
656
                        strncpy(bl_file, optarg, FILENAME_MAX);
 
657
                        break;
 
658
                case 'w':
 
659
                        flags |= FL_WHITE;
 
660
                        strncpy(wl_file, optarg, FILENAME_MAX);
 
661
                        break;
 
662
                case 't':
 
663
                        flags |= FL_STRICT;
 
664
                        break;
 
665
                case 'f':
 
666
                        flags |= FL_OUTPUT;
 
667
                        of = fopen(optarg, "w");
 
668
                        if (of == NULL) {
 
669
                                fprintf(stderr, "%s: Failed to open file %s\n",
 
670
                                        argv[0], optarg);
 
671
                                return ECGOTHER;
 
672
                        }
 
673
                        break;
 
674
                default:
 
675
                        usage(1, argv[0]);
 
676
                        return -1;
 
677
                }
 
678
        }
 
679
 
 
680
        /* read the list of controllers */
 
681
        while (optind < argc) {
 
682
                flags |= FL_LIST;
 
683
                strncpy(wanted_cont[c_number], argv[optind], FILENAME_MAX);
 
684
                (wanted_cont[c_number])[FILENAME_MAX-1] = '\0';
 
685
                c_number++;
 
686
                optind++;
 
687
                if (optind == CG_CONTROLLER_MAX-1) {
 
688
                        fprintf(stderr, "too many parameters\n");
 
689
                        break;
 
690
                }
 
691
        }
 
692
 
 
693
        if ((flags & FL_OUTPUT) == 0)
 
694
                of = stdout;
 
695
 
 
696
        /* blacklkist */
 
697
        if (flags & FL_BLACK) {
 
698
                ret  = load_list(bl_file, &black_list);
 
699
        } else {
 
700
                /* load the blacklist from the default location */
 
701
                ret  = load_list(BLACKLIST_CONF, &black_list);
 
702
        }
 
703
        if (ret != 0)
 
704
                return ret;
 
705
 
 
706
        /* whitelist */
 
707
        if (flags & FL_WHITE)
 
708
                ret = load_list(wl_file, &white_list);
 
709
        if (ret != 0)
 
710
                return ret;
 
711
 
 
712
        /* print the header */
 
713
        fprintf(of, "# Configuration file generated by cgsnapshot\n");
 
714
 
 
715
        /* initialize libcgroup */
 
716
        ret = cgroup_init();
 
717
 
 
718
        if (ret) {
 
719
                /* empty configuration file */
 
720
                return ret;
 
721
        }
 
722
 
 
723
        /* print mount points section */
 
724
        ret = parse_mountpoints(wanted_cont, argv[0]);
 
725
 
 
726
        /* print hierarchies section */
 
727
        ret = parse_controllers(wanted_cont, argv[0]);
 
728
 
 
729
        free_list(black_list);
 
730
        free_list(white_list);
 
731
 
 
732
        if (of != stdout)
 
733
                fclose(of);
 
734
 
 
735
        return ret;
 
736
}