Skip to contentSkip to conversation

selftests: Improves rtctest error handling.

PATCH Superseded Atul Kumar Pant 2 participants 4 messages Apr 8, 2023 – Apr 18, 2023

Subsystems (2)

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftests: Improves rtctest error handling.

When running the rtctest without root privileges the test fails expectedly, but prints the logs that are not useful to point to the issue. Similarly, if we pass wrong rtc device file as an argument, the test output failure logs do not point to the issue that the rtc file is invalid.
To handle these issues, this patch adds checks to verify uid with which the test is run and also if the rtc_file is valid.

---
 tools/testing/selftests/rtc/rtctest.c | 34 +++++++++++++++++++--------
 1 file changed, 24 insertions(+), 10 deletions(-)
tools/testing/selftests/rtc/rtctest.c +24 -10
index 2b9d929a24ed..9564346c63eb 100644
@@ -388,16 +388,30 @@ __constructor_order_last(void)
 
 int main(int argc, char **argv)
 {
-	switch (argc) {
-	case 2:
-		rtc_file = argv[1];
-		/* FALLTHROUGH */
-	case 1:
-		break;
-	default:
-		fprintf(stderr, "usage: %s [rtcdev]\n", argv[0]);
-		return 1;
+    int ret = -1;
+
+    // Verify if the test is run as root
+    if (getuid() != 0) {
+		ksft_exit_skip("[ERROR]: Please run the test as root - Exiting.\n");
+    }
+
+    switch (argc) {
+    case 2:
+        rtc_file = argv[1];
+        /* FALLTHROUGH */
+    case 1:
+        break;
+    default:
+        fprintf(stderr, "usage: %s [rtcdev]\n", argv[0]);
+        return 1;
 	}
 
-	return test_harness_run(argc, argv);
+    // Run the test if rtc_file is valid
+    if (access(rtc_file, F_OK) == 0) {
+	    ret = test_harness_run(argc, argv);
+    } else {
+		ksft_exit_skip("[ERROR]: %s : File does not exists - Exiting\n", rtc_file);
+    }
+
+    return ret;
 }
Signature · 6 lines
2.25.1

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
#1 lore raw
Quoted 8 lines · Atul Kumar Pant

When running the rtctest without root privileges the test fails expectedly, but prints the logs that are not useful to point to the issue. Similarly, if we pass wrong rtc device file as an argument, the test output failure logs do not point to the issue that the rtc file is invalid.
To handle these issues, this patch adds checks to verify uid with which the test is run and also if the rtc_file is valid.

Quoted 5 lines
---
 tools/testing/selftests/rtc/rtctest.c | 34 +++++++++++++++++++--------
 1 file changed, 24 insertions(+), 10 deletions(-)

Please run your patch through checkpatch.pl, the indentation and comment style are not correct.

Quoted 12 lines
tools/testing/selftests/rtc/rtctest.c +0 -4
index 2b9d929a24ed..9564346c63eb 100644
@@ -388,16 +388,30 @@ __constructor_order_last(void)
 
 int main(int argc, char **argv)
 {
-	switch (argc) {
-	case 2:
-		rtc_file = argv[1];
-		/* FALLTHROUGH */

tools/testing/selftests/rtc/rtctest.c __constructor_order_last(void) · line 395

Quoted 10 lines
tools/testing/selftests/rtc/rtctest.c +5 -5
-	case 1:
-		break;
-	default:
-		fprintf(stderr, "usage: %s [rtcdev]\n", argv[0]);
-		return 1;
+    int ret = -1;
+
+    // Verify if the test is run as root
+    if (getuid() != 0) {
+		ksft_exit_skip("[ERROR]: Please run the test as root - Exiting.\n");

If this is an error, should we really return skip? This may end up being silently ignored whereas the expectation is to test the rtc.

Also, you may use the rtc as non root so I guess the access() test should be enough.

Quoted 28 lines
tools/testing/selftests/rtc/rtctest.c +19 -1
+    }
+
+    switch (argc) {
+    case 2:
+        rtc_file = argv[1];
+        /* FALLTHROUGH */
+    case 1:
+        break;
+    default:
+        fprintf(stderr, "usage: %s [rtcdev]\n", argv[0]);
+        return 1;
 	}
 
-	return test_harness_run(argc, argv);
+    // Run the test if rtc_file is valid
+    if (access(rtc_file, F_OK) == 0) {
+	    ret = test_harness_run(argc, argv);
+    } else {
+		ksft_exit_skip("[ERROR]: %s : File does not exists - Exiting\n", rtc_file);
 
 
+    }
+
+    return ret;
 }
Signature · 1 line
2.25.1
Signature · 7 lines
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
#2 lore raw
Quoted 9 lines · Alexandre Belloni
Quoted 8 lines · Atul Kumar Pant

When running the rtctest without root privileges the test fails expectedly, but prints the logs that are not useful to point to the issue. Similarly, if we pass wrong rtc device file as an argument, the test output failure logs do not point to the issue that the rtc file is invalid.
To handle these issues, this patch adds checks to verify uid with which the test is run and also if the rtc_file is valid.

Quoted 8 lines
Quoted 5 lines
---
 tools/testing/selftests/rtc/rtctest.c | 34 +++++++++++++++++++--------
 1 file changed, 24 insertions(+), 10 deletions(-)

Please run your patch through checkpatch.pl, the indentation and comment style are not correct.

Sure I'll fix this is next patch.

Quoted 18 lines
Quoted 18 lines
tools/testing/selftests/rtc/rtctest.c +0 -9
index 2b9d929a24ed..9564346c63eb 100644
@@ -388,16 +388,30 @@ __constructor_order_last(void)
 
 int main(int argc, char **argv)
 {
-	switch (argc) {
-	case 2:
-		rtc_file = argv[1];
-		/* FALLTHROUGH */
-	case 1:
-		break;
-	default:
-		fprintf(stderr, "usage: %s [rtcdev]\n", argv[0]);
-		return 1;
Quoted 9 lines

tools/testing/selftests/rtc/rtctest.c __constructor_order_last(void) · line 395

Quoted 5 lines
tools/testing/selftests/rtc/rtctest.c +5 -0
+    int ret = -1;
+
+    // Verify if the test is run as root
+    if (getuid() != 0) {
+		ksft_exit_skip("[ERROR]: Please run the test as root - Exiting.\n");

If this is an error, should we really return skip? This may end up being silently ignored whereas the expectation is to test the rtc.

Yes, we should not skip the test. In that case we can use 'ksft_exit_fail_msg'. I'll fix this in next patch.

Quoted 2 lines

Also, you may use the rtc as non root so I guess the access() test should be enough.

I added this check for the use case when we use the rtc file from /dev/ which will require root permissions. Should I keep this check ?

Quoted 22 lines
Quoted 20 lines
tools/testing/selftests/rtc/rtctest.c +16 -1
+    }
+
+    switch (argc) {
+    case 2:
+        rtc_file = argv[1];
+        /* FALLTHROUGH */
+    case 1:
+        break;
+    default:
+        fprintf(stderr, "usage: %s [rtcdev]\n", argv[0]);
+        return 1;
 	}
 
-	return test_harness_run(argc, argv);
+    // Run the test if rtc_file is valid
+    if (access(rtc_file, F_OK) == 0) {
+	    ret = test_harness_run(argc, argv);
+    } else {
+		ksft_exit_skip("[ERROR]: %s : File does not exists - Exiting\n", rtc_file);
Quoted 12 lines
Quoted 7 lines
tools/testing/selftests/rtc/rtctest.c +3 -0
+    }
+
+    return ret;
 }
Signature · 1 line
2.25.1
Signature · 3 lines
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

#3 lore raw
Quoted 49 lines · Atul Kumar Pant
Quoted 9 lines · Alexandre Belloni
Quoted 8 lines · Atul Kumar Pant

When running the rtctest without root privileges the test fails expectedly, but prints the logs that are not useful to point to the issue. Similarly, if we pass wrong rtc device file as an argument, the test output failure logs do not point to the issue that the rtc file is invalid.
To handle these issues, this patch adds checks to verify uid with which the test is run and also if the rtc_file is valid.

Quoted 8 lines
Quoted 5 lines
---
 tools/testing/selftests/rtc/rtctest.c | 34 +++++++++++++++++++--------
 1 file changed, 24 insertions(+), 10 deletions(-)

Please run your patch through checkpatch.pl, the indentation and comment style are not correct.

Sure I'll fix this is next patch.

Quoted 18 lines
Quoted 18 lines
tools/testing/selftests/rtc/rtctest.c +0 -9
index 2b9d929a24ed..9564346c63eb 100644
@@ -388,16 +388,30 @@ __constructor_order_last(void)
 
 int main(int argc, char **argv)
 {
-	switch (argc) {
-	case 2:
-		rtc_file = argv[1];
-		/* FALLTHROUGH */
-	case 1:
-		break;
-	default:
-		fprintf(stderr, "usage: %s [rtcdev]\n", argv[0]);
-		return 1;
Quoted 9 lines

tools/testing/selftests/rtc/rtctest.c __constructor_order_last(void) · line 395

Quoted 5 lines
tools/testing/selftests/rtc/rtctest.c +5 -0
+    int ret = -1;
+
+    // Verify if the test is run as root
+    if (getuid() != 0) {
+		ksft_exit_skip("[ERROR]: Please run the test as root - Exiting.\n");

If this is an error, should we really return skip? This may end up being silently ignored whereas the expectation is to test the rtc.

Quoted 8 lines

Yes, we should not skip the test. In that case we can use 'ksft_exit_fail_msg'. I'll fix this in next patch.

Quoted 2 lines

Also, you may use the rtc as non root so I guess the access() test should be enough.

I added this check for the use case when we use the rtc file from /dev/ which will require root permissions. Should I keep this check ?

You don't need to be root, you need CAP_SYS_TIME and CAP_SYS_RESOURCE, I wouldn't check for uid 0.

Quoted 35 lines
Quoted 35 lines
Quoted 30 lines
tools/testing/selftests/rtc/rtctest.c +19 -1
+    }
+
+    switch (argc) {
+    case 2:
+        rtc_file = argv[1];
+        /* FALLTHROUGH */
+    case 1:
+        break;
+    default:
+        fprintf(stderr, "usage: %s [rtcdev]\n", argv[0]);
+        return 1;
 	}
 
-	return test_harness_run(argc, argv);
+    // Run the test if rtc_file is valid
+    if (access(rtc_file, F_OK) == 0) {
+	    ret = test_harness_run(argc, argv);
+    } else {
+		ksft_exit_skip("[ERROR]: %s : File does not exists - Exiting\n", rtc_file);
 
 
+    }
+
+    return ret;
 }
Signature · 1 line
2.25.1
Signature · 3 lines
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
Signature · 7 lines
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees