Skip to contentSkip to conversation

utils.c: get_available_ptests allow to specify relative directories

PATCH In review Anibal Limon 2 participants 6 messages Mar 23, 2021 – Mar 24, 2021 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/4 utils.c: get_available_ptests allow to specify relative directories

Fixes,

$ ./ptest-runner -d ./tests/data bash
---
 utils.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
utils.c +7 -3
index a4e190e..43ab03b 100644
@@ -34,6 +34,7 @@
 #include <poll.h>
 #include <pty.h>
 #include <signal.h>
+#include <limits.h>
 #include <stdlib.h>
 #include <string.h>
 #include <time.h>
@@ -85,6 +86,9 @@ get_available_ptests(const char *dir)
 	struct dirent **namelist;
 	int fail;
 	int saved_errno = -1; /* Initalize to invalid errno. */
+	char realdir[PATH_MAX];
+
+	realpath(dir, realdir);
 
 	do
 	{
@@ -93,7 +97,7 @@ get_available_ptests(const char *dir)
 		if (head == NULL)
 			break;
 
-		if (stat(dir, &st_buf) == -1) {
+		if (stat(realdir, &st_buf) == -1) {
 			PTEST_LIST_FREE_CLEAN(head);
 			break;
 		}
@@ -104,7 +108,7 @@ get_available_ptests(const char *dir)
 			break;
 		}
 
-		n = scandir(dir, &namelist, NULL, alphasort);
+		n = scandir(realdir, &namelist, NULL, alphasort);
 		if (n == -1) {
 			PTEST_LIST_FREE_CLEAN(head);
 			break;
@@ -130,7 +134,7 @@ get_available_ptests(const char *dir)
 			}
 
 			if (asprintf(&run_ptest, "%s/%s/ptest/run-ptest",
-			    dir, d_name) == -1)  {
+			    realdir, d_name) == -1)  {
 				fail = 1;
 				saved_errno = errno;
 				free(d_name);
Signature · 1 line
2.31.0
#1 lore raw

PATCH 2/4 utils.c: Fix exit status of a child

Modify testcase to validate the actual exit status.

[YOCTO #14217]

---
 tests/data/fail/ptest/run-ptest | 3 +++
 tests/utils.c                   | 2 +-
 utils.c                         | 3 +++
 3 files changed, 7 insertions(+), 1 deletion(-)
 mode change 100644 => 100755 tests/data/fail/ptest/run-ptest
tests/data/fail/ptest/run-ptest +3 -0
old mode 100644
new mode 100755
index e69de29..f4e5d0b
@@ -0,0 +1,3 @@
+#!/bin/bash
+
+exit 10
tests/utils.c +1 -1
index 132d98f..105e0c8 100644
@@ -234,7 +234,7 @@ END_TEST
 static void
 search_for_fail(const int rp, FILE *fp_stdout)
 {
-        const char *fail_str = "ERROR: Exit status is";
+        const char *fail_str = "ERROR: Exit status is 10";
         char line_buf[PRINT_PTEST_BUF_SIZE];
         int found_fail = 0;
         char *line = NULL;
utils.c +3 -0
index 43ab03b..d784736 100644
@@ -352,6 +352,9 @@ wait_child(pid_t pid, int timeout, int *fds, FILE **fps, int *timeouted)
 
 			clock_gettime(clock, &sentinel);
 		}
+
+		if (WIFEXITED(status))
+			status = WEXITSTATUS(status);
 	}
 
 	fflush(fps[0]);
Signature · 1 line
2.31.0
#2 lore raw

PATCH 3/4 utils.c: Use a thread to read from child

In reply to Anibal Limon

In order to handle large output add a thread for read from childs using a pipe and remove non-blocking option.

Modify bash unittest to output large data and cover this scenario.

[YOCTO #14220]

---
 Makefile                        |   2 +-
 tests/data/bash/ptest/run-ptest |   4 +-
 utils.c                         | 112 +++++++++++++++++++++-----------
 3 files changed, 78 insertions(+), 40 deletions(-)
Makefile +1 -1
index 3cca17b..1aa1830 100644
@@ -31,7 +31,7 @@ TEST_DATA=$(shell echo `pwd`/tests/data)
 all: $(SOURCES) $(EXECUTABLE)
 
 $(EXECUTABLE): $(OBJECTS)
-	$(CC) $(LDFLAGS) $(OBJECTS) -lutil -o $@
+	$(CC) $(LDFLAGS) $(OBJECTS) -pthread -lutil -o $@
 
 tests: $(TEST_SOURCES) $(TEST_EXECUTABLE)
 
tests/data/bash/ptest/run-ptest +3 -1
index 09f997f..f36e077 100755
@@ -1,4 +1,4 @@
-#!/bin/sh
+#!/bin/bash
 
 echo "bash"
 printf "Hello World!,stderr\n" >&2
@@ -7,3 +7,5 @@ printf "Hello World!,stderr2\n" >&2
 echo "bash3"
 echo "bash4"
 printf "Hello World!,stderr3\n" >&2
+
+echo {1..1000000}\\n
utils.c +74 -38

201 lines not on this page · raw message

Signature · 1 line
2.31.0
#3 lore raw

PATCH 4/4 utils.c: wait_child reimplement timeout using alarm

In reply to Anibal Limon

Since we are using threads to read from child, no complex logic is needed for handle timeouts use alarm(2).

[YOCTO #14220]

---
 utils.c | 60 ++++++++++++++++++++-------------------------------------
 1 file changed, 21 insertions(+), 39 deletions(-)
utils.c +21 -39
index 84cb570..1a3c90f 100644
@@ -57,6 +57,9 @@
 static struct {
 	int fds[2];
 	FILE *fps[2];
+
+	int timeouted;
+	pid_t pid;
 } _child_reader;
 
 static inline char *
@@ -335,45 +338,25 @@ run_child(char *run_ptest, int fd_stdout, int fd_stderr)
 	/* exit(1); not needed? */
 }
 
-static inline int
-wait_child(pid_t pid, int timeout, int *timeouted)
+static void
+timeout_child_handler(int signo)
 {
-	struct timespec sentinel;
-	clockid_t clock = CLOCK_MONOTONIC;
-	int looping = 1;
+	_child_reader.timeouted = 1;
+	kill(-_child_reader.pid, SIGKILL);
+}
 
+static inline int
+wait_child(pid_t pid, int timeout)
+{
 	int status = -1;
-	int waitflags;
-
-	if (clock_gettime(clock, &sentinel) == -1) {
-		clock = CLOCK_REALTIME;
-		clock_gettime(clock, &sentinel);
-	}
-
-	*timeouted = 0;
 
-	while (looping) {
-		waitflags = WNOHANG;
+	_child_reader.timeouted = 0;
+	_child_reader.pid = pid;
 
-		if (timeout >= 0) {
-			struct timespec time;
-
-			clock_gettime(clock, &time);
-			if ((time.tv_sec - sentinel.tv_sec) > timeout) {
-				*timeouted = 1;
-				kill(-pid, SIGKILL);
-				waitflags = 0;
-			}
-		}
-
-		if (waitpid(pid, &status, waitflags) == pid)
-			looping = 0;
-
-		clock_gettime(clock, &sentinel);
-
-		if (WIFEXITED(status))
-			status = WEXITSTATUS(status);
-	}
+	alarm(timeout);
+	waitpid(pid, &status, 0);
+	if (WIFEXITED(status))
+		status = WEXITSTATUS(status);
 
 	return status;
 }
@@ -438,7 +421,6 @@ run_ptests(struct ptest_list *head, const struct ptest_options opts,
 	pid_t child;
 	int pipefd_stdout[2];
 	int pipefd_stderr[2];
-	int timeouted;
 	time_t sttime, entime;
 	time_t duration;
 	int slave;
@@ -477,6 +459,7 @@ run_ptests(struct ptest_list *head, const struct ptest_options opts,
 			close(pipefd_stdout[1]);
 			break;
 		}
+		signal(SIGALRM, timeout_child_handler);
 
 		fprintf(fp, "START: %s\n", progname);
 		PTEST_LIST_ITERATE_START(head, p)
@@ -527,8 +510,7 @@ run_ptests(struct ptest_list *head, const struct ptest_options opts,
 				fprintf(fp, "BEGIN: %s\n", ptest_dir);
 
 
-				status = wait_child(child, opts.timeout, &timeouted);
-
+				status = wait_child(child, opts.timeout);
 
 				entime = time(NULL);
 				duration = entime - sttime;
@@ -538,11 +520,11 @@ run_ptests(struct ptest_list *head, const struct ptest_options opts,
 					rc += 1;
 				}
 				fprintf(fp, "DURATION: %d\n", (int) duration);
-				if (timeouted)
+				if (_child_reader.timeouted)
 					fprintf(fp, "TIMEOUT: %s\n", ptest_dir);
 
 				if (opts.xml_filename)
-					xml_add_case(xh, status, ptest_dir, timeouted, (int) duration);
+					xml_add_case(xh, status, ptest_dir, _child_reader.timeouted, (int) duration);
 
 				fprintf(fp, "END: %s\n", ptest_dir);
 				fprintf(fp, "%s\n", get_stime(stime, GET_STIME_BUF_SIZE, entime));
Signature · 1 line
2.31.0
#4 lore raw

Re: [ptest-runner][PATCH 4/4] utils.c: wait_child reimplement timeout using alarm

Quoted 2 lines · Aníbal Limón

Since we are using threads to read from child, no complex logic is needed for handle timeouts use alarm(2).

TY for the patches.

This would change its behaviour to timeout exactly at X seconds.

New behaviour: if test exceeds X seconds, gets killed.
Old behaviour: if test exceeds X seconds with no output, get killed. If output is detected X-1 seconds, the timeout timer resets. so a program could take 2X seconds and not get killed

Comment about the other patches:

We don't even need an extra thread because before the wait_for_child had to do 2 things:

* read a pipe and output that pipe to stdout (child_reader does that)
* check for timeout (sigalarm takes care of that now)

last thing, can you push this to a branch (let's say) master-next, so I can test it with a full ptest run.

yifan

#5 lore raw

Re: [ptest-runner][PATCH 4/4] utils.c: wait_child reimplement timeout using alarm

Quoted 12 lines · Yi Fan Yu
Quoted 2 lines · Aníbal Limón

Since we are using threads to read from child, no complex logic is needed for handle timeouts use alarm(2).

TY for the patches.

This would change its behaviour to timeout exactly at X seconds.

New behaviour: if test exceeds X seconds, gets killed.
Old behaviour: if test exceeds X seconds with no output, get killed. If output is detected X-1 seconds, the timeout timer resets.
so a program could take 2X seconds and not get killed

Yeah, thanks for remind me the old behavior, some years and I forget about it, I will fix it and put certain comments about that.

Quoted 7 lines

Comment about the other patches:

We don't even need an extra thread because before the wait_for_child had to do 2 things:

Quoted 8 lines

* read a pipe and output that pipe to stdout (child_reader does that)
* check for timeout (sigalarm takes care of that now)

last thing, can you push this to a branch (let's say) master-next, so I can test it with a full ptest run.

I pushed to dev branch,

http://git.yoctoproject.org/cgit/cgit.cgi/ptest-runner2/log/?h=dev

Regards,
Anibal

Quoted 3 lines

yifan