summaryrefslogtreecommitdiffstats
path: root/src/tools
diff options
context:
space:
mode:
authorJan Safranek <jsafrane@redhat.com>2009-03-13 15:16:19 +0100
committerJan Safranek <jsafrane@redhat.com>2009-03-26 09:34:18 +0100
commitf8e05fc8c129a13fed256b03a23537ef94c77152 (patch)
treec64ea7d9f7daeefd307feec1bcb90ea5e3e6d600 /src/tools
parent04bb98f8bd9751dd8a514b0e3a6c4862ceabeae9 (diff)
downloadlibcg-f8e05fc8c129a13fed256b03a23537ef94c77152.tar.gz
libcg-f8e05fc8c129a13fed256b03a23537ef94c77152.tar.xz
libcg-f8e05fc8c129a13fed256b03a23537ef94c77152.zip
Distribute files to various subdirectories
Signed-off-by: Jan Safranek <jsafrane@redhat.com>
Diffstat (limited to 'src/tools')
-rw-r--r--src/tools/cgclassify.c219
-rw-r--r--src/tools/cgconfig.c74
-rw-r--r--src/tools/cgexec.c122
-rw-r--r--src/tools/tools-common.c88
-rw-r--r--src/tools/tools-common.h54
5 files changed, 557 insertions, 0 deletions
diff --git a/src/tools/cgclassify.c b/src/tools/cgclassify.c
new file mode 100644
index 0000000..c044608
--- /dev/null
+++ b/src/tools/cgclassify.c
@@ -0,0 +1,219 @@
+/*
+ * Copyright RedHat Inc. 2008
+ *
+ * Authors: Vivek Goyal <vgoyal@redhat.com>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of version 2.1 of the GNU Lesser General Public License
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it would be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <libcgroup.h>
+#include <limits.h>
+#include <pwd.h>
+#include <unistd.h>
+#include <getopt.h>
+#include <sys/mount.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+
+#include "tools-common.h"
+
+#define TEMP_BUF 81
+
+/*
+ * Go through /proc/<pid>/status file to determine the euid of the
+ * process.
+ * It returns 0 on success and negative values on failure.
+ */
+
+int euid_of_pid(pid_t pid)
+{
+ FILE *fp;
+ char path[FILENAME_MAX];
+ char buf[TEMP_BUF];
+ uid_t ruid, euid, suid, fsuid;
+
+ sprintf(path, "/proc/%d/status", pid);
+ fp = fopen(path, "r");
+ if (!fp) {
+ cgroup_dbg("Error in opening file %s:%s\n", path,
+ strerror(errno));
+ return -1;
+ }
+
+ while (fgets(buf, TEMP_BUF, fp)) {
+ if (!strncmp(buf, "Uid:", 4)) {
+ sscanf((buf + 5), "%d%d%d%d", (int *)&ruid,
+ (int *)&euid, (int *)&suid, (int *)&fsuid);
+ cgroup_dbg("Scanned proc values are %d %d %d %d\n",
+ ruid, euid, suid, fsuid);
+ return euid;
+ }
+ }
+
+ /* If we are here, we could not find euid. Return error. */
+ return -1;
+}
+
+/*
+ * Go through /proc/<pid>/status file to determine the egid of the
+ * process.
+ * It returns 0 on success and negative values on failure.
+ */
+
+int egid_of_pid(pid_t pid)
+{
+ FILE *fp;
+ char path[FILENAME_MAX];
+ char buf[TEMP_BUF];
+ gid_t rgid, egid, sgid, fsgid;
+
+ sprintf(path, "/proc/%d/status", pid);
+ fp = fopen(path, "r");
+ if (!fp) {
+ cgroup_dbg("Error in opening file %s:%s\n", path,
+ strerror(errno));
+ return -1;
+ }
+
+ while (fgets(buf, TEMP_BUF, fp)) {
+ if (!strncmp(buf, "Gid:", 4)) {
+ sscanf((buf + 5), "%d%d%d%d", (int *)&rgid,
+ (int *)&egid, (int *)&sgid, (int *)&fsgid);
+ cgroup_dbg("Scanned proc values are %d %d %d %d\n",
+ rgid, egid, sgid, fsgid);
+ return egid;
+ }
+ }
+
+ /* If we are here, we could not find egid. Return error. */
+ return -1;
+}
+
+/*
+ * Change process group as specified on command line.
+ */
+int change_group_path(pid_t pid, struct cgroup_group_spec *cgroup_list[])
+{
+ int i;
+ int ret = 0;
+
+ for (i = 0; i < CG_HIER_MAX; i++) {
+ if (!cgroup_list[i])
+ break;
+
+ ret = cgroup_change_cgroup_path(cgroup_list[i]->path, pid,
+ cgroup_list[i]->controllers);
+ if (ret)
+ fprintf(stderr, "Error changing group of pid %d: %s\n",
+ pid, cgroup_strerror(ret));
+ return -1;
+ }
+
+ return 0;
+}
+
+/*
+ * Change process group as specified in cgrules.conf.
+ */
+int change_group_uid_gid(pid_t pid)
+{
+ uid_t euid;
+ gid_t egid;
+ int ret;
+
+ /* Put pid into right cgroup as per rules in /etc/cgrules.conf */
+ euid = euid_of_pid(pid);
+ if (euid == -1) {
+ fprintf(stderr, "Error in determining euid of"
+ " pid %d\n", pid);
+ return -1;
+ }
+
+ egid = egid_of_pid(pid);
+ if (egid == -1) {
+ fprintf(stderr, "Error in determining egid of"
+ " pid %d\n", pid);
+ return -1;
+ }
+
+ /* Change the cgroup by determining the rules based on uid */
+ ret = cgroup_change_cgroup_uid_gid(euid, egid, pid);
+ if (ret) {
+ fprintf(stderr, "Error: change of cgroup failed for"
+ " pid %d: %s\n",
+ pid, cgroup_strerror(ret));
+ return -1;
+ }
+
+ return 0;
+}
+
+int main(int argc, char *argv[])
+{
+ int ret = 0, i, exit_code = 0;
+ pid_t pid;
+ int cg_specified = 0;
+ struct cgroup_group_spec *cgroup_list[CG_HIER_MAX];
+ int c;
+
+
+ if (argc < 2) {
+ fprintf(stderr, "usage is %s "
+ "[-g <list of controllers>:<relative path to cgroup>] "
+ "<list of pids> \n",
+ argv[0]);
+ exit(2);
+ }
+
+ memset(cgroup_list, 0, sizeof(cgroup_list));
+ while ((c = getopt(argc, argv, "+g:")) > 0) {
+ switch (c) {
+ case 'g':
+ if (parse_cgroup_spec(cgroup_list, optarg)) {
+ fprintf(stderr, "cgroup controller and path"
+ "parsing failed\n");
+ return -1;
+ }
+ cg_specified = 1;
+ break;
+ default:
+ fprintf(stderr, "Invalid command line option\n");
+ exit(2);
+ break;
+ }
+ }
+
+
+ /* Initialize libcg */
+ ret = cgroup_init();
+ if (ret) {
+ fprintf(stderr, "libcgroup initialization failed:%d\n", ret);
+ return ret;
+ }
+
+ for (i = optind; i < argc; i++) {
+ pid = (uid_t) atoi(argv[i]);
+
+ if (cg_specified)
+ ret = change_group_path(pid, cgroup_list);
+ else
+ ret = change_group_uid_gid(pid);
+
+ /* if any group change fails */
+ if (ret)
+ exit_code = 1;
+ }
+ return exit_code;
+
+}
diff --git a/src/tools/cgconfig.c b/src/tools/cgconfig.c
new file mode 100644
index 0000000..cc33ad9
--- /dev/null
+++ b/src/tools/cgconfig.c
@@ -0,0 +1,74 @@
+
+/*
+ * Copyright IBM Corporation. 2007
+ *
+ * Authors: Dhaval Giani <dhaval@linux.vnet.ibm.com>
+ * Balbir Singh <balbir@linux.vnet.ibm.com>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of version 2.1 of the GNU Lesser General Public License
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it would be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * Code initiated and designed by Dhaval Giani. All faults are most likely
+ * his mistake.
+ */
+
+#include <libcgroup.h>
+#include <libcgroup-internal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <getopt.h>
+
+
+static void usage(char *progname)
+{
+ printf("Usage: %s [OPTION] [FILE]\n", basename(progname));
+ printf("Parse and load the specified cgroups configuration file\n");
+ printf("\n");
+ printf(" -h, --help Display this help\n");
+ printf(" -l, --load=FILE Parse and load the cgroups configuration file\n");
+ exit(2);
+}
+
+int main(int argc, char *argv[])
+{
+ int c;
+ char filename[PATH_MAX];
+ int ret;
+ static struct option options[] = {
+ {"help", 0, 0, 'h'},
+ {"load", 1, 0, 'l'},
+ {0, 0, 0, 0}
+ };
+
+ if (argc < 2)
+ usage(argv[0]); /* usage() exits */
+
+ while ((c = getopt_long(argc, argv, "hl:", options, NULL)) > 0) {
+ switch (c) {
+ case 'h':
+ usage(argv[0]);
+ break;
+ case 'l':
+ strncpy(filename, optarg, PATH_MAX);
+ ret = cgroup_config_load_config(filename);
+ if (ret) {
+ printf("Loading configuration file %s "
+ "failed\n%s\n", filename,
+ cgroup_strerror(ret));
+ exit(3);
+ }
+ return 0;
+ default:
+ usage(argv[0]);
+ break;
+ }
+ }
+ return 0;
+}
diff --git a/src/tools/cgexec.c b/src/tools/cgexec.c
new file mode 100644
index 0000000..167d873
--- /dev/null
+++ b/src/tools/cgexec.c
@@ -0,0 +1,122 @@
+/*
+ * Copyright RedHat Inc. 2008
+ *
+ * Authors: Vivek Goyal <vgoyal@redhat.com>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of version 2.1 of the GNU Lesser General Public License
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it would be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ */
+
+#include <errno.h>
+#include <grp.h>
+#include <libcgroup.h>
+#include <limits.h>
+#include <pwd.h>
+#include <search.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <getopt.h>
+#include <sys/mount.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+
+#include "tools-common.h"
+
+int main(int argc, char *argv[])
+{
+ int ret = 0, i;
+ int cg_specified = 0;
+ uid_t euid;
+ pid_t pid;
+ gid_t egid;
+ char c;
+ struct cgroup_group_spec *cgroup_list[CG_HIER_MAX];
+
+ if (argc < 2) {
+ fprintf(stderr, "Usage is %s"
+ " [-g <list of controllers>:<relative path to cgroup>]"
+ " command [arguments] \n",
+ argv[0]);
+ exit(2);
+ }
+
+ memset(cgroup_list, 0, sizeof(cgroup_list));
+
+ while ((c = getopt(argc, argv, "+g:")) > 0) {
+ switch (c) {
+ case 'g':
+ if (parse_cgroup_spec(cgroup_list, optarg)) {
+ fprintf(stderr, "cgroup controller and path"
+ "parsing failed\n");
+ return -1;
+ }
+ cg_specified = 1;
+ break;
+ default:
+ fprintf(stderr, "Invalid command line option\n");
+ exit(1);
+ break;
+ }
+ }
+
+ /* Executable name */
+ if (!argv[optind]) {
+ fprintf(stderr, "No command specified\n");
+ exit(1);
+ }
+
+ /* Initialize libcg */
+ ret = cgroup_init();
+ if (ret) {
+ fprintf(stderr, "libcgroup initialization failed:%d", ret);
+ return ret;
+ }
+
+ euid = geteuid();
+ egid = getegid();
+ pid = getpid();
+
+ if (cg_specified) {
+ /*
+ * User has specified the list of control group and
+ * controllers
+ * */
+ for (i = 0; i < CG_HIER_MAX; i++) {
+ if (!cgroup_list[i])
+ break;
+
+ ret = cgroup_change_cgroup_path(cgroup_list[i]->path,
+ pid,
+ cgroup_list[i]->controllers);
+ if (ret) {
+ fprintf(stderr,
+ "cgroup change of group failed\n");
+ return ret;
+ }
+ }
+ } else {
+
+ /* Change the cgroup by determining the rules based on euid */
+ ret = cgroup_change_cgroup_uid_gid(euid, egid, pid);
+ if (ret) {
+ fprintf(stderr, "cgroup change of group failed\n");
+ return ret;
+ }
+ }
+
+ /* Now exec the new process */
+ ret = execvp(argv[optind], &argv[optind]);
+ if (ret == -1) {
+ fprintf(stderr, "%s", strerror(errno));
+ return -1;
+ }
+ return 0;
+}
diff --git a/src/tools/tools-common.c b/src/tools/tools-common.c
new file mode 100644
index 0000000..0bb666f
--- /dev/null
+++ b/src/tools/tools-common.c
@@ -0,0 +1,88 @@
+/*
+ * Copyright Red Hat, Inc. 2009
+ *
+ * Author: Vivek Goyal <vgoyal@redhat.com>
+ * Jan Safranek <jsafrane@redhat.com>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of version 2.1 of the GNU Lesser General Public License
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it would be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ */
+
+#include <errno.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+
+#include <libcgroup.h>
+#include "tools-common.h"
+
+int parse_cgroup_spec(struct cgroup_group_spec *cdptr[], char *optarg)
+{
+ struct cgroup_group_spec *ptr;
+ int i, j;
+ char *cptr, *pathptr, *temp;
+
+ ptr = *cdptr;
+
+ /* Find first free entry inside the cgroup data array */
+ for (i = 0; i < CG_HIER_MAX; i++, ptr++) {
+ if (!cdptr[i])
+ break;
+ }
+
+ if (i == CG_HIER_MAX) {
+ /* No free slot found */
+ fprintf(stderr, "Max allowed hierarchies %d reached\n",
+ CG_HIER_MAX);
+ return -1;
+ }
+
+ /* Extract list of controllers */
+ cptr = strtok(optarg, ":");
+ cgroup_dbg("list of controllers is %s\n", cptr);
+ if (!cptr)
+ return -1;
+
+ /* Extract cgroup path */
+ pathptr = strtok(NULL, ":");
+ cgroup_dbg("cgroup path is %s\n", pathptr);
+ if (!pathptr)
+ return -1;
+
+ /* instanciate cgroup_data. */
+ cdptr[i] = malloc(sizeof(struct cgroup_group_spec));
+ if (!cdptr[i]) {
+ fprintf(stderr, "%s\n", strerror(errno));
+ return -1;
+ }
+ /* Convert list of controllers into an array of strings. */
+ j = 0;
+ do {
+ if (j == 0)
+ temp = strtok(cptr, ",");
+ else
+ temp = strtok(NULL, ",");
+
+ if (temp) {
+ cdptr[i]->controllers[j] = strdup(temp);
+ if (!cdptr[i]->controllers[j]) {
+ free(cdptr[i]);
+ fprintf(stderr, "%s\n", strerror(errno));
+ return -1;
+ }
+ }
+ j++;
+ } while (temp);
+
+ /* Store path to the cgroup */
+ strncpy(cdptr[i]->path, pathptr, FILENAME_MAX);
+ cdptr[i]->path[FILENAME_MAX-1] = '\0';
+
+ return 0;
+}
diff --git a/src/tools/tools-common.h b/src/tools/tools-common.h
new file mode 100644
index 0000000..3437973
--- /dev/null
+++ b/src/tools/tools-common.h
@@ -0,0 +1,54 @@
+/*
+ * Copyright Red Hat, Inc. 2009
+ *
+ * Author: Vivek Goyal <vgoyal@redhat.com>
+ * Jan Safranek <jsafrane@redhat.com>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of version 2.1 of the GNU Lesser General Public License
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it would be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ */
+
+#ifndef __TOOLS_COMMON
+
+#define __TOOLS_COMMON
+
+#include "config.h"
+#include <libcgroup.h>
+
+#ifdef CGROUP_DEBUG
+#define cgroup_dbg(x...) printf(x)
+#else
+#define cgroup_dbg(x...) do {} while (0)
+#endif
+
+/**
+ * Auxiliary specifier of group, used to store parsed command line options.
+ */
+struct cgroup_group_spec {
+ char path[FILENAME_MAX];
+ char *controllers[CG_CONTROLLER_MAX];
+};
+
+
+/**
+ * Parse command line option with group specifier into provided data structure.
+ * The option must have form of 'controller1,controller2,..:group_name'.
+ *
+ * The parsed list of controllers and group name is added at the end of
+ * provided cdptr.
+ *
+ * @param cdptr Target data structure to fill. New item is allocated and added
+ * at the end.
+ * @param optarg Argument to parse.
+ * @return 0 on success, != 0 on error.
+ */
+int parse_cgroup_spec(struct cgroup_group_spec *cdptr[], char *optarg);
+
+
+#endif /* TOOLS_COMMON */