]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
remove the perm part from cgsnapshot output if possible
authorIvana Hutarova Varekova <varekova@redhat.com>
Tue, 16 Nov 2010 11:32:47 +0000 (12:32 +0100)
committerDhaval Giani <dhaval.giani@gmail.com>
Tue, 16 Nov 2010 15:39:17 +0000 (16:39 +0100)
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 <varekova@redhat.com>
Acked-By: Jan Safranek <jsafrane@redhat.com>
Signed-off-by: Dhaval Giani <dhaval.giani@gmail.com>
src/tools/cgsnapshot.c

index f347b39341421679ea3d4713f4d69b221967366f..cb3b22ddda415af024f5a5a40dd73e01c26fe763 100644 (file)
@@ -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;
 }