On Wed, Mar 18, 2020 at 06:32:15PM +0100, Michal Privoznik wrote:
When running a function in a forked child, so far the only thing
we could report is exit status of the child and the error
message. However, it may be beneficial to the caller to know the
actual error that happened in the child.
Signed-off-by: Michal Privoznik <mprivozn(a)redhat.com>
---
build-aux/syntax-check.mk | 2 +-
src/util/virprocess.c | 51 ++++++++++++++++++++++++++++++++++++---
tests/commandtest.c | 43 +++++++++++++++++++++++++++++++++
3 files changed, 91 insertions(+), 5 deletions(-)
diff --git a/tests/commandtest.c b/tests/commandtest.c
index a64aa9ad33..f4a2c67c05 100644
--- a/tests/commandtest.c
+++ b/tests/commandtest.c
@@ -1257,6 +1257,48 @@ static int test27(const void *unused G_GNUC_UNUSED)
}
+static int
+test28Callback(pid_t pid G_GNUC_UNUSED,
+ void *opaque G_GNUC_UNUSED)
+{
+ virReportSystemError(ENODATA, "%s", "some error message");
+ return -1;
+}
+
+
+static int
+test28(const void *unused G_GNUC_UNUSED)
+{
+ /* Not strictly a virCommand test, but this is the easiest place
+ * to test this lower-level interface. */
+ virErrorPtr err;
+
+ if (virProcessRunInFork(test28Callback, NULL) != -1) {
+ fprintf(stderr, "virProcessRunInFork did not fail\n");
+ return -1;
+ }
+
+ if (!(err = virGetLastError())) {
+ fprintf(stderr, "Expected error but got nothing\n");
+ return -1;
+ }
+
+ if (!(err->code == VIR_ERR_SYSTEM_ERROR &&
+ err->domain == 0 &&
+ STREQ(err->message, "some error message: No data available")
&&
+ err->level == VIR_ERR_ERROR &&
+ STREQ(err->str1, "%s") &&
+ STREQ(err->str2, "some error message: No data available")
&&
+ err->int1 == ENODATA &&
+ err->int2 == -1)) {
+ fprintf(stderr, "Unexpected error object\n");
+ return -1;
+ }
This new test fails on FreeBSD
$ VIR_TEST_DEBUG=1 VIR_TEST_RANGE=28 ./commandtest
TEST: commandtest
28) Command Exec test28 test ... Unexpected error
object
libvirt: error : some error message: Input/output error
FAILED
IIUC the problem here is that the STREQ check is assuming that the
ENODATA errno results in the string "No data available". The strings
are not standardized by POSIX AFAIK, so C libraries can use any reasonable
text for them. So this check is not portable.
Perhaps its enough to use STRPREFIX(err->str2, "some error message:") ?
Regards,
Daniel
--
|:
https://berrange.com -o-
https://www.flickr.com/photos/dberrange :|
|:
https://libvirt.org -o-
https://fstop138.berrange.com :|
|:
https://entangle-photo.org -o-
https://www.instagram.com/dberrange :|