summaryrefslogtreecommitdiffstats
path: root/tools-common.c
diff options
context:
space:
mode:
authorDhaval Giani <dhaval@linux.vnet.ibm.com>2009-02-25 13:04:34 +0000
committerDhaval Giani <dhaval@linux.vnet.ibm.com>2009-02-25 13:04:34 +0000
commit3b09fd0c50de2188a0772d329061bb2444e0f091 (patch)
treea33ea1d492501708edd757348f2dfe8dc250b665 /tools-common.c
parent680b9e7311f8ccd0fa5190ec31a5274fbcd8a4be (diff)
downloadlibcg-3b09fd0c50de2188a0772d329061bb2444e0f091.tar.gz
libcg-3b09fd0c50de2188a0772d329061bb2444e0f091.tar.xz
libcg-3b09fd0c50de2188a0772d329061bb2444e0f091.zip
libcgroup: Move parse_cgroup_data to separate .c file
From: Jan Safranek <jsafrane@redhat.com> Cgclassify could benefit from parsing of -g command line option, let's move it to separate file, where both cgexec and cgclassify can use it. The data structures and function names are also more descriptive now. I added also the copyright notice and license to the new files. Signed-off-by: Jan Safranek <jsafrane@redhat.com> Signed-off-by: Dhaval Giani <dhaval@linux.vnet.ibm.com> git-svn-id: https://libcg.svn.sourceforge.net/svnroot/libcg/trunk@339 4f4bb910-9a46-0410-90c8-c897d4f1cd53
Diffstat (limited to 'tools-common.c')
-rw-r--r--tools-common.c88
1 files changed, 88 insertions, 0 deletions
diff --git a/tools-common.c b/tools-common.c
new file mode 100644
index 0000000..2cedf23
--- /dev/null
+++ b/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, ":");
+ dbg("list of controllers is %s\n", cptr);
+ if (!cptr)
+ return -1;
+
+ /* Extract cgroup path */
+ pathptr = strtok(NULL, ":");
+ 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;
+}