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

« back to all changes in this revision

Viewing changes to src/tools/cgclassify.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 RedHat Inc. 2008
 
3
 *
 
4
 * Authors:     Vivek Goyal <vgoyal@redhat.com>
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or modify it
 
7
 * under the terms of version 2.1 of the GNU Lesser General Public License
 
8
 * as published by the Free Software Foundation.
 
9
 *
 
10
 * This program is distributed in the hope that it would be useful, but
 
11
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
13
 *
 
14
 */
 
15
 
 
16
#include <stdio.h>
 
17
#include <stdlib.h>
 
18
#include <string.h>
 
19
#include <errno.h>
 
20
#include <libcgroup.h>
 
21
#include <libcgroup-internal.h>
 
22
#include <limits.h>
 
23
#include <pwd.h>
 
24
#include <unistd.h>
 
25
#include <getopt.h>
 
26
#include <sys/mount.h>
 
27
#include <sys/stat.h>
 
28
#include <sys/types.h>
 
29
 
 
30
#include "tools-common.h"
 
31
 
 
32
#define TEMP_BUF        81
 
33
 
 
34
/*
 
35
 * Change process group as specified on command line.
 
36
 */
 
37
int change_group_path(pid_t pid, struct cgroup_group_spec *cgroup_list[])
 
38
{
 
39
        int i;
 
40
        int ret = 0;
 
41
 
 
42
        for (i = 0; i < CG_HIER_MAX; i++) {
 
43
                if (!cgroup_list[i])
 
44
                        break;
 
45
 
 
46
                ret = cgroup_change_cgroup_path(cgroup_list[i]->path, pid,
 
47
                        cgroup_list[i]->controllers);
 
48
                if (ret)
 
49
                        fprintf(stderr, "Error changing group of pid %d: %s\n",
 
50
                                pid, cgroup_strerror(ret));
 
51
                        return -1;
 
52
        }
 
53
 
 
54
        return 0;
 
55
}
 
56
 
 
57
/*
 
58
 * Change process group as specified in cgrules.conf.
 
59
 */
 
60
int change_group_based_on_rule(pid_t pid)
 
61
{
 
62
        uid_t euid;
 
63
        gid_t egid;
 
64
        char *procname = NULL;
 
65
        int ret = -1;
 
66
 
 
67
        /* Put pid into right cgroup as per rules in /etc/cgrules.conf */
 
68
        if (cgroup_get_uid_gid_from_procfs(pid, &euid, &egid)) {
 
69
                fprintf(stderr, "Error in determining euid/egid of"
 
70
                " pid %d\n", pid);
 
71
                goto out;
 
72
        }
 
73
        ret = cgroup_get_procname_from_procfs(pid, &procname);
 
74
        if (ret) {
 
75
                fprintf(stderr, "Error in determining process name of"
 
76
                " pid %d\n", pid);
 
77
                goto out;
 
78
        }
 
79
 
 
80
        /* Change the cgroup by determining the rules */
 
81
        ret = cgroup_change_cgroup_flags(euid, egid, procname, pid, 0);
 
82
        if (ret) {
 
83
                fprintf(stderr, "Error: change of cgroup failed for"
 
84
                " pid %d: %s\n", pid, cgroup_strerror(ret));
 
85
                goto out;
 
86
        }
 
87
        ret = 0;
 
88
out:
 
89
        if (procname)
 
90
                free(procname);
 
91
        return ret;
 
92
}
 
93
 
 
94
static struct option longopts[] = {
 
95
        {"sticky", no_argument, NULL, 's'}, 
 
96
        {0, 0, 0, 0}
 
97
};
 
98
 
 
99
int main(int argc, char *argv[])
 
100
{
 
101
        int ret = 0, i, exit_code = 0;
 
102
        pid_t pid;
 
103
        int cg_specified = 0;
 
104
        int flag_child = 0;
 
105
        struct cgroup_group_spec *cgroup_list[CG_HIER_MAX];
 
106
        int c;
 
107
 
 
108
 
 
109
        if (argc < 2) {
 
110
                fprintf(stderr, "usage is %s "
 
111
                        "[-g <list of controllers>:<relative path to cgroup>] "
 
112
                        "[--sticky] <list of pids>  \n",
 
113
                        argv[0]);
 
114
                exit(2);
 
115
        }
 
116
 
 
117
        memset(cgroup_list, 0, sizeof(cgroup_list));
 
118
        while ((c = getopt_long(argc, argv, "+g:s", longopts, NULL)) > 0) {
 
119
                switch (c) {
 
120
                case 'g':
 
121
                        if (parse_cgroup_spec(cgroup_list, optarg)) {
 
122
                                fprintf(stderr, "cgroup controller and path"
 
123
                                                "parsing failed\n");
 
124
                                return -1;
 
125
                        }
 
126
                        cg_specified = 1;
 
127
                        break;
 
128
                case 's':
 
129
                        flag_child |= CGROUP_DAEMON_UNCHANGE_CHILDREN;
 
130
                        break;
 
131
                default:
 
132
                        fprintf(stderr, "Invalid command line option\n");
 
133
                        exit(2);
 
134
                        break;
 
135
                }
 
136
        }
 
137
 
 
138
 
 
139
        /* Initialize libcg */
 
140
        ret = cgroup_init();
 
141
        if (ret) {
 
142
                fprintf(stderr, "libcgroup initialization failed:%d\n", ret);
 
143
                return ret;
 
144
        }
 
145
 
 
146
        for (i = optind; i < argc; i++) {
 
147
                pid = (uid_t) atoi(argv[i]);
 
148
 
 
149
                if (flag_child)
 
150
                        ret = cgroup_register_unchanged_process(pid, flag_child);
 
151
                if (ret)
 
152
                        exit_code = 1;
 
153
 
 
154
                if (cg_specified)
 
155
                        ret = change_group_path(pid, cgroup_list);
 
156
                else
 
157
                        ret = change_group_based_on_rule(pid);
 
158
 
 
159
                /* if any group change fails */
 
160
                if (ret)
 
161
                        exit_code = 1;
 
162
        }
 
163
        return exit_code;
 
164
 
 
165
}