/* display all controllers attached to the given hierarchy */ static int print_all_controllers_in_hierarchy(const char *tname, int hierarchy, int flags) { int ret = 0; void *handle; struct controller_data info; int first = 1; cont_name_t cont_names; cont_name_t cont_name; /* * Initialize libcgroup and intentionally ignore its result, * no mounted controller is valid use case. */ (void) cgroup_init(); ret = cgroup_get_all_controller_begin(&handle, &info); if ((ret != 0) && (ret != ECGEOF)) { fprintf(stderr, "cannot read controller data: %s\n", cgroup_strerror(ret)); return ret; } while (ret != ECGEOF) { /* controller is in the hierrachy */ if (info.hierarchy != hierarchy) goto next; if (first) { /* the first controller in the hierarchy */ memset(cont_name, 0, FILENAME_MAX); strncpy(cont_name, info.name, FILENAME_MAX-1); memset(cont_names, 0, FILENAME_MAX); strncpy(cont_names, info.name, FILENAME_MAX-1); first = 0; } else { /* the next controller in the hierarchy */ strncat(cont_names, ",", FILENAME_MAX-1); strncat(cont_names, info.name, FILENAME_MAX-1); } next: ret = cgroup_get_all_controller_next(&handle, &info); if (ret && ret != ECGEOF) goto end; } ret = print_controller_mount(cont_name, flags, cont_names, hierarchy); end: cgroup_get_all_controller_end(&handle); if (ret == ECGEOF) ret = 0; return ret; }
/* * Skip adding controller which points to the same cgroup when delete * cgroup with specifying multi controllers. Just skip controller which * cgroup and hierarchy number is same */ static int skip_add_controller(int counter, int *skip, struct ext_cgroup_record *ecg_list) { int k; struct controller_data info; void *handle; int ret = 0; /* find out hierarchy number of added cgroup */ ecg_list[counter].h_number = 0; ret = cgroup_get_all_controller_begin(&handle, &info); while (ret == 0) { if (!strcmp(info.name, ecg_list[counter].name)) { /* hierarchy number found out, set it */ ecg_list[counter].h_number = info.hierarchy; break; } ret = cgroup_get_all_controller_next(&handle, &info); } cgroup_get_all_controller_end(&handle); /* deal with cgroup_get_controller_begin/next ret values */ if (ret == ECGEOF) ret = 0; if (ret) { fprintf(stderr, "cgroup_get_controller_begin/next failed(%s)\n", cgroup_strerror(ret)); return ret; } /* found out whether the hierarchy should be skipped */ *skip = 0; for (k = 0; k < counter; k++) { if ((!strcmp(ecg_list[k].name, ecg_list[counter].name)) && (ecg_list[k].h_number == ecg_list[counter].h_number)) { /* we found a control group in the same hierarchy */ if (strcmp(ecg_list[k].controller, ecg_list[counter].controller)) { /* * it is a different controller -> * if there is not one cgroup for the same * controller, skip it */ *skip = 1; } else { /* * there is the identical group,controller pair * don't skip it */ *skip = 0; return ret; } } } return ret; }
/* go through the list of all controllers gather them based on hierarchy number and print them */ static int cgroup_list_all_controllers(const char *tname, cont_name_t cont_name[CG_CONTROLLER_MAX], int c_number, int flags) { int ret = 0; void *handle; struct controller_data info; int h_list[CG_CONTROLLER_MAX]; /* list of hierarchies */ int counter = 0; int j; int is_on_list = 0; ret = cgroup_get_all_controller_begin(&handle, &info); while (ret == 0) { if (info.hierarchy == 0) { /* the controller is not attached to any hierrachy */ if (flags & FL_ALL) /* display only if -a flag is set */ printf("%s\n", info.name); } is_on_list = 0; j = 0; while ((is_on_list == 0) && (j < c_number)) { if (strcmp(info.name, cont_name[j]) == 0) { is_on_list = 1; break; } j++; } if ((info.hierarchy != 0) && ((flags & FL_ALL) || (!(flags & FL_LIST) || (is_on_list == 1)))) { /* the controller is attached to some hierarchy and either should be output all controllers, or the controller is on the output list */ h_list[counter] = info.hierarchy; counter++; for (j = 0; j < counter-1; j++) { /* * the hierarchy already was on the list * so remove the new record */ if (h_list[j] == info.hierarchy) { counter--; break; } } } ret = cgroup_get_all_controller_next(&handle, &info); } cgroup_get_all_controller_end(&handle); if (ret == ECGEOF) ret = 0; if (ret) { fprintf(stderr, "cgroup_get_controller_begin/next failed (%s)\n", cgroup_strerror(ret)); return ret; } for (j = 0; j < counter; j++) ret = print_all_controllers_in_hierarchy(tname, h_list[j], flags); return ret; }