Commit a8911d6d authored by Stanislav Fomichev's avatar Stanislav Fomichev Committed by Alexei Starovoitov
Browse files

selftests/bpf: fix incorrect users of create_and_get_cgroup



We have some tests that assume create_and_get_cgroup returns -1 on error
which is incorrect (it returns 0 on error). Since fd might be zero in
general case, change create_and_get_cgroup to return -1 on error
and fix the users that assume 0 on error.

Fixes: f269099a ("tools/bpf: add a selftest for bpf_get_current_cgroup_id() helper")
Fixes: 7d2c6cfc ("bpf: use --cgroup in test_suite if supplied")

v2:
- instead of fixing the uses that assume -1 on error, convert the users
  that assume 0 on error (fd might be zero in general case)

Signed-off-by: default avatarStanislav Fomichev <sdf@google.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parent 97274b61
Loading
Loading
Loading
Loading
+7 −7
Original line number Diff line number Diff line
@@ -77,7 +77,7 @@ static int test_foo_bar(void)

	/* Create cgroup /foo, get fd, and join it */
	foo = create_and_get_cgroup(FOO);
	if (!foo)
	if (foo < 0)
		goto err;

	if (join_cgroup(FOO))
@@ -94,7 +94,7 @@ static int test_foo_bar(void)

	/* Create cgroup /foo/bar, get fd, and join it */
	bar = create_and_get_cgroup(BAR);
	if (!bar)
	if (bar < 0)
		goto err;

	if (join_cgroup(BAR))
@@ -298,19 +298,19 @@ static int test_multiprog(void)
		goto err;

	cg1 = create_and_get_cgroup("/cg1");
	if (!cg1)
	if (cg1 < 0)
		goto err;
	cg2 = create_and_get_cgroup("/cg1/cg2");
	if (!cg2)
	if (cg2 < 0)
		goto err;
	cg3 = create_and_get_cgroup("/cg1/cg2/cg3");
	if (!cg3)
	if (cg3 < 0)
		goto err;
	cg4 = create_and_get_cgroup("/cg1/cg2/cg3/cg4");
	if (!cg4)
	if (cg4 < 0)
		goto err;
	cg5 = create_and_get_cgroup("/cg1/cg2/cg3/cg4/cg5");
	if (!cg5)
	if (cg5 < 0)
		goto err;

	if (join_cgroup("/cg1/cg2/cg3/cg4/cg5"))
+1 −1
Original line number Diff line number Diff line
@@ -32,7 +32,7 @@ int main(int argc, char **argv)

	cg2 = create_and_get_cgroup(CGROUP_PATH);

	if (!cg2)
	if (cg2 < 0)
		goto err;

	if (bpf_map_update_elem(map_fd[0], &idx, &cg2, BPF_ANY)) {
+3 −3
Original line number Diff line number Diff line
@@ -155,7 +155,7 @@ void cleanup_cgroup_environment(void)
 * This function creates a cgroup under the top level workdir and returns the
 * file descriptor. It is idempotent.
 *
 * On success, it returns the file descriptor. On failure it returns 0.
 * On success, it returns the file descriptor. On failure it returns -1.
 * If there is a failure, it prints the error to stderr.
 */
int create_and_get_cgroup(const char *path)
@@ -166,13 +166,13 @@ int create_and_get_cgroup(const char *path)
	format_cgroup_path(cgroup_path, path);
	if (mkdir(cgroup_path, 0777) && errno != EEXIST) {
		log_err("mkdiring cgroup %s .. %s", path, cgroup_path);
		return 0;
		return -1;
	}

	fd = open(cgroup_path, O_RDONLY);
	if (fd < 0) {
		log_err("Opening Cgroup");
		return 0;
		return -1;
	}

	return fd;
+1 −1
Original line number Diff line number Diff line
@@ -81,7 +81,7 @@ int main(int argc, char **argv)

	/* Create a cgroup, get fd, and join it */
	cgroup_fd = create_and_get_cgroup(TEST_CGROUP);
	if (!cgroup_fd) {
	if (cgroup_fd < 0) {
		printf("Failed to create test cgroup\n");
		goto err;
	}
+1 −1
Original line number Diff line number Diff line
@@ -43,7 +43,7 @@ int main(int argc, char **argv)

	/* Create a cgroup, get fd, and join it */
	cgroup_fd = create_and_get_cgroup(TEST_CGROUP);
	if (!cgroup_fd) {
	if (cgroup_fd < 0) {
		printf("Failed to create test cgroup\n");
		goto err;
	}
Loading