From: Ivana Hutarova Varekova Date: Wed, 10 Nov 2010 15:08:21 +0000 (+0100) Subject: This patch add options -f and -d to cgcreate, it cause the permission change of the... X-Git-Tag: v0.37~33 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7949bedee0cae603fca701adef61b3b711a6b19b;p=thirdparty%2Flibcgroup.git This patch add options -f and -d to cgcreate, it cause the permission change of the files of the created group -d, --dperm mode sets the permission mode of control groups directory. The mode have to be set using octal numbers e.g. -d 775. -f, --fperm mode sets the permission mode of control groups files. The mode have to be set using octal numbers e.g. -f 775. CHANGELOG v1: * fix jsafrane foodback thanks for it) EXAMPLE: #cgcreate -f 775 -d 775 -g devices:with #cgcreate -g devices:without # ll /cgroup/devices/ | grep with drwxrwxr-x. 2 root root 0 Oct 29 06:55 with drwxr-xr-x. 2 root root 0 Oct 29 06:55 without # ll /cgroup/devices/with total 0 -rwxrwxr-x. 1 root root 0 Oct 29 06:55 cgroup.event_control -rwxrwxr-x. 1 root root 0 Oct 29 06:55 cgroup.procs -rwxrwxr-x. 1 root root 0 Oct 29 06:55 devices.allow -rwxrwxr-x. 1 root root 0 Oct 29 06:55 devices.deny -rwxrwxr-x. 1 root root 0 Oct 29 06:55 devices.list -rwxrwxr-x. 1 root root 0 Oct 29 06:55 notify_on_release -rwxrwxr-x. 1 root root 0 Oct 29 06:55 tasks # ll /cgroup/devices/without/ total 0 --w--w--w-. 1 root root 0 Oct 29 06:55 cgroup.event_control -r--r--r--. 1 root root 0 Oct 29 06:55 cgroup.procs --w-------. 1 root root 0 Oct 29 06:55 devices.allow --w-------. 1 root root 0 Oct 29 06:55 devices.deny -r--r--r--. 1 root root 0 Oct 29 06:55 devices.list -rw-r--r--. 1 root root 0 Oct 29 06:55 notify_on_release -rw-r--r--. 1 root root 0 Oct 29 06:55 tasks Signed-off-by: Ivana Hutarova Varekova Signed-off-by: Jan Safranek --- diff --git a/doc/man/cgcreate.1 b/doc/man/cgcreate.1 index d8e0449d..d067126b 100644 --- a/doc/man/cgcreate.1 +++ b/doc/man/cgcreate.1 @@ -5,13 +5,16 @@ cgcreate \- create new cgroup(s) .SH SYNOPSIS -\fBcgcreate\fR [\fB-h\fR] [\fB-t\fR <\fItuid>:] -[\fB-a\fR <\fIagid>:] \fB-g\fR <\fIcontrollers>: [-g ...] +\fBcgcreate\fR [\fB-h\fR] [\fB-s\fR] [\fB-t\fR <\fItuid>:] +[\fB-a\fR <\fIagid>:] [\fB-f\fR mode] [\fB-d\fR mode] +\fB-g\fR <\fIcontrollers>: [-g ...] .SH DESCRIPTION The command creates new cgroup(s) defined by option \fB-g\fR. + + .TP .B -t : defines the name of the user and the group, which owns tasks @@ -26,6 +29,18 @@ rest of the defined control group’s files. These users are allowed to set subsystem parameters and create subgroups. The default value is the same as has the parent cgroup. +.TP +.B -d, --dperm mode +sets the permission mode of control groups directory. +The mode have to be set using octal numbers e.g. +\fB-d 775\fR. + +.TP +.B -f, --fperm mode +sets the permission mode of control groups files. +The mode have to be set using octal numbers e.g. +\fB-f 775\fR. + .TP .B -g : defines control groups which will be added. diff --git a/src/tools/cgcreate.c b/src/tools/cgcreate.c index c79183ee..8a728ed1 100644 --- a/src/tools/cgcreate.c +++ b/src/tools/cgcreate.c @@ -27,6 +27,7 @@ #include #include "tools-common.h" + /* * Display the usage */ @@ -37,8 +38,9 @@ static void usage(int status, const char *program_name) " try %s -h' for more information.\n", program_name); } else { - fprintf(stdout, "Usage: %s [-h] [-t :] "\ - "[-a :] -g : [-g ...]\n", + fprintf(stdout, "Usage: %s [-h] [-f mode] [-d mode] "\ + "[-t :] [-a :] "\ + "-g : [-g ...]\n", program_name); fprintf(stdout, " -t : Set "\ "the task permission\n"); @@ -48,9 +50,45 @@ static void usage(int status, const char *program_name) "group which should be added\n"); fprintf(stdout, " -h,--help Display "\ "this help\n"); + fprintf(stdout, " -f, --fperm mode Group "\ + "file permissions\n"); + fprintf(stdout, " -d, --dperm mode Group "\ + "direrory permissions\n"); } } +/* allowed mode strings are octal version: "755" */ + +int parse_mode(char *string, mode_t *pmode, const char *program_name) +{ + mode_t mode = 0; + int pos = 0; /* position of the number iin string */ + int i; + int j = 64; + + while (pos < 3) { + if ('0' <= string[pos] && string[pos] < '8') { + i = (int)string[pos] - (int)'0'; + /* parse the permission triple*/ + mode = mode + i*j; + j = j / 8; + } else { + fprintf(stdout, "%s wrong mode format %s", + program_name, string); + return -1; + } + pos++; + } + + /* the string have contains three characters */ + if (string[pos] != '\0') { + fprintf(stdout, "%s wrong mode format %s", + program_name, string); + return -1; + } + *pmode = mode; + return 0; +} int main(int argc, char *argv[]) { @@ -63,6 +101,8 @@ int main(int argc, char *argv[]) {"task", required_argument, NULL, 't'}, {"admin", required_argument, NULL, 'a'}, {"", required_argument, NULL, 'g'}, + {"dperm", required_argument, NULL, 'd'}, + {"fperm", required_argument, NULL, 'f' }, {0, 0, 0, 0}, }; @@ -84,6 +124,12 @@ int main(int argc, char *argv[]) /* approximation of max. numbers of groups that will be created */ int capacity = argc; + /* permission variables */ + mode_t dir_mode = 0; + mode_t file_mode = 0; + int dirm_change = 0; + int filem_change = 0; + /* no parametr on input */ if (argc < 2) { usage(1, argv[0]); @@ -96,7 +142,8 @@ int main(int argc, char *argv[]) } /* parse arguments */ - while ((c = getopt_long(argc, argv, "a:t:g:h", long_opts, NULL)) > 0) { + while ((c = getopt_long(argc, argv, "a:t:g:hd:f:", long_opts, NULL)) + > 0) { switch (c) { case 'h': usage(0, argv[0]); @@ -178,6 +225,14 @@ int main(int argc, char *argv[]) return -1; } break; + case 'd': + dirm_change = 1; + ret = parse_mode(optarg, &dir_mode, argv[0]); + break; + case 'f': + filem_change = 1; + ret = parse_mode(optarg, &file_mode, argv[0]); + break; default: usage(1, argv[0]); return -1; @@ -248,6 +303,17 @@ int main(int argc, char *argv[]) cgroup_free(&cgroup); goto err; } + if (dirm_change + filem_change > 0) { + ret = cg_chmod_recursive(cgroup, dir_mode, dirm_change, + file_mode, filem_change); + if (ret) { + fprintf(stderr, "%s: can't change permission " \ + "of cgroup %s: %s\n", argv[0], + cgroup->name, cgroup_strerror(ret)); + cgroup_free(&cgroup); + goto err; + } + } cgroup_free(&cgroup); } err: