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

« back to all changes in this revision

Viewing changes to src/tools/cgexec.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 <errno.h>
 
17
#include <grp.h>
 
18
#include <libcgroup.h>
 
19
#include <limits.h>
 
20
#include <pwd.h>
 
21
#include <search.h>
 
22
#include <stdio.h>
 
23
#include <stdlib.h>
 
24
#include <string.h>
 
25
#include <unistd.h>
 
26
#include <getopt.h>
 
27
#include <sys/mount.h>
 
28
#include <sys/stat.h>
 
29
#include <sys/types.h>
 
30
 
 
31
#include "tools-common.h"
 
32
 
 
33
static struct option longopts[] = {
 
34
        {"sticky", no_argument, NULL, 's'}, 
 
35
        {0, 0, 0, 0}
 
36
};
 
37
 
 
38
int main(int argc, char *argv[])
 
39
{
 
40
        int ret = 0, i;
 
41
        int cg_specified = 0;
 
42
        int flag_child = 0;
 
43
        uid_t uid;
 
44
        gid_t gid;
 
45
        pid_t pid;
 
46
        char c;
 
47
        struct cgroup_group_spec *cgroup_list[CG_HIER_MAX];
 
48
 
 
49
        if (argc < 2) {
 
50
                fprintf(stderr, "Usage is %s"
 
51
                        " [-g <list of controllers>:<relative path to cgroup>]"
 
52
                        " [--sticky] command [arguments]  \n",
 
53
                        argv[0]);
 
54
                exit(2);
 
55
        }
 
56
 
 
57
        memset(cgroup_list, 0, sizeof(cgroup_list));
 
58
 
 
59
        while ((c = getopt_long(argc, argv, "+g:s", longopts, NULL)) > 0) {
 
60
                switch (c) {
 
61
                case 'g':
 
62
                        if (parse_cgroup_spec(cgroup_list, optarg)) {
 
63
                                fprintf(stderr, "cgroup controller and path"
 
64
                                                "parsing failed\n");
 
65
                                return -1;
 
66
                        }
 
67
                        cg_specified = 1;
 
68
                        break;
 
69
                case 's':
 
70
                        flag_child |= CGROUP_DAEMON_UNCHANGE_CHILDREN;
 
71
                        break;
 
72
                default:
 
73
                        fprintf(stderr, "Invalid command line option\n");
 
74
                        exit(1);
 
75
                        break;
 
76
                }
 
77
        }
 
78
 
 
79
        /* Executable name */
 
80
        if (!argv[optind]) {
 
81
                fprintf(stderr, "No command specified\n");
 
82
                exit(1);
 
83
        }
 
84
 
 
85
        /* Initialize libcg */
 
86
        ret = cgroup_init();
 
87
        if (ret) {
 
88
                fprintf(stderr, "libcgroup initialization failed: %s\n",
 
89
                        cgroup_strerror(ret));
 
90
                return ret;
 
91
        }
 
92
 
 
93
        uid = getuid();
 
94
        gid = getgid();
 
95
        pid = getpid();
 
96
 
 
97
        ret = cgroup_register_unchanged_process(pid, flag_child);
 
98
        if (ret) {
 
99
                fprintf(stderr, "registration of process failed\n");
 
100
                return ret;
 
101
        }
 
102
 
 
103
        /*
 
104
         * 'cgexec' command file needs the root privilege for executing
 
105
         * a cgroup_register_unchanged_process() by using unix domain
 
106
         * socket, and an euid should be changed to the executing user
 
107
         * from a root user.
 
108
         */
 
109
        if (seteuid(uid)) {
 
110
                fprintf(stderr, "%s", strerror(errno));
 
111
                return -1;
 
112
        }
 
113
        if (cg_specified) {
 
114
                /*
 
115
                 * User has specified the list of control group and
 
116
                 * controllers
 
117
                 * */
 
118
                for (i = 0; i < CG_HIER_MAX; i++) {
 
119
                        if (!cgroup_list[i])
 
120
                                break;
 
121
 
 
122
                        ret = cgroup_change_cgroup_path(cgroup_list[i]->path,
 
123
                                                        pid,
 
124
                                                cgroup_list[i]->controllers);
 
125
                        if (ret) {
 
126
                                fprintf(stderr,
 
127
                                        "cgroup change of group failed\n");
 
128
                                return ret;
 
129
                        }
 
130
                }
 
131
        } else {
 
132
 
 
133
                /* Change the cgroup by determining the rules based on uid */
 
134
                ret = cgroup_change_cgroup_flags(uid, gid,
 
135
                                                 argv[optind], pid, 0);
 
136
                if (ret) {
 
137
                        fprintf(stderr, "cgroup change of group failed\n");
 
138
                        return ret;
 
139
                }
 
140
        }
 
141
 
 
142
        /* Now exec the new process */
 
143
        ret = execvp(argv[optind], &argv[optind]);
 
144
        if (ret == -1) {
 
145
                fprintf(stderr, "%s", strerror(errno));
 
146
                return -1;
 
147
        }
 
148
        return 0;
 
149
}