selftests: prctl: Add new prctl test for PR_SET_NAME
authorOsama Muhammad <osmtendev@gmail.com>
Wed, 7 Jun 2023 15:36:00 +0000 (20:36 +0500)
committerShuah Khan <skhan@linuxfoundation.org>
Mon, 24 Jul 2023 15:00:42 +0000 (09:00 -0600)
This patch will add the new test, which covers the prctl call
PR_SET_NAME command. The test tries to give a name using the PR_SET_NAME
call and then confirm it that it changed correctly by using  PR_GET_NAME.
It also tries to rename it with empty name.In the test PR_GET_NAME is
tested by passing null pointer to it and check its behaviour.

Signed-off-by: Osama Muhammad <osmtendev@gmail.com>
Reviewed-by: Muhammad Usama Anjum <usama.anjum@collabora.com>
Tested-by: Muhammad Usama Anjum <usama.anjum@collabora.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
tools/testing/selftests/prctl/Makefile
tools/testing/selftests/prctl/set-process-name.c [new file with mode: 0644]

index c058b81..cfc35d2 100644 (file)
@@ -5,7 +5,7 @@ ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/x86/ -e s/x86_64/x86/)
 
 ifeq ($(ARCH),x86)
 TEST_PROGS := disable-tsc-ctxt-sw-stress-test disable-tsc-on-off-stress-test \
-               disable-tsc-test set-anon-vma-name-test
+               disable-tsc-test set-anon-vma-name-test set-process-name
 all: $(TEST_PROGS)
 
 include ../lib.mk
diff --git a/tools/testing/selftests/prctl/set-process-name.c b/tools/testing/selftests/prctl/set-process-name.c
new file mode 100644 (file)
index 0000000..3bc5e0e
--- /dev/null
@@ -0,0 +1,62 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * This test covers the PR_SET_NAME functionality of prctl calls
+ */
+
+#include <errno.h>
+#include <sys/prctl.h>
+#include <string.h>
+
+#include "../kselftest_harness.h"
+
+#define CHANGE_NAME "changename"
+#define EMPTY_NAME ""
+#define TASK_COMM_LEN 16
+
+int set_name(char *name)
+{
+       int res;
+
+       res = prctl(PR_SET_NAME, name, NULL, NULL, NULL);
+
+       if (res < 0)
+               return -errno;
+       return res;
+}
+
+int check_is_name_correct(char *check_name)
+{
+       char name[TASK_COMM_LEN];
+       int res;
+
+       res = prctl(PR_GET_NAME, name, NULL, NULL, NULL);
+
+       if (res < 0)
+               return -errno;
+
+       return !strcmp(name, check_name);
+}
+
+int check_null_pointer(char *check_name)
+{
+       char *name = NULL;
+       int res;
+
+       res = prctl(PR_GET_NAME, name, NULL, NULL, NULL);
+
+       return res;
+}
+
+TEST(rename_process) {
+
+       EXPECT_GE(set_name(CHANGE_NAME), 0);
+       EXPECT_TRUE(check_is_name_correct(CHANGE_NAME));
+
+       EXPECT_GE(set_name(EMPTY_NAME), 0);
+       EXPECT_TRUE(check_is_name_correct(EMPTY_NAME));
+
+       EXPECT_GE(set_name(CHANGE_NAME), 0);
+       EXPECT_LT(check_null_pointer(CHANGE_NAME), 0);
+}
+
+TEST_HARNESS_MAIN