From: Ivana Hutarova Varekova Date: Tue, 16 Nov 2010 11:32:47 +0000 (+0100) Subject: remove the perm part from cgsnapshot output if possible X-Git-Tag: v0.37~10 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=619dd636ad5550ab0002824fe7b62e0789dbada7;p=thirdparty%2Flibcgroup.git remove the perm part from cgsnapshot output if possible This patch removes perm part from cgsnapshot output if it is set to the default one: EXAMPLE: old version: # Configuration file generated by cgsnapshot mount { memory = /cgroup/cpu2; devices = /cgroup/cpu3; } group CBSFxx { perm { admin { uid = root; gid = root; } task { uid = root; gid = root; } } devices { devices.deny="a *:* rwm"; devices.allow="a *:* rwm"; } } new version: # Configuration file generated by cgsnapshot mount { memory = /cgroup/cpu2; devices = /cgroup/cpu3; } group CBSFxx { devices { devices.deny="a *:* rwm"; devices.allow="a *:* rwm"; } } Signed-off-by: Ivana Hutarova Varekova Acked-By: Jan Safranek Signed-off-by: Dhaval Giani --- diff --git a/src/tools/cgsnapshot.c b/src/tools/cgsnapshot.c index f347b393..cb3b22dd 100644 --- a/src/tools/cgsnapshot.c +++ b/src/tools/cgsnapshot.c @@ -190,76 +190,86 @@ static int display_permissions(const char *path, const char *program_name) { int ret; - struct stat sb; + struct stat sba; + struct stat sbt; struct passwd *pw; struct group *gr; char tasks_path[FILENAME_MAX]; - /* print the header */ - fprintf(of, "\tperm {\n"); - /* admin permissions record */ /* get the directory statistic */ - ret = stat(path, &sb); + ret = stat(path, &sba); if (ret) { fprintf(stderr, "ERROR: can't read statistics about %s\n", path); return -1; } - /* find out the user and group name */ - pw = getpwuid(sb.st_uid); - if (pw == NULL) { - fprintf(stderr, "ERROR: can't get %d user name\n", sb.st_uid); - return -1; - } - - gr = getgrgid(sb.st_gid); - if (gr == NULL) { - fprintf(stderr, "ERROR: can't get %d group name\n", sb.st_gid); - return -1; - } - - /* print the admin record */ - fprintf(of, "\t\tadmin {\n"\ - "\t\t\tuid = %s;\n"\ - "\t\t\tgid = %s;\n"\ - "\t\t}\n", pw->pw_name, gr->gr_name); - /* tasks permissions record */ /* get tasks file statistic */ strncpy(tasks_path, path, FILENAME_MAX); tasks_path[FILENAME_MAX-1] = '\0'; strncat(tasks_path, "/tasks", FILENAME_MAX); tasks_path[FILENAME_MAX-1] = '\0'; - - ret = stat(tasks_path, &sb); + ret = stat(tasks_path, &sbt); if (ret) { fprintf(stderr, "ERROR: can't read statistics about %s\n", tasks_path); return -1; } - /* find out the user and group name */ - pw = getpwuid(sb.st_uid); - if (pw == NULL) { - fprintf(stderr, "ERROR: can't get %d user name\n", sb.st_uid); - return -1; - } + if ((sba.st_uid) || (sba.st_gid) || + (sbt.st_uid) || (sbt.st_gid)) { + /* some uid or gid is nonroot, admin permission + tag is necessery */ - gr = getgrgid(sb.st_gid); - if (gr == NULL) { - fprintf(stderr, "ERROR: can't get %d group name\n", sb.st_gid); - return -1; - } + /* print the header */ + fprintf(of, "\tperm {\n"); - /* print the task record */ - fprintf(of, "\t\ttask {\n"\ - "\t\t\tuid = %s;\n"\ - "\t\t\tgid = %s;\n"\ - "\t\t}\n", pw->pw_name, gr->gr_name); + /* find out the user and group name */ + pw = getpwuid(sba.st_uid); + if (pw == NULL) { + fprintf(stderr, "ERROR: can't get %d user name\n", + sba.st_uid); + return -1; + } + + gr = getgrgid(sba.st_gid); + if (gr == NULL) { + fprintf(stderr, "ERROR: can't get %d group name\n", + sba.st_gid); + return -1; + } + + /* print the admin record */ + fprintf(of, "\t\tadmin {\n"\ + "\t\t\tuid = %s;\n"\ + "\t\t\tgid = %s;\n"\ + "\t\t}\n", pw->pw_name, gr->gr_name); + + /* find out the user and group name */ + pw = getpwuid(sbt.st_uid); + if (pw == NULL) { + fprintf(stderr, "ERROR: can't get %d user name\n", + sbt.st_uid); + return -1; + } + + gr = getgrgid(sbt.st_gid); + if (gr == NULL) { + fprintf(stderr, "ERROR: can't get %d group name\n", + sbt.st_gid); + return -1; + } - fprintf(of, "\t}\n"); + /* print the task record */ + fprintf(of, "\t\ttask {\n"\ + "\t\t\tuid = %s;\n"\ + "\t\t\tgid = %s;\n"\ + "\t\t}\n", pw->pw_name, gr->gr_name); + + fprintf(of, "\t}\n"); + } return 0; }