On Thu, May 21, 2009 at 01:14:35PM +0200, Ivana Varekova wrote:
> ---------------------
> Fixed valgrind output.
> ---------------------
> 
> 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     |  177 
> +++++++++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 180 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..390190d
> --- /dev/null
> +++ b/src/tools/cgset.c
> @@ -0,0 +1,177 @@
> +#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;

I'm not very happy with the use of the internal structures here, but I
don;t have a better option.

> +     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");
> +                                     ret = -1;
> +                                     goto err;
> +                             }
> +                     }
> +
> +                     /* 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);
> +                             ret = -1;

freeing name_value?

> +                             goto err;
> +                     }
> +
> +                     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;

here as well.

> +                     }
> +                     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");
> +                     ret = -1;
> +                     goto err;
> +                     break;
> +             }
> +     }
> +
> +     /* no cgroup name */
> +     if (!argv[optind]) {
> +             fprintf(stderr, "cgset: no cgroup specified\n");
> +             ret = -1;
> +             goto err;
> +     }
> +
> +     if (nv_number == 0) {
> +             fprintf(stderr, "cgset: no name-value pair was set\n");
> +             ret = -1;
> +             goto err;
> +     }
> +
> +     /* initialize libcg */

libcgroup please :)

> +     ret = cgroup_init();
> +     if (ret) {
> +             fprintf(stderr, "cgset: libcgroup initialization failed: %s\n",
> +                     cgroup_strerror(ret));
> +             goto err;
> +     }
> +
> +     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));
> +                     ret = -1;
> +                     goto err;
> +             }
> +
> +             /* 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);
> +                             ret = -1;
> +                             goto gerr;
> +                     }
> +
> +                     if (strcpy(con, name_value[i].name) == NULL) {

strncpy

> +                             ret =  ECGFAIL;
> +                             fprintf(stderr, "cgset: strcpy error.\n");
> +                             goto gerr;
> +                     }
> +                     strtok(con, ".");

Check the return please.

> +
> +                     /* add relevant controller */
> +                     cgc = cgroup_add_controller(cgroup, con);
> +                     if (!cgc) {
> +                             fprintf(stderr, "cgset: "
> +                                     "controller %s can't be add\n", con);
> +                             goto gerr;
> +                     }
> +
> +                     /* 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);
> +                             goto gerr;
> +                     }
> +             }
> +
> +             /* modify cgroup */
> +             ret = cgroup_modify_cgroup(cgroup);
> +             if (ret) {
> +                     fprintf(stderr, "cgset: "
> +                             "the group can't be modified\n");
> +                     goto gerr;
> +             }
> +
> +             optind++;
> +             cgroup_free(&cgroup);
> +     }
> +gerr:

why gerr? Can we have a better name please.

> +     if (ret)
> +             cgroup_free(&cgroup);
> +err:
> +     free(name_value);
> +     return ret;
> +}


Ivana, this is almost ready to be merged. Can you please handle the
strncpy and the issues with the return value of strtok please so we can
merge it in.

Thanks a lot for doing this!
-- 
regards,
Dhaval

------------------------------------------------------------------------------
Register Now for Creativity and Technology (CaT), June 3rd, NYC. CaT 
is a gathering of tech-side developers & brand creativity professionals. Meet
the minds behind Google Creative Lab, Visual Complexity, Processing, & 
iPhoneDevCamp as they present alongside digital heavyweights like Barbarian 
Group, R/GA, & Big Spaceship. http://p.sf.net/sfu/creativitycat-com 
_______________________________________________
Libcg-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/libcg-devel

Reply via email to