On Thu, May 21, 2009 at 12:08:29PM +0200, Ivana Varekova wrote: > Now fixed valgrind errors. > > ------------------------------------------ > > This patch adds cgcreate tool, which creates cgroups based on input > parameters - the syntax is: > > cgcreate -t <tuid>:<tgid> -a <agid>:<auid> -g <list of controllers>:<relative > path to cgroup> > > where: > -a enables user to define admin gid and uid > -t enables user to define task gid and uid > -g sets pairs list of controllers-relative path to cgroup > > regarding Dhaval comments: > > >> -bin_PROGRAMS = cgexec cgclassify > >> +bin_PROGRAMS = cgexec cgclassify cgcreate > > > >I wonder. Should this come in sbin if you are going to give absolute > >path. Would it be a good idea to look at classifying it wrt the user's > >cgroup? (the one where cgclassify/cgrulesengd classifies a task to) > > I prefer to have the tool in bin directory, just for the case in which there > is a cgroup in which the user have the right to create group there. >
OK. What about the other idea? Will that be somthing that would be interesting. A user just does a cgcreate browser and a cgroup should be created relative to the user's default cgroup (dependening on the cgrulesengd config file) > > >> + fprintf(stderr, "cgcreate: " > >> + "controller %s can't be add\n", > >> + cgroup_list[i]->controllers[j]); > >> + goto err; > > > >argv[0] instead of cgcreate ? (I'm not sure if that is the correct > >programming practice, so please take this with a grain of salt) > > I found both cases - some programs uses name (gcc), some uses argv[0] (ls) > So if you preffer it I can change this. > I think I might prefer using argv[0] here :). > > >> + goto err; > >> + } > >> + } > > > >cgroup_free() ? > > Thanks, changed > > And fix the problem with cgroup_free. > > Signed-off-by: Ivana Varekova <[email protected]> > --- > > src/tools/Makefile.am | 4 + > src/tools/cgcreate.c | 203 > +++++++++++++++++++++++++++++++++++++++++++++++++ > 2 files changed, 206 insertions(+), 1 deletions(-) > create mode 100644 src/tools/cgcreate.c > > diff --git a/src/tools/Makefile.am b/src/tools/Makefile.am > index e1cace3..e3a3add 100644 > --- a/src/tools/Makefile.am > +++ b/src/tools/Makefile.am > @@ -1,12 +1,14 @@ > INCLUDES = -I$(top_srcdir)/src -I$(top_srcdir)/include > LDADD = $(top_srcdir)/src/.libs/libcgroup.la > > -bin_PROGRAMS = cgexec cgclassify > +bin_PROGRAMS = cgexec cgclassify cgcreate > sbin_PROGRAMS = cgconfigparser > > cgexec_SOURCES = cgexec.c tools-common.c tools-common.h > > cgclassify_SOURCES = cgclassify.c tools-common.c tools-common.h > > +cgcreate_SOURCES = cgcreate.c tools-common.c tools-common.h > + > cgconfigparser_SOURCES = cgconfig.c > > diff --git a/src/tools/cgcreate.c b/src/tools/cgcreate.c > new file mode 100644 > index 0000000..285d88f > --- /dev/null > +++ b/src/tools/cgcreate.c > @@ -0,0 +1,203 @@ > +#include <libcgroup.h> > +#include <libcgroup-internal.h> > + > +#include <stdio.h> > +#include <stdlib.h> > +#include <string.h> > +#include <pwd.h> > +#include <sys/types.h> > +#include <errno.h> > + > +#include "tools-common.h" > + > +int main(int argc, char *argv[]) > +{ > + int ret = 0; > + int i, j; > + char c; > + > + /* Structure to get GID from group name */ > + struct group *grp = NULL; > + char *grp_string = NULL; > + > + /* Structure to get UID from user name */ > + struct passwd *pwd = NULL; > + char *pwd_string = NULL; > + > + uid_t tuid = CGRULE_INVALID, auid = CGRULE_INVALID; > + gid_t tgid = CGRULE_INVALID, agid = CGRULE_INVALID; > + > + struct cgroup_group_spec *cgroup_list[CG_HIER_MAX]; > + struct cgroup *cgroup; > + struct cgroup_controller *cgc; > + > + /* no parametr on input */ > + if (argc < 2) { > + fprintf(stderr, "Usage is %s " > + "-t <tuid>:<tgid> -a <agid>:<auid> " > + "-g <list of controllers>:<relative path to cgroup>\n", > + argv[0]); > + return -1; > + } > + > + memset(cgroup_list, 0, sizeof(cgroup_list)); > + /* parse arguments */ > + while ((c = getopt(argc, argv, "a:t:g:")) > 0) { > + switch (c) { > + case 'a': > + /* set admin uid/gid */ > + if (optarg[0] == ':') > + grp_string = strtok(optarg, ":"); > + else { > + pwd_string = strtok(optarg, ":"); > + if (pwd_string != NULL) > + grp_string = strtok(NULL, ":"); > + } > + > + if (pwd_string != NULL) { > + pwd = getpwnam(pwd_string); > + if (pwd != NULL) { > + auid = pwd->pw_uid; > + } else { > + fprintf(stderr, "cgcreate: " > + "can't find uid of user %s.\n", > + pwd_string); > + return -1; > + } > + } > + if (grp_string != NULL) { > + grp = getgrnam(grp_string); > + if (grp != NULL) > + agid = grp->gr_gid; > + else { > + fprintf(stderr, "cgcreate: " > + "can't find gid of group %s.\n", > + grp_string); > + return -1; > + } > + } > + > + break; > + case 't': > + /* set task uid/gid */ > + if (optarg[0] == ':') > + grp_string = strtok(optarg, ":"); > + else { > + pwd_string = strtok(optarg, ":"); > + if (pwd_string != NULL) > + grp_string = strtok(NULL, ":"); > + } > + > + if (pwd_string != NULL) { > + pwd = getpwnam(pwd_string); > + if (pwd != NULL) { > + tuid = pwd->pw_uid; > + } else { > + fprintf(stderr, "cgcreate: " > + "can't find uid of user %s.\n", > + pwd_string); > + return -1; > + } > + } > + if (grp_string != NULL) { > + grp = getgrnam(grp_string); > + if (grp != NULL) > + tgid = grp->gr_gid; > + else { > + fprintf(stderr, "cgcreate: " > + "can't find gid of group %s.\n", > + grp_string); > + return -1; > + } > + } > + break; > + case 'g': > + if (parse_cgroup_spec(cgroup_list, optarg)) { > + fprintf(stderr, "cgcreate: " > + "cgroup controller and path" > + "parsing failed (%s)\n", argv[optind]); > + return -1; > + } > + break; > + default: > + fprintf(stderr, "cgcreate: " > + "invalid command line option\n"); > + return -1; > + break; > + } > + } > + > + /* no cgroup name */ > + if (argv[optind]) { > + fprintf(stderr, "cgcreate: " > + "wrong arguments (%s)\n", argv[optind]); > + ret = -1; > + goto err; > + } > + > + /* initialize libcg */ > + ret = cgroup_init(); > + if (ret) { > + fprintf(stderr, "cgcreate: " > + "libcgroup initialization failed: %s\n", > + cgroup_strerror(ret)); > + goto err; > + } > + > + /* for each new cgroup */ > + for (i = 0; i < CG_HIER_MAX; i++) { > + if (!cgroup_list[i]) > + break; > + > + /* create the new cgroup structure */ > + cgroup = cgroup_new_cgroup(cgroup_list[i]->path); > + if (!cgroup) { > + ret = ECGFAIL; > + fprintf(stderr, "cgcreate: can't add new cgroup: %s\n", > + cgroup_strerror(ret)); > + goto err; > + } > + > + /* set uid and gid for the new cgroup based on input options */ > + ret = cgroup_set_uid_gid(cgroup, tuid, tgid, auid, agid); > + if (ret) > + goto err; > + > + /* add controllers to the new cgroup */ > + j = 0; > + while (cgroup_list[i]->controllers[j]) { > + cgc = cgroup_add_controller(cgroup, > + cgroup_list[i]->controllers[j]); > + if (!cgc) { > + ret = ECGINVAL; > + fprintf(stderr, "cgcreate: " > + "controller %s can't be add\n", > + cgroup_list[i]->controllers[j]); > + goto err; We might want to do a cgroup_free here. > + } > + j++; > + } > + > + /* no controller in given cgroup */ > + if (!cgroup->index) { Not sure why this check is needed.cgroup_add_controller has to succeed. Also, I am not sure if accessing internal data structures is a good idea. Balbir, Jan, Bharata what do you say? > + ret = ECGROUPNOTEXIST; > + goto err; > + } > + > + /* all variables set so create cgroup */ > + ret = cgroup_create_cgroup(cgroup, 0); > + if (ret) { > + fprintf(stderr, "cgcreate: " > + "can't create cgroup %s: %s\n", > + cgroup->name, cgroup_strerror(ret)); > + goto err; cgroup_free() here as well. > + } > + cgroup_free(&cgroup); > + } > +err: > + for (i = 0; i < CG_HIER_MAX; i++) { > + if (cgroup_list[i]) > + cgroup_free_group_spec(cgroup_list[i]); > + } > + return ret; > +} > -- 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 asthey present alongside digital heavyweights like Barbarian Group, R/GA, & Big Spaceship. http://www.creativitycat.com _______________________________________________ Libcg-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/libcg-devel
