int flags; /* used input method */
+static char *program_name;
+
static struct cgroup *copy_name_value_from_cgroup(char src_cg_path[FILENAME_MAX])
{
struct cgroup *src_cgroup;
return NULL;
}
-static void usage(int status, const char *program_name)
+static int cgroup_set_cgroup_values(struct cgroup *src_cgrp, const char * const new_cgrp,
+ bool ignore_unmappable, enum cg_version_t src_version)
+{
+ struct cgroup *cgrp, *converted_src_cgrp;
+ int ret = 0;
+
+ /* create new cgroup */
+ cgrp = cgroup_new_cgroup(new_cgrp);
+ if (!cgrp) {
+ ret = ECGFAIL;
+ err("%s: can't add new cgroup: %s\n", program_name, cgroup_strerror(ret));
+ return ret;
+ }
+
+ /* copy the values from the source cgroup to new one */
+ ret = cgroup_copy_cgroup(cgrp, src_cgrp);
+ if (ret != 0) {
+ err("%s: cgroup %s error: %s\n", program_name, src_cgrp->name,
+ cgroup_strerror(ret));
+ goto err;
+ }
+
+ converted_src_cgrp = cgroup_new_cgroup(cgrp->name);
+ if (converted_src_cgrp == NULL) {
+ ret = ECGCONTROLLERCREATEFAILED;
+ goto err;
+ }
+
+ /*
+ * If a failure occurs between this comment and the "End of above comment"
+ * below, then we need to manually free converted_src_cgroup.
+ */
+
+ ret = cgroup_convert_cgroup(converted_src_cgrp, CGROUP_DISK, src_cgrp, src_version);
+ if ((ret && ret != ECGNOVERSIONCONVERT) ||
+ (ret == ECGNOVERSIONCONVERT && !ignore_unmappable)) {
+ /*
+ * If the user not has specified that we ignore any errors
+ * due to being unable to map from v1 to v2 or vice versa,
+ * return error, else ignore the error and continue.
+ */
+ cgroup_free(&converted_src_cgrp);
+ goto err;
+ }
+
+ /*
+ * End of above comment about converted_src_cgroup needing to be manually freed.
+ */
+ cgroup_free(&cgrp);
+ cgrp = converted_src_cgrp;
+
+ /* modify cgroup based on values of the new one */
+ ret = cgroup_modify_cgroup(cgrp);
+ if (ret) {
+ err("%s: cgroup modify error: %s\n", program_name, cgroup_strerror(ret));
+ goto err;
+ }
+
+err:
+ cgroup_free(&cgrp);
+ return ret;
+}
+
+static void usage(int status)
{
if (status != 0) {
err("Wrong input parameters, ");
int nv_number = 0;
int nv_max = 0;
- struct cgroup *converted_src_cgroup = NULL;
char src_cg_path[FILENAME_MAX] = "\0";
struct cgroup *src_cgroup = NULL;
- struct cgroup *cgroup = NULL;
enum cg_version_t src_version = CGROUP_UNK;
bool ignore_unmappable = false;
int ret = 0;
int c;
+ program_name = argv[0];
+
/* no parameter on input */
if (argc < 2) {
- err("Usage is %s -r <name=value> <relative path to cgroup>\n", argv[0]);
+ err("Usage is %s -r <name=value> <relative path to cgroup>\n", program_name);
return -1;
}
switch (c) {
#endif
case 'h':
- usage(0, argv[0]);
+ usage(0);
ret = 0;
goto err;
case 'r':
if ((flags & FL_COPY) != 0) {
- usage(1, argv[0]);
+ usage(1);
ret = EXIT_BADARGS;
goto err;
}
name_value = (struct control_value *)
realloc(name_value, nv_max * sizeof(struct control_value));
if (!name_value) {
- err("%s: not enough memory\n", argv[0]);
+ err("%s: not enough memory\n", program_name);
ret = -1;
goto err;
}
}
- ret = parse_r_flag(argv[0], optarg, &name_value[nv_number]);
+ ret = parse_r_flag(program_name, optarg, &name_value[nv_number]);
if (ret)
goto err;
break;
case COPY_FROM_OPTION:
if (flags != 0) {
- usage(1, argv[0]);
+ usage(1);
ret = EXIT_BADARGS;
goto err;
}
ignore_unmappable = true;
break;
default:
- usage(1, argv[0]);
+ usage(1);
ret = EXIT_BADARGS;
goto err;
}
/* no cgroup name */
if (!argv[optind]) {
- err("%s: no cgroup specified\n", argv[0]);
+ err("%s: no cgroup specified\n", program_name);
ret = EXIT_BADARGS;
goto err;
}
if (flags == 0) {
- err("%s: no name-value pair was set\n", argv[0]);
+ err("%s: no name-value pair was set\n", program_name);
ret = EXIT_BADARGS;
goto err;
}
/* initialize libcgroup */
ret = cgroup_init();
if (ret) {
- err("%s: libcgroup initialization failed: %s\n", argv[0],
+ err("%s: libcgroup initialization failed: %s\n", program_name,
cgroup_strerror(ret));
goto err;
}
}
while (optind < argc) {
- /* create new cgroup */
- cgroup = cgroup_new_cgroup(argv[optind]);
- if (!cgroup) {
- ret = ECGFAIL;
- err("%s: can't add new cgroup: %s\n", argv[0], cgroup_strerror(ret));
- goto err;
- }
-
- /* copy the values from the source cgroup to new one */
- ret = cgroup_copy_cgroup(cgroup, src_cgroup);
- if (ret != 0) {
- err("%s: cgroup %s error: %s\n", argv[0], src_cg_path,
- cgroup_strerror(ret));
- goto err;
- }
-
- converted_src_cgroup = cgroup_new_cgroup(cgroup->name);
- if (converted_src_cgroup == NULL) {
- ret = ECGCONTROLLERCREATEFAILED;
- goto err;
- }
-
- /*
- * If a failure occurs between this comment and the "End of above comment"
- * below, then we need to manually free converted_src_cgroup.
- */
-
- ret = cgroup_convert_cgroup(converted_src_cgroup, CGROUP_DISK, src_cgroup,
- src_version);
- if ((ret && ret != ECGNOVERSIONCONVERT) ||
- (ret == ECGNOVERSIONCONVERT && !ignore_unmappable)) {
- /*
- * If the user not has specified that we ignore any errors
- * due to being unable to map from v1 to v2 or vice versa,
- * return error, else ignore the error and continue.
- */
- free(converted_src_cgroup);
- goto err;
- }
-
- /*
- * End of above comment about converted_src_cgroup needing to be manually freed.
- */
- cgroup_free(&cgroup);
- cgroup = converted_src_cgroup;
-
- /* modify cgroup based on values of the new one */
- ret = cgroup_modify_cgroup(cgroup);
- if (ret) {
- err("%s: cgroup modify error: %s\n", argv[0], cgroup_strerror(ret));
+ ret = cgroup_set_cgroup_values(src_cgroup, argv[optind], ignore_unmappable,
+ src_version);
+ if (ret)
goto err;
- }
optind++;
- cgroup_free(&cgroup);
}
err:
- if (cgroup)
- cgroup_free(&cgroup);
- if (src_cgroup)
- cgroup_free(&src_cgroup);
+ cgroup_free(&src_cgroup);
free(name_value);
return ret;