From: Dhaval Giani Date: Wed, 25 Feb 2009 13:04:34 +0000 (+0000) Subject: libcgroup: Move parse_cgroup_data to separate .c file X-Git-Tag: v0.34~105 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3b09fd0c50de2188a0772d329061bb2444e0f091;p=thirdparty%2Flibcgroup.git libcgroup: Move parse_cgroup_data to separate .c file From: Jan Safranek 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 Signed-off-by: Dhaval Giani git-svn-id: https://libcg.svn.sourceforge.net/svnroot/libcg/trunk@339 4f4bb910-9a46-0410-90c8-c897d4f1cd53 --- diff --git a/Makefile.in b/Makefile.in index d1971cf5..349f1836 100644 --- a/Makefile.in +++ b/Makefile.in @@ -37,8 +37,8 @@ all: $(TARGETS) cgconfigparser: libcgroup.so cgconfig.c libcgroup.h $(CC) $(CFLAGS) $(INC) -Wall -o $@ cgconfig.c $(LDFLAGS) $(LIBS) -cgexec: libcgroup.so cgexec.c libcgroup.h - $(CC) $(CFLAGS) -Wall -o $@ cgexec.c $(LDFLAGS) $(LIBS) +cgexec: libcgroup.so cgexec.c libcgroup.h tools-common.c tools-common.h + $(CC) $(CFLAGS) -Wall -o $@ cgexec.c tools-common.c $(LDFLAGS) $(LIBS) cgclassify: libcgroup.so cgclassify.c $(CC) $(CFLAGS) -Wall -o $@ cgclassify.c $(LDFLAGS) $(LIBS) diff --git a/cgexec.c b/cgexec.c index 9558bca0..167d873b 100644 --- a/cgexec.c +++ b/cgexec.c @@ -28,74 +28,7 @@ #include #include - -struct cgroup_data { - char path[FILENAME_MAX]; - char *controllers[CG_CONTROLLER_MAX]; -}; - -int parse_cgroup_data(struct cgroup_data *cdptr[], char *optarg) -{ - struct cgroup_data *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_data)); - 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 */ - strcpy(cdptr[i]->path, pathptr); - return 0; -} +#include "tools-common.h" int main(int argc, char *argv[]) { @@ -105,7 +38,7 @@ int main(int argc, char *argv[]) pid_t pid; gid_t egid; char c; - struct cgroup_data *cgroup_list[CG_HIER_MAX]; + struct cgroup_group_spec *cgroup_list[CG_HIER_MAX]; if (argc < 2) { fprintf(stderr, "Usage is %s" @@ -120,7 +53,7 @@ int main(int argc, char *argv[]) while ((c = getopt(argc, argv, "+g:")) > 0) { switch (c) { case 'g': - if (parse_cgroup_data(cgroup_list, optarg)) { + if (parse_cgroup_spec(cgroup_list, optarg)) { fprintf(stderr, "cgroup controller and path" "parsing failed\n"); return -1; diff --git a/tools-common.c b/tools-common.c new file mode 100644 index 00000000..2cedf23e --- /dev/null +++ b/tools-common.c @@ -0,0 +1,88 @@ +/* + * Copyright Red Hat, Inc. 2009 + * + * Author: Vivek Goyal + * Jan Safranek + * + * 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 +#include +#include +#include + +#include +#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; +} diff --git a/tools-common.h b/tools-common.h new file mode 100644 index 00000000..cd16804c --- /dev/null +++ b/tools-common.h @@ -0,0 +1,48 @@ +/* + * Copyright Red Hat, Inc. 2009 + * + * Author: Vivek Goyal + * Jan Safranek + * + * 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 +#include "libcgroup-internal.h" + +/** + * 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 */