This patch adds cgset tool, which sets parameters of controllers
for given cgroup based on input name-variable pairs
- the syntax is:

cgset -r <name=value> <relative path to cgroup>


Signed-off-by: Ivana Varekova <[email protected]>
---

 src/tools/Makefile.am |    4 +
 src/tools/cgset.c     |  165 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 168 insertions(+), 1 deletions(-)
 create mode 100644 src/tools/cgset.c

diff --git a/src/tools/Makefile.am b/src/tools/Makefile.am
index e3a3add..b804814 100644
--- a/src/tools/Makefile.am
+++ b/src/tools/Makefile.am
@@ -1,7 +1,7 @@
 INCLUDES = -I$(top_srcdir)/src -I$(top_srcdir)/include
 LDADD = $(top_srcdir)/src/.libs/libcgroup.la
 
-bin_PROGRAMS = cgexec cgclassify cgcreate
+bin_PROGRAMS = cgexec cgclassify cgcreate cgset
 sbin_PROGRAMS = cgconfigparser
 
 cgexec_SOURCES = cgexec.c tools-common.c tools-common.h
@@ -10,5 +10,7 @@ cgclassify_SOURCES = cgclassify.c tools-common.c 
tools-common.h
 
 cgcreate_SOURCES = cgcreate.c tools-common.c tools-common.h
 
+cgset_SOURCES = cgset.c tools-common.c tools-common.h
+
 cgconfigparser_SOURCES = cgconfig.c
 
diff --git a/src/tools/cgset.c b/src/tools/cgset.c
new file mode 100644
index 0000000..383be1c
--- /dev/null
+++ b/src/tools/cgset.c
@@ -0,0 +1,165 @@
+#include <libcgroup.h>
+#include <libcgroup-internal.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "tools-common.h"
+
+int main(int argc, char *argv[])
+{
+       int ret = 0;
+       int i;
+       char c;
+
+       char *buf;
+       char con[FILENAME_MAX];
+       struct control_value *name_value = NULL;
+       int nv_number = 0;
+       int nv_max = 0;
+
+       struct cgroup *cgroup;
+       struct cgroup_controller *cgc;
+
+       /* no parametr on input */
+       if (argc < 2) {
+               fprintf(stderr, "Usage is %s -r <name=value> "
+                       "<relative path to cgroup>\n", argv[0]);
+               return -1;
+       }
+
+       /* parse arguments */
+       while ((c = getopt(argc, argv, "r:")) > 0) {
+               switch (c) {
+               case 'r':
+                       /* add name-value pair to buffer
+                               (= name_value variable) */
+                       if (nv_number >= nv_max) {
+                               nv_max += CG_NV_MAX;
+                               name_value = (struct control_value *)
+                                       realloc(name_value,
+                                       nv_max * sizeof(struct control_value));
+                               if (!name_value) {
+                                       fprintf(stderr, "cgset: "
+                                               "not enough memory\n");
+                                       return -1;
+                               }
+                       }
+
+                       /* parse optarg value */
+                       /* there is necessary to input the tuple n=v */
+                       buf = strtok(optarg, "=");
+                       if (buf == NULL) {
+                               fprintf(stderr, "cgset: "
+                                       "wrong parameter of option -r: %s\n",
+                                       optarg);
+                               return -1;
+                       }
+
+                       strncpy(name_value[nv_number].name, buf, FILENAME_MAX);
+                       name_value[nv_number].name[FILENAME_MAX-1] = '\0';
+
+                       buf = strtok(NULL, "=");
+                       if (buf == NULL) {
+                               fprintf(stderr, "cgset: "
+                                       "wrong parameter of option -r: %s\n",
+                                       optarg);
+                               return -1;
+                       }
+                       strncpy(name_value[nv_number].value, buf, CG_VALUE_MAX);
+                       name_value[nv_number].value[CG_VALUE_MAX-1] = '\0';
+
+                       nv_number++;
+                       break;
+               default:
+                       fprintf(stderr, "cgset: invalid command line option\n");
+                       return -1;
+                       break;
+               }
+       }
+
+       /* no cgroup name */
+       if (!argv[optind]) {
+               fprintf(stderr, "cgset: no cgroup specified\n");
+               return -1;
+       }
+
+       if (nv_number == 0) {
+               fprintf(stderr, "cgset: no name-value pair was set\n");
+               return -1;
+       }
+
+       /* initialize libcg */
+       ret = cgroup_init();
+       if (ret) {
+               fprintf(stderr, "cgset: libcgroup initialization failed: %s\n",
+                       cgroup_strerror(ret));
+               return ret;
+       }
+
+       while (optind < argc) {
+
+               /* create new cgroup */
+               cgroup = cgroup_new_cgroup(argv[optind]);
+               if (!cgroup) {
+                       ret = ECGFAIL;
+                       fprintf(stderr, "cgset: can't add new cgroup: %s\n",
+                               cgroup_strerror(ret));
+                       return -1;
+               }
+
+               /* add pairs name-value to
+                  relevant controllers of this cgroup */
+               for (i = 0; i < nv_number; i++) {
+
+                       if ((strchr(name_value[i].name, '.')) == NULL) {
+                               fprintf(stderr, "cgset: "
+                                       "wrong -r  parameter (%s=%s)\n",
+                                       name_value[i].name,
+                                       name_value[i].value);
+                               return -1;
+                       }
+
+                       if (strcpy(con, name_value[i].name) == NULL) {
+                               ret =  ECGFAIL;
+                               fprintf(stderr, "cgset: strcpy error.\n");
+                               return ret;
+                       }
+                       strtok(con, ".");
+
+                       /* add relevant controller */
+                       cgc = cgroup_add_controller(cgroup, con);
+                       if (!cgc) {
+                               fprintf(stderr, "cgset: "
+                                       "controller %s can't be add\n", con);
+                               return ret;
+                       }
+
+                       /* add name-value pair to this controller */
+                       ret = cgroup_add_value_string(cgc,
+                               name_value[i].name, name_value[i].value);
+                       if (ret) {
+                               fprintf(stderr, "cgset: "
+                                       "name-value pair %s=%s "
+                                       "can't be set\n",
+                                       name_value[i].name,
+                                       name_value[i].value);
+                               return ret;
+                       }
+               }
+
+               /* modify cgroup */
+               ret = cgroup_modify_cgroup(cgroup);
+               if (ret) {
+                       fprintf(stderr, "cgset: "
+                               "the group can't be modified\n");
+                       return ret;
+               }
+
+               optind++;
+       }
+
+       return ret;
+}


------------------------------------------------------------------------------
Crystal Reports - New Free Runtime and 30 Day Trial
Check out the new simplified licensing option that enables 
unlimited royalty-free distribution of the report engine 
for externally facing server and web deployment. 
http://p.sf.net/sfu/businessobjects
_______________________________________________
Libcg-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/libcg-devel

Reply via email to