2
* Copyright RedHat Inc. 2008
4
* Authors: Vivek Goyal <vgoyal@redhat.com>
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.
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.
20
#include <libcgroup.h>
21
#include <libcgroup-internal.h>
26
#include <sys/mount.h>
28
#include <sys/types.h>
30
#include "tools-common.h"
35
* Change process group as specified on command line.
37
int change_group_path(pid_t pid, struct cgroup_group_spec *cgroup_list[])
42
for (i = 0; i < CG_HIER_MAX; i++) {
46
ret = cgroup_change_cgroup_path(cgroup_list[i]->path, pid,
47
cgroup_list[i]->controllers);
49
fprintf(stderr, "Error changing group of pid %d: %s\n",
50
pid, cgroup_strerror(ret));
58
* Change process group as specified in cgrules.conf.
60
int change_group_based_on_rule(pid_t pid)
64
char *procname = NULL;
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"
73
ret = cgroup_get_procname_from_procfs(pid, &procname);
75
fprintf(stderr, "Error in determining process name of"
80
/* Change the cgroup by determining the rules */
81
ret = cgroup_change_cgroup_flags(euid, egid, procname, pid, 0);
83
fprintf(stderr, "Error: change of cgroup failed for"
84
" pid %d: %s\n", pid, cgroup_strerror(ret));
94
static struct option longopts[] = {
95
{"sticky", no_argument, NULL, 's'},
99
int main(int argc, char *argv[])
101
int ret = 0, i, exit_code = 0;
103
int cg_specified = 0;
105
struct cgroup_group_spec *cgroup_list[CG_HIER_MAX];
110
fprintf(stderr, "usage is %s "
111
"[-g <list of controllers>:<relative path to cgroup>] "
112
"[--sticky] <list of pids> \n",
117
memset(cgroup_list, 0, sizeof(cgroup_list));
118
while ((c = getopt_long(argc, argv, "+g:s", longopts, NULL)) > 0) {
121
if (parse_cgroup_spec(cgroup_list, optarg)) {
122
fprintf(stderr, "cgroup controller and path"
129
flag_child |= CGROUP_DAEMON_UNCHANGE_CHILDREN;
132
fprintf(stderr, "Invalid command line option\n");
139
/* Initialize libcg */
142
fprintf(stderr, "libcgroup initialization failed:%d\n", ret);
146
for (i = optind; i < argc; i++) {
147
pid = (uid_t) atoi(argv[i]);
150
ret = cgroup_register_unchanged_process(pid, flag_child);
155
ret = change_group_path(pid, cgroup_list);
157
ret = change_group_based_on_rule(pid);
159
/* if any group change fails */