From 8675dfe3cca82e66f9d9b7fc4e227713f7a942d5 Mon Sep 17 00:00:00 2001 From: Richard Huang Date: Wed, 8 Jan 2020 10:33:13 +0000 Subject: [PATCH] Fix random crash in TCT when stderr is closed but we try to write to it Change-Id: Ic09eba9f9f9c0cbfaf419d23bad5a937a770690d --- .../dali/dali-test-suite-utils/test-harness.cpp | 23 +++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/automated-tests/src/dali/dali-test-suite-utils/test-harness.cpp b/automated-tests/src/dali/dali-test-suite-utils/test-harness.cpp index c4a27d1..b42b9f9 100644 --- a/automated-tests/src/dali/dali-test-suite-utils/test-harness.cpp +++ b/automated-tests/src/dali/dali-test-suite-utils/test-harness.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017 Samsung Electronics Co., Ltd. + * Copyright (c) 2020 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,6 +23,7 @@ #include #include #include +#include namespace TestHarness { @@ -41,6 +42,20 @@ const char* basename(const char* path) return slash; } +void SuppressLogOutput() +{ + // Close stdout and stderr to suppress the log output + close(STDOUT_FILENO); // File descriptor number for stdout is 1 + close(STDERR_FILENO); // File descriptor number for stderr is 2 + + // The POSIX specification requires that /dev/null must be provided, + // The open function always chooses the lowest unused file descriptor + // It is sufficient for stdout to be writable. + open("/dev/null", O_WRONLY); // Redirect file descriptor number 1 (i.e. stdout) to /dev/null + // When stderr is opened it must be both readable and writable. + open("/dev/null", O_RDWR); // Redirect file descriptor number 2 (i.e. stderr) to /dev/null +} + int32_t RunTestCase( struct ::testcase_s& testCase ) { int32_t result = EXIT_STATUS_TESTCASE_FAILED; @@ -78,8 +93,7 @@ int32_t RunTestCaseInChildProcess( struct ::testcase_s& testCase, bool suppressO { if( suppressOutput ) { - close(STDOUT_FILENO); - close(STDERR_FILENO); + SuppressLogOutput(); } else { @@ -222,8 +236,7 @@ int32_t RunAllInParallel( const char* processName, ::testcase tc_array[], bool int32_t pid = fork(); if( pid == 0 ) // Child process { - close(STDOUT_FILENO); - close(STDERR_FILENO); + SuppressLogOutput(); exit( RunTestCase( tc_array[nextTestCase] ) ); } else if(pid == -1) -- 2.7.4