From f215e02bf85f68d3a6106c2a1f4f7f063f819064 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Thu, 11 Apr 2024 10:17:27 +0200 Subject: Adding upstream version 7.0.14-dfsg. Signed-off-by: Daniel Baumann --- src/VBox/ValidationKit/utils/Makefile.kmk | 78 + .../ValidationKit/utils/TestExecServ/Makefile.kmk | 87 + .../utils/TestExecServ/TestExecService.cpp | 4037 ++++++++++++ .../utils/TestExecServ/TestExecServiceInternal.h | 232 + .../utils/TestExecServ/TestExecServiceSerial.cpp | 417 ++ .../utils/TestExecServ/TestExecServiceTcp.cpp | 842 +++ .../utils/TestExecServ/linux/vboxtxs-nat.sh | 174 + .../utils/TestExecServ/linux/vboxtxs-runvm.desktop | 6 + .../utils/TestExecServ/linux/vboxtxs-runvm.sh | 208 + .../utils/TestExecServ/linux/vboxtxs.service | 18 + .../utils/TestExecServ/linux/vboxtxs.sh | 173 + .../utils/TestExecServ/solaris/vboxtxs-sol10.xml | 84 + .../utils/TestExecServ/solaris/vboxtxs.sh | 64 + .../utils/TestExecServ/solaris/vboxtxs.xml | 84 + .../utils/TestExecServ/vboxtxs-readme.txt | 145 + .../utils/TestExecServ/vboxtxs-runvm-readme.txt | 54 + .../utils/TestExecServ/win/vboxtxs-nat.cmd | 41 + .../utils/TestExecServ/win/vboxtxs-nat.reg | 13 + .../utils/TestExecServ/win/vboxtxs.cmd | 40 + .../utils/TestExecServ/win/vboxtxs.reg | 13 + .../utils/TestExecServ/win/vboxtxs.xml | 75 + src/VBox/ValidationKit/utils/audio/Makefile.kmk | 220 + .../ValidationKit/utils/audio/ntPlayToneWaveX.cpp | 226 + src/VBox/ValidationKit/utils/audio/readme.txt | 2 + src/VBox/ValidationKit/utils/audio/vkat.cpp | 1649 +++++ .../ValidationKit/utils/audio/vkatCmdGeneric.cpp | 1169 ++++ .../ValidationKit/utils/audio/vkatCmdSelfTest.cpp | 481 ++ src/VBox/ValidationKit/utils/audio/vkatCommon.cpp | 1760 +++++ .../ValidationKit/utils/audio/vkatDriverStack.cpp | 1621 +++++ src/VBox/ValidationKit/utils/audio/vkatInternal.h | 547 ++ .../ValidationKit/utils/clipboard/ClipUtil.cpp | 1793 +++++ .../ValidationKit/utils/clipboard/Makefile.kmk | 53 + src/VBox/ValidationKit/utils/cpu/Makefile.kmk | 84 + src/VBox/ValidationKit/utils/cpu/cidet-app.cpp | 1376 ++++ src/VBox/ValidationKit/utils/cpu/cidet-appA.asm | 319 + src/VBox/ValidationKit/utils/cpu/cidet-core.cpp | 2368 +++++++ src/VBox/ValidationKit/utils/cpu/cidet-instr-1.cpp | 297 + src/VBox/ValidationKit/utils/cpu/cidet.h | 1092 ++++ src/VBox/ValidationKit/utils/cpu/cidet.mac | 75 + .../ValidationKit/utils/cpu/cpu-alloc-all-mem.cpp | 223 + src/VBox/ValidationKit/utils/cpu/cpu-numa.cpp | 205 + .../ValidationKit/utils/cpu/exceptionsR3-asm.asm | 160 + src/VBox/ValidationKit/utils/cpu/exceptionsR3.cpp | 272 + src/VBox/ValidationKit/utils/cpu/rdtsc-asm.asm | 162 + src/VBox/ValidationKit/utils/cpu/rdtsc.cpp | 294 + src/VBox/ValidationKit/utils/cpu/xmmsaving-asm.asm | 162 + src/VBox/ValidationKit/utils/cpu/xmmsaving.cpp | 130 + src/VBox/ValidationKit/utils/dos/DosSleep.c | 67 + src/VBox/ValidationKit/utils/dos/DosVmOff.asm | 79 + src/VBox/ValidationKit/utils/dos/WinExit.asm | 95 + src/VBox/ValidationKit/utils/fs/FsPerf.cpp | 6834 ++++++++++++++++++++ src/VBox/ValidationKit/utils/fs/Makefile.kmk | 49 + src/VBox/ValidationKit/utils/misc/Makefile.kmk | 84 + .../ValidationKit/utils/misc/loadgenerator.cpp | 366 ++ .../ValidationKit/utils/misc/loadgeneratorR0.cpp | 101 + src/VBox/ValidationKit/utils/misc/vts_rm.cpp | 54 + src/VBox/ValidationKit/utils/misc/vts_tar.cpp | 54 + src/VBox/ValidationKit/utils/network/Makefile.kmk | 49 + src/VBox/ValidationKit/utils/network/NetPerf.cpp | 2008 ++++++ src/VBox/ValidationKit/utils/nt/Makefile.kmk | 63 + .../utils/nt/ntFlushVirtualMemory.cpp | 498 ++ src/VBox/ValidationKit/utils/nt/ntsetfreq.cpp | 161 + src/VBox/ValidationKit/utils/nt/nttimesources.cpp | 246 + src/VBox/ValidationKit/utils/serial/Makefile.kmk | 49 + src/VBox/ValidationKit/utils/serial/SerialTest.cpp | 1115 ++++ src/VBox/ValidationKit/utils/storage/IoPerf.cpp | 1405 ++++ src/VBox/ValidationKit/utils/storage/Makefile.kmk | 49 + src/VBox/ValidationKit/utils/usb/Makefile.kmk | 74 + src/VBox/ValidationKit/utils/usb/UsbTest.cpp | 667 ++ .../ValidationKit/utils/usb/UsbTestService.cpp | 1658 +++++ .../utils/usb/UsbTestServiceGadget.cpp | 211 + .../ValidationKit/utils/usb/UsbTestServiceGadget.h | 546 ++ .../utils/usb/UsbTestServiceGadgetCfg.cpp | 462 ++ .../utils/usb/UsbTestServiceGadgetClassTest.cpp | 470 ++ .../utils/usb/UsbTestServiceGadgetHost.cpp | 179 + .../utils/usb/UsbTestServiceGadgetHostInternal.h | 132 + .../utils/usb/UsbTestServiceGadgetHostUsbIp.cpp | 263 + .../utils/usb/UsbTestServiceGadgetInternal.h | 118 + .../utils/usb/UsbTestServiceInternal.h | 226 + .../utils/usb/UsbTestServicePlatform-linux.cpp | 448 ++ .../utils/usb/UsbTestServicePlatform.h | 105 + .../utils/usb/UsbTestServiceProtocol.cpp | 120 + .../utils/usb/UsbTestServiceProtocol.h | 373 ++ .../ValidationKit/utils/usb/UsbTestServiceTcp.cpp | 512 ++ 84 files changed, 43615 insertions(+) create mode 100644 src/VBox/ValidationKit/utils/Makefile.kmk create mode 100644 src/VBox/ValidationKit/utils/TestExecServ/Makefile.kmk create mode 100644 src/VBox/ValidationKit/utils/TestExecServ/TestExecService.cpp create mode 100644 src/VBox/ValidationKit/utils/TestExecServ/TestExecServiceInternal.h create mode 100644 src/VBox/ValidationKit/utils/TestExecServ/TestExecServiceSerial.cpp create mode 100644 src/VBox/ValidationKit/utils/TestExecServ/TestExecServiceTcp.cpp create mode 100755 src/VBox/ValidationKit/utils/TestExecServ/linux/vboxtxs-nat.sh create mode 100644 src/VBox/ValidationKit/utils/TestExecServ/linux/vboxtxs-runvm.desktop create mode 100755 src/VBox/ValidationKit/utils/TestExecServ/linux/vboxtxs-runvm.sh create mode 100644 src/VBox/ValidationKit/utils/TestExecServ/linux/vboxtxs.service create mode 100755 src/VBox/ValidationKit/utils/TestExecServ/linux/vboxtxs.sh create mode 100644 src/VBox/ValidationKit/utils/TestExecServ/solaris/vboxtxs-sol10.xml create mode 100755 src/VBox/ValidationKit/utils/TestExecServ/solaris/vboxtxs.sh create mode 100644 src/VBox/ValidationKit/utils/TestExecServ/solaris/vboxtxs.xml create mode 100644 src/VBox/ValidationKit/utils/TestExecServ/vboxtxs-readme.txt create mode 100644 src/VBox/ValidationKit/utils/TestExecServ/vboxtxs-runvm-readme.txt create mode 100644 src/VBox/ValidationKit/utils/TestExecServ/win/vboxtxs-nat.cmd create mode 100644 src/VBox/ValidationKit/utils/TestExecServ/win/vboxtxs-nat.reg create mode 100644 src/VBox/ValidationKit/utils/TestExecServ/win/vboxtxs.cmd create mode 100644 src/VBox/ValidationKit/utils/TestExecServ/win/vboxtxs.reg create mode 100644 src/VBox/ValidationKit/utils/TestExecServ/win/vboxtxs.xml create mode 100644 src/VBox/ValidationKit/utils/audio/Makefile.kmk create mode 100644 src/VBox/ValidationKit/utils/audio/ntPlayToneWaveX.cpp create mode 100644 src/VBox/ValidationKit/utils/audio/readme.txt create mode 100644 src/VBox/ValidationKit/utils/audio/vkat.cpp create mode 100644 src/VBox/ValidationKit/utils/audio/vkatCmdGeneric.cpp create mode 100644 src/VBox/ValidationKit/utils/audio/vkatCmdSelfTest.cpp create mode 100644 src/VBox/ValidationKit/utils/audio/vkatCommon.cpp create mode 100644 src/VBox/ValidationKit/utils/audio/vkatDriverStack.cpp create mode 100644 src/VBox/ValidationKit/utils/audio/vkatInternal.h create mode 100644 src/VBox/ValidationKit/utils/clipboard/ClipUtil.cpp create mode 100644 src/VBox/ValidationKit/utils/clipboard/Makefile.kmk create mode 100644 src/VBox/ValidationKit/utils/cpu/Makefile.kmk create mode 100644 src/VBox/ValidationKit/utils/cpu/cidet-app.cpp create mode 100644 src/VBox/ValidationKit/utils/cpu/cidet-appA.asm create mode 100644 src/VBox/ValidationKit/utils/cpu/cidet-core.cpp create mode 100644 src/VBox/ValidationKit/utils/cpu/cidet-instr-1.cpp create mode 100644 src/VBox/ValidationKit/utils/cpu/cidet.h create mode 100644 src/VBox/ValidationKit/utils/cpu/cidet.mac create mode 100644 src/VBox/ValidationKit/utils/cpu/cpu-alloc-all-mem.cpp create mode 100644 src/VBox/ValidationKit/utils/cpu/cpu-numa.cpp create mode 100644 src/VBox/ValidationKit/utils/cpu/exceptionsR3-asm.asm create mode 100644 src/VBox/ValidationKit/utils/cpu/exceptionsR3.cpp create mode 100644 src/VBox/ValidationKit/utils/cpu/rdtsc-asm.asm create mode 100644 src/VBox/ValidationKit/utils/cpu/rdtsc.cpp create mode 100644 src/VBox/ValidationKit/utils/cpu/xmmsaving-asm.asm create mode 100644 src/VBox/ValidationKit/utils/cpu/xmmsaving.cpp create mode 100644 src/VBox/ValidationKit/utils/dos/DosSleep.c create mode 100644 src/VBox/ValidationKit/utils/dos/DosVmOff.asm create mode 100644 src/VBox/ValidationKit/utils/dos/WinExit.asm create mode 100644 src/VBox/ValidationKit/utils/fs/FsPerf.cpp create mode 100644 src/VBox/ValidationKit/utils/fs/Makefile.kmk create mode 100644 src/VBox/ValidationKit/utils/misc/Makefile.kmk create mode 100644 src/VBox/ValidationKit/utils/misc/loadgenerator.cpp create mode 100644 src/VBox/ValidationKit/utils/misc/loadgeneratorR0.cpp create mode 100644 src/VBox/ValidationKit/utils/misc/vts_rm.cpp create mode 100644 src/VBox/ValidationKit/utils/misc/vts_tar.cpp create mode 100644 src/VBox/ValidationKit/utils/network/Makefile.kmk create mode 100644 src/VBox/ValidationKit/utils/network/NetPerf.cpp create mode 100644 src/VBox/ValidationKit/utils/nt/Makefile.kmk create mode 100644 src/VBox/ValidationKit/utils/nt/ntFlushVirtualMemory.cpp create mode 100644 src/VBox/ValidationKit/utils/nt/ntsetfreq.cpp create mode 100644 src/VBox/ValidationKit/utils/nt/nttimesources.cpp create mode 100644 src/VBox/ValidationKit/utils/serial/Makefile.kmk create mode 100644 src/VBox/ValidationKit/utils/serial/SerialTest.cpp create mode 100644 src/VBox/ValidationKit/utils/storage/IoPerf.cpp create mode 100644 src/VBox/ValidationKit/utils/storage/Makefile.kmk create mode 100644 src/VBox/ValidationKit/utils/usb/Makefile.kmk create mode 100644 src/VBox/ValidationKit/utils/usb/UsbTest.cpp create mode 100644 src/VBox/ValidationKit/utils/usb/UsbTestService.cpp create mode 100644 src/VBox/ValidationKit/utils/usb/UsbTestServiceGadget.cpp create mode 100644 src/VBox/ValidationKit/utils/usb/UsbTestServiceGadget.h create mode 100644 src/VBox/ValidationKit/utils/usb/UsbTestServiceGadgetCfg.cpp create mode 100644 src/VBox/ValidationKit/utils/usb/UsbTestServiceGadgetClassTest.cpp create mode 100644 src/VBox/ValidationKit/utils/usb/UsbTestServiceGadgetHost.cpp create mode 100644 src/VBox/ValidationKit/utils/usb/UsbTestServiceGadgetHostInternal.h create mode 100644 src/VBox/ValidationKit/utils/usb/UsbTestServiceGadgetHostUsbIp.cpp create mode 100644 src/VBox/ValidationKit/utils/usb/UsbTestServiceGadgetInternal.h create mode 100644 src/VBox/ValidationKit/utils/usb/UsbTestServiceInternal.h create mode 100644 src/VBox/ValidationKit/utils/usb/UsbTestServicePlatform-linux.cpp create mode 100644 src/VBox/ValidationKit/utils/usb/UsbTestServicePlatform.h create mode 100644 src/VBox/ValidationKit/utils/usb/UsbTestServiceProtocol.cpp create mode 100644 src/VBox/ValidationKit/utils/usb/UsbTestServiceProtocol.h create mode 100644 src/VBox/ValidationKit/utils/usb/UsbTestServiceTcp.cpp (limited to 'src/VBox/ValidationKit/utils') diff --git a/src/VBox/ValidationKit/utils/Makefile.kmk b/src/VBox/ValidationKit/utils/Makefile.kmk new file mode 100644 index 00000000..b527a640 --- /dev/null +++ b/src/VBox/ValidationKit/utils/Makefile.kmk @@ -0,0 +1,78 @@ +# $Id: Makefile.kmk $ +## @file +# VirtualBox Validation Kit - Utilities. +# + +# +# Copyright (C) 2010-2023 Oracle and/or its affiliates. +# +# This file is part of VirtualBox base platform packages, as +# available from https://www.virtualbox.org. +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation, in version 3 of the +# License. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, see . +# +# The contents of this file may alternatively be used under the terms +# of the Common Development and Distribution License Version 1.0 +# (CDDL), a copy of it is provided in the "COPYING.CDDL" file included +# in the VirtualBox distribution, in which case the provisions of the +# CDDL are applicable instead of those of the GPL. +# +# You may elect to license modified versions of this file under the +# terms and conditions of either the GPL or the CDDL or both. +# +# SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0 +# + +SUB_DEPTH = ../../../.. +include $(KBUILD_PATH)/subheader.kmk + +# +# Include sub-makefiles. +# +include $(PATH_SUB_CURRENT)/TestExecServ/Makefile.kmk +include $(PATH_SUB_CURRENT)/audio/Makefile.kmk +ifeq ($(KBUILD_TARGET),win) + include $(PATH_SUB_CURRENT)/clipboard/Makefile.kmk +endif +include $(PATH_SUB_CURRENT)/cpu/Makefile.kmk +include $(PATH_SUB_CURRENT)/fs/Makefile.kmk +include $(PATH_SUB_CURRENT)/misc/Makefile.kmk +include $(PATH_SUB_CURRENT)/network/Makefile.kmk +ifeq ($(KBUILD_TARGET),win) + include $(PATH_SUB_CURRENT)/nt/Makefile.kmk +endif +include $(PATH_SUB_CURRENT)/serial/Makefile.kmk +include $(PATH_SUB_CURRENT)/storage/Makefile.kmk +ifeq ($(KBUILD_TARGET),linux) + include $(PATH_SUB_CURRENT)/usb/Makefile.kmk +endif + +# +# On OS/2 the binaries requires the libc DLLs +# (no official static linking support). +# +INSTALLS.os2 += ValidationKitOs2LibC +ValidationKitOs2LibC_TEMPLATE = VBoxValidationKitR3 +ValidationKitOs2LibC_SOURCES = \ + $(KBUILD_BIN_PATH)/libc06.dll \ + $(KBUILD_BIN_PATH)/libc061.dll \ + $(KBUILD_BIN_PATH)/libc062.dll \ + $(KBUILD_BIN_PATH)/libc063.dll \ + $(KBUILD_BIN_PATH)/libc064.dll \ + $(KBUILD_BIN_PATH)/libc065.dll \ + $(KBUILD_BIN_PATH)/libc066.dll + + +include $(FILE_KBUILD_SUB_FOOTER) + diff --git a/src/VBox/ValidationKit/utils/TestExecServ/Makefile.kmk b/src/VBox/ValidationKit/utils/TestExecServ/Makefile.kmk new file mode 100644 index 00000000..7fbf9c47 --- /dev/null +++ b/src/VBox/ValidationKit/utils/TestExecServ/Makefile.kmk @@ -0,0 +1,87 @@ +# $Id: Makefile.kmk $ +## @file +# VirtualBox Validation Kit - The Basic Remote Execution Service. +# + +# +# Copyright (C) 2010-2023 Oracle and/or its affiliates. +# +# This file is part of VirtualBox base platform packages, as +# available from https://www.virtualbox.org. +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation, in version 3 of the +# License. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, see . +# +# The contents of this file may alternatively be used under the terms +# of the Common Development and Distribution License Version 1.0 +# (CDDL), a copy of it is provided in the "COPYING.CDDL" file included +# in the VirtualBox distribution, in which case the provisions of the +# CDDL are applicable instead of those of the GPL. +# +# You may elect to license modified versions of this file under the +# terms and conditions of either the GPL or the CDDL or both. +# +# SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0 +# + +SUB_DEPTH = ../../../../.. +include $(KBUILD_PATH)/subheader.kmk + + +PROGRAMS += TestExecService +TestExecService_TEMPLATE = VBoxValidationKitR3 +ifdef VBOX_WITH_AUTOMATIC_DEFS_QUOTING + TestExecService_DEFS = \ + KBUILD_TARGET="$(KBUILD_TARGET)" \ + KBUILD_TARGET_ARCH="$(KBUILD_TARGET_ARCH)" +else + TestExecService_DEFS = \ + KBUILD_TARGET=\"$(KBUILD_TARGET)\" \ + KBUILD_TARGET_ARCH=\"$(KBUILD_TARGET_ARCH)\" +endif +TestExecService_SOURCES = \ + TestExecService.cpp \ + TestExecServiceTcp.cpp + +ifn1of ($(KBUILD_TARGET), os2) + TestExecService_SOURCES += \ + TestExecServiceSerial.cpp +endif + +INSTALLS += TestExecServiceFiles +TestExecServiceFiles_TEMPLATE = VBoxValidationKitR3 +TestExecServiceFiles_INST = $(INST_VALIDATIONKIT) +TestExecServiceFiles_SOURCES := \ + vboxtxs-readme.txt + +TestExecServiceFiles_EXEC_SOURCES.linux := \ + $(PATH_SUB_CURRENT)/linux/vboxtxs.sh=>linux/vboxtxs \ + $(PATH_SUB_CURRENT)/linux/vboxtxs.service=>linux/vboxtxs.service \ + $(PATH_SUB_CURRENT)/linux/vboxtxs-nat.sh=>linux/vboxtxs-nat + +TestExecServiceFiles_SOURCES.solaris := \ + $(PATH_SUB_CURRENT)/solaris/vboxtxs.xml=>solaris/vboxtxs.xml \ + $(PATH_SUB_CURRENT)/solaris/vboxtxs-sol10.xml=>solaris/vboxtxs-sol10.xml +TestExecServiceFiles_EXEC_SOURCES.solaris := \ + $(PATH_SUB_CURRENT)/solaris/vboxtxs.sh=>solaris/vboxtxs.sh + +TestExecServiceFiles_SOURCES.win := \ + $(PATH_SUB_CURRENT)/win/vboxtxs.reg=>win/vboxtxs.reg \ + $(PATH_SUB_CURRENT)/win/vboxtxs-nat.reg=>win/vboxtxs-nat.reg +TestExecServiceFiles_EXEC_SOURCES.win := \ + $(PATH_SUB_CURRENT)/win/vboxtxs.cmd=>win/vboxtxs.cmd \ + $(PATH_SUB_CURRENT)/win/vboxtxs-nat.cmd=>win/vboxtxs-nat.cmd + + +include $(FILE_KBUILD_SUB_FOOTER) + diff --git a/src/VBox/ValidationKit/utils/TestExecServ/TestExecService.cpp b/src/VBox/ValidationKit/utils/TestExecServ/TestExecService.cpp new file mode 100644 index 00000000..002d746b --- /dev/null +++ b/src/VBox/ValidationKit/utils/TestExecServ/TestExecService.cpp @@ -0,0 +1,4037 @@ +/* $Id: TestExecService.cpp $ */ +/** @file + * TestExecServ - Basic Remote Execution Service. + */ + +/* + * Copyright (C) 2010-2023 Oracle and/or its affiliates. + * + * This file is part of VirtualBox base platform packages, as + * available from https://www.virtualbox.org. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, in version 3 of the + * License. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + * + * The contents of this file may alternatively be used under the terms + * of the Common Development and Distribution License Version 1.0 + * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included + * in the VirtualBox distribution, in which case the provisions of the + * CDDL are applicable instead of those of the GPL. + * + * You may elect to license modified versions of this file under the + * terms and conditions of either the GPL or the CDDL or both. + * + * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0 + */ + + +/********************************************************************************************************************************* +* Header Files * +*********************************************************************************************************************************/ +#define LOG_GROUP RTLOGGROUP_DEFAULT +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include "product-generated.h" + +#include +#include + +#include "product-generated.h" +#include "TestExecServiceInternal.h" + + + +/********************************************************************************************************************************* +* Structures and Typedefs * +*********************************************************************************************************************************/ +/** + * Handle IDs used by txsDoExec for the poll set. + */ +typedef enum TXSEXECHNDID +{ + TXSEXECHNDID_STDIN = 0, + TXSEXECHNDID_STDOUT, + TXSEXECHNDID_STDERR, + TXSEXECHNDID_TESTPIPE, + TXSEXECHNDID_STDIN_WRITABLE, + TXSEXECHNDID_TRANSPORT, + TXSEXECHNDID_THREAD +} TXSEXECHNDID; + + +/** + * For buffering process input supplied by the client. + */ +typedef struct TXSEXECSTDINBUF +{ + /** The mount of buffered data. */ + size_t cb; + /** The current data offset. */ + size_t off; + /** The data buffer. */ + char *pch; + /** The amount of allocated buffer space. */ + size_t cbAllocated; + /** Send further input into the bit bucket (stdin is dead). */ + bool fBitBucket; + /** The CRC-32 for standard input (received part). */ + uint32_t uCrc32; +} TXSEXECSTDINBUF; +/** Pointer to a standard input buffer. */ +typedef TXSEXECSTDINBUF *PTXSEXECSTDINBUF; + +/** + * TXS child process info. + */ +typedef struct TXSEXEC +{ + PCTXSPKTHDR pPktHdr; + RTMSINTERVAL cMsTimeout; + int rcReplySend; + + RTPOLLSET hPollSet; + RTPIPE hStdInW; + RTPIPE hStdOutR; + RTPIPE hStdErrR; + RTPIPE hTestPipeR; + RTPIPE hWakeUpPipeR; + RTTHREAD hThreadWaiter; + + /** @name For the setup phase + * @{ */ + struct StdPipe + { + RTHANDLE hChild; + PRTHANDLE phChild; + } StdIn, + StdOut, + StdErr; + RTPIPE hTestPipeW; + RTENV hEnv; + /** @} */ + + /** For serializating some access. */ + RTCRITSECT CritSect; + /** @name Members protected by the critical section. + * @{ */ + RTPROCESS hProcess; + /** The process status. Only valid when fProcessAlive is cleared. */ + RTPROCSTATUS ProcessStatus; + /** Set when the process is alive, clear when dead. */ + bool volatile fProcessAlive; + /** The end of the pipe that hThreadWaiter writes to. */ + RTPIPE hWakeUpPipeW; + /** @} */ +} TXSEXEC; +/** Pointer to a the TXS child process info. */ +typedef TXSEXEC *PTXSEXEC; + + +/********************************************************************************************************************************* +* Global Variables * +*********************************************************************************************************************************/ +/** + * Transport layers. + */ +static const PCTXSTRANSPORT g_apTransports[] = +{ + &g_TcpTransport, +#ifndef RT_OS_OS2 + &g_SerialTransport, +#endif + //&g_FileSysTransport, + //&g_GuestPropTransport, + //&g_TestDevTransport, +}; + +/** The release logger. */ +static PRTLOGGER g_pRelLogger; +/** The select transport layer. */ +static PCTXSTRANSPORT g_pTransport; +/** The scratch path. */ +static char g_szScratchPath[RTPATH_MAX]; +/** The default scratch path. */ +static char g_szDefScratchPath[RTPATH_MAX]; +/** The CD/DVD-ROM path. */ +static char g_szCdRomPath[RTPATH_MAX]; +/** The default CD/DVD-ROM path. */ +static char g_szDefCdRomPath[RTPATH_MAX]; +/** The directory containing the TXS executable. */ +static char g_szTxsDir[RTPATH_MAX]; +/** The current working directory for TXS (doesn't change). */ +static char g_szCwd[RTPATH_MAX]; +/** The operating system short name. */ +static char g_szOsShortName[16]; +/** The CPU architecture short name. */ +static char g_szArchShortName[16]; +/** The combined "OS.arch" name. */ +static char g_szOsDotArchShortName[32]; +/** The combined "OS/arch" name. */ +static char g_szOsSlashArchShortName[32]; +/** The executable suffix. */ +static char g_szExeSuff[8]; +/** The shell script suffix. */ +static char g_szScriptSuff[8]; +/** UUID identifying this TXS instance. This can be used to see if TXS + * has been restarted or not. */ +static RTUUID g_InstanceUuid; +/** Whether to display the output of the child process or not. */ +static bool g_fDisplayOutput = true; +/** Whether to terminate or not. + * @todo implement signals and stuff. */ +static bool volatile g_fTerminate = false; +/** Verbosity level. */ +uint32_t g_cVerbose = 1; + + +/** + * Calculates the checksum value, zero any padding space and send the packet. + * + * @returns IPRT status code. + * @param pPkt The packet to send. Must point to a correctly + * aligned buffer. + */ +static int txsSendPkt(PTXSPKTHDR pPkt) +{ + Assert(pPkt->cb >= sizeof(*pPkt)); + pPkt->uCrc32 = RTCrc32(pPkt->achOpcode, pPkt->cb - RT_UOFFSETOF(TXSPKTHDR, achOpcode)); + if (pPkt->cb != RT_ALIGN_32(pPkt->cb, TXSPKT_ALIGNMENT)) + memset((uint8_t *)pPkt + pPkt->cb, '\0', RT_ALIGN_32(pPkt->cb, TXSPKT_ALIGNMENT) - pPkt->cb); + + Log(("txsSendPkt: cb=%#x opcode=%.8s\n", pPkt->cb, pPkt->achOpcode)); + Log2(("%.*Rhxd\n", RT_MIN(pPkt->cb, 256), pPkt)); + int rc = g_pTransport->pfnSendPkt(pPkt); + while (RT_UNLIKELY(rc == VERR_INTERRUPTED) && !g_fTerminate) + rc = g_pTransport->pfnSendPkt(pPkt); + if (RT_FAILURE(rc)) + Log(("txsSendPkt: rc=%Rrc\n", rc)); + + return rc; +} + +/** + * Sends a babble reply and disconnects the client (if applicable). + * + * @param pszOpcode The BABBLE opcode. + */ +static void txsReplyBabble(const char *pszOpcode) +{ + TXSPKTHDR Reply; + Reply.cb = sizeof(Reply); + Reply.uCrc32 = 0; + memcpy(Reply.achOpcode, pszOpcode, sizeof(Reply.achOpcode)); + + g_pTransport->pfnBabble(&Reply, 20*1000); +} + +/** + * Receive and validate a packet. + * + * Will send bable responses to malformed packets that results in a error status + * code. + * + * @returns IPRT status code. + * @param ppPktHdr Where to return the packet on success. Free + * with RTMemFree. + * @param fAutoRetryOnFailure Whether to retry on error. + */ +static int txsRecvPkt(PPTXSPKTHDR ppPktHdr, bool fAutoRetryOnFailure) +{ + for (;;) + { + PTXSPKTHDR pPktHdr; + int rc = g_pTransport->pfnRecvPkt(&pPktHdr); + if (RT_SUCCESS(rc)) + { + /* validate the packet. */ + if ( pPktHdr->cb >= sizeof(TXSPKTHDR) + && pPktHdr->cb < TXSPKT_MAX_SIZE) + { + Log2(("txsRecvPkt: pPktHdr=%p cb=%#x crc32=%#x opcode=%.8s\n" + "%.*Rhxd\n", + pPktHdr, pPktHdr->cb, pPktHdr->uCrc32, pPktHdr->achOpcode, RT_MIN(pPktHdr->cb, 256), pPktHdr)); + uint32_t uCrc32Calc = pPktHdr->uCrc32 != 0 + ? RTCrc32(&pPktHdr->achOpcode[0], pPktHdr->cb - RT_UOFFSETOF(TXSPKTHDR, achOpcode)) + : 0; + if (pPktHdr->uCrc32 == uCrc32Calc) + { + AssertCompileMemberSize(TXSPKTHDR, achOpcode, 8); + if ( RT_C_IS_UPPER(pPktHdr->achOpcode[0]) + && RT_C_IS_UPPER(pPktHdr->achOpcode[1]) + && (RT_C_IS_UPPER(pPktHdr->achOpcode[2]) || pPktHdr->achOpcode[2] == ' ') + && (RT_C_IS_PRINT(pPktHdr->achOpcode[3]) || pPktHdr->achOpcode[3] == ' ') + && (RT_C_IS_PRINT(pPktHdr->achOpcode[4]) || pPktHdr->achOpcode[4] == ' ') + && (RT_C_IS_PRINT(pPktHdr->achOpcode[5]) || pPktHdr->achOpcode[5] == ' ') + && (RT_C_IS_PRINT(pPktHdr->achOpcode[6]) || pPktHdr->achOpcode[6] == ' ') + && (RT_C_IS_PRINT(pPktHdr->achOpcode[7]) || pPktHdr->achOpcode[7] == ' ') + ) + { + Log(("txsRecvPkt: cb=%#x opcode=%.8s\n", pPktHdr->cb, pPktHdr->achOpcode)); + *ppPktHdr = pPktHdr; + return rc; + } + + rc = VERR_IO_BAD_COMMAND; + } + else + { + Log(("txsRecvPkt: cb=%#x opcode=%.8s crc32=%#x actual=%#x\n", + pPktHdr->cb, pPktHdr->achOpcode, pPktHdr->uCrc32, uCrc32Calc)); + rc = VERR_IO_CRC; + } + } + else + rc = VERR_IO_BAD_LENGTH; + + /* Send babble reply and disconnect the client if the transport is + connection oriented. */ + if (rc == VERR_IO_BAD_LENGTH) + txsReplyBabble("BABBLE L"); + else if (rc == VERR_IO_CRC) + txsReplyBabble("BABBLE C"); + else if (rc == VERR_IO_BAD_COMMAND) + txsReplyBabble("BABBLE O"); + else + txsReplyBabble("BABBLE "); + RTMemFree(pPktHdr); + } + + /* Try again or return failure? */ + if ( g_fTerminate + || rc != VERR_INTERRUPTED + || !fAutoRetryOnFailure + ) + { + Log(("txsRecvPkt: rc=%Rrc\n", rc)); + return rc; + } + } +} + +/** + * Make a simple reply, only status opcode. + * + * @returns IPRT status code of the send. + * @param pReply The reply packet. + * @param pszOpcode The status opcode. Exactly 8 chars long, padd + * with space. + * @param cbExtra Bytes in addition to the header. + */ +static int txsReplyInternal(PTXSPKTHDR pReply, const char *pszOpcode, size_t cbExtra) +{ + /* copy the opcode, don't be too strict in case of a padding screw up. */ + size_t cchOpcode = strlen(pszOpcode); + if (RT_LIKELY(cchOpcode == sizeof(pReply->achOpcode))) + memcpy(pReply->achOpcode, pszOpcode, sizeof(pReply->achOpcode)); + else + { + Assert(cchOpcode == sizeof(pReply->achOpcode)); + while (cchOpcode > 0 && pszOpcode[cchOpcode - 1] == ' ') + cchOpcode--; + AssertMsgReturn(cchOpcode < sizeof(pReply->achOpcode), ("%d/'%.8s'\n", cchOpcode, pszOpcode), VERR_INTERNAL_ERROR_4); + memcpy(pReply->achOpcode, pszOpcode, cchOpcode); + memset(&pReply->achOpcode[cchOpcode], ' ', sizeof(pReply->achOpcode) - cchOpcode); + } + + pReply->cb = (uint32_t)sizeof(TXSPKTHDR) + (uint32_t)cbExtra; + pReply->uCrc32 = 0; /* (txsSendPkt sets it) */ + + return txsSendPkt(pReply); +} + +/** + * Make a simple reply, only status opcode. + * + * @returns IPRT status code of the send. + * @param pPktHdr The original packet (for future use). + * @param pszOpcode The status opcode. Exactly 8 chars long, padd + * with space. + */ +static int txsReplySimple(PCTXSPKTHDR pPktHdr, const char *pszOpcode) +{ + TXSPKTHDR Pkt; + NOREF(pPktHdr); + return txsReplyInternal(&Pkt, pszOpcode, 0); +} + +/** + * Acknowledges a packet with success. + * + * @returns IPRT status code of the send. + * @param pPktHdr The original packet (for future use). + */ +static int txsReplyAck(PCTXSPKTHDR pPktHdr) +{ + return txsReplySimple(pPktHdr, "ACK "); +} + +/** + * Replies with a failure. + * + * @returns IPRT status code of the send. + * @param pPktHdr The original packet (for future use). + * @param pszOpcode The status opcode. Exactly 8 chars long, padd + * with space. + * @param pszDetailFmt Longer description of the problem (format + * string). + * @param va Format arguments. + */ +static int txsReplyFailureV(PCTXSPKTHDR pPktHdr, const char *pszOpcode, const char *pszDetailFmt, va_list va) +{ + NOREF(pPktHdr); + union + { + TXSPKTHDR Hdr; + char ach[256]; + } uPkt; + + size_t cchDetail = RTStrPrintfV(&uPkt.ach[sizeof(TXSPKTHDR)], + sizeof(uPkt) - sizeof(TXSPKTHDR), + pszDetailFmt, va); + return txsReplyInternal(&uPkt.Hdr, pszOpcode, cchDetail + 1); +} + +/** + * Replies with a failure. + * + * @returns IPRT status code of the send. + * @param pPktHdr The original packet (for future use). + * @param pszOpcode The status opcode. Exactly 8 chars long, padd + * with space. + * @param pszDetailFmt Longer description of the problem (format + * string). + * @param ... Format arguments. + */ +static int txsReplyFailure(PCTXSPKTHDR pPktHdr, const char *pszOpcode, const char *pszDetailFmt, ...) +{ + va_list va; + va_start(va, pszDetailFmt); + int rc = txsReplyFailureV(pPktHdr, pszOpcode, pszDetailFmt, va); + va_end(va); + return rc; +} + +/** + * Replies according to the return code. + * + * @returns IPRT status code of the send. + * @param pPktHdr The packet to reply to. + * @param rcOperation The status code to report. + * @param pszOperationFmt The operation that failed. Typically giving the + * function call with important arguments. + * @param ... Arguments to the format string. + */ +static int txsReplyRC(PCTXSPKTHDR pPktHdr, int rcOperation, const char *pszOperationFmt, ...) +{ + if (RT_SUCCESS(rcOperation)) + return txsReplyAck(pPktHdr); + + char szOperation[128]; + va_list va; + va_start(va, pszOperationFmt); + RTStrPrintfV(szOperation, sizeof(szOperation), pszOperationFmt, va); + va_end(va); + + return txsReplyFailure(pPktHdr, "FAILED ", "%s failed with rc=%Rrc (opcode '%.8s')", + szOperation, rcOperation, pPktHdr->achOpcode); +} + +/** + * Signal a bad packet minum size. + * + * @returns IPRT status code of the send. + * @param pPktHdr The packet to reply to. + * @param cbMin The minimum size. + */ +static int txsReplyBadMinSize(PCTXSPKTHDR pPktHdr, size_t cbMin) +{ + return txsReplyFailure(pPktHdr, "BAD SIZE", "Expected at least %zu bytes, got %u (opcode '%.8s')", + cbMin, pPktHdr->cb, pPktHdr->achOpcode); +} + +/** + * Signal a bad packet exact size. + * + * @returns IPRT status code of the send. + * @param pPktHdr The packet to reply to. + * @param cb The wanted size. + */ +static int txsReplyBadSize(PCTXSPKTHDR pPktHdr, size_t cb) +{ + return txsReplyFailure(pPktHdr, "BAD SIZE", "Expected at %zu bytes, got %u (opcode '%.8s')", + cb, pPktHdr->cb, pPktHdr->achOpcode); +} + +/** + * Deals with a command that isn't implemented yet. + * @returns IPRT status code of the send. + * @param pPktHdr The packet which opcode isn't implemented. + */ +static int txsReplyNotImplemented(PCTXSPKTHDR pPktHdr) +{ + return txsReplyFailure(pPktHdr, "NOT IMPL", "Opcode '%.8s' is not implemented", pPktHdr->achOpcode); +} + +/** + * Deals with a unknown command. + * @returns IPRT status code of the send. + * @param pPktHdr The packet to reply to. + */ +static int txsReplyUnknown(PCTXSPKTHDR pPktHdr) +{ + return txsReplyFailure(pPktHdr, "UNKNOWN ", "Opcode '%.8s' is not known", pPktHdr->achOpcode); +} + +/** + * Replaces a variable with its value. + * + * @returns VINF_SUCCESS or VERR_NO_STR_MEMORY. + * @param ppszNew In/Out. + * @param pcchNew In/Out. (Messed up on failure.) + * @param offVar Variable offset. + * @param cchVar Variable length. + * @param pszValue The value. + * @param cchValue Value length. + */ +static int txsReplaceStringVariable(char **ppszNew, size_t *pcchNew, size_t offVar, size_t cchVar, + const char *pszValue, size_t cchValue) +{ + size_t const cchAfter = *pcchNew - offVar - cchVar; + if (cchVar < cchValue) + { + *pcchNew += cchValue - cchVar; + int rc = RTStrRealloc(ppszNew, *pcchNew + 1); + if (RT_FAILURE(rc)) + return rc; + } + + char *pszNew = *ppszNew; + memmove(&pszNew[offVar + cchValue], &pszNew[offVar + cchVar], cchAfter + 1); + memcpy(&pszNew[offVar], pszValue, cchValue); + return VINF_SUCCESS; +} + +/** + * Replace the variables found in the source string, returning a new string that + * lives on the string heap. + * + * @returns Boolean success indicator. Will reply to the client with all the + * gory detail on failure. + * @param pPktHdr The packet the string relates to. For replying + * on error. + * @param pszSrc The source string. + * @param ppszNew Where to return the new string. + * @param prcSend Where to return the status code of the send on + * failure. + */ +static int txsReplaceStringVariables(PCTXSPKTHDR pPktHdr, const char *pszSrc, char **ppszNew, int *prcSend) +{ + /* Lazy approach that employs memmove. */ + size_t cchNew = strlen(pszSrc); + char *pszNew = RTStrDup(pszSrc); + char *pszDollar = pszNew; + while (pszDollar && (pszDollar = strchr(pszDollar, '$')) != NULL) + { + if (pszDollar[1] == '{') + { + char *pszEnd = strchr(&pszDollar[2], '}'); + if (pszEnd) + { +#define IF_VARIABLE_DO(pszDollar, szVarExpr, pszValue) \ + if ( cchVar == sizeof(szVarExpr) - 1 \ + && !memcmp(pszDollar, szVarExpr, sizeof(szVarExpr) - 1) ) \ + { \ + size_t const cchValue = strlen(pszValue); \ + rc = txsReplaceStringVariable(&pszNew, &cchNew, offDollar, \ + sizeof(szVarExpr) - 1, pszValue, cchValue); \ + offDollar += cchValue; \ + } + int rc; + size_t const cchVar = pszEnd - pszDollar + 1; /* includes "${}" */ + size_t offDollar = pszDollar - pszNew; + IF_VARIABLE_DO(pszDollar, "${CDROM}", g_szCdRomPath) + else IF_VARIABLE_DO(pszDollar, "${SCRATCH}", g_szScratchPath) + else IF_VARIABLE_DO(pszDollar, "${ARCH}", g_szArchShortName) + else IF_VARIABLE_DO(pszDollar, "${OS}", g_szOsShortName) + else IF_VARIABLE_DO(pszDollar, "${OS.ARCH}", g_szOsDotArchShortName) + else IF_VARIABLE_DO(pszDollar, "${OS/ARCH}", g_szOsSlashArchShortName) + else IF_VARIABLE_DO(pszDollar, "${EXESUFF}", g_szExeSuff) + else IF_VARIABLE_DO(pszDollar, "${SCRIPTSUFF}", g_szScriptSuff) + else IF_VARIABLE_DO(pszDollar, "${TXSDIR}", g_szTxsDir) + else IF_VARIABLE_DO(pszDollar, "${CWD}", g_szCwd) + else if ( cchVar >= sizeof("${env.") + 1 + && memcmp(pszDollar, RT_STR_TUPLE("${env.")) == 0) + { + const char *pszEnvVar = pszDollar + 6; + size_t cchValue = 0; + char szValue[RTPATH_MAX]; + *pszEnd = '\0'; + rc = RTEnvGetEx(RTENV_DEFAULT, pszEnvVar, szValue, sizeof(szValue), &cchValue); + if (RT_SUCCESS(rc)) + { + *pszEnd = '}'; + rc = txsReplaceStringVariable(&pszNew, &cchNew, offDollar, cchVar, szValue, cchValue); + offDollar += cchValue; + } + else + { + if (rc == VERR_ENV_VAR_NOT_FOUND) + *prcSend = txsReplyFailure(pPktHdr, "UNKN VAR", "Environment variable '%s' encountered in '%s'", + pszEnvVar, pszSrc); + else + *prcSend = txsReplyFailure(pPktHdr, "FAILDENV", + "RTEnvGetEx(,'%s',,,) failed with %Rrc (opcode '%.8s')", + pszEnvVar, rc, pPktHdr->achOpcode); + RTStrFree(pszNew); + *ppszNew = NULL; + return false; + } + } + else + { + RTStrFree(pszNew); + *prcSend = txsReplyFailure(pPktHdr, "UNKN VAR", "Unknown variable '%.*s' encountered in '%s'", + cchVar, pszDollar, pszSrc); + *ppszNew = NULL; + return false; + } + pszDollar = &pszNew[offDollar]; + + if (RT_FAILURE(rc)) + { + RTStrFree(pszNew); + *prcSend = txsReplyRC(pPktHdr, rc, "RTStrRealloc"); + *ppszNew = NULL; + return false; + } +#undef IF_VARIABLE_DO + } + } + /* Undo dollar escape sequences: $$ -> $ */ + else if (pszDollar[1] == '$') + { + size_t cchLeft = cchNew - (&pszDollar[1] - pszNew); + memmove(pszDollar, &pszDollar[1], cchLeft); + pszDollar[cchLeft] = '\0'; + cchNew -= 1; + } + else /* No match, move to next char to avoid endless looping. */ + pszDollar++; + } + + *ppszNew = pszNew; + *prcSend = VINF_SUCCESS; + return true; +} + +/** + * Checks if the string is valid and returns the expanded version. + * + * @returns true if valid, false if invalid. + * @param pPktHdr The packet being unpacked. + * @param pszArgName The argument name. + * @param psz Pointer to the string within pPktHdr. + * @param ppszExp Where to return the expanded string. Must be + * freed by calling RTStrFree(). + * @param ppszNext Where to return the pointer to the next field. + * If NULL, then we assume this string is at the + * end of the packet and will make sure it has the + * advertised length. + * @param prcSend Where to return the status code of the send on + * failure. + */ +static bool txsIsStringValid(PCTXSPKTHDR pPktHdr, const char *pszArgName, const char *psz, + char **ppszExp, const char **ppszNext, int *prcSend) +{ + *ppszExp = NULL; + if (ppszNext) + *ppszNext = NULL; + + size_t const off = psz - (const char *)pPktHdr; + if (pPktHdr->cb <= off) + { + *prcSend = txsReplyFailure(pPktHdr, "STR MISS", "Missing string argument '%s' in '%.8s'", + pszArgName, pPktHdr->achOpcode); + return false; + } + + size_t const cchMax = pPktHdr->cb - off; + const char *pszEnd = RTStrEnd(psz, cchMax); + if (!pszEnd) + { + *prcSend = txsReplyFailure(pPktHdr, "STR TERM", "The string argument '%s' in '%.8s' is unterminated", + pszArgName, pPktHdr->achOpcode); + return false; + } + + if (!ppszNext && (size_t)(pszEnd - psz) != cchMax - 1) + { + *prcSend = txsReplyFailure(pPktHdr, "STR SHRT", "The string argument '%s' in '%.8s' is shorter than advertised", + pszArgName, pPktHdr->achOpcode); + return false; + } + + if (!txsReplaceStringVariables(pPktHdr, psz, ppszExp, prcSend)) + return false; + if (ppszNext) + *ppszNext = pszEnd + 1; + return true; +} + +/** + * Validates a packet with a single string after the header. + * + * @returns true if valid, false if invalid. + * @param pPktHdr The packet. + * @param pszArgName The argument name. + * @param ppszExp Where to return the string pointer. Variables + * will be replaced and it must therefore be freed + * by calling RTStrFree(). + * @param prcSend Where to return the status code of the send on + * failure. + */ +static bool txsIsStringPktValid(PCTXSPKTHDR pPktHdr, const char *pszArgName, char **ppszExp, int *prcSend) +{ + if (pPktHdr->cb < sizeof(TXSPKTHDR) + 2) + { + *ppszExp = NULL; + *prcSend = txsReplyBadMinSize(pPktHdr, sizeof(TXSPKTHDR) + 2); + return false; + } + + return txsIsStringValid(pPktHdr, pszArgName, (const char *)(pPktHdr + 1), ppszExp, NULL, prcSend); +} + +/** + * Checks if the two opcodes match. + * + * @returns true on match, false on mismatch. + * @param pPktHdr The packet header. + * @param pszOpcode2 The opcode we're comparing with. Does not have + * to be the whole 8 chars long. + */ +DECLINLINE(bool) txsIsSameOpcode(PCTXSPKTHDR pPktHdr, const char *pszOpcode2) +{ + if (pPktHdr->achOpcode[0] != pszOpcode2[0]) + return false; + if (pPktHdr->achOpcode[1] != pszOpcode2[1]) + return false; + + unsigned i = 2; + while ( i < RT_SIZEOFMEMB(TXSPKTHDR, achOpcode) + && pszOpcode2[i] != '\0') + { + if (pPktHdr->achOpcode[i] != pszOpcode2[i]) + break; + i++; + } + + if ( i < RT_SIZEOFMEMB(TXSPKTHDR, achOpcode) + && pszOpcode2[i] == '\0') + { + while ( i < RT_SIZEOFMEMB(TXSPKTHDR, achOpcode) + && pPktHdr->achOpcode[i] == ' ') + i++; + } + + return i == RT_SIZEOFMEMB(TXSPKTHDR, achOpcode); +} + +/** + * Used by txsDoGetFile to wait for a reply ACK from the client. + * + * @returns VINF_SUCCESS on ACK, VERR_GENERAL_FAILURE on NACK, + * VERR_NET_NOT_CONNECTED on unknown response (sending a bable reply), + * or whatever txsRecvPkt returns. + * @param pPktHdr The original packet (for future use). + */ +static int txsWaitForAck(PCTXSPKTHDR pPktHdr) +{ + NOREF(pPktHdr); + /** @todo timeout? */ + PTXSPKTHDR pReply; + int rc = txsRecvPkt(&pReply, false /*fAutoRetryOnFailure*/); + if (RT_SUCCESS(rc)) + { + if (txsIsSameOpcode(pReply, "ACK")) + rc = VINF_SUCCESS; + else if (txsIsSameOpcode(pReply, "NACK")) + rc = VERR_GENERAL_FAILURE; + else + { + txsReplyBabble("BABBLE "); + rc = VERR_NET_NOT_CONNECTED; + } + RTMemFree(pReply); + } + return rc; +} + +/** + * Expands the variables in the string and sends it back to the host. + * + * @returns IPRT status code from send. + * @param pPktHdr The expand string packet. + */ +static int txsDoExpandString(PCTXSPKTHDR pPktHdr) +{ + int rc; + char *pszExpanded; + if (!txsIsStringPktValid(pPktHdr, "string", &pszExpanded, &rc)) + return rc; + + struct + { + TXSPKTHDR Hdr; + char szString[_64K]; + char abPadding[TXSPKT_ALIGNMENT]; + } Pkt; + + size_t const cbExpanded = strlen(pszExpanded) + 1; + if (cbExpanded <= sizeof(Pkt.szString)) + { + memcpy(Pkt.szString, pszExpanded, cbExpanded); + rc = txsReplyInternal(&Pkt.Hdr, "STRING ", cbExpanded); + } + else + { + memcpy(Pkt.szString, pszExpanded, sizeof(Pkt.szString)); + Pkt.szString[0] = '\0'; + rc = txsReplyInternal(&Pkt.Hdr, "SHORTSTR", sizeof(Pkt.szString)); + } + + RTStrFree(pszExpanded); + return rc; +} + +/** + * Packs a tar file / directory. + * + * @returns IPRT status code from send. + * @param pPktHdr The pack file packet. + */ +static int txsDoPackFile(PCTXSPKTHDR pPktHdr) +{ + int rc; + char *pszFile = NULL; + char *pszSource = NULL; + + /* Packet cursor. */ + const char *pch = (const char *)(pPktHdr + 1); + + if (txsIsStringValid(pPktHdr, "file", pch, &pszFile, &pch, &rc)) + { + if (txsIsStringValid(pPktHdr, "source", pch, &pszSource, &pch, &rc)) + { + char *pszSuff = RTPathSuffix(pszFile); + + const char *apszArgs[7]; + unsigned cArgs = 0; + + apszArgs[cArgs++] = "RTTar"; + apszArgs[cArgs++] = "--create"; + + apszArgs[cArgs++] = "--file"; + apszArgs[cArgs++] = pszFile; + + if ( pszSuff + && ( !RTStrICmp(pszSuff, ".gz") + || !RTStrICmp(pszSuff, ".tgz"))) + apszArgs[cArgs++] = "--gzip"; + + apszArgs[cArgs++] = pszSource; + + RTEXITCODE rcExit = RTZipTarCmd(cArgs, (char **)apszArgs); + if (rcExit != RTEXITCODE_SUCCESS) + rc = VERR_GENERAL_FAILURE; /** @todo proper return code. */ + else + rc = VINF_SUCCESS; + + rc = txsReplyRC(pPktHdr, rc, "RTZipTarCmd(\"%s\",\"%s\")", + pszFile, pszSource); + + RTStrFree(pszSource); + } + RTStrFree(pszFile); + } + + return rc; +} + +/** + * Unpacks a tar file. + * + * @returns IPRT status code from send. + * @param pPktHdr The unpack file packet. + */ +static int txsDoUnpackFile(PCTXSPKTHDR pPktHdr) +{ + int rc; + char *pszFile = NULL; + char *pszDirectory = NULL; + + /* Packet cursor. */ + const char *pch = (const char *)(pPktHdr + 1); + + if (txsIsStringValid(pPktHdr, "file", pch, &pszFile, &pch, &rc)) + { + if (txsIsStringValid(pPktHdr, "directory", pch, &pszDirectory, &pch, &rc)) + { + char *pszSuff = RTPathSuffix(pszFile); + + const char *apszArgs[7]; + unsigned cArgs = 0; + + apszArgs[cArgs++] = "RTTar"; + apszArgs[cArgs++] = "--extract"; + + apszArgs[cArgs++] = "--file"; + apszArgs[cArgs++] = pszFile; + + apszArgs[cArgs++] = "--directory"; + apszArgs[cArgs++] = pszDirectory; + + if ( pszSuff + && ( !RTStrICmp(pszSuff, ".gz") + || !RTStrICmp(pszSuff, ".tgz"))) + apszArgs[cArgs++] = "--gunzip"; + + RTEXITCODE rcExit = RTZipTarCmd(cArgs, (char **)apszArgs); + if (rcExit != RTEXITCODE_SUCCESS) + rc = VERR_GENERAL_FAILURE; /** @todo proper return code. */ + else + rc = VINF_SUCCESS; + + rc = txsReplyRC(pPktHdr, rc, "RTZipTarCmd(\"%s\",\"%s\")", + pszFile, pszDirectory); + + RTStrFree(pszDirectory); + } + RTStrFree(pszFile); + } + + return rc; +} + +/** + * Downloads a file to the client. + * + * The transfer sends a stream of DATA packets (0 or more) and ends it all with + * a ACK packet. If an error occurs, a FAILURE packet is sent and the transfer + * aborted. + * + * @returns IPRT status code from send. + * @param pPktHdr The get file packet. + */ +static int txsDoGetFile(PCTXSPKTHDR pPktHdr) +{ + int rc; + char *pszPath; + if (!txsIsStringPktValid(pPktHdr, "file", &pszPath, &rc)) + return rc; + + RTFILE hFile; + rc = RTFileOpen(&hFile, pszPath, RTFILE_O_READ | RTFILE_O_DENY_NONE | RTFILE_O_OPEN); + if (RT_SUCCESS(rc)) + { + uint32_t uMyCrc32 = RTCrc32Start(); + for (;;) + { + struct + { + TXSPKTHDR Hdr; + uint32_t uCrc32; + char ab[_64K]; + char abPadding[TXSPKT_ALIGNMENT]; + } Pkt; + size_t cbRead; + rc = RTFileRead(hFile, &Pkt.ab[0], _64K, &cbRead); + if (RT_FAILURE(rc) || cbRead == 0) + { + if (rc == VERR_EOF || (RT_SUCCESS(rc) && cbRead == 0)) + { + Pkt.uCrc32 = RTCrc32Finish(uMyCrc32); + rc = txsReplyInternal(&Pkt.Hdr, "DATA EOF", sizeof(uint32_t)); + if (RT_SUCCESS(rc)) + rc = txsWaitForAck(&Pkt.Hdr); + } + else + rc = txsReplyRC(pPktHdr, rc, "RTFileRead"); + break; + } + + uMyCrc32 = RTCrc32Process(uMyCrc32, &Pkt.ab[0], cbRead); + Pkt.uCrc32 = RTCrc32Finish(uMyCrc32); + rc = txsReplyInternal(&Pkt.Hdr, "DATA ", cbRead + sizeof(uint32_t)); + if (RT_FAILURE(rc)) + break; + rc = txsWaitForAck(&Pkt.Hdr); + if (RT_FAILURE(rc)) + break; + } + + RTFileClose(hFile); + } + else + rc = txsReplyRC(pPktHdr, rc, "RTFileOpen(,\"%s\",)", pszPath); + + RTStrFree(pszPath); + return rc; +} + +/** + * Copies a file from the source to the destination locally. + * + * @returns IPRT status code from send. + * @param pPktHdr The copy file packet. + */ +static int txsDoCopyFile(PCTXSPKTHDR pPktHdr) +{ + /* After the packet header follows a 32-bit file mode, + * the remainder of the packet are two zero terminated paths. */ + size_t const cbMin = sizeof(TXSPKTHDR) + sizeof(RTFMODE) + 2; + if (pPktHdr->cb < cbMin) + return txsReplyBadMinSize(pPktHdr, cbMin); + + /* Packet cursor. */ + const char *pch = (const char *)(pPktHdr + 1); + + int rc; + + RTFMODE const fMode = *(RTFMODE const *)pch; + + char *pszSrc; + if (txsIsStringValid(pPktHdr, "source", (const char *)pch + sizeof(RTFMODE), &pszSrc, &pch, &rc)) + { + char *pszDst; + if (txsIsStringValid(pPktHdr, "dest", pch, &pszDst, NULL /* Check for string termination */, &rc)) + { + rc = RTFileCopy(pszSrc, pszDst); + if (RT_SUCCESS(rc)) + { + if (fMode) /* Do we need to set the file mode? */ + { + rc = RTPathSetMode(pszDst, fMode); + if (RT_FAILURE(rc)) + rc = txsReplyRC(pPktHdr, rc, "RTPathSetMode(\"%s\", %#x)", pszDst, fMode); + } + + if (RT_SUCCESS(rc)) + rc = txsReplyAck(pPktHdr); + } + else + rc = txsReplyRC(pPktHdr, rc, "RTFileCopy"); + RTStrFree(pszDst); + } + + RTStrFree(pszSrc); + } + + return rc; +} + +/** + * Uploads a file from the client. + * + * The transfer sends a stream of DATA packets (0 or more) and ends it all with + * a DATA EOF packet. We ACK each of these, so that if a write error occurs we + * can abort the transfer straight away. + * + * @returns IPRT status code from send. + * @param pPktHdr The put file packet. + * @param fHasMode Set if the packet starts with a mode field. + */ +static int txsDoPutFile(PCTXSPKTHDR pPktHdr, bool fHasMode) +{ + int rc; + RTFMODE fMode = 0; + char *pszPath; + if (!fHasMode) + { + if (!txsIsStringPktValid(pPktHdr, "file", &pszPath, &rc)) + return rc; + } + else + { + /* After the packet header follows a mode mask and the remainder of + the packet is the zero terminated file name. */ + size_t const cbMin = sizeof(TXSPKTHDR) + sizeof(RTFMODE) + 2; + if (pPktHdr->cb < cbMin) + return txsReplyBadMinSize(pPktHdr, cbMin); + if (!txsIsStringValid(pPktHdr, "file", (const char *)(pPktHdr + 1) + sizeof(RTFMODE), &pszPath, NULL, &rc)) + return rc; + fMode = *(RTFMODE const *)(pPktHdr + 1); + fMode <<= RTFILE_O_CREATE_MODE_SHIFT; + fMode &= RTFILE_O_CREATE_MODE_MASK; + } + + RTFILE hFile; + rc = RTFileOpen(&hFile, pszPath, RTFILE_O_WRITE | RTFILE_O_DENY_WRITE | RTFILE_O_CREATE_REPLACE | fMode); + if (RT_SUCCESS(rc)) + { + bool fSuccess = false; + rc = txsReplyAck(pPktHdr); + if (RT_SUCCESS(rc)) + { + if (fMode) + RTFileSetMode(hFile, fMode); + + /* + * Read client command packets and process them. + */ + uint32_t uMyCrc32 = RTCrc32Start(); + for (;;) + { + PTXSPKTHDR pDataPktHdr; + rc = txsRecvPkt(&pDataPktHdr, false /*fAutoRetryOnFailure*/); + if (RT_FAILURE(rc)) + break; + + if (txsIsSameOpcode(pDataPktHdr, "DATA")) + { + size_t const cbMin = sizeof(TXSPKTHDR) + sizeof(uint32_t); + if (pDataPktHdr->cb >= cbMin) + { + size_t cbData = pDataPktHdr->cb - cbMin; + const void *pvData = (const char *)pDataPktHdr + cbMin; + uint32_t uCrc32 = *(uint32_t const *)(pDataPktHdr + 1); + + uMyCrc32 = RTCrc32Process(uMyCrc32, pvData, cbData); + if (RTCrc32Finish(uMyCrc32) == uCrc32) + { + rc = RTFileWrite(hFile, pvData, cbData, NULL); + if (RT_SUCCESS(rc)) + { + rc = txsReplyAck(pDataPktHdr); + RTMemFree(pDataPktHdr); + continue; + } + + rc = txsReplyRC(pDataPktHdr, rc, "RTFileWrite"); + } + else + rc = txsReplyFailure(pDataPktHdr, "BAD DCRC", "mycrc=%#x your=%#x", uMyCrc32, uCrc32); + } + else + rc = txsReplyBadMinSize(pPktHdr, cbMin); + } + else if (txsIsSameOpcode(pDataPktHdr, "DATA EOF")) + { + if (pDataPktHdr->cb == sizeof(TXSPKTHDR) + sizeof(uint32_t)) + { + uint32_t uCrc32 = *(uint32_t const *)(pDataPktHdr + 1); + if (RTCrc32Finish(uMyCrc32) == uCrc32) + { + rc = txsReplyAck(pDataPktHdr); + fSuccess = RT_SUCCESS(rc); + } + else + rc = txsReplyFailure(pDataPktHdr, "BAD DCRC", "mycrc=%#x your=%#x", uMyCrc32, uCrc32); + } + else + rc = txsReplyAck(pDataPktHdr); + } + else if (txsIsSameOpcode(pDataPktHdr, "ABORT")) + rc = txsReplyAck(pDataPktHdr); + else + rc = txsReplyFailure(pDataPktHdr, "UNKNOWN ", "Opcode '%.8s' is not known or not recognized during PUT FILE", pDataPktHdr->achOpcode); + RTMemFree(pDataPktHdr); + break; + } + } + + RTFileClose(hFile); + + /* + * Delete the file on failure. + */ + if (!fSuccess) + RTFileDelete(pszPath); + } + else + rc = txsReplyRC(pPktHdr, rc, "RTFileOpen(,\"%s\",)", pszPath); + + RTStrFree(pszPath); + return rc; +} + +/** + * List the entries in the specified directory. + * + * @returns IPRT status code from send. + * @param pPktHdr The list packet. + */ +static int txsDoList(PCTXSPKTHDR pPktHdr) +{ + int rc; + char *pszPath; + if (!txsIsStringPktValid(pPktHdr, "dir", &pszPath, &rc)) + return rc; + + rc = txsReplyNotImplemented(pPktHdr); + + RTStrFree(pszPath); + return rc; +} + +/** + * Worker for STAT and LSTAT for packing down the file info reply. + * + * @returns IPRT status code from send. + * @param pInfo The info to pack down. + */ +static int txsReplyObjInfo(PCRTFSOBJINFO pInfo) +{ + struct + { + TXSPKTHDR Hdr; + int64_t cbObject; + int64_t cbAllocated; + int64_t nsAccessTime; + int64_t nsModificationTime; + int64_t nsChangeTime; + int64_t nsBirthTime; + uint32_t fMode; + uint32_t uid; + uint32_t gid; + uint32_t cHardLinks; + uint64_t INodeIdDevice; + uint64_t INodeId; + uint64_t Device; + char abPadding[TXSPKT_ALIGNMENT]; + } Pkt; + + Pkt.cbObject = pInfo->cbObject; + Pkt.cbAllocated = pInfo->cbAllocated; + Pkt.nsAccessTime = RTTimeSpecGetNano(&pInfo->AccessTime); + Pkt.nsModificationTime = RTTimeSpecGetNano(&pInfo->ModificationTime); + Pkt.nsChangeTime = RTTimeSpecGetNano(&pInfo->ChangeTime); + Pkt.nsBirthTime = RTTimeSpecGetNano(&pInfo->BirthTime); + Pkt.fMode = pInfo->Attr.fMode; + Pkt.uid = pInfo->Attr.u.Unix.uid; + Pkt.gid = pInfo->Attr.u.Unix.gid; + Pkt.cHardLinks = pInfo->Attr.u.Unix.cHardlinks; + Pkt.INodeIdDevice = pInfo->Attr.u.Unix.INodeIdDevice; + Pkt.INodeId = pInfo->Attr.u.Unix.INodeId; + Pkt.Device = pInfo->Attr.u.Unix.Device; + + return txsReplyInternal(&Pkt.Hdr, "FILEINFO", sizeof(Pkt) - TXSPKT_ALIGNMENT - sizeof(TXSPKTHDR)); +} + +/** + * Get info about a file system object, following all but the symbolic links + * except in the final path component. + * + * @returns IPRT status code from send. + * @param pPktHdr The lstat packet. + */ +static int txsDoLStat(PCTXSPKTHDR pPktHdr) +{ + int rc; + char *pszPath; + if (!txsIsStringPktValid(pPktHdr, "path", &pszPath, &rc)) + return rc; + + RTFSOBJINFO Info; + rc = RTPathQueryInfoEx(pszPath, &Info, RTFSOBJATTRADD_UNIX, RTPATH_F_ON_LINK); + if (RT_SUCCESS(rc)) + rc = txsReplyObjInfo(&Info); + else + rc = txsReplyRC(pPktHdr, rc, "RTPathQueryInfoEx(\"%s\",,UNIX,ON_LINK)", pszPath); + + RTStrFree(pszPath); + return rc; +} + +/** + * Get info about a file system object, following all symbolic links. + * + * @returns IPRT status code from send. + * @param pPktHdr The stat packet. + */ +static int txsDoStat(PCTXSPKTHDR pPktHdr) +{ + int rc; + char *pszPath; + if (!txsIsStringPktValid(pPktHdr, "path", &pszPath, &rc)) + return rc; + + RTFSOBJINFO Info; + rc = RTPathQueryInfoEx(pszPath, &Info, RTFSOBJATTRADD_UNIX, RTPATH_F_FOLLOW_LINK); + if (RT_SUCCESS(rc)) + rc = txsReplyObjInfo(&Info); + else + rc = txsReplyRC(pPktHdr, rc, "RTPathQueryInfoEx(\"%s\",,UNIX,FOLLOW_LINK)", pszPath); + + RTStrFree(pszPath); + return rc; +} + +/** + * Checks if the specified path is a symbolic link. + * + * @returns IPRT status code from send. + * @param pPktHdr The issymlnk packet. + */ +static int txsDoIsSymlnk(PCTXSPKTHDR pPktHdr) +{ + int rc; + char *pszPath; + if (!txsIsStringPktValid(pPktHdr, "symlink", &pszPath, &rc)) + return rc; + + RTFSOBJINFO Info; + rc = RTPathQueryInfoEx(pszPath, &Info, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK); + if (RT_SUCCESS(rc) && RTFS_IS_SYMLINK(Info.Attr.fMode)) + rc = txsReplySimple(pPktHdr, "TRUE "); + else + rc = txsReplySimple(pPktHdr, "FALSE "); + + RTStrFree(pszPath); + return rc; +} + +/** + * Checks if the specified path is a file or not. + * + * If the final path element is a symbolic link to a file, we'll return + * FALSE. + * + * @returns IPRT status code from send. + * @param pPktHdr The isfile packet. + */ +static int txsDoIsFile(PCTXSPKTHDR pPktHdr) +{ + int rc; + char *pszPath; + if (!txsIsStringPktValid(pPktHdr, "dir", &pszPath, &rc)) + return rc; + + RTFSOBJINFO Info; + rc = RTPathQueryInfoEx(pszPath, &Info, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK); + if (RT_SUCCESS(rc) && RTFS_IS_FILE(Info.Attr.fMode)) + rc = txsReplySimple(pPktHdr, "TRUE "); + else + rc = txsReplySimple(pPktHdr, "FALSE "); + + RTStrFree(pszPath); + return rc; +} + +/** + * Checks if the specified path is a directory or not. + * + * If the final path element is a symbolic link to a directory, we'll return + * FALSE. + * + * @returns IPRT status code from send. + * @param pPktHdr The isdir packet. + */ +static int txsDoIsDir(PCTXSPKTHDR pPktHdr) +{ + int rc; + char *pszPath; + if (!txsIsStringPktValid(pPktHdr, "dir", &pszPath, &rc)) + return rc; + + RTFSOBJINFO Info; + rc = RTPathQueryInfoEx(pszPath, &Info, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK); + if (RT_SUCCESS(rc) && RTFS_IS_DIRECTORY(Info.Attr.fMode)) + rc = txsReplySimple(pPktHdr, "TRUE "); + else + rc = txsReplySimple(pPktHdr, "FALSE "); + + RTStrFree(pszPath); + return rc; +} + +/** + * Changes the owner of a file, directory or symbolic link. + * + * @returns IPRT status code from send. + * @param pPktHdr The chmod packet. + */ +static int txsDoChOwn(PCTXSPKTHDR pPktHdr) +{ +#ifdef RT_OS_WINDOWS + return txsReplyNotImplemented(pPktHdr); +#else + /* After the packet header follows a 32-bit UID and 32-bit GID, while the + remainder of the packet is the zero terminated path. */ + size_t const cbMin = sizeof(TXSPKTHDR) + sizeof(RTFMODE) + 2; + if (pPktHdr->cb < cbMin) + return txsReplyBadMinSize(pPktHdr, cbMin); + + int rc; + char *pszPath; + if (!txsIsStringValid(pPktHdr, "path", (const char *)(pPktHdr + 1) + sizeof(uint32_t) * 2, &pszPath, NULL, &rc)) + return rc; + + uint32_t uid = ((uint32_t const *)(pPktHdr + 1))[0]; + uint32_t gid = ((uint32_t const *)(pPktHdr + 1))[1]; + + rc = RTPathSetOwnerEx(pszPath, uid, gid, RTPATH_F_ON_LINK); + + rc = txsReplyRC(pPktHdr, rc, "RTPathSetOwnerEx(\"%s\", %u, %u)", pszPath, uid, gid); + RTStrFree(pszPath); + return rc; +#endif +} + +/** + * Changes the mode of a file or directory. + * + * @returns IPRT status code from send. + * @param pPktHdr The chmod packet. + */ +static int txsDoChMod(PCTXSPKTHDR pPktHdr) +{ + /* After the packet header follows a mode mask and the remainder of + the packet is the zero terminated file name. */ + size_t const cbMin = sizeof(TXSPKTHDR) + sizeof(RTFMODE) + 2; + if (pPktHdr->cb < cbMin) + return txsReplyBadMinSize(pPktHdr, cbMin); + + int rc; + char *pszPath; + if (!txsIsStringValid(pPktHdr, "path", (const char *)(pPktHdr + 1) + sizeof(RTFMODE), &pszPath, NULL, &rc)) + return rc; + + RTFMODE fMode = *(RTFMODE const *)(pPktHdr + 1); + + rc = RTPathSetMode(pszPath, fMode); + + rc = txsReplyRC(pPktHdr, rc, "RTPathSetMode(\"%s\", %o)", pszPath, fMode); + RTStrFree(pszPath); + return rc; +} + +/** + * Removes a directory tree. + * + * @returns IPRT status code from send. + * @param pPktHdr The rmtree packet. + */ +static int txsDoRmTree(PCTXSPKTHDR pPktHdr) +{ + int rc; + char *pszPath; + if (!txsIsStringPktValid(pPktHdr, "dir", &pszPath, &rc)) + return rc; + + rc = RTDirRemoveRecursive(pszPath, 0 /*fFlags*/); + + rc = txsReplyRC(pPktHdr, rc, "RTDirRemoveRecusive(\"%s\",0)", pszPath); + RTStrFree(pszPath); + return rc; +} + +/** + * Removes a symbolic link. + * + * @returns IPRT status code from send. + * @param pPktHdr The rmsymlink packet. + */ +static int txsDoRmSymlnk(PCTXSPKTHDR pPktHdr) +{ + int rc; + char *pszPath; + if (!txsIsStringPktValid(pPktHdr, "symlink", &pszPath, &rc)) + return rc; + + rc = RTSymlinkDelete(pszPath, 0); + + rc = txsReplyRC(pPktHdr, rc, "RTSymlinkDelete(\"%s\")", pszPath); + RTStrFree(pszPath); + return rc; +} + +/** + * Removes a file. + * + * @returns IPRT status code from send. + * @param pPktHdr The rmfile packet. + */ +static int txsDoRmFile(PCTXSPKTHDR pPktHdr) +{ + int rc; + char *pszPath; + if (!txsIsStringPktValid(pPktHdr, "file", &pszPath, &rc)) + return rc; + + rc = RTFileDelete(pszPath); + + rc = txsReplyRC(pPktHdr, rc, "RTFileDelete(\"%s\")", pszPath); + RTStrFree(pszPath); + return rc; +} + +/** + * Removes a directory. + * + * @returns IPRT status code from send. + * @param pPktHdr The rmdir packet. + */ +static int txsDoRmDir(PCTXSPKTHDR pPktHdr) +{ + int rc; + char *pszPath; + if (!txsIsStringPktValid(pPktHdr, "dir", &pszPath, &rc)) + return rc; + + rc = RTDirRemove(pszPath); + + rc = txsReplyRC(pPktHdr, rc, "RTDirRemove(\"%s\")", pszPath); + RTStrFree(pszPath); + return rc; +} + +/** + * Creates a symbolic link. + * + * @returns IPRT status code from send. + * @param pPktHdr The mksymlnk packet. + */ +static int txsDoMkSymlnk(PCTXSPKTHDR pPktHdr) +{ + return txsReplyNotImplemented(pPktHdr); +} + +/** + * Creates a directory and all its parents. + * + * @returns IPRT status code from send. + * @param pPktHdr The mkdir -p packet. + */ +static int txsDoMkDrPath(PCTXSPKTHDR pPktHdr) +{ + /* The same format as the MKDIR command. */ + if (pPktHdr->cb < sizeof(TXSPKTHDR) + sizeof(RTFMODE) + 2) + return txsReplyBadMinSize(pPktHdr, sizeof(TXSPKTHDR) + sizeof(RTFMODE) + 2); + + int rc; + char *pszPath; + if (!txsIsStringValid(pPktHdr, "dir", (const char *)(pPktHdr + 1) + sizeof(RTFMODE), &pszPath, NULL, &rc)) + return rc; + + RTFMODE fMode = *(RTFMODE const *)(pPktHdr + 1); + + rc = RTDirCreateFullPathEx(pszPath, fMode, RTDIRCREATE_FLAGS_IGNORE_UMASK); + + rc = txsReplyRC(pPktHdr, rc, "RTDirCreateFullPath(\"%s\", %#x)", pszPath, fMode); + RTStrFree(pszPath); + return rc; +} + +/** + * Creates a directory. + * + * @returns IPRT status code from send. + * @param pPktHdr The mkdir packet. + */ +static int txsDoMkDir(PCTXSPKTHDR pPktHdr) +{ + /* After the packet header follows a mode mask and the remainder of + the packet is the zero terminated directory name. */ + size_t const cbMin = sizeof(TXSPKTHDR) + sizeof(RTFMODE) + 2; + if (pPktHdr->cb < cbMin) + return txsReplyBadMinSize(pPktHdr, cbMin); + + int rc; + char *pszPath; + if (!txsIsStringValid(pPktHdr, "dir", (const char *)(pPktHdr + 1) + sizeof(RTFMODE), &pszPath, NULL, &rc)) + return rc; + + RTFMODE fMode = *(RTFMODE const *)(pPktHdr + 1); + rc = RTDirCreate(pszPath, fMode, RTDIRCREATE_FLAGS_IGNORE_UMASK); + + rc = txsReplyRC(pPktHdr, rc, "RTDirCreate(\"%s\", %#x)", pszPath, fMode); + RTStrFree(pszPath); + return rc; +} + +/** + * Cleans up the scratch area. + * + * @returns IPRT status code from send. + * @param pPktHdr The shutdown packet. + */ +static int txsDoCleanup(PCTXSPKTHDR pPktHdr) +{ + int rc = RTDirRemoveRecursive(g_szScratchPath, RTDIRRMREC_F_CONTENT_ONLY); + return txsReplyRC(pPktHdr, rc, "RTDirRemoveRecursive(\"%s\", CONTENT_ONLY)", g_szScratchPath); +} + +/** + * Ejects the specified DVD/CD drive. + * + * @returns IPRT status code from send. + * @param pPktHdr The eject packet. + */ +static int txsDoCdEject(PCTXSPKTHDR pPktHdr) +{ + /* After the packet header follows a uint32_t ordinal. */ + size_t const cbExpected = sizeof(TXSPKTHDR) + sizeof(uint32_t); + if (pPktHdr->cb != cbExpected) + return txsReplyBadSize(pPktHdr, cbExpected); + uint32_t iOrdinal = *(uint32_t const *)(pPktHdr + 1); + + RTCDROM hCdrom; + int rc = RTCdromOpenByOrdinal(iOrdinal, RTCDROM_O_CONTROL, &hCdrom); + if (RT_FAILURE(rc)) + return txsReplyRC(pPktHdr, rc, "RTCdromOpenByOrdinal(%u, RTCDROM_O_CONTROL, )", iOrdinal); + rc = RTCdromEject(hCdrom, true /*fForce*/); + RTCdromRelease(hCdrom); + + return txsReplyRC(pPktHdr, rc, "RTCdromEject(ord=%u, fForce=true)", iOrdinal); +} + +/** + * Common worker for txsDoShutdown and txsDoReboot. + * + * @returns IPRT status code from send. + * @param pPktHdr The reboot packet. + * @param fAction Which action to take. + */ +static int txsCommonShutdownReboot(PCTXSPKTHDR pPktHdr, uint32_t fAction) +{ + /* + * We ACK the reboot & shutdown before actually performing them, then we + * terminate the transport layer. + * + * This is to make sure the client isn't stuck with a dead connection. The + * transport layer termination also make sure we won't accept new + * connections in case the client is too eager to reconnect to a rebooted + * test victim. On the down side, we cannot easily report RTSystemShutdown + * failures failures this way. But the client can kind of figure it out by + * reconnecting and seeing that our UUID was unchanged. + */ + int rc; + if (pPktHdr->cb != sizeof(TXSPKTHDR)) + return txsReplyBadSize(pPktHdr, sizeof(TXSPKTHDR)); + g_pTransport->pfnNotifyReboot(); + rc = txsReplyAck(pPktHdr); + RTThreadSleep(2560); /* fudge factor */ + g_pTransport->pfnTerm(); + + /* + * Do the job, if it fails we'll restart the transport layer. + */ +#if 0 + rc = VINF_SUCCESS; +#else + rc = RTSystemShutdown(0 /*cMsDelay*/, + fAction | RTSYSTEM_SHUTDOWN_PLANNED | RTSYSTEM_SHUTDOWN_FORCE, + "Test Execution Service"); +#endif + if (RT_SUCCESS(rc)) + { + RTMsgInfo(fAction == RTSYSTEM_SHUTDOWN_REBOOT ? "Rebooting...\n" : "Shutting down...\n"); + g_fTerminate = true; + } + else + { + RTMsgError("RTSystemShutdown w/ fAction=%#x failed: %Rrc", fAction, rc); + + int rc2 = g_pTransport->pfnInit(); + if (RT_FAILURE(rc2)) + { + g_fTerminate = true; + rc = rc2; + } + } + return rc; +} + +/** + * Shuts down the machine, powering it off if possible. + * + * @returns IPRT status code from send. + * @param pPktHdr The shutdown packet. + */ +static int txsDoShutdown(PCTXSPKTHDR pPktHdr) +{ + return txsCommonShutdownReboot(pPktHdr, RTSYSTEM_SHUTDOWN_POWER_OFF_HALT); +} + +/** + * Reboots the machine. + * + * @returns IPRT status code from send. + * @param pPktHdr The reboot packet. + */ +static int txsDoReboot(PCTXSPKTHDR pPktHdr) +{ + return txsCommonShutdownReboot(pPktHdr, RTSYSTEM_SHUTDOWN_REBOOT); +} + +/** + * Verifies and acknowledges a "UUID" request. + * + * @returns IPRT status code. + * @param pPktHdr The UUID packet. + */ +static int txsDoUuid(PCTXSPKTHDR pPktHdr) +{ + if (pPktHdr->cb != sizeof(TXSPKTHDR)) + return txsReplyBadSize(pPktHdr, sizeof(TXSPKTHDR)); + + struct + { + TXSPKTHDR Hdr; + char szUuid[RTUUID_STR_LENGTH]; + char abPadding[TXSPKT_ALIGNMENT]; + } Pkt; + + int rc = RTUuidToStr(&g_InstanceUuid, Pkt.szUuid, sizeof(Pkt.szUuid)); + if (RT_FAILURE(rc)) + return txsReplyRC(pPktHdr, rc, "RTUuidToStr"); + return txsReplyInternal(&Pkt.Hdr, "ACK UUID", strlen(Pkt.szUuid) + 1); +} + +/** + * Verifies and acknowledges a "BYE" request. + * + * @returns IPRT status code. + * @param pPktHdr The bye packet. + */ +static int txsDoBye(PCTXSPKTHDR pPktHdr) +{ + int rc; + if (pPktHdr->cb == sizeof(TXSPKTHDR)) + rc = txsReplyAck(pPktHdr); + else + rc = txsReplyBadSize(pPktHdr, sizeof(TXSPKTHDR)); + g_pTransport->pfnNotifyBye(); + return rc; +} + +/** + * Verifies and acknowledges a "VER" request. + * + * @returns IPRT status code. + * @param pPktHdr The version packet. + */ +static int txsDoVer(PCTXSPKTHDR pPktHdr) +{ + if (pPktHdr->cb != sizeof(TXSPKTHDR)) + return txsReplyBadSize(pPktHdr, sizeof(TXSPKTHDR)); + + struct + { + TXSPKTHDR Hdr; + char szVer[96]; + char abPadding[TXSPKT_ALIGNMENT]; + } Pkt; + + if (RTStrPrintf2(Pkt.szVer, sizeof(Pkt.szVer), "%s r%s %s.%s (%s %s)", + RTBldCfgVersion(), RTBldCfgRevisionStr(), KBUILD_TARGET, KBUILD_TARGET_ARCH, __DATE__, __TIME__) > 0) + { + return txsReplyInternal(&Pkt.Hdr, "ACK VER ", strlen(Pkt.szVer) + 1); + } + + return txsReplyRC(pPktHdr, VERR_BUFFER_OVERFLOW, "RTStrPrintf2"); +} + +/** + * Verifies and acknowledges a "HOWDY" request. + * + * @returns IPRT status code. + * @param pPktHdr The howdy packet. + */ +static int txsDoHowdy(PCTXSPKTHDR pPktHdr) +{ + if (pPktHdr->cb != sizeof(TXSPKTHDR)) + return txsReplyBadSize(pPktHdr, sizeof(TXSPKTHDR)); + int rc = txsReplyAck(pPktHdr); + if (RT_SUCCESS(rc)) + { + g_pTransport->pfnNotifyHowdy(); + RTDirRemoveRecursive(g_szScratchPath, RTDIRRMREC_F_CONTENT_ONLY); + } + return rc; +} + +/** + * Replies according to the return code. + * + * @returns rcOperation and pTxsExec->rcReplySend. + * @param pTxsExec The TXSEXEC instance. + * @param rcOperation The status code to report. + * @param pszOperationFmt The operation that failed. Typically giving the + * function call with important arguments. + * @param ... Arguments to the format string. + */ +static int txsExecReplyRC(PTXSEXEC pTxsExec, int rcOperation, const char *pszOperationFmt, ...) +{ + AssertStmt(RT_FAILURE_NP(rcOperation), rcOperation = VERR_IPE_UNEXPECTED_INFO_STATUS); + + char szOperation[128]; + va_list va; + va_start(va, pszOperationFmt); + RTStrPrintfV(szOperation, sizeof(szOperation), pszOperationFmt, va); + va_end(va); + + pTxsExec->rcReplySend = txsReplyFailure(pTxsExec->pPktHdr, "FAILED ", + "%s failed with rc=%Rrc (opcode '%.8s')", + szOperation, rcOperation, pTxsExec->pPktHdr->achOpcode); + return rcOperation; +} + + +/** + * Sends the process exit status reply to the TXS client. + * + * @returns IPRT status code of the send. + * @param pTxsExec The TXSEXEC instance. + * @param fProcessAlive Whether the process is still alive (against our + * will). + * @param fProcessTimedOut Whether the process timed out. + * @param MsProcessKilled When the process was killed, UINT64_MAX if not. + */ +static int txsExecSendExitStatus(PTXSEXEC pTxsExec, bool fProcessAlive, bool fProcessTimedOut, uint64_t MsProcessKilled) +{ + int rc; + if ( fProcessTimedOut && !fProcessAlive && MsProcessKilled != UINT64_MAX) + { + rc = txsReplySimple(pTxsExec->pPktHdr, "PROC TOK"); + if (g_fDisplayOutput) + RTPrintf("txs: Process timed out and was killed\n"); + } + else if (fProcessTimedOut && fProcessAlive && MsProcessKilled != UINT64_MAX) + { + rc = txsReplySimple(pTxsExec->pPktHdr, "PROC TOA"); + if (g_fDisplayOutput) + RTPrintf("txs: Process timed out and was not killed successfully\n"); + } + else if (g_fTerminate && (fProcessAlive || MsProcessKilled != UINT64_MAX)) + rc = txsReplySimple(pTxsExec->pPktHdr, "PROC DWN"); + else if (fProcessAlive) + { + rc = txsReplyFailure(pTxsExec->pPktHdr, "PROC DOO", "Doofus! process is alive when it should not"); + AssertFailed(); + } + else if (MsProcessKilled != UINT64_MAX) + { + rc = txsReplyFailure(pTxsExec->pPktHdr, "PROC DOO", "Doofus! process has been killed when it should not"); + AssertFailed(); + } + else if ( pTxsExec->ProcessStatus.enmReason == RTPROCEXITREASON_NORMAL + && pTxsExec->ProcessStatus.iStatus == 0) + { + rc = txsReplySimple(pTxsExec->pPktHdr, "PROC OK "); + if (g_fDisplayOutput) + RTPrintf("txs: Process exited with status: 0\n"); + } + else if (pTxsExec->ProcessStatus.enmReason == RTPROCEXITREASON_NORMAL) + { + rc = txsReplyFailure(pTxsExec->pPktHdr, "PROC NOK", "%d", pTxsExec->ProcessStatus.iStatus); + if (g_fDisplayOutput) + RTPrintf("txs: Process exited with status: %d\n", pTxsExec->ProcessStatus.iStatus); + } + else if (pTxsExec->ProcessStatus.enmReason == RTPROCEXITREASON_SIGNAL) + { + rc = txsReplyFailure(pTxsExec->pPktHdr, "PROC SIG", "%d", pTxsExec->ProcessStatus.iStatus); + if (g_fDisplayOutput) + RTPrintf("txs: Process exited with status: signal %d\n", pTxsExec->ProcessStatus.iStatus); + } + else if (pTxsExec->ProcessStatus.enmReason == RTPROCEXITREASON_ABEND) + { + rc = txsReplyFailure(pTxsExec->pPktHdr, "PROC ABD", ""); + if (g_fDisplayOutput) + RTPrintf("txs: Process exited with status: abend\n"); + } + else + { + rc = txsReplyFailure(pTxsExec->pPktHdr, "PROC DOO", "enmReason=%d iStatus=%d", + pTxsExec->ProcessStatus.enmReason, pTxsExec->ProcessStatus.iStatus); + AssertMsgFailed(("enmReason=%d iStatus=%d", pTxsExec->ProcessStatus.enmReason, pTxsExec->ProcessStatus.iStatus)); + } + return rc; +} + +/** + * Handle pending output data or error on standard out, standard error or the + * test pipe. + * + * @returns IPRT status code from client send. + * @param hPollSet The polling set. + * @param fPollEvt The event mask returned by RTPollNoResume. + * @param phPipeR The pipe handle. + * @param puCrc32 The current CRC-32 of the stream. (In/Out) + * @param enmHndId The handle ID. + * @param pszOpcode The opcode for the data upload. + * + * @todo Put the last 4 parameters into a struct! + */ +static int txsDoExecHlpHandleOutputEvent(RTPOLLSET hPollSet, uint32_t fPollEvt, PRTPIPE phPipeR, + uint32_t *puCrc32, TXSEXECHNDID enmHndId, const char *pszOpcode) +{ + Log(("txsDoExecHlpHandleOutputEvent: %s fPollEvt=%#x\n", pszOpcode, fPollEvt)); + + /* + * Try drain the pipe before acting on any errors. + */ + int rc = VINF_SUCCESS; + struct + { + TXSPKTHDR Hdr; + uint32_t uCrc32; + char abBuf[_64K]; + char abPadding[TXSPKT_ALIGNMENT]; + } Pkt; + size_t cbRead; + int rc2 = RTPipeRead(*phPipeR, Pkt.abBuf, sizeof(Pkt.abBuf), &cbRead); + if (RT_SUCCESS(rc2) && cbRead) + { + Log(("Crc32=%#x ", *puCrc32)); + *puCrc32 = RTCrc32Process(*puCrc32, Pkt.abBuf, cbRead); + Log(("cbRead=%#x Crc32=%#x \n", cbRead, *puCrc32)); + Pkt.uCrc32 = RTCrc32Finish(*puCrc32); + if (g_fDisplayOutput) + { + if (enmHndId == TXSEXECHNDID_STDOUT) + RTStrmPrintf(g_pStdErr, "%.*s", cbRead, Pkt.abBuf); + else if (enmHndId == TXSEXECHNDID_STDERR) + RTStrmPrintf(g_pStdErr, "%.*s", cbRead, Pkt.abBuf); + } + + rc = txsReplyInternal(&Pkt.Hdr, pszOpcode, cbRead + sizeof(uint32_t)); + + /* Make sure we go another poll round in case there was too much data + for the buffer to hold. */ + fPollEvt &= RTPOLL_EVT_ERROR; + } + else if (RT_FAILURE(rc2)) + { + fPollEvt |= RTPOLL_EVT_ERROR; + AssertMsg(rc2 == VERR_BROKEN_PIPE, ("%Rrc\n", rc)); + } + + /* + * If an error was raised signalled, + */ + if (fPollEvt & RTPOLL_EVT_ERROR) + { + rc2 = RTPollSetRemove(hPollSet, enmHndId); + AssertRC(rc2); + + rc2 = RTPipeClose(*phPipeR); + AssertRC(rc2); + *phPipeR = NIL_RTPIPE; + } + return rc; +} + +/** + * Try write some more data to the standard input of the child. + * + * @returns IPRT status code. + * @param pStdInBuf The standard input buffer. + * @param hStdInW The standard input pipe. + */ +static int txsDoExecHlpWriteStdIn(PTXSEXECSTDINBUF pStdInBuf, RTPIPE hStdInW) +{ + size_t cbToWrite = pStdInBuf->cb - pStdInBuf->off; + size_t cbWritten; + int rc = RTPipeWrite(hStdInW, &pStdInBuf->pch[pStdInBuf->off], cbToWrite, &cbWritten); + if (RT_SUCCESS(rc)) + { + Assert(cbWritten == cbToWrite); + pStdInBuf->off += cbWritten; + } + return rc; +} + +/** + * Handle an error event on standard input. + * + * @param hPollSet The polling set. + * @param fPollEvt The event mask returned by RTPollNoResume. + * @param phStdInW The standard input pipe handle. + * @param pStdInBuf The standard input buffer. + */ +static void txsDoExecHlpHandleStdInErrorEvent(RTPOLLSET hPollSet, uint32_t fPollEvt, PRTPIPE phStdInW, + PTXSEXECSTDINBUF pStdInBuf) +{ + NOREF(fPollEvt); + int rc2; + if (pStdInBuf->off < pStdInBuf->cb) + { + rc2 = RTPollSetRemove(hPollSet, TXSEXECHNDID_STDIN_WRITABLE); + AssertRC(rc2); + } + + rc2 = RTPollSetRemove(hPollSet, TXSEXECHNDID_STDIN); + AssertRC(rc2); + + rc2 = RTPipeClose(*phStdInW); + AssertRC(rc2); + *phStdInW = NIL_RTPIPE; + + RTMemFree(pStdInBuf->pch); + pStdInBuf->pch = NULL; + pStdInBuf->off = 0; + pStdInBuf->cb = 0; + pStdInBuf->cbAllocated = 0; + pStdInBuf->fBitBucket = true; +} + +/** + * Handle an event indicating we can write to the standard input pipe of the + * child process. + * + * @param hPollSet The polling set. + * @param fPollEvt The event mask returned by RTPollNoResume. + * @param phStdInW The standard input pipe. + * @param pStdInBuf The standard input buffer. + */ +static void txsDoExecHlpHandleStdInWritableEvent(RTPOLLSET hPollSet, uint32_t fPollEvt, PRTPIPE phStdInW, + PTXSEXECSTDINBUF pStdInBuf) +{ + int rc; + if (!(fPollEvt & RTPOLL_EVT_ERROR)) + { + rc = txsDoExecHlpWriteStdIn(pStdInBuf, *phStdInW); + if (RT_FAILURE(rc) && rc != VERR_BAD_PIPE) + { + /** @todo do we need to do something about this error condition? */ + AssertRC(rc); + } + + if (pStdInBuf->off < pStdInBuf->cb) + { + rc = RTPollSetRemove(hPollSet, TXSEXECHNDID_STDIN_WRITABLE); + AssertRC(rc); + } + } + else + txsDoExecHlpHandleStdInErrorEvent(hPollSet, fPollEvt, phStdInW, pStdInBuf); +} + +/** + * Handle a transport event or successful pfnPollIn() call. + * + * @returns IPRT status code from client send. + * @retval VINF_EOF indicates ABORT command. + * + * @param hPollSet The polling set. + * @param fPollEvt The event mask returned by RTPollNoResume. + * @param idPollHnd The handle ID. + * @param phStdInW The standard input pipe. + * @param pStdInBuf The standard input buffer. + */ +static int txsDoExecHlpHandleTransportEvent(RTPOLLSET hPollSet, uint32_t fPollEvt, uint32_t idPollHnd, + PRTPIPE phStdInW, PTXSEXECSTDINBUF pStdInBuf) +{ + /* ASSUMES the transport layer will detect or clear any error condition. */ + NOREF(fPollEvt); NOREF(idPollHnd); + Log(("txsDoExecHlpHandleTransportEvent\n")); + /** @todo Use a callback for this case? */ + + /* + * Read client command packet and process it. + */ + /** @todo Sometimes this hangs on windows because there isn't any data pending. + * We probably get woken up at the wrong time or in the wrong way, i.e. RTPoll() + * is busted for sockets. + * + * Temporary workaround: Poll for input before trying to read it. */ + if (!g_pTransport->pfnPollIn()) + { + Log(("Bad transport event\n")); + RTThreadYield(); + return VINF_SUCCESS; + } + PTXSPKTHDR pPktHdr; + int rc = txsRecvPkt(&pPktHdr, false /*fAutoRetryOnFailure*/); + if (RT_FAILURE(rc)) + return rc; + Log(("Bad transport event\n")); + + /* + * The most common thing here would be a STDIN request with data + * for the child process. + */ + if (txsIsSameOpcode(pPktHdr, "STDIN")) + { + if ( !pStdInBuf->fBitBucket + && pPktHdr->cb >= sizeof(TXSPKTHDR) + sizeof(uint32_t)) + { + uint32_t uCrc32 = *(uint32_t *)(pPktHdr + 1); + const char *pch = (const char *)(pPktHdr + 1) + sizeof(uint32_t); + size_t cb = pPktHdr->cb - sizeof(TXSPKTHDR) - sizeof(uint32_t); + + /* Check the CRC */ + pStdInBuf->uCrc32 = RTCrc32Process(pStdInBuf->uCrc32, pch, cb); + if (RTCrc32Finish(pStdInBuf->uCrc32) == uCrc32) + { + + /* Rewind the buffer if it's empty. */ + size_t cbInBuf = pStdInBuf->cb - pStdInBuf->off; + bool const fAddToSet = cbInBuf == 0; + if (fAddToSet) + pStdInBuf->cb = pStdInBuf->off = 0; + + /* Try and see if we can simply append the data. */ + if (cb + pStdInBuf->cb <= pStdInBuf->cbAllocated) + { + memcpy(&pStdInBuf->pch[pStdInBuf->cb], pch, cb); + pStdInBuf->cb += cb; + rc = txsReplyAck(pPktHdr); + } + else + { + /* Try write a bit or two before we move+realloc the buffer. */ + if (cbInBuf > 0) + txsDoExecHlpWriteStdIn(pStdInBuf, *phStdInW); + + /* Move any buffered data to the front. */ + cbInBuf = pStdInBuf->cb - pStdInBuf->off; + if (cbInBuf == 0) + pStdInBuf->cb = pStdInBuf->off = 0; + else + { + memmove(pStdInBuf->pch, &pStdInBuf->pch[pStdInBuf->off], cbInBuf); + pStdInBuf->cb = cbInBuf; + pStdInBuf->off = 0; + } + + /* Do we need to grow the buffer? */ + if (cb + pStdInBuf->cb > pStdInBuf->cbAllocated) + { + size_t cbAlloc = pStdInBuf->cb + cb; + cbAlloc = RT_ALIGN_Z(cbAlloc, _64K); + void *pvNew = RTMemRealloc(pStdInBuf->pch, cbAlloc); + if (pvNew) + { + pStdInBuf->pch = (char *)pvNew; + pStdInBuf->cbAllocated = cbAlloc; + } + } + + /* Finally, copy the data. */ + if (cb + pStdInBuf->cb <= pStdInBuf->cbAllocated) + { + memcpy(&pStdInBuf->pch[pStdInBuf->cb], pch, cb); + pStdInBuf->cb += cb; + rc = txsReplyAck(pPktHdr); + } + else + rc = txsReplySimple(pPktHdr, "STDINMEM"); + } + + /* + * Flush the buffered data and add/remove the standard input + * handle from the set. + */ + txsDoExecHlpWriteStdIn(pStdInBuf, *phStdInW); + if (fAddToSet && pStdInBuf->off < pStdInBuf->cb) + { + int rc2 = RTPollSetAddPipe(hPollSet, *phStdInW, RTPOLL_EVT_WRITE, TXSEXECHNDID_STDIN_WRITABLE); + AssertRC(rc2); + } + else if (!fAddToSet && pStdInBuf->off >= pStdInBuf->cb) + { + int rc2 = RTPollSetRemove(hPollSet, TXSEXECHNDID_STDIN_WRITABLE); + AssertRC(rc2); + } + } + else + rc = txsReplyFailure(pPktHdr, "STDINCRC", "Invalid CRC checksum expected %#x got %#x", + pStdInBuf->uCrc32, uCrc32); + } + else if (pPktHdr->cb < sizeof(TXSPKTHDR) + sizeof(uint32_t)) + rc = txsReplySimple(pPktHdr, "STDINBAD"); + else + rc = txsReplySimple(pPktHdr, "STDINIGN"); + } + /* + * Marks the end of the stream for stdin. + */ + else if (txsIsSameOpcode(pPktHdr, "STDINEOS")) + { + if (RT_LIKELY(pPktHdr->cb == sizeof(TXSPKTHDR))) + { + /* Close the pipe. */ + txsDoExecHlpHandleStdInErrorEvent(hPollSet, fPollEvt, phStdInW, pStdInBuf); + rc = txsReplyAck(pPktHdr); + } + else + rc = txsReplySimple(pPktHdr, "STDINBAD"); + } + /* + * The only other two requests are connection oriented and we return a error + * code so that we unwind the whole EXEC shebang and start afresh. + */ + else if (txsIsSameOpcode(pPktHdr, "BYE")) + { + rc = txsDoBye(pPktHdr); + if (RT_SUCCESS(rc)) + rc = VERR_NET_NOT_CONNECTED; + } + else if (txsIsSameOpcode(pPktHdr, "HOWDY")) + { + rc = txsDoHowdy(pPktHdr); + if (RT_SUCCESS(rc)) + rc = VERR_NET_NOT_CONNECTED; + } + else if (txsIsSameOpcode(pPktHdr, "ABORT")) + { + rc = txsReplyAck(pPktHdr); + if (RT_SUCCESS(rc)) + rc = VINF_EOF; /* this is but ugly! */ + } + else + rc = txsReplyFailure(pPktHdr, "UNKNOWN ", "Opcode '%.8s' is not known or not recognized during EXEC", pPktHdr->achOpcode); + + RTMemFree(pPktHdr); + return rc; +} + +/** + * Handles the output and input of the process, waits for it finish up. + * + * @returns IPRT status code from reply send. + * @param pTxsExec The TXSEXEC instance. + */ +static int txsDoExecHlp2(PTXSEXEC pTxsExec) +{ + int rc; /* client send. */ + int rc2; + TXSEXECSTDINBUF StdInBuf = { 0, 0, NULL, 0, pTxsExec->hStdInW == NIL_RTPIPE, RTCrc32Start() }; + uint32_t uStdOutCrc32 = RTCrc32Start(); + uint32_t uStdErrCrc32 = uStdOutCrc32; + uint32_t uTestPipeCrc32 = uStdOutCrc32; + uint64_t const MsStart = RTTimeMilliTS(); + bool fProcessTimedOut = false; + uint64_t MsProcessKilled = UINT64_MAX; + RTMSINTERVAL const cMsPollBase = g_pTransport->pfnPollSetAdd || pTxsExec->hStdInW == NIL_RTPIPE + ? RT_MS_5SEC : 100; + RTMSINTERVAL cMsPollCur = 0; + + /* + * Before entering the loop, tell the client that we've started the guest + * and that it's now OK to send input to the process. (This is not the + * final ACK, so the packet header is NULL ... kind of bogus.) + */ + rc = txsReplyAck(NULL); + + /* + * Process input, output, the test pipe and client requests. + */ + while ( RT_SUCCESS(rc) + && RT_UNLIKELY(!g_fTerminate)) + { + /* + * Wait/Process all pending events. + */ + uint32_t idPollHnd; + uint32_t fPollEvt; + Log3(("Calling RTPollNoResume(,%u,)...\n", cMsPollCur)); + rc2 = RTPollNoResume(pTxsExec->hPollSet, cMsPollCur, &fPollEvt, &idPollHnd); + Log3(("RTPollNoResume -> fPollEvt=%#x idPollHnd=%u\n", fPollEvt, idPollHnd)); + if (g_fTerminate) + continue; + cMsPollCur = 0; /* no rest until we've checked everything. */ + + if (RT_SUCCESS(rc2)) + { + switch (idPollHnd) + { + case TXSEXECHNDID_STDOUT: + rc = txsDoExecHlpHandleOutputEvent(pTxsExec->hPollSet, fPollEvt, &pTxsExec->hStdOutR, &uStdOutCrc32, + TXSEXECHNDID_STDOUT, "STDOUT "); + break; + + case TXSEXECHNDID_STDERR: + rc = txsDoExecHlpHandleOutputEvent(pTxsExec->hPollSet, fPollEvt, &pTxsExec->hStdErrR, &uStdErrCrc32, + TXSEXECHNDID_STDERR, "STDERR "); + break; + + case TXSEXECHNDID_TESTPIPE: + rc = txsDoExecHlpHandleOutputEvent(pTxsExec->hPollSet, fPollEvt, &pTxsExec->hTestPipeR, &uTestPipeCrc32, + TXSEXECHNDID_TESTPIPE, "TESTPIPE"); + break; + + case TXSEXECHNDID_STDIN: + txsDoExecHlpHandleStdInErrorEvent(pTxsExec->hPollSet, fPollEvt, &pTxsExec->hStdInW, &StdInBuf); + break; + + case TXSEXECHNDID_STDIN_WRITABLE: + txsDoExecHlpHandleStdInWritableEvent(pTxsExec->hPollSet, fPollEvt, &pTxsExec->hStdInW, &StdInBuf); + break; + + case TXSEXECHNDID_THREAD: + rc2 = RTPollSetRemove(pTxsExec->hPollSet, TXSEXECHNDID_THREAD); AssertRC(rc2); + break; + + default: + rc = txsDoExecHlpHandleTransportEvent(pTxsExec->hPollSet, fPollEvt, idPollHnd, &pTxsExec->hStdInW, + &StdInBuf); + break; + } + if (RT_FAILURE(rc) || rc == VINF_EOF) + break; /* abort command, or client dead or something */ + continue; + } + + /* + * Check for incoming data. + */ + if (g_pTransport->pfnPollIn()) + { + rc = txsDoExecHlpHandleTransportEvent(pTxsExec->hPollSet, 0, UINT32_MAX, &pTxsExec->hStdInW, &StdInBuf); + if (RT_FAILURE(rc) || rc == VINF_EOF) + break; /* abort command, or client dead or something */ + continue; + } + + /* + * If the process has terminated, we're should head out. + */ + if (!ASMAtomicReadBool(&pTxsExec->fProcessAlive)) + break; + + /* + * Check for timed out, killing the process. + */ + uint32_t cMilliesLeft = RT_INDEFINITE_WAIT; + if (pTxsExec->cMsTimeout != RT_INDEFINITE_WAIT) + { + uint64_t u64Now = RTTimeMilliTS(); + uint64_t cMsElapsed = u64Now - MsStart; + if (cMsElapsed >= pTxsExec->cMsTimeout) + { + fProcessTimedOut = true; + if ( MsProcessKilled == UINT64_MAX + || u64Now - MsProcessKilled > RT_MS_1SEC) + { + if ( MsProcessKilled != UINT64_MAX + && u64Now - MsProcessKilled > 20*RT_MS_1MIN) + break; /* give up after 20 mins */ + RTCritSectEnter(&pTxsExec->CritSect); + if (pTxsExec->fProcessAlive) + RTProcTerminate(pTxsExec->hProcess); + RTCritSectLeave(&pTxsExec->CritSect); + MsProcessKilled = u64Now; + continue; + } + cMilliesLeft = RT_MS_10SEC; + } + else + cMilliesLeft = pTxsExec->cMsTimeout - (uint32_t)cMsElapsed; + } + + /* Reset the polling interval since we've done all pending work. */ + cMsPollCur = cMilliesLeft >= cMsPollBase ? cMsPollBase : cMilliesLeft; + } + + /* + * At this point we should hopefully only have to wait 0 ms on the thread + * to release the handle... But if for instance the process refuses to die, + * we'll have to try kill it again. Bothersome. + */ + for (size_t i = 0; i < 22; i++) + { + rc2 = RTThreadWait(pTxsExec->hThreadWaiter, RT_MS_1SEC / 2, NULL); + if (RT_SUCCESS(rc)) + { + pTxsExec->hThreadWaiter = NIL_RTTHREAD; + Assert(!pTxsExec->fProcessAlive); + break; + } + if (i == 0 || i == 10 || i == 15 || i == 18 || i > 20) + { + RTCritSectEnter(&pTxsExec->CritSect); + if (pTxsExec->fProcessAlive) + RTProcTerminate(pTxsExec->hProcess); + RTCritSectLeave(&pTxsExec->CritSect); + } + } + + /* + * If we don't have a client problem (RT_FAILURE(rc) we'll reply to the + * clients exec packet now. + */ + if (RT_SUCCESS(rc)) + rc = txsExecSendExitStatus(pTxsExec, pTxsExec->fProcessAlive, fProcessTimedOut, MsProcessKilled); + + RTMemFree(StdInBuf.pch); + return rc; +} + +/** + * Creates a poll set for the pipes and let the transport layer add stuff to it + * as well. + * + * @returns IPRT status code, reply to client made on error. + * @param pTxsExec The TXSEXEC instance. + */ +static int txsExecSetupPollSet(PTXSEXEC pTxsExec) +{ + int rc = RTPollSetCreate(&pTxsExec->hPollSet); + if (RT_FAILURE(rc)) + return txsExecReplyRC(pTxsExec, rc, "RTPollSetCreate"); + + rc = RTPollSetAddPipe(pTxsExec->hPollSet, pTxsExec->hStdInW, RTPOLL_EVT_ERROR, TXSEXECHNDID_STDIN); + if (RT_FAILURE(rc)) + return txsExecReplyRC(pTxsExec, rc, "RTPollSetAddPipe/stdin"); + + rc = RTPollSetAddPipe(pTxsExec->hPollSet, pTxsExec->hStdOutR, RTPOLL_EVT_READ | RTPOLL_EVT_ERROR, + TXSEXECHNDID_STDOUT); + if (RT_FAILURE(rc)) + return txsExecReplyRC(pTxsExec, rc, "RTPollSetAddPipe/stdout"); + + rc = RTPollSetAddPipe(pTxsExec->hPollSet, pTxsExec->hStdErrR, RTPOLL_EVT_READ | RTPOLL_EVT_ERROR, + TXSEXECHNDID_STDERR); + if (RT_FAILURE(rc)) + return txsExecReplyRC(pTxsExec, rc, "RTPollSetAddPipe/stderr"); + + rc = RTPollSetAddPipe(pTxsExec->hPollSet, pTxsExec->hTestPipeR, RTPOLL_EVT_READ | RTPOLL_EVT_ERROR, + TXSEXECHNDID_TESTPIPE); + if (RT_FAILURE(rc)) + return txsExecReplyRC(pTxsExec, rc, "RTPollSetAddPipe/test"); + + rc = RTPollSetAddPipe(pTxsExec->hPollSet, pTxsExec->hWakeUpPipeR, RTPOLL_EVT_READ | RTPOLL_EVT_ERROR, + TXSEXECHNDID_THREAD); + if (RT_FAILURE(rc)) + return txsExecReplyRC(pTxsExec, rc, "RTPollSetAddPipe/wakeup"); + + if (g_pTransport->pfnPollSetAdd) + { + rc = g_pTransport->pfnPollSetAdd(pTxsExec->hPollSet, TXSEXECHNDID_TRANSPORT); + if (RT_FAILURE(rc)) + return txsExecReplyRC(pTxsExec, rc, "%s->pfnPollSetAdd/stdin", g_pTransport->szName); + } + + return VINF_SUCCESS; +} + +/** + * Thread that calls RTProcWait and signals the main thread when it returns. + * + * The thread is created before the process is started and is waiting for a user + * signal from the main thread before it calls RTProcWait. + * + * @returns VINF_SUCCESS (ignored). + * @param hThreadSelf The thread handle. + * @param pvUser The TXEEXEC structure. + */ +static DECLCALLBACK(int) txsExecWaitThreadProc(RTTHREAD hThreadSelf, void *pvUser) +{ + PTXSEXEC pTxsExec = (PTXSEXEC)pvUser; + + /* Wait for the go ahead... */ + int rc = RTThreadUserWait(hThreadSelf, RT_INDEFINITE_WAIT); AssertRC(rc); + + RTCritSectEnter(&pTxsExec->CritSect); + for (;;) + { + RTCritSectLeave(&pTxsExec->CritSect); + rc = RTProcWaitNoResume(pTxsExec->hProcess, RTPROCWAIT_FLAGS_BLOCK, &pTxsExec->ProcessStatus); + RTCritSectEnter(&pTxsExec->CritSect); + + /* If the pipe is NIL, the destructor wants us to get lost ASAP. */ + if (pTxsExec->hWakeUpPipeW == NIL_RTPIPE) + break; + + if (RT_FAILURE(rc)) + { + rc = RTProcWait(pTxsExec->hProcess, RTPROCWAIT_FLAGS_NOBLOCK, &pTxsExec->ProcessStatus); + if (rc == VERR_PROCESS_RUNNING) + continue; + + if (RT_FAILURE(rc)) + { + AssertRC(rc); + pTxsExec->ProcessStatus.iStatus = rc; + pTxsExec->ProcessStatus.enmReason = RTPROCEXITREASON_ABEND; + } + } + + /* The process finished, signal the main thread over the pipe. */ + ASMAtomicWriteBool(&pTxsExec->fProcessAlive, false); + size_t cbIgnored; + RTPipeWrite(pTxsExec->hWakeUpPipeW, "done", 4, &cbIgnored); + RTPipeClose(pTxsExec->hWakeUpPipeW); + pTxsExec->hWakeUpPipeW = NIL_RTPIPE; + break; + } + RTCritSectLeave(&pTxsExec->CritSect); + + return VINF_SUCCESS; +} + +/** + * Sets up the thread that waits for the process to complete. + * + * @returns IPRT status code, reply to client made on error. + * @param pTxsExec The TXSEXEC instance. + */ +static int txsExecSetupThread(PTXSEXEC pTxsExec) +{ + int rc = RTPipeCreate(&pTxsExec->hWakeUpPipeR, &pTxsExec->hWakeUpPipeW, 0 /*fFlags*/); + if (RT_FAILURE(rc)) + { + pTxsExec->hWakeUpPipeR = pTxsExec->hWakeUpPipeW = NIL_RTPIPE; + return txsExecReplyRC(pTxsExec, rc, "RTPipeCreate/wait"); + } + + rc = RTThreadCreate(&pTxsExec->hThreadWaiter, txsExecWaitThreadProc, + pTxsExec, 0 /*cbStack */, RTTHREADTYPE_DEFAULT, + RTTHREADFLAGS_WAITABLE, "TxsProcW"); + if (RT_FAILURE(rc)) + { + pTxsExec->hThreadWaiter = NIL_RTTHREAD; + return txsExecReplyRC(pTxsExec, rc, "RTThreadCreate"); + } + + return VINF_SUCCESS; +} + +/** + * Sets up the test pipe. + * + * @returns IPRT status code, reply to client made on error. + * @param pTxsExec The TXSEXEC instance. + * @param pszTestPipe How to set up the test pipe. + */ +static int txsExecSetupTestPipe(PTXSEXEC pTxsExec, const char *pszTestPipe) +{ + if (strcmp(pszTestPipe, "|")) + return VINF_SUCCESS; + + int rc = RTPipeCreate(&pTxsExec->hTestPipeR, &pTxsExec->hTestPipeW, RTPIPE_C_INHERIT_WRITE); + if (RT_FAILURE(rc)) + { + pTxsExec->hTestPipeR = pTxsExec->hTestPipeW = NIL_RTPIPE; + return txsExecReplyRC(pTxsExec, rc, "RTPipeCreate/test/%s", pszTestPipe); + } + + char szVal[64]; + RTStrPrintf(szVal, sizeof(szVal), "%#llx", (uint64_t)RTPipeToNative(pTxsExec->hTestPipeW)); + rc = RTEnvSetEx(pTxsExec->hEnv, "IPRT_TEST_PIPE", szVal); + if (RT_FAILURE(rc)) + return txsExecReplyRC(pTxsExec, rc, "RTEnvSetEx/test/%s", pszTestPipe); + + return VINF_SUCCESS; +} + +/** + * Sets up the redirection / pipe / nothing for one of the standard handles. + * + * @returns IPRT status code, reply to client made on error. + * @param pTxsExec The TXSEXEC instance. + * @param pszHowTo How to set up this standard handle. + * @param pszStdWhat For what to setup redirection (stdin/stdout/stderr). + * @param fd Which standard handle it is (0 == stdin, 1 == + * stdout, 2 == stderr). + * @param ph The generic handle that @a pph may be set + * pointing to. Always set. + * @param pph Pointer to the RTProcCreateExec argument. + * Always set. + * @param phPipe Where to return the end of the pipe that we + * should service. Always set. + */ +static int txsExecSetupRedir(PTXSEXEC pTxsExec, const char *pszHowTo, const char *pszStdWhat, int fd, PRTHANDLE ph, PRTHANDLE *pph, PRTPIPE phPipe) +{ + ph->enmType = RTHANDLETYPE_PIPE; + ph->u.hPipe = NIL_RTPIPE; + *pph = NULL; + *phPipe = NIL_RTPIPE; + + int rc; + if (!strcmp(pszHowTo, "|")) + { + /* + * Setup a pipe for forwarding to/from the client. + */ + if (fd == 0) + rc = RTPipeCreate(&ph->u.hPipe, phPipe, RTPIPE_C_INHERIT_READ); + else + rc = RTPipeCreate(phPipe, &ph->u.hPipe, RTPIPE_C_INHERIT_WRITE); + if (RT_FAILURE(rc)) + return txsExecReplyRC(pTxsExec, rc, "RTPipeCreate/%s/%s", pszStdWhat, pszHowTo); + ph->enmType = RTHANDLETYPE_PIPE; + *pph = ph; + } + else if (!strcmp(pszHowTo, "/dev/null")) + { + /* + * Redirect to/from /dev/null. + */ + RTFILE hFile; + rc = RTFileOpenBitBucket(&hFile, fd == 0 ? RTFILE_O_READ : RTFILE_O_WRITE); + if (RT_FAILURE(rc)) + return txsExecReplyRC(pTxsExec, rc, "RTFileOpenBitBucket/%s/%s", pszStdWhat, pszHowTo); + + ph->enmType = RTHANDLETYPE_FILE; + ph->u.hFile = hFile; + *pph = ph; + } + else if (*pszHowTo) + { + /* + * Redirect to/from file. + */ + uint32_t fFlags; + if (fd == 0) + fFlags = RTFILE_O_READ | RTFILE_O_DENY_WRITE | RTFILE_O_OPEN; + else + { + if (pszHowTo[0] != '>' || pszHowTo[1] != '>') + fFlags = RTFILE_O_WRITE | RTFILE_O_DENY_WRITE | RTFILE_O_CREATE_REPLACE; + else + { + /* append */ + pszHowTo += 2; + fFlags = RTFILE_O_WRITE | RTFILE_O_DENY_NONE | RTFILE_O_OPEN_CREATE | RTFILE_O_APPEND; + } + } + + RTFILE hFile; + rc = RTFileOpen(&hFile, pszHowTo, fFlags); + if (RT_FAILURE(rc)) + return txsExecReplyRC(pTxsExec, rc, "RTFileOpen/%s/%s", pszStdWhat, pszHowTo); + + ph->enmType = RTHANDLETYPE_FILE; + ph->u.hFile = hFile; + *pph = ph; + } + else + /* same as parent (us) */ + rc = VINF_SUCCESS; + return rc; +} + +/** + * Create the environment. + * + * @returns IPRT status code, reply to client made on error. + * @param pTxsExec The TXSEXEC instance. + * @param cEnvVars The number of environment variables. + * @param papszEnv The environment variables (var=value). + */ +static int txsExecSetupEnv(PTXSEXEC pTxsExec, uint32_t cEnvVars, const char * const *papszEnv) +{ + /* + * Create the environment. + */ + int rc = RTEnvClone(&pTxsExec->hEnv, RTENV_DEFAULT); + if (RT_FAILURE(rc)) + return txsExecReplyRC(pTxsExec, rc, "RTEnvClone"); + + for (size_t i = 0; i < cEnvVars; i++) + { + rc = RTEnvPutEx(pTxsExec->hEnv, papszEnv[i]); + if (RT_FAILURE(rc)) + return txsExecReplyRC(pTxsExec, rc, "RTEnvPutEx(,'%s')", papszEnv[i]); + } + return VINF_SUCCESS; +} + +/** + * Deletes the TXSEXEC structure and frees the memory backing it. + * + * @param pTxsExec The structure to destroy. + */ +static void txsExecDestroy(PTXSEXEC pTxsExec) +{ + int rc2; + + rc2 = RTEnvDestroy(pTxsExec->hEnv); AssertRC(rc2); + pTxsExec->hEnv = NIL_RTENV; + rc2 = RTPipeClose(pTxsExec->hTestPipeW); AssertRC(rc2); + pTxsExec->hTestPipeW = NIL_RTPIPE; + rc2 = RTHandleClose(pTxsExec->StdErr.phChild); AssertRC(rc2); + pTxsExec->StdErr.phChild = NULL; + rc2 = RTHandleClose(pTxsExec->StdOut.phChild); AssertRC(rc2); + pTxsExec->StdOut.phChild = NULL; + rc2 = RTHandleClose(pTxsExec->StdIn.phChild); AssertRC(rc2); + pTxsExec->StdIn.phChild = NULL; + + rc2 = RTPipeClose(pTxsExec->hTestPipeR); AssertRC(rc2); + pTxsExec->hTestPipeR = NIL_RTPIPE; + rc2 = RTPipeClose(pTxsExec->hStdErrR); AssertRC(rc2); + pTxsExec->hStdErrR = NIL_RTPIPE; + rc2 = RTPipeClose(pTxsExec->hStdOutR); AssertRC(rc2); + pTxsExec->hStdOutR = NIL_RTPIPE; + rc2 = RTPipeClose(pTxsExec->hStdInW); AssertRC(rc2); + pTxsExec->hStdInW = NIL_RTPIPE; + + RTPollSetDestroy(pTxsExec->hPollSet); + pTxsExec->hPollSet = NIL_RTPOLLSET; + + /* + * If the process is still running we're in a bit of a fix... Try kill it, + * although that's potentially racing process termination and reusage of + * the pid. + */ + RTCritSectEnter(&pTxsExec->CritSect); + + RTPipeClose(pTxsExec->hWakeUpPipeW); + pTxsExec->hWakeUpPipeW = NIL_RTPIPE; + RTPipeClose(pTxsExec->hWakeUpPipeR); + pTxsExec->hWakeUpPipeR = NIL_RTPIPE; + + if ( pTxsExec->hProcess != NIL_RTPROCESS + && pTxsExec->fProcessAlive) + RTProcTerminate(pTxsExec->hProcess); + + RTCritSectLeave(&pTxsExec->CritSect); + + int rcThread = VINF_SUCCESS; + if (pTxsExec->hThreadWaiter != NIL_RTTHREAD) + rcThread = RTThreadWait(pTxsExec->hThreadWaiter, 5000, NULL); + if (RT_SUCCESS(rcThread)) + { + pTxsExec->hThreadWaiter = NIL_RTTHREAD; + RTCritSectDelete(&pTxsExec->CritSect); + RTMemFree(pTxsExec); + } + /* else: leak it or RTThreadWait may cause heap corruption later. */ +} + +/** + * Initializes the TXSEXEC structure. + * + * @returns VINF_SUCCESS and non-NULL *ppTxsExec on success, reply send status + * and *ppTxsExec set to NULL on failure. + * @param pPktHdr The exec packet. + * @param cMsTimeout The time parameter. + * @param ppTxsExec Where to return the structure. + */ +static int txsExecCreate(PCTXSPKTHDR pPktHdr, RTMSINTERVAL cMsTimeout, PTXSEXEC *ppTxsExec) +{ + *ppTxsExec = NULL; + + /* + * Allocate the basic resources. + */ + PTXSEXEC pTxsExec = (PTXSEXEC)RTMemAlloc(sizeof(*pTxsExec)); + if (!pTxsExec) + return txsReplyRC(pPktHdr, VERR_NO_MEMORY, "RTMemAlloc(%zu)", sizeof(*pTxsExec)); + int rc = RTCritSectInit(&pTxsExec->CritSect); + if (RT_FAILURE(rc)) + { + RTMemFree(pTxsExec); + return txsReplyRC(pPktHdr, rc, "RTCritSectInit"); + } + + /* + * Initialize the member to NIL values. + */ + pTxsExec->pPktHdr = pPktHdr; + pTxsExec->cMsTimeout = cMsTimeout; + pTxsExec->rcReplySend = VINF_SUCCESS; + + pTxsExec->hPollSet = NIL_RTPOLLSET; + pTxsExec->hStdInW = NIL_RTPIPE; + pTxsExec->hStdOutR = NIL_RTPIPE; + pTxsExec->hStdErrR = NIL_RTPIPE; + pTxsExec->hTestPipeR = NIL_RTPIPE; + pTxsExec->hWakeUpPipeR = NIL_RTPIPE; + pTxsExec->hThreadWaiter = NIL_RTTHREAD; + + pTxsExec->StdIn.phChild = NULL; + pTxsExec->StdOut.phChild = NULL; + pTxsExec->StdErr.phChild = NULL; + pTxsExec->hTestPipeW = NIL_RTPIPE; + pTxsExec->hEnv = NIL_RTENV; + + pTxsExec->hProcess = NIL_RTPROCESS; + pTxsExec->ProcessStatus.iStatus = 254; + pTxsExec->ProcessStatus.enmReason = RTPROCEXITREASON_ABEND; + pTxsExec->fProcessAlive = false; + pTxsExec->hWakeUpPipeW = NIL_RTPIPE; + + *ppTxsExec = pTxsExec; + return VINF_SUCCESS; +} + +/** + * txsDoExec helper that takes over when txsDoExec has expanded the packet. + * + * @returns IPRT status code from send. + * @param pPktHdr The exec packet. + * @param fFlags Flags, reserved for future use. + * @param pszExecName The executable name. + * @param cArgs The argument count. + * @param papszArgs The arguments. + * @param cEnvVars The environment variable count. + * @param papszEnv The environment variables. + * @param pszStdIn How to deal with standard in. + * @param pszStdOut How to deal with standard out. + * @param pszStdErr How to deal with standard err. + * @param pszTestPipe How to deal with the test pipe. + * @param pszUsername The user to run the program as. + * @param cMillies The process time limit in milliseconds. + */ +static int txsDoExecHlp(PCTXSPKTHDR pPktHdr, uint32_t fFlags, const char *pszExecName, + uint32_t cArgs, const char * const *papszArgs, + uint32_t cEnvVars, const char * const *papszEnv, + const char *pszStdIn, const char *pszStdOut, const char *pszStdErr, const char *pszTestPipe, + const char *pszUsername, RTMSINTERVAL cMillies) +{ + int rc2; + RT_NOREF_PV(fFlags); + + /* + * Input validation, filter out things we don't yet support.. + */ + Assert(!fFlags); + if (!*pszExecName) + return txsReplyFailure(pPktHdr, "STR ZERO", "Executable name is empty"); + if (!*pszStdIn) + return txsReplyFailure(pPktHdr, "STR ZERO", "The stdin howto is empty"); + if (!*pszStdOut) + return txsReplyFailure(pPktHdr, "STR ZERO", "The stdout howto is empty"); + if (!*pszStdErr) + return txsReplyFailure(pPktHdr, "STR ZERO", "The stderr howto is empty"); + if (!*pszTestPipe) + return txsReplyFailure(pPktHdr, "STR ZERO", "The testpipe howto is empty"); + if (strcmp(pszTestPipe, "|") && strcmp(pszTestPipe, "/dev/null")) + return txsReplyFailure(pPktHdr, "BAD TSTP", "Only \"|\" and \"/dev/null\" are allowed as testpipe howtos ('%s')", + pszTestPipe); + if (*pszUsername) + return txsReplyFailure(pPktHdr, "NOT IMPL", "Executing as a specific user is not implemented ('%s')", pszUsername); + + /* + * Prepare for process launch. + */ + PTXSEXEC pTxsExec; + int rc = txsExecCreate(pPktHdr, cMillies, &pTxsExec); + if (pTxsExec == NULL) + return rc; + rc = txsExecSetupEnv(pTxsExec, cEnvVars, papszEnv); + if (RT_SUCCESS(rc)) + rc = txsExecSetupRedir(pTxsExec, pszStdIn, "StdIn", 0, &pTxsExec->StdIn.hChild, &pTxsExec->StdIn.phChild, &pTxsExec->hStdInW); + if (RT_SUCCESS(rc)) + rc = txsExecSetupRedir(pTxsExec, pszStdOut, "StdOut", 1, &pTxsExec->StdOut.hChild, &pTxsExec->StdOut.phChild, &pTxsExec->hStdOutR); + if (RT_SUCCESS(rc)) + rc = txsExecSetupRedir(pTxsExec, pszStdErr, "StdErr", 2, &pTxsExec->StdErr.hChild, &pTxsExec->StdErr.phChild, &pTxsExec->hStdErrR); + if (RT_SUCCESS(rc)) + rc = txsExecSetupTestPipe(pTxsExec, pszTestPipe); + if (RT_SUCCESS(rc)) + rc = txsExecSetupThread(pTxsExec); + if (RT_SUCCESS(rc)) + rc = txsExecSetupPollSet(pTxsExec); + if (RT_SUCCESS(rc)) + { + char szPathResolved[RTPATH_MAX + 1]; + rc = RTPathReal(pszExecName, szPathResolved, sizeof(szPathResolved)); + if (RT_SUCCESS(rc)) + { + /* + * Create the process. + */ + if (g_fDisplayOutput) + { + RTPrintf("txs: Executing \"%s\" -> \"%s\": ", pszExecName, szPathResolved); + for (uint32_t i = 0; i < cArgs; i++) + RTPrintf(" \"%s\"", papszArgs[i]); + RTPrintf("\n"); + } + + rc = RTProcCreateEx(szPathResolved, papszArgs, pTxsExec->hEnv, 0 /*fFlags*/, + pTxsExec->StdIn.phChild, pTxsExec->StdOut.phChild, pTxsExec->StdErr.phChild, + *pszUsername ? pszUsername : NULL, NULL, NULL, + &pTxsExec->hProcess); + if (RT_SUCCESS(rc)) + { + ASMAtomicWriteBool(&pTxsExec->fProcessAlive, true); + rc2 = RTThreadUserSignal(pTxsExec->hThreadWaiter); AssertRC(rc2); + + /* + * Close the child ends of any pipes and redirected files. + */ + rc2 = RTHandleClose(pTxsExec->StdIn.phChild); AssertRC(rc2); + pTxsExec->StdIn.phChild = NULL; + rc2 = RTHandleClose(pTxsExec->StdOut.phChild); AssertRC(rc2); + pTxsExec->StdOut.phChild = NULL; + rc2 = RTHandleClose(pTxsExec->StdErr.phChild); AssertRC(rc2); + pTxsExec->StdErr.phChild = NULL; + rc2 = RTPipeClose(pTxsExec->hTestPipeW); AssertRC(rc2); + pTxsExec->hTestPipeW = NIL_RTPIPE; + + /* + * Let another worker function funnel output and input to the + * client as well as the process exit code. + */ + rc = txsDoExecHlp2(pTxsExec); + } + } + + if (RT_FAILURE(rc)) + rc = txsReplyFailure(pPktHdr, "FAILED ", "Executing process \"%s\" failed with %Rrc", + pszExecName, rc); + } + else + rc = pTxsExec->rcReplySend; + txsExecDestroy(pTxsExec); + return rc; +} + +/** + * Execute a program. + * + * @returns IPRT status code from send. + * @param pPktHdr The exec packet. + */ +static int txsDoExec(PCTXSPKTHDR pPktHdr) +{ + /* + * This packet has a lot of parameters, most of which are zero terminated + * strings. The strings used in items 7 thru 10 are either file names, + * "/dev/null" or a pipe char (|). + * + * Packet content: + * 1. Flags reserved for future use (32-bit unsigned). + * 2. The executable name (string). + * 3. The argument count given as a 32-bit unsigned integer. + * 4. The arguments strings. + * 5. The number of environment strings (32-bit unsigned). + * 6. The environment strings (var=val) to apply the environment. + * 7. What to do about standard in (string). + * 8. What to do about standard out (string). + * 9. What to do about standard err (string). + * 10. What to do about the test pipe (string). + * 11. The name of the user to run the program as (string). Empty string + * means running it as the current user. + * 12. Process time limit in milliseconds (32-bit unsigned). Max == no limit. + */ + size_t const cbMin = sizeof(TXSPKTHDR) + + sizeof(uint32_t) /* flags */ + 2 + + sizeof(uint32_t) /* argc */ + 2 /* argv */ + + sizeof(uint32_t) + 0 /* environ */ + + 4 * 1 + + sizeof(uint32_t) /* timeout */; + if (pPktHdr->cb < cbMin) + return txsReplyBadMinSize(pPktHdr, cbMin); + + /* unpack the packet */ + char const *pchEnd = (char const *)pPktHdr + pPktHdr->cb; + char const *pch = (char const *)(pPktHdr + 1); /* cursor */ + + /* 1. flags */ + uint32_t const fFlags = *(uint32_t const *)pch; + pch += sizeof(uint32_t); + if (fFlags != 0) + return txsReplyFailure(pPktHdr, "BAD FLAG", "Invalid EXEC flags %#x, expected 0", fFlags); + + /* 2. exec name */ + int rc; + char *pszExecName = NULL; + if (!txsIsStringValid(pPktHdr, "execname", pch, &pszExecName, &pch, &rc)) + return rc; + + /* 3. argc */ + uint32_t const cArgs = (size_t)(pchEnd - pch) > sizeof(uint32_t) ? *(uint32_t const *)pch : 0xff; + pch += sizeof(uint32_t); + if (cArgs * 1 >= (size_t)(pchEnd - pch)) + rc = txsReplyFailure(pPktHdr, "BAD ARGC", "Bad or missing argument count (%#x)", cArgs); + else if (cArgs > 128) + rc = txsReplyFailure(pPktHdr, "BAD ARGC", "Too many arguments (%#x)", cArgs); + else + { + char **papszArgs = (char **)RTMemTmpAllocZ(sizeof(char *) * (cArgs + 1)); + if (papszArgs) + { + /* 4. argv */ + bool fOk = true; + for (size_t i = 0; i < cArgs && fOk; i++) + { + fOk = txsIsStringValid(pPktHdr, "argvN", pch, &papszArgs[i], &pch, &rc); + if (!fOk) + break; + } + if (fOk) + { + /* 5. cEnvVars */ + uint32_t const cEnvVars = (size_t)(pchEnd - pch) > sizeof(uint32_t) ? *(uint32_t const *)pch : 0xfff; + pch += sizeof(uint32_t); + if (cEnvVars * 1 >= (size_t)(pchEnd - pch)) + rc = txsReplyFailure(pPktHdr, "BAD ENVC", "Bad or missing environment variable count (%#x)", cEnvVars); + else if (cEnvVars > 256) + rc = txsReplyFailure(pPktHdr, "BAD ENVC", "Too many environment variables (%#x)", cEnvVars); + else + { + char **papszEnv = (char **)RTMemTmpAllocZ(sizeof(char *) * (cEnvVars + 1)); + if (papszEnv) + { + /* 6. environ */ + for (size_t i = 0; i < cEnvVars && fOk; i++) + { + fOk = txsIsStringValid(pPktHdr, "envN", pch, &papszEnv[i], &pch, &rc); + if (!fOk) /* Bail out on error. */ + break; + } + if (fOk) + { + /* 7. stdout */ + char *pszStdIn; + if (txsIsStringValid(pPktHdr, "stdin", pch, &pszStdIn, &pch, &rc)) + { + /* 8. stdout */ + char *pszStdOut; + if (txsIsStringValid(pPktHdr, "stdout", pch, &pszStdOut, &pch, &rc)) + { + /* 9. stderr */ + char *pszStdErr; + if (txsIsStringValid(pPktHdr, "stderr", pch, &pszStdErr, &pch, &rc)) + { + /* 10. testpipe */ + char *pszTestPipe; + if (txsIsStringValid(pPktHdr, "testpipe", pch, &pszTestPipe, &pch, &rc)) + { + /* 11. username */ + char *pszUsername; + if (txsIsStringValid(pPktHdr, "username", pch, &pszUsername, &pch, &rc)) + { + /** @todo No password value? */ + + /* 12. time limit */ + uint32_t const cMillies = (size_t)(pchEnd - pch) >= sizeof(uint32_t) + ? *(uint32_t const *)pch + : 0; + if ((size_t)(pchEnd - pch) > sizeof(uint32_t)) + rc = txsReplyFailure(pPktHdr, "BAD END ", "Timeout argument not at end of packet (%#x)", (size_t)(pchEnd - pch)); + else if ((size_t)(pchEnd - pch) < sizeof(uint32_t)) + rc = txsReplyFailure(pPktHdr, "BAD NOTO", "No timeout argument"); + else if (cMillies < 1000) + rc = txsReplyFailure(pPktHdr, "BAD TO ", "Timeout is less than a second (%#x)", cMillies); + else + { + pch += sizeof(uint32_t); + + /* + * Time to employ a helper here before we go way beyond + * the right margin... + */ + rc = txsDoExecHlp(pPktHdr, fFlags, pszExecName, + cArgs, papszArgs, + cEnvVars, papszEnv, + pszStdIn, pszStdOut, pszStdErr, pszTestPipe, + pszUsername, + cMillies == UINT32_MAX ? RT_INDEFINITE_WAIT : cMillies); + } + RTStrFree(pszUsername); + } + RTStrFree(pszTestPipe); + } + RTStrFree(pszStdErr); + } + RTStrFree(pszStdOut); + } + RTStrFree(pszStdIn); + } + } + for (size_t i = 0; i < cEnvVars; i++) + RTStrFree(papszEnv[i]); + RTMemTmpFree(papszEnv); + } + else + rc = txsReplyFailure(pPktHdr, "NO MEM ", "Failed to allocate %zu bytes environ", sizeof(char *) * (cEnvVars + 1)); + } + } + for (size_t i = 0; i < cArgs; i++) + RTStrFree(papszArgs[i]); + RTMemTmpFree(papszArgs); + } + else + rc = txsReplyFailure(pPktHdr, "NO MEM ", "Failed to allocate %zu bytes for argv", sizeof(char *) * (cArgs + 1)); + } + RTStrFree(pszExecName); + + return rc; +} + +/** + * The main loop. + * + * @returns exit code. + */ +static RTEXITCODE txsMainLoop(void) +{ + if (g_cVerbose > 0) + RTMsgInfo("txsMainLoop: start...\n"); + RTEXITCODE enmExitCode = RTEXITCODE_SUCCESS; + while (!g_fTerminate) + { + /* + * Read client command packet and process it. + */ + PTXSPKTHDR pPktHdr; + int rc = txsRecvPkt(&pPktHdr, true /*fAutoRetryOnFailure*/); + if (RT_FAILURE(rc)) + continue; + if (g_cVerbose > 0) + RTMsgInfo("txsMainLoop: CMD: %.8s...", pPktHdr->achOpcode); + + /* + * Do a string switch on the opcode bit. + */ + /* Connection: */ + if ( txsIsSameOpcode(pPktHdr, "HOWDY ")) + rc = txsDoHowdy(pPktHdr); + else if (txsIsSameOpcode(pPktHdr, "BYE ")) + rc = txsDoBye(pPktHdr); + else if (txsIsSameOpcode(pPktHdr, "VER ")) + rc = txsDoVer(pPktHdr); + else if (txsIsSameOpcode(pPktHdr, "UUID ")) + rc = txsDoUuid(pPktHdr); + /* Process: */ + else if (txsIsSameOpcode(pPktHdr, "EXEC ")) + rc = txsDoExec(pPktHdr); + /* Admin: */ + else if (txsIsSameOpcode(pPktHdr, "REBOOT ")) + rc = txsDoReboot(pPktHdr); + else if (txsIsSameOpcode(pPktHdr, "SHUTDOWN")) + rc = txsDoShutdown(pPktHdr); + /* CD/DVD control: */ + else if (txsIsSameOpcode(pPktHdr, "CD EJECT")) + rc = txsDoCdEject(pPktHdr); + /* File system: */ + else if (txsIsSameOpcode(pPktHdr, "CLEANUP ")) + rc = txsDoCleanup(pPktHdr); + else if (txsIsSameOpcode(pPktHdr, "MKDIR ")) + rc = txsDoMkDir(pPktHdr); + else if (txsIsSameOpcode(pPktHdr, "MKDRPATH")) + rc = txsDoMkDrPath(pPktHdr); + else if (txsIsSameOpcode(pPktHdr, "MKSYMLNK")) + rc = txsDoMkSymlnk(pPktHdr); + else if (txsIsSameOpcode(pPktHdr, "RMDIR ")) + rc = txsDoRmDir(pPktHdr); + else if (txsIsSameOpcode(pPktHdr, "RMFILE ")) + rc = txsDoRmFile(pPktHdr); + else if (txsIsSameOpcode(pPktHdr, "RMSYMLNK")) + rc = txsDoRmSymlnk(pPktHdr); + else if (txsIsSameOpcode(pPktHdr, "RMTREE ")) + rc = txsDoRmTree(pPktHdr); + else if (txsIsSameOpcode(pPktHdr, "CHMOD ")) + rc = txsDoChMod(pPktHdr); + else if (txsIsSameOpcode(pPktHdr, "CHOWN ")) + rc = txsDoChOwn(pPktHdr); + else if (txsIsSameOpcode(pPktHdr, "ISDIR ")) + rc = txsDoIsDir(pPktHdr); + else if (txsIsSameOpcode(pPktHdr, "ISFILE ")) + rc = txsDoIsFile(pPktHdr); + else if (txsIsSameOpcode(pPktHdr, "ISSYMLNK")) + rc = txsDoIsSymlnk(pPktHdr); + else if (txsIsSameOpcode(pPktHdr, "STAT ")) + rc = txsDoStat(pPktHdr); + else if (txsIsSameOpcode(pPktHdr, "LSTAT ")) + rc = txsDoLStat(pPktHdr); + else if (txsIsSameOpcode(pPktHdr, "LIST ")) + rc = txsDoList(pPktHdr); + else if (txsIsSameOpcode(pPktHdr, "CPFILE ")) + rc = txsDoCopyFile(pPktHdr); + else if (txsIsSameOpcode(pPktHdr, "PUT FILE")) + rc = txsDoPutFile(pPktHdr, false /*fHasMode*/); + else if (txsIsSameOpcode(pPktHdr, "PUT2FILE")) + rc = txsDoPutFile(pPktHdr, true /*fHasMode*/); + else if (txsIsSameOpcode(pPktHdr, "GET FILE")) + rc = txsDoGetFile(pPktHdr); + else if (txsIsSameOpcode(pPktHdr, "PKFILE ")) + rc = txsDoPackFile(pPktHdr); + else if (txsIsSameOpcode(pPktHdr, "UNPKFILE")) + rc = txsDoUnpackFile(pPktHdr); + /* Misc: */ + else if (txsIsSameOpcode(pPktHdr, "EXP STR ")) + rc = txsDoExpandString(pPktHdr); + else + rc = txsReplyUnknown(pPktHdr); + + if (g_cVerbose > 0) + RTMsgInfo("txsMainLoop: CMD: %.8s -> %Rrc", pPktHdr->achOpcode, rc); + RTMemFree(pPktHdr); + } + + if (g_cVerbose > 0) + RTMsgInfo("txsMainLoop: end\n"); + return enmExitCode; +} + + +/** + * Finalizes the scratch directory, making sure it exists. + * + * @returns exit code. + */ +static RTEXITCODE txsFinalizeScratch(void) +{ + RTPathStripTrailingSlash(g_szScratchPath); + char *pszFilename = RTPathFilename(g_szScratchPath); + if (!pszFilename) + return RTMsgErrorExit(RTEXITCODE_FAILURE, "cannot use root for scratch (%s)\n", g_szScratchPath); + + int rc; + if (strchr(pszFilename, 'X')) + { + char ch = *pszFilename; + rc = RTDirCreateFullPath(g_szScratchPath, 0700); + *pszFilename = ch; + if (RT_SUCCESS(rc)) + rc = RTDirCreateTemp(g_szScratchPath, 0700); + } + else + { + if (RTDirExists(g_szScratchPath)) + rc = VINF_SUCCESS; + else + rc = RTDirCreateFullPath(g_szScratchPath, 0700); + } + if (RT_FAILURE(rc)) + return RTMsgErrorExit(RTEXITCODE_FAILURE, "failed to create scratch directory: %Rrc (%s)\n", rc, g_szScratchPath); + return RTEXITCODE_SUCCESS; +} + +/** + * Attempts to complete an upgrade by updating the original and relaunching + * ourselves from there again. + * + * On failure, we'll continue running as the temporary copy. + * + * @returns Exit code. Exit if this is non-zero or @a *pfExit is set. + * @param argc The number of arguments. + * @param argv The argument vector. + * @param pfExit For indicating exit when the exit code is zero. + * @param pszUpgrading The upgraded image path. + */ +static RTEXITCODE txsAutoUpdateStage2(int argc, char **argv, bool *pfExit, const char *pszUpgrading) +{ + if (g_cVerbose > 0) + RTMsgInfo("Auto update stage 2..."); + + /* + * Copy the current executable onto the original. + * Note that we're racing the original program on some platforms, thus the + * 60 sec sleep mess. + */ + char szUpgradePath[RTPATH_MAX]; + if (!RTProcGetExecutablePath(szUpgradePath, sizeof(szUpgradePath))) + { + RTMsgError("RTProcGetExecutablePath failed (step 2)\n"); + return RTEXITCODE_SUCCESS; + } + void *pvUpgrade; + size_t cbUpgrade; + int rc = RTFileReadAll(szUpgradePath, &pvUpgrade, &cbUpgrade); + if (RT_FAILURE(rc)) + { + RTMsgError("RTFileReadAllEx(\"%s\"): %Rrc (step 2)\n", szUpgradePath, rc); + return RTEXITCODE_SUCCESS; + } + + uint64_t StartMilliTS = RTTimeMilliTS(); + RTFILE hFile; + rc = RTFileOpen(&hFile, pszUpgrading, + RTFILE_O_WRITE | RTFILE_O_DENY_WRITE | RTFILE_O_OPEN_CREATE | RTFILE_O_TRUNCATE + | (0755 << RTFILE_O_CREATE_MODE_SHIFT)); + while ( RT_FAILURE(rc) + && RTTimeMilliTS() - StartMilliTS < 60000) + { + RTThreadSleep(1000); + rc = RTFileOpen(&hFile, pszUpgrading, + RTFILE_O_WRITE | RTFILE_O_DENY_WRITE | RTFILE_O_OPEN_CREATE | RTFILE_O_TRUNCATE + | (0755 << RTFILE_O_CREATE_MODE_SHIFT)); + } + if (RT_SUCCESS(rc)) + { + rc = RTFileWrite(hFile, pvUpgrade, cbUpgrade, NULL); + RTFileClose(hFile); + if (RT_SUCCESS(rc)) + { + /* + * Relaunch the service with the original name, foricbly barring + * further upgrade cycles in case of bugs (and simplifying the code). + */ + const char **papszArgs = (const char **)RTMemAlloc((argc + 1 + 1) * sizeof(char **)); + if (papszArgs) + { + papszArgs[0] = pszUpgrading; + for (int i = 1; i < argc; i++) + papszArgs[i] = argv[i]; + papszArgs[argc] = "--no-auto-upgrade"; + papszArgs[argc + 1] = NULL; + + RTMsgInfo("Launching upgraded image: \"%s\"\n", pszUpgrading); + RTPROCESS hProc; + rc = RTProcCreate(pszUpgrading, papszArgs, RTENV_DEFAULT, 0 /*fFlags*/, &hProc); + if (RT_SUCCESS(rc)) + *pfExit = true; + else + RTMsgError("RTProcCreate(\"%s\"): %Rrc (upgrade stage 2)\n", pszUpgrading, rc); + RTMemFree(papszArgs); + } + else + RTMsgError("RTMemAlloc failed during upgrade attempt (stage 2)\n"); + } + else + RTMsgError("RTFileWrite(%s,,%zu): %Rrc (step 2) - BAD\n", pszUpgrading, cbUpgrade, rc); + } + else + RTMsgError("RTFileOpen(,%s,): %Rrc\n", pszUpgrading, rc); + RTFileReadAllFree(pvUpgrade, cbUpgrade); + return RTEXITCODE_SUCCESS; +} + +/** + * Checks for an upgrade and respawns if there is. + * + * @returns Exit code. Exit if this is non-zero or @a *pfExit is set. + * @param argc The number of arguments. + * @param argv The argument vector. + * @param cSecsCdWait Number of seconds to wait on the CD. + * @param pfExit For indicating exit when the exit code is zero. + */ +static RTEXITCODE txsAutoUpdateStage1(int argc, char **argv, uint32_t cSecsCdWait, bool *pfExit) +{ + if (g_cVerbose > 1) + RTMsgInfo("Auto update stage 1..."); + + /* + * Figure names of the current service image and the potential upgrade. + */ + char szOrgPath[RTPATH_MAX]; + if (!RTProcGetExecutablePath(szOrgPath, sizeof(szOrgPath))) + { + RTMsgError("RTProcGetExecutablePath failed\n"); + return RTEXITCODE_SUCCESS; + } + + char szUpgradePath[RTPATH_MAX]; + int rc = RTPathJoin(szUpgradePath, sizeof(szUpgradePath), g_szCdRomPath, g_szOsSlashArchShortName); + if (RT_SUCCESS(rc)) + rc = RTPathAppend(szUpgradePath, sizeof(szUpgradePath), RTPathFilename(szOrgPath)); + if (RT_FAILURE(rc)) + { + RTMsgError("Failed to construct path to potential service upgrade: %Rrc\n", rc); + return RTEXITCODE_SUCCESS; + } + + /* + * Query information about the two images and read the entire potential source file. + * Because the CD may take a little time to be mounted when the system boots, we + * need to do some fudging here. + */ + uint64_t nsStart = RTTimeNanoTS(); + RTFSOBJINFO UpgradeInfo; + for (;;) + { + rc = RTPathQueryInfo(szUpgradePath, &UpgradeInfo, RTFSOBJATTRADD_NOTHING); + if (RT_SUCCESS(rc)) + break; + if ( rc != VERR_FILE_NOT_FOUND + && rc != VERR_PATH_NOT_FOUND + && rc != VERR_MEDIA_NOT_PRESENT + && rc != VERR_MEDIA_NOT_RECOGNIZED) + { + RTMsgError("RTPathQueryInfo(\"%s\"): %Rrc (upgrade)\n", szUpgradePath, rc); + return RTEXITCODE_SUCCESS; + } + uint64_t cNsElapsed = RTTimeNanoTS() - nsStart; + if (cNsElapsed >= cSecsCdWait * RT_NS_1SEC_64) + { + if (g_cVerbose > 0) + RTMsgInfo("Auto update: Giving up waiting for media."); + return RTEXITCODE_SUCCESS; + } + RTThreadSleep(500); + } + + RTFSOBJINFO OrgInfo; + rc = RTPathQueryInfo(szOrgPath, &OrgInfo, RTFSOBJATTRADD_NOTHING); + if (RT_FAILURE(rc)) + { + RTMsgError("RTPathQueryInfo(\"%s\"): %Rrc (old)\n", szOrgPath, rc); + return RTEXITCODE_SUCCESS; + } + + void *pvUpgrade; + size_t cbUpgrade; + rc = RTFileReadAllEx(szUpgradePath, 0, UpgradeInfo.cbObject, RTFILE_RDALL_O_DENY_NONE, &pvUpgrade, &cbUpgrade); + if (RT_FAILURE(rc)) + { + RTMsgError("RTPathQueryInfo(\"%s\"): %Rrc (old)\n", szOrgPath, rc); + return RTEXITCODE_SUCCESS; + } + + /* + * Compare and see if we've got a different service image or not. + */ + if (OrgInfo.cbObject == UpgradeInfo.cbObject) + { + /* must compare bytes. */ + void *pvOrg; + size_t cbOrg; + rc = RTFileReadAllEx(szOrgPath, 0, OrgInfo.cbObject, RTFILE_RDALL_O_DENY_NONE, &pvOrg, &cbOrg); + if (RT_FAILURE(rc)) + { + RTMsgError("RTFileReadAllEx(\"%s\"): %Rrc\n", szOrgPath, rc); + RTFileReadAllFree(pvUpgrade, cbUpgrade); + return RTEXITCODE_SUCCESS; + } + bool fSame = !memcmp(pvUpgrade, pvOrg, OrgInfo.cbObject); + RTFileReadAllFree(pvOrg, cbOrg); + if (fSame) + { + RTFileReadAllFree(pvUpgrade, cbUpgrade); + if (g_cVerbose > 0) + RTMsgInfo("Auto update: Not necessary."); + return RTEXITCODE_SUCCESS; + } + } + + /* + * Should upgrade. Start by creating an executable copy of the update + * image in the scratch area. + */ + RTEXITCODE rcExit = txsFinalizeScratch(); + if (rcExit == RTEXITCODE_SUCCESS) + { + char szTmpPath[RTPATH_MAX]; + rc = RTPathJoin(szTmpPath, sizeof(szTmpPath), g_szScratchPath, RTPathFilename(szOrgPath)); + if (RT_SUCCESS(rc)) + { + RTFileDelete(szTmpPath); /* shouldn't hurt. */ + + RTFILE hFile; + rc = RTFileOpen(&hFile, szTmpPath, + RTFILE_O_WRITE | RTFILE_O_DENY_WRITE | RTFILE_O_CREATE_REPLACE + | (0755 << RTFILE_O_CREATE_MODE_SHIFT)); + if (RT_SUCCESS(rc)) + { + rc = RTFileWrite(hFile, pvUpgrade, UpgradeInfo.cbObject, NULL); + RTFileClose(hFile); + if (RT_SUCCESS(rc)) + { + /* + * Try execute the new image and quit if it works. + */ + const char **papszArgs = (const char **)RTMemAlloc((argc + 2 + 1) * sizeof(char **)); + if (papszArgs) + { + papszArgs[0] = szTmpPath; + for (int i = 1; i < argc; i++) + papszArgs[i] = argv[i]; + papszArgs[argc] = "--upgrading"; + papszArgs[argc + 1] = szOrgPath; + papszArgs[argc + 2] = NULL; + + RTMsgInfo("Launching intermediate automatic upgrade stage: \"%s\"\n", szTmpPath); + RTPROCESS hProc; + rc = RTProcCreate(szTmpPath, papszArgs, RTENV_DEFAULT, 0 /*fFlags*/, &hProc); + if (RT_SUCCESS(rc)) + *pfExit = true; + else + RTMsgError("RTProcCreate(\"%s\"): %Rrc (upgrade stage 1)\n", szTmpPath, rc); + RTMemFree(papszArgs); + } + else + RTMsgError("RTMemAlloc failed during upgrade attempt (stage)\n"); + } + else + RTMsgError("RTFileWrite(%s,,%zu): %Rrc\n", szTmpPath, UpgradeInfo.cbObject, rc); + } + else + RTMsgError("RTFileOpen(,%s,): %Rrc\n", szTmpPath, rc); + } + else + RTMsgError("Failed to construct path to temporary upgrade image: %Rrc\n", rc); + } + + RTFileReadAllFree(pvUpgrade, cbUpgrade); + return rcExit; +} + +/** + * Determines the default configuration. + */ +static void txsSetDefaults(void) +{ + /* + * OS and ARCH. + */ + AssertCompile(sizeof(KBUILD_TARGET) <= sizeof(g_szOsShortName)); + strcpy(g_szOsShortName, KBUILD_TARGET); + + AssertCompile(sizeof(KBUILD_TARGET_ARCH) <= sizeof(g_szArchShortName)); + strcpy(g_szArchShortName, KBUILD_TARGET_ARCH); + + AssertCompile(sizeof(KBUILD_TARGET) + sizeof(KBUILD_TARGET_ARCH) <= sizeof(g_szOsDotArchShortName)); + strcpy(g_szOsDotArchShortName, KBUILD_TARGET); + g_szOsDotArchShortName[sizeof(KBUILD_TARGET) - 1] = '.'; + strcpy(&g_szOsDotArchShortName[sizeof(KBUILD_TARGET)], KBUILD_TARGET_ARCH); + + AssertCompile(sizeof(KBUILD_TARGET) + sizeof(KBUILD_TARGET_ARCH) <= sizeof(g_szOsSlashArchShortName)); + strcpy(g_szOsSlashArchShortName, KBUILD_TARGET); + g_szOsSlashArchShortName[sizeof(KBUILD_TARGET) - 1] = '/'; + strcpy(&g_szOsSlashArchShortName[sizeof(KBUILD_TARGET)], KBUILD_TARGET_ARCH); + +#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2) + strcpy(g_szExeSuff, ".exe"); + strcpy(g_szScriptSuff, ".cmd"); +#else + strcpy(g_szExeSuff, ""); + strcpy(g_szScriptSuff, ".sh"); +#endif + + int rc = RTPathGetCurrent(g_szCwd, sizeof(g_szCwd)); + if (RT_FAILURE(rc)) + RTMsgError("RTPathGetCurrent failed: %Rrc\n", rc); + g_szCwd[sizeof(g_szCwd) - 1] = '\0'; + + if (!RTProcGetExecutablePath(g_szTxsDir, sizeof(g_szTxsDir))) + RTMsgError("RTProcGetExecutablePath failed!\n"); + g_szTxsDir[sizeof(g_szTxsDir) - 1] = '\0'; + RTPathStripFilename(g_szTxsDir); + RTPathStripTrailingSlash(g_szTxsDir); + + /* + * The CD/DVD-ROM location. + */ + /** @todo do a better job here :-) */ +#ifdef RT_OS_WINDOWS + strcpy(g_szDefCdRomPath, "D:/"); +#elif defined(RT_OS_OS2) + strcpy(g_szDefCdRomPath, "D:/"); +#else + if (RTDirExists("/media")) + strcpy(g_szDefCdRomPath, "/media/cdrom"); + else + strcpy(g_szDefCdRomPath, "/mnt/cdrom"); +#endif + strcpy(g_szCdRomPath, g_szDefCdRomPath); + + /* + * Temporary directory. + */ + rc = RTPathTemp(g_szDefScratchPath, sizeof(g_szDefScratchPath)); + if (RT_SUCCESS(rc)) +#if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS) || defined(RT_OS_DOS) + rc = RTPathAppend(g_szDefScratchPath, sizeof(g_szDefScratchPath), "txs-XXXX.tmp"); +#else + rc = RTPathAppend(g_szDefScratchPath, sizeof(g_szDefScratchPath), "txs-XXXXXXXXX.tmp"); +#endif + if (RT_FAILURE(rc)) + { + RTMsgError("RTPathTemp/Append failed when constructing scratch path: %Rrc\n", rc); + strcpy(g_szDefScratchPath, "/tmp/txs-XXXX.tmp"); + } + strcpy(g_szScratchPath, g_szDefScratchPath); + + /* + * The default transporter is the first one. + */ + g_pTransport = g_apTransports[0]; +} + +/** + * Prints the usage. + * + * @param pStrm Where to print it. + * @param pszArgv0 The program name (argv[0]). + */ +static void txsUsage(PRTSTREAM pStrm, const char *pszArgv0) +{ + RTStrmPrintf(pStrm, + "Usage: %Rbn [options]\n" + "\n" + "Options:\n" + " --cdrom \n" + " Where the CD/DVD-ROM will be mounted.\n" + " Default: %s\n" + " --scratch \n" + " Where to put scratch files.\n" + " Default: %s \n" + , + pszArgv0, + g_szDefCdRomPath, + g_szDefScratchPath); + RTStrmPrintf(pStrm, + " --transport \n" + " Use the specified transport layer, one of the following:\n"); + for (size_t i = 0; i < RT_ELEMENTS(g_apTransports); i++) + RTStrmPrintf(pStrm, " %s - %s\n", g_apTransports[i]->szName, g_apTransports[i]->pszDesc); + RTStrmPrintf(pStrm, " Default: %s\n", g_pTransport->szName); + RTStrmPrintf(pStrm, + " --auto-upgrade, --no-auto-upgrade\n" + " To enable or disable the automatic upgrade mechanism where any different\n" + " version found on the CD-ROM on startup will replace the initial copy.\n" + " Default: --auto-upgrade\n" + " --wait-cdrom \n" + " Number of seconds to wait for the CD-ROM to be mounted before giving up\n" + " on automatic upgrading.\n" + " Default: --wait-cdrom 1; solaris: --wait-cdrom 8\n" + " --upgrading \n" + " Internal use only.\n"); + RTStrmPrintf(pStrm, + " --display-output, --no-display-output\n" + " Display the output and the result of all child processes.\n"); + RTStrmPrintf(pStrm, + " --foreground\n" + " Don't daemonize, run in the foreground.\n"); + RTStrmPrintf(pStrm, + " --verbose, -v\n" + " Increases the verbosity level. Can be specified multiple times.\n"); + RTStrmPrintf(pStrm, + " --quiet, -q\n" + " Mutes any logging output.\n"); + RTStrmPrintf(pStrm, + " --help, -h, -?\n" + " Display this message and exit.\n" + " --version, -V\n" + " Display the version and exit.\n"); + + for (size_t i = 0; i < RT_ELEMENTS(g_apTransports); i++) + if (g_apTransports[i]->cOpts) + { + RTStrmPrintf(pStrm, + "\n" + "Options for %s:\n", g_apTransports[i]->szName); + g_apTransports[i]->pfnUsage(g_pStdOut); + } +} + +/** + * Parses the arguments. + * + * @returns Exit code. Exit if this is non-zero or @a *pfExit is set. + * @param argc The number of arguments. + * @param argv The argument vector. + * @param pfExit For indicating exit when the exit code is zero. + */ +static RTEXITCODE txsParseArgv(int argc, char **argv, bool *pfExit) +{ + *pfExit = false; + + /* + * Storage for locally handled options. + */ + bool fAutoUpgrade = true; + bool fDaemonize = true; + bool fDaemonized = false; + const char *pszUpgrading = NULL; +#ifdef RT_OS_SOLARIS + uint32_t cSecsCdWait = 8; +#else + uint32_t cSecsCdWait = 1; +#endif + + /* + * Combine the base and transport layer option arrays. + */ + static const RTGETOPTDEF s_aBaseOptions[] = + { + { "--transport", 't', RTGETOPT_REQ_STRING }, + { "--cdrom", 'c', RTGETOPT_REQ_STRING }, + { "--wait-cdrom", 'w', RTGETOPT_REQ_UINT32 }, + { "--scratch", 's', RTGETOPT_REQ_STRING }, + { "--auto-upgrade", 'a', RTGETOPT_REQ_NOTHING }, + { "--no-auto-upgrade", 'A', RTGETOPT_REQ_NOTHING }, + { "--upgrading", 'U', RTGETOPT_REQ_STRING }, + { "--display-output", 'd', RTGETOPT_REQ_NOTHING }, + { "--no-display-output",'D', RTGETOPT_REQ_NOTHING }, + { "--foreground", 'f', RTGETOPT_REQ_NOTHING }, + { "--daemonized", 'Z', RTGETOPT_REQ_NOTHING }, + { "--quiet", 'q', RTGETOPT_REQ_NOTHING }, + { "--verbose", 'v', RTGETOPT_REQ_NOTHING }, + }; + + size_t cOptions = RT_ELEMENTS(s_aBaseOptions); + for (size_t i = 0; i < RT_ELEMENTS(g_apTransports); i++) + cOptions += g_apTransports[i]->cOpts; + + PRTGETOPTDEF paOptions = (PRTGETOPTDEF)alloca(cOptions * sizeof(RTGETOPTDEF)); + if (!paOptions) + return RTMsgErrorExit(RTEXITCODE_FAILURE, "alloca failed\n"); + + memcpy(paOptions, s_aBaseOptions, sizeof(s_aBaseOptions)); + cOptions = RT_ELEMENTS(s_aBaseOptions); + for (size_t i = 0; i < RT_ELEMENTS(g_apTransports); i++) + { + memcpy(&paOptions[cOptions], g_apTransports[i]->paOpts, g_apTransports[i]->cOpts * sizeof(RTGETOPTDEF)); + cOptions += g_apTransports[i]->cOpts; + } + + /* + * Parse the arguments. + */ + RTGETOPTSTATE GetState; + int rc = RTGetOptInit(&GetState, argc, argv, paOptions, cOptions, 1, 0 /* fFlags */); + AssertRC(rc); + + int ch; + RTGETOPTUNION Val; + while ((ch = RTGetOpt(&GetState, &Val))) + { + switch (ch) + { + case 'a': + fAutoUpgrade = true; + break; + + case 'A': + fAutoUpgrade = false; + break; + + case 'c': + rc = RTStrCopy(g_szCdRomPath, sizeof(g_szCdRomPath), Val.psz); + if (RT_FAILURE(rc)) + return RTMsgErrorExit(RTEXITCODE_FAILURE, "CD/DVD-ROM is path too long (%Rrc)\n", rc); + break; + + case 'd': + g_fDisplayOutput = true; + break; + + case 'D': + g_fDisplayOutput = false; + break; + + case 'f': + fDaemonize = false; + break; + + case 'h': + txsUsage(g_pStdOut, argv[0]); + *pfExit = true; + return RTEXITCODE_SUCCESS; + + case 's': + rc = RTStrCopy(g_szScratchPath, sizeof(g_szScratchPath), Val.psz); + if (RT_FAILURE(rc)) + return RTMsgErrorExit(RTEXITCODE_FAILURE, "scratch path is too long (%Rrc)\n", rc); + break; + + case 't': + { + PCTXSTRANSPORT pTransport = NULL; + for (size_t i = 0; i < RT_ELEMENTS(g_apTransports); i++) + if (!strcmp(g_apTransports[i]->szName, Val.psz)) + { + pTransport = g_apTransports[i]; + break; + } + if (!pTransport) + return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Unknown transport layer name '%s'\n", Val.psz); + g_pTransport = pTransport; + break; + } + + case 'U': + pszUpgrading = Val.psz; + break; + + case 'w': + cSecsCdWait = Val.u32; + break; + + case 'q': + g_cVerbose = 0; + break; + + case 'v': + g_cVerbose++; + break; + + case 'V': + RTPrintf("$Revision: 155244 $\n"); + *pfExit = true; + return RTEXITCODE_SUCCESS; + + case 'Z': + fDaemonized = true; + fDaemonize = false; + break; + + default: + { + rc = VERR_TRY_AGAIN; + for (size_t i = 0; i < RT_ELEMENTS(g_apTransports); i++) + if (g_apTransports[i]->cOpts) + { + rc = g_apTransports[i]->pfnOption(ch, &Val); + if (RT_SUCCESS(rc)) + break; + if (rc != VERR_TRY_AGAIN) + { + *pfExit = true; + return RTEXITCODE_SYNTAX; + } + } + if (rc == VERR_TRY_AGAIN) + { + *pfExit = true; + return RTGetOptPrintError(ch, &Val); + } + break; + } + } + } + + /* + * Handle automatic upgrading of the service. + */ + if (fAutoUpgrade && !*pfExit) + { + RTEXITCODE rcExit; + if (pszUpgrading) + rcExit = txsAutoUpdateStage2(argc, argv, pfExit, pszUpgrading); + else + rcExit = txsAutoUpdateStage1(argc, argv, cSecsCdWait, pfExit); + if ( *pfExit + || rcExit != RTEXITCODE_SUCCESS) + return rcExit; + } + + /* + * Daemonize ourselves if asked to. + */ + if (fDaemonize && !*pfExit) + { + if (g_cVerbose > 0) + RTMsgInfo("Daemonizing..."); + rc = RTProcDaemonize(argv, "--daemonized"); + if (RT_FAILURE(rc)) + return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTProcDaemonize: %Rrc\n", rc); + *pfExit = true; + } + + return RTEXITCODE_SUCCESS; +} + +/** + * @callback_method_impl{FNRTLOGPHASE, Release logger callback} + */ +static DECLCALLBACK(void) logHeaderFooter(PRTLOGGER pLoggerRelease, RTLOGPHASE enmPhase, PFNRTLOGPHASEMSG pfnLog) +{ + /* Some introductory information. */ + static RTTIMESPEC s_TimeSpec; + char szTmp[256]; + if (enmPhase == RTLOGPHASE_BEGIN) + RTTimeNow(&s_TimeSpec); + RTTimeSpecToString(&s_TimeSpec, szTmp, sizeof(szTmp)); + + switch (enmPhase) + { + case RTLOGPHASE_BEGIN: + { + pfnLog(pLoggerRelease, + "TestExecService (Validation Kit TxS) %s r%s (verbosity: %u) %s %s (%s %s) release log\n" + "Copyright (C) " VBOX_C_YEAR " " VBOX_VENDOR "\n\n" + "Log opened %s\n", + RTBldCfgVersion(), RTBldCfgRevisionStr(), g_cVerbose, + KBUILD_TARGET, KBUILD_TARGET_ARCH, + __DATE__, __TIME__, szTmp); + + int vrc = RTSystemQueryOSInfo(RTSYSOSINFO_PRODUCT, szTmp, sizeof(szTmp)); + if (RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW) + pfnLog(pLoggerRelease, "OS Product: %s\n", szTmp); + vrc = RTSystemQueryOSInfo(RTSYSOSINFO_RELEASE, szTmp, sizeof(szTmp)); + if (RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW) + pfnLog(pLoggerRelease, "OS Release: %s\n", szTmp); + vrc = RTSystemQueryOSInfo(RTSYSOSINFO_VERSION, szTmp, sizeof(szTmp)); + if (RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW) + pfnLog(pLoggerRelease, "OS Version: %s\n", szTmp); + vrc = RTSystemQueryOSInfo(RTSYSOSINFO_SERVICE_PACK, szTmp, sizeof(szTmp)); + if (RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW) + pfnLog(pLoggerRelease, "OS Service Pack: %s\n", szTmp); + + /* the package type is interesting for Linux distributions */ + char szExecName[RTPATH_MAX]; + char *pszExecName = RTProcGetExecutablePath(szExecName, sizeof(szExecName)); + pfnLog(pLoggerRelease, + "Executable: %s\n" + "Process ID: %u\n" + "Package type: %s" +#ifdef VBOX_OSE + " (OSE)" +#endif + "\n", + pszExecName ? pszExecName : "unknown", + RTProcSelf(), + VBOX_PACKAGE_STRING); + break; + } + + case RTLOGPHASE_PREROTATE: + pfnLog(pLoggerRelease, "Log rotated - Log started %s\n", szTmp); + break; + + case RTLOGPHASE_POSTROTATE: + pfnLog(pLoggerRelease, "Log continuation - Log started %s\n", szTmp); + break; + + case RTLOGPHASE_END: + pfnLog(pLoggerRelease, "End of log file - Log started %s\n", szTmp); + break; + + default: + /* nothing */ + break; + } +} + +int main(int argc, char **argv) +{ + /* + * Initialize the runtime. + */ + int rc = RTR3InitExe(argc, &argv, 0); + if (RT_FAILURE(rc)) + return RTMsgInitFailure(rc); + + /* + * Determine defaults and parse the arguments. + */ + txsSetDefaults(); + bool fExit; + RTEXITCODE rcExit = txsParseArgv(argc, argv, &fExit); + if (rcExit != RTEXITCODE_SUCCESS || fExit) + return rcExit; + + /* + * Enable (release) TxS logging to stdout + file. This is independent from the actual test cases being run. + * + * Keep the log file path + naming predictable (the OS' temp dir) so that we later can retrieve it + * from the host side without guessing much. + * + * If enabling logging fails for some reason, just tell but don't bail out to not make tests fail. + */ + char szLogFile[RTPATH_MAX]; + rc = RTPathTemp(szLogFile, sizeof(szLogFile)); + if (RT_SUCCESS(rc)) + { + rc = RTPathAppend(szLogFile, sizeof(szLogFile), "vbox-txs-release.log"); + if (RT_FAILURE(rc)) + RTMsgError("RTPathAppend failed when constructing log file path: %Rrc\n", rc); + } + else + RTMsgError("RTPathTemp failed when constructing log file path: %Rrc\n", rc); + + if (RT_SUCCESS(rc)) + { + RTUINT fFlags = RTLOGFLAGS_PREFIX_THREAD | RTLOGFLAGS_PREFIX_TIME_PROG; +#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2) + fFlags |= RTLOGFLAGS_USECRLF; +#endif + static const char * const s_apszLogGroups[] = VBOX_LOGGROUP_NAMES; + rc = RTLogCreateEx(&g_pRelLogger, "VBOX_TXS_RELEASE_LOG", fFlags, "all", + RT_ELEMENTS(s_apszLogGroups), s_apszLogGroups, UINT32_MAX /* cMaxEntriesPerGroup */, + 0 /*cBufDescs*/, NULL /* paBufDescs */, RTLOGDEST_STDOUT | RTLOGDEST_FILE, + logHeaderFooter /* pfnPhase */ , + 10 /* cHistory */, 100 * _1M /* cbHistoryFileMax */, RT_SEC_1DAY /* cSecsHistoryTimeSlot */, + NULL /*pOutputIf*/, NULL /*pvOutputIfUser*/, + NULL /* pErrInfo */, "%s", szLogFile); + if (RT_SUCCESS(rc)) + { + RTLogRelSetDefaultInstance(g_pRelLogger); + if (g_cVerbose) + { + RTMsgInfo("Setting verbosity logging to level %u\n", g_cVerbose); + switch (g_cVerbose) /* Not very elegant, but has to do it for now. */ + { + case 1: + rc = RTLogGroupSettings(g_pRelLogger, "all.e.l.l2"); + break; + + case 2: + rc = RTLogGroupSettings(g_pRelLogger, "all.e.l.l2.l3"); + break; + + case 3: + rc = RTLogGroupSettings(g_pRelLogger, "all.e.l.l2.l3.l4"); + break; + + case 4: + RT_FALL_THROUGH(); + default: + rc = RTLogGroupSettings(g_pRelLogger, "all.e.l.l2.l3.l4.f"); + break; + } + if (RT_FAILURE(rc)) + RTMsgError("Setting logging groups failed, rc=%Rrc\n", rc); + } + } + else + RTMsgError("Failed to create release logger: %Rrc", rc); + + if (RT_SUCCESS(rc)) + RTMsgInfo("Log file written to '%s'\n", szLogFile); + } + + /* + * Generate a UUID for this TXS instance. + */ + rc = RTUuidCreate(&g_InstanceUuid); + if (RT_FAILURE(rc)) + return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTUuidCreate failed: %Rrc", rc); + if (g_cVerbose > 0) + RTMsgInfo("Instance UUID: %RTuuid", &g_InstanceUuid); + + /* + * Finalize the scratch directory and initialize the transport layer. + */ + rcExit = txsFinalizeScratch(); + if (rcExit != RTEXITCODE_SUCCESS) + return rcExit; + + rc = g_pTransport->pfnInit(); + if (RT_FAILURE(rc)) + return RTEXITCODE_FAILURE; + + /* + * Ok, start working + */ + rcExit = txsMainLoop(); + + /* + * Cleanup. + */ + g_pTransport->pfnTerm(); + + return rcExit; +} diff --git a/src/VBox/ValidationKit/utils/TestExecServ/TestExecServiceInternal.h b/src/VBox/ValidationKit/utils/TestExecServ/TestExecServiceInternal.h new file mode 100644 index 00000000..3aaf4578 --- /dev/null +++ b/src/VBox/ValidationKit/utils/TestExecServ/TestExecServiceInternal.h @@ -0,0 +1,232 @@ +/* $Id: TestExecServiceInternal.h $ */ +/** @file + * TestExecServ - Basic Remote Execution Service, Internal Header. + */ + +/* + * Copyright (C) 2010-2023 Oracle and/or its affiliates. + * + * This file is part of VirtualBox base platform packages, as + * available from https://www.virtualbox.org. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, in version 3 of the + * License. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + * + * The contents of this file may alternatively be used under the terms + * of the Common Development and Distribution License Version 1.0 + * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included + * in the VirtualBox distribution, in which case the provisions of the + * CDDL are applicable instead of those of the GPL. + * + * You may elect to license modified versions of this file under the + * terms and conditions of either the GPL or the CDDL or both. + * + * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0 + */ + +#ifndef VBOX_INCLUDED_SRC_TestExecServ_TestExecServiceInternal_h +#define VBOX_INCLUDED_SRC_TestExecServ_TestExecServiceInternal_h +#ifndef RT_WITHOUT_PRAGMA_ONCE +# pragma once +#endif + +#include +#include + +RT_C_DECLS_BEGIN + +/** + * Packet header. + */ +typedef struct TXSPKTHDR +{ + /** The unpadded packet length. This include this header. */ + uint32_t cb; + /** The CRC-32 for the packet starting from the opcode field. 0 if the packet + * hasn't been CRCed. */ + uint32_t uCrc32; + /** Packet opcode, an unterminated ASCII string. */ + uint8_t achOpcode[8]; +} TXSPKTHDR; +AssertCompileSize(TXSPKTHDR, 16); +/** Pointer to a packet header. */ +typedef TXSPKTHDR *PTXSPKTHDR; +/** Pointer to a packet header. */ +typedef TXSPKTHDR const *PCTXSPKTHDR; +/** Pointer to a packet header pointer. */ +typedef PTXSPKTHDR *PPTXSPKTHDR; + +/** Packet alignment. */ +#define TXSPKT_ALIGNMENT 16 +/** Max packet size. */ +#define TXSPKT_MAX_SIZE _256K + + +/** + * Transport layer descriptor. + */ +typedef struct TXSTRANSPORT +{ + /** The name. */ + char szName[16]; + /** The description. */ + const char *pszDesc; + /** Pointer to an array of options. */ + PCRTGETOPTDEF paOpts; + /** The number of options in the array. */ + size_t cOpts; + + /** + * Print the usage information for this transport layer. + * + * @param pStream The stream to print the usage info to. + * + * @remarks This is only required if TXSTRANSPORT::cOpts is greater than 0. + */ + DECLR3CALLBACKMEMBER(void, pfnUsage,(PRTSTREAM pStream)); + + /** + * Handle an option. + * + * When encountering an options that is not part of the base options, we'll call + * this method for each transport layer until one handles it. + * + * @retval VINF_SUCCESS if handled. + * @retval VERR_TRY_AGAIN if not handled. + * @retval VERR_INVALID_PARAMETER if we should exit with a non-zero status. + * + * @param ch The short option value. + * @param pVal Pointer to the value union. + * + * @remarks This is only required if TXSTRANSPORT::cOpts is greater than 0. + */ + DECLR3CALLBACKMEMBER(int, pfnOption,(int ch, PCRTGETOPTUNION pVal)); + + /** + * Initializes the transport layer. + * + * @returns IPRT status code. On errors, the transport layer shall call + * RTMsgError to display the error details to the user. + */ + DECLR3CALLBACKMEMBER(int, pfnInit,(void)); + + /** + * Terminate the transport layer, closing and freeing resources. + * + * On errors, the transport layer shall call RTMsgError to display the error + * details to the user. + */ + DECLR3CALLBACKMEMBER(void, pfnTerm,(void)); + + /** + * Polls for incoming packets. + * + * @returns true if there are pending packets, false if there isn't. + */ + DECLR3CALLBACKMEMBER(bool, pfnPollIn,(void)); + + /** + * Adds any pollable handles to the poll set. + * + * This is optional and layers that doesn't have anything that can be polled + * shall set this method pointer to NULL to indicate that pfnPollIn must be used + * instead. + * + * @returns IPRT status code. + * @param hPollSet The poll set to add them to. + * @param idStart The handle ID to start at. + */ + DECLR3CALLBACKMEMBER(int, pfnPollSetAdd,(RTPOLLSET hPollSet, uint32_t idStart)); + + /** + * Receives an incoming packet. + * + * This will block until the data becomes available or we're interrupted by a + * signal or something. + * + * @returns IPRT status code. On error conditions other than VERR_INTERRUPTED, + * the current operation will be aborted when applicable. When + * interrupted, the transport layer will store the data until the next + * receive call. + * + * @param ppPktHdr Where to return the pointer to the packet we've + * read. This is allocated from the heap using + * RTMemAlloc (w/ TXSPKT_ALIGNMENT) and must be + * free by calling RTMemFree. + */ + DECLR3CALLBACKMEMBER(int, pfnRecvPkt,(PPTXSPKTHDR ppPktHdr)); + + /** + * Sends an outgoing packet. + * + * This will block until the data has been written. + * + * @returns IPRT status code. + * @retval VERR_INTERRUPTED if interrupted before anything was sent. + * + * @param pPktHdr The packet to send. The size is given by + * aligning the size in the header by + * TXSPKT_ALIGNMENT. + */ + DECLR3CALLBACKMEMBER(int, pfnSendPkt,(PCTXSPKTHDR pPktHdr)); + + /** + * Sends a babble packet and disconnects the client (if applicable). + * + * @param pPktHdr The packet to send. The size is given by + * aligning the size in the header by + * TXSPKT_ALIGNMENT. + * @param cMsSendTimeout The send timeout measured in milliseconds. + */ + DECLR3CALLBACKMEMBER(void, pfnBabble,(PCTXSPKTHDR pPktHdr, RTMSINTERVAL cMsSendTimeout)); + + /** + * Notification about a client HOWDY. + */ + DECLR3CALLBACKMEMBER(void, pfnNotifyHowdy,(void)); + + /** + * Notification about a client BYE. + * + * For connection oriented transport layers, it would be good to disconnect the + * client at this point. + */ + DECLR3CALLBACKMEMBER(void, pfnNotifyBye,(void)); + + /** + * Notification about a REBOOT or SHUTDOWN. + * + * For connection oriented transport layers, stop listening for and + * accepting at this point. + */ + DECLR3CALLBACKMEMBER(void, pfnNotifyReboot,(void)); + + /** Non-zero end marker. */ + uint32_t u32EndMarker; +} TXSTRANSPORT; +/** Pointer to a const transport layer descriptor. */ +typedef const struct TXSTRANSPORT *PCTXSTRANSPORT; + + +extern TXSTRANSPORT const g_TcpTransport; +extern TXSTRANSPORT const g_SerialTransport; +extern TXSTRANSPORT const g_FileSysTransport; +extern TXSTRANSPORT const g_GuestPropTransport; +extern TXSTRANSPORT const g_TestDevTransport; + +extern uint32_t g_cVerbose; + +RT_C_DECLS_END + +#endif /* !VBOX_INCLUDED_SRC_TestExecServ_TestExecServiceInternal_h */ + diff --git a/src/VBox/ValidationKit/utils/TestExecServ/TestExecServiceSerial.cpp b/src/VBox/ValidationKit/utils/TestExecServ/TestExecServiceSerial.cpp new file mode 100644 index 00000000..5bfa1059 --- /dev/null +++ b/src/VBox/ValidationKit/utils/TestExecServ/TestExecServiceSerial.cpp @@ -0,0 +1,417 @@ +/* $Id: TestExecServiceSerial.cpp $ */ +/** @file + * TestExecServ - Basic Remote Execution Service, Serial port Transport Layer. + */ + +/* + * Copyright (C) 2018-2023 Oracle and/or its affiliates. + * + * This file is part of VirtualBox base platform packages, as + * available from https://www.virtualbox.org. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, in version 3 of the + * License. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + * + * The contents of this file may alternatively be used under the terms + * of the Common Development and Distribution License Version 1.0 + * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included + * in the VirtualBox distribution, in which case the provisions of the + * CDDL are applicable instead of those of the GPL. + * + * You may elect to license modified versions of this file under the + * terms and conditions of either the GPL or the CDDL or both. + * + * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0 + */ + + +/********************************************************************************************************************************* +* Header Files * +*********************************************************************************************************************************/ +#define LOG_GROUP RTLOGGROUP_DEFAULT +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "TestExecServiceInternal.h" + + +/********************************************************************************************************************************* +* Defined Constants And Macros * +*********************************************************************************************************************************/ +/** The default baud rate port. */ +#define TXS_SERIAL_DEF_BAUDRATE 115200 +/** The default serial device to use. */ +#if defined(RT_OS_LINUX) +# define TXS_SERIAL_DEF_DEVICE "/dev/ttyS0" +#elif defined(RT_OS_WINDOWS) +# define TXS_SERIAL_DEF_DEVICE "COM1" +#elif defined(RT_OS_SOLARIS) +# define TXS_SERIAL_DEF_DEVICE "" +#elif defined(RT_OS_FREEBSD) +# define TXS_SERIAL_DEF_DEVICE "" +#elif defined(RT_OS_DARWIN) +# define TXS_SERIAL_DEF_DEVICE "" +#else +# error "Port me" +#endif + + +/********************************************************************************************************************************* +* Global Variables * +*********************************************************************************************************************************/ +/** @name Serial Parameters + * @{ */ +/** The addresses to bind to. Empty string means any. */ +static uint32_t g_uSerialBaudRate = TXS_SERIAL_DEF_BAUDRATE; +/** The serial port device to use. */ +static char g_szSerialDevice[256] = TXS_SERIAL_DEF_DEVICE; +/** @} */ + +/** The serial port handle. */ +static RTSERIALPORT g_hSerialPort = NIL_RTSERIALPORT; +/** The size of the stashed data. */ +static size_t g_cbSerialStashed = 0; +/** The size of the stashed data allocation. */ +static size_t g_cbSerialStashedAlloced = 0; +/** The stashed data. */ +static uint8_t *g_pbSerialStashed = NULL; + + + +/** + * @interface_method_impl{TXSTRANSPORT,pfnNotifyReboot} + */ +static DECLCALLBACK(void) txsSerialNotifyReboot(void) +{ + /* nothing to do here */ +} + +/** + * @interface_method_impl{TXSTRANSPORT,pfnNotifyBye} + */ +static DECLCALLBACK(void) txsSerialNotifyBye(void) +{ + /* nothing to do here */ +} + +/** + * @interface_method_impl{TXSTRANSPORT,pfnNotifyHowdy} + */ +static DECLCALLBACK(void) txsSerialNotifyHowdy(void) +{ + /* nothing to do here */ +} + +/** + * @interface_method_impl{TXSTRANSPORT,pfnBabble} + */ +static DECLCALLBACK(void) txsSerialBabble(PCTXSPKTHDR pPktHdr, RTMSINTERVAL cMsSendTimeout) +{ + Assert(g_hSerialPort != NIL_RTSERIALPORT); + + /* + * Try send the babble reply. + */ + NOREF(cMsSendTimeout); /** @todo implement the timeout here; non-blocking write + select-on-write. */ + int rc; + size_t cbToSend = RT_ALIGN_Z(pPktHdr->cb, TXSPKT_ALIGNMENT); + do rc = RTSerialPortWrite(g_hSerialPort, pPktHdr, cbToSend, NULL); + while (rc == VERR_INTERRUPTED); + + /* + * Disconnect the client. + */ + Log(("txsSerialBabble: RTSerialPortWrite rc=%Rrc\n", rc)); +} + +/** + * @interface_method_impl{TXSTRANSPORT,pfnSendPkt} + */ +static DECLCALLBACK(int) txsSerialSendPkt(PCTXSPKTHDR pPktHdr) +{ + Assert(g_hSerialPort != NIL_RTSERIALPORT); + Assert(pPktHdr->cb >= sizeof(TXSPKTHDR)); + + /* + * Write it. + */ + size_t cbToSend = RT_ALIGN_Z(pPktHdr->cb, TXSPKT_ALIGNMENT); + int rc = RTSerialPortWrite(g_hSerialPort, pPktHdr, cbToSend, NULL); + if ( RT_FAILURE(rc) + && rc != VERR_INTERRUPTED) + { + /* assume fatal connection error. */ + Log(("RTSerialPortWrite -> %Rrc\n", rc)); + } + + return rc; +} + +/** + * @interface_method_impl{TXSTRANSPORT,pfnRecvPkt} + */ +static DECLCALLBACK(int) txsSerialRecvPkt(PPTXSPKTHDR ppPktHdr) +{ + Assert(g_hSerialPort != NIL_RTSERIALPORT); + + int rc = VINF_SUCCESS; + *ppPktHdr = NULL; + + /* + * Read state. + */ + size_t offData = 0; + size_t cbData = 0; + size_t cbDataAlloced; + uint8_t *pbData = NULL; + + /* + * Any stashed data? + */ + if (g_cbSerialStashedAlloced) + { + offData = g_cbSerialStashed; + cbDataAlloced = g_cbSerialStashedAlloced; + pbData = g_pbSerialStashed; + + g_cbSerialStashed = 0; + g_cbSerialStashedAlloced = 0; + g_pbSerialStashed = NULL; + } + else + { + cbDataAlloced = RT_ALIGN_Z(64, TXSPKT_ALIGNMENT); + pbData = (uint8_t *)RTMemAlloc(cbDataAlloced); + if (!pbData) + return VERR_NO_MEMORY; + } + + /* + * Read and valid the length. + */ + while (offData < sizeof(uint32_t)) + { + size_t cbRead = sizeof(uint32_t) - offData; + rc = RTSerialPortRead(g_hSerialPort, pbData + offData, cbRead, NULL); + if (RT_FAILURE(rc)) + break; + offData += cbRead; + } + if (RT_SUCCESS(rc)) + { + ASMCompilerBarrier(); /* paranoia^3 */ + cbData = *(uint32_t volatile *)pbData; + if (cbData >= sizeof(TXSPKTHDR) && cbData <= TXSPKT_MAX_SIZE) + { + /* + * Align the length and reallocate the return packet it necessary. + */ + cbData = RT_ALIGN_Z(cbData, TXSPKT_ALIGNMENT); + if (cbData > cbDataAlloced) + { + void *pvNew = RTMemRealloc(pbData, cbData); + if (pvNew) + { + pbData = (uint8_t *)pvNew; + cbDataAlloced = cbData; + } + else + rc = VERR_NO_MEMORY; + } + if (RT_SUCCESS(rc)) + { + /* + * Read the remainder of the data. + */ + while (offData < cbData) + { + size_t cbRead = cbData - offData; + rc = RTSerialPortRead(g_hSerialPort, pbData + offData, cbRead, NULL); + if (RT_FAILURE(rc)) + break; + offData += cbRead; + } + } + } + else + rc = VERR_NET_PROTOCOL_ERROR; + } + if (RT_SUCCESS(rc)) + *ppPktHdr = (PTXSPKTHDR)pbData; + else + { + /* + * Deal with errors. + */ + if (rc == VERR_INTERRUPTED) + { + /* stash it away for the next call. */ + g_cbSerialStashed = cbData; + g_cbSerialStashedAlloced = cbDataAlloced; + g_pbSerialStashed = pbData; + } + else + { + RTMemFree(pbData); + + /* assume fatal connection error. */ + Log(("txsSerialRecvPkt: RTSerialPortRead -> %Rrc\n", rc)); + } + } + + return rc; +} + +/** + * @interface_method_impl{TXSTRANSPORT,pfnPollIn} + */ +static DECLCALLBACK(bool) txsSerialPollIn(void) +{ + Assert(g_hSerialPort != NIL_RTSERIALPORT); + + uint32_t fEvtsRecv = 0; + int rc = RTSerialPortEvtPoll(g_hSerialPort, RTSERIALPORT_EVT_F_DATA_RX, + &fEvtsRecv, 0/*cMillies*/); + return RT_SUCCESS(rc); +} + +/** + * @interface_method_impl{TXSTRANSPORT,pfnTerm} + */ +static DECLCALLBACK(void) txsSerialTerm(void) +{ + if (g_hSerialPort != NIL_RTSERIALPORT) + RTSerialPortClose(g_hSerialPort); + + /* Clean up stashing. */ + if (g_pbSerialStashed) + RTMemFree(g_pbSerialStashed); + g_pbSerialStashed = NULL; + g_cbSerialStashed = 0; + g_cbSerialStashedAlloced = 0; + + Log(("txsSerialTerm: done\n")); +} + +/** + * @interface_method_impl{TXSTRANSPORT,pfnInit} + */ +static DECLCALLBACK(int) txsSerialInit(void) +{ + uint32_t fOpenFlags = RTSERIALPORT_OPEN_F_READ | RTSERIALPORT_OPEN_F_WRITE; + int rc = RTSerialPortOpen(&g_hSerialPort, &g_szSerialDevice[0], fOpenFlags); + if (RT_SUCCESS(rc)) + { + RTSERIALPORTCFG SerPortCfg; + + SerPortCfg.uBaudRate = g_uSerialBaudRate; + SerPortCfg.enmParity = RTSERIALPORTPARITY_NONE; + SerPortCfg.enmDataBitCount = RTSERIALPORTDATABITS_8BITS; + SerPortCfg.enmStopBitCount = RTSERIALPORTSTOPBITS_ONE; + rc = RTSerialPortCfgSet(g_hSerialPort, &SerPortCfg, NULL); + if (RT_FAILURE(rc)) + { + RTMsgError("RTSerialPortCfgSet() failed: %Rrc\n", rc); + RTSerialPortClose(g_hSerialPort); + g_hSerialPort = NIL_RTSERIALPORT; + } + } + else + RTMsgError("RTSerialPortOpen(, %s, %#x) failed: %Rrc\n", + g_szSerialDevice, fOpenFlags, rc); + + return rc; +} + +/** Options */ +enum TXSSERIALOPT +{ + TXSSERIALOPT_BAUDRATE = 1000, + TXSSERIALOPT_DEVICE +}; + +/** + * @interface_method_impl{TXSTRANSPORT,pfnOption} + */ +static DECLCALLBACK(int) txsSerialOption(int ch, PCRTGETOPTUNION pVal) +{ + int rc; + + switch (ch) + { + case TXSSERIALOPT_DEVICE: + rc = RTStrCopy(g_szSerialDevice, sizeof(g_szSerialDevice), pVal->psz); + if (RT_FAILURE(rc)) + return RTMsgErrorRc(VERR_INVALID_PARAMETER, "Serial port device path is too long (%Rrc)", rc); + if (!g_szSerialDevice[0]) + strcpy(g_szSerialDevice, TXS_SERIAL_DEF_DEVICE); + return VINF_SUCCESS; + case TXSSERIALOPT_BAUDRATE: + g_uSerialBaudRate = pVal->u32 == 0 ? TXS_SERIAL_DEF_BAUDRATE : pVal->u32; + return VINF_SUCCESS; + } + return VERR_TRY_AGAIN; +} + +/** + * @interface_method_impl{TXSTRANSPORT,pfnUsage} + */ +DECLCALLBACK(void) txsSerialUsage(PRTSTREAM pStream) +{ + RTStrmPrintf(pStream, + " --serial-device \n" + " Selects the serial port to use.\n" + " Default: %s\n" + " --serial-baudrate \n" + " Selects the baudrate to set the serial port to.\n" + " Default: %u\n" + , TXS_SERIAL_DEF_DEVICE, TXS_SERIAL_DEF_BAUDRATE); +} + +/** Command line options for the serial transport layer. */ +static const RTGETOPTDEF g_SerialOpts[] = +{ + { "--serial-device", TXSSERIALOPT_DEVICE, RTGETOPT_REQ_STRING }, + { "--serial-baudrate", TXSSERIALOPT_BAUDRATE, RTGETOPT_REQ_UINT32 } +}; + +/** Serial port transport layer. */ +const TXSTRANSPORT g_SerialTransport = +{ + /* .szName = */ "serial", + /* .pszDesc = */ "Serial", + /* .cOpts = */ &g_SerialOpts[0], + /* .paOpts = */ RT_ELEMENTS(g_SerialOpts), + /* .pfnUsage = */ txsSerialUsage, + /* .pfnOption = */ txsSerialOption, + /* .pfnInit = */ txsSerialInit, + /* .pfnTerm = */ txsSerialTerm, + /* .pfnPollIn = */ txsSerialPollIn, + /* .pfnPollSetAdd = */ NULL, + /* .pfnRecvPkt = */ txsSerialRecvPkt, + /* .pfnSendPkt = */ txsSerialSendPkt, + /* .pfnBabble = */ txsSerialBabble, + /* .pfnNotifyHowdy = */ txsSerialNotifyHowdy, + /* .pfnNotifyBye = */ txsSerialNotifyBye, + /* .pfnNotifyReboot = */ txsSerialNotifyReboot, + /* .u32EndMarker = */ UINT32_C(0x12345678) +}; + diff --git a/src/VBox/ValidationKit/utils/TestExecServ/TestExecServiceTcp.cpp b/src/VBox/ValidationKit/utils/TestExecServ/TestExecServiceTcp.cpp new file mode 100644 index 00000000..2c78477e --- /dev/null +++ b/src/VBox/ValidationKit/utils/TestExecServ/TestExecServiceTcp.cpp @@ -0,0 +1,842 @@ +/* $Id: TestExecServiceTcp.cpp $ */ +/** @file + * TestExecServ - Basic Remote Execution Service, TCP/IP Transport Layer. + */ + +/* + * Copyright (C) 2010-2023 Oracle and/or its affiliates. + * + * This file is part of VirtualBox base platform packages, as + * available from https://www.virtualbox.org. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, in version 3 of the + * License. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + * + * The contents of this file may alternatively be used under the terms + * of the Common Development and Distribution License Version 1.0 + * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included + * in the VirtualBox distribution, in which case the provisions of the + * CDDL are applicable instead of those of the GPL. + * + * You may elect to license modified versions of this file under the + * terms and conditions of either the GPL or the CDDL or both. + * + * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0 + */ + + +/********************************************************************************************************************************* +* Header Files * +*********************************************************************************************************************************/ +#define LOG_GROUP RTLOGGROUP_DEFAULT +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "TestExecServiceInternal.h" + + +/********************************************************************************************************************************* +* Defined Constants And Macros * +*********************************************************************************************************************************/ +/** The default server port. */ +#define TXS_TCP_DEF_BIND_PORT 5042 +/** The default client port. */ +#define TXS_TCP_DEF_CONNECT_PORT 5048 + +/** The default server bind address. */ +#define TXS_TCP_DEF_BIND_ADDRESS "" +/** The default client connect address (i.e. of the host server). */ +#define TXS_TCP_DEF_CONNECT_ADDRESS "10.0.2.2" + + +/********************************************************************************************************************************* +* Global Variables * +*********************************************************************************************************************************/ +/** @name TCP Parameters + * @{ */ +static enum { TXSTCPMODE_BOTH, TXSTCPMODE_CLIENT, TXSTCPMODE_SERVER } + g_enmTcpMode = TXSTCPMODE_BOTH; + +/** The addresses to bind to. Empty string means any. */ +static char g_szTcpBindAddr[256] = TXS_TCP_DEF_BIND_ADDRESS; +/** The TCP port to listen to. */ +static uint32_t g_uTcpBindPort = TXS_TCP_DEF_BIND_PORT; +/** The addresses to connect to if fRevesedSetupMode is @c true. */ +static char g_szTcpConnectAddr[256] = TXS_TCP_DEF_CONNECT_ADDRESS; +/** The TCP port to listen to. */ +static uint32_t g_uTcpConnectPort = TXS_TCP_DEF_CONNECT_PORT; +/** @} */ + +/** Critical section for serializing access to the next few variables. */ +static RTCRITSECT g_TcpCritSect; +/** Pointer to the TCP server instance. */ +static PRTTCPSERVER g_pTcpServer = NULL; +/** Thread calling RTTcpServerListen2. */ +static RTTHREAD g_hThreadTcpServer = NIL_RTTHREAD; +/** Thread calling RTTcpClientConnect. */ +static RTTHREAD g_hThreadTcpConnect = NIL_RTTHREAD; +/** The main thread handle (for signalling). */ +static RTTHREAD g_hThreadMain = NIL_RTTHREAD; +/** Stop connecting attempts when set. */ +static bool g_fTcpStopConnecting = false; +/** Connect cancel cookie. */ +static PRTTCPCLIENTCONNECTCANCEL volatile g_pTcpConnectCancelCookie = NULL; + +/** Socket of the current client. */ +static RTSOCKET g_hTcpClient = NIL_RTSOCKET; +/** Indicates whether g_hTcpClient comes from the server or from a client + * connect (relevant when closing it). */ +static bool g_fTcpClientFromServer = false; +/** The size of the stashed data. */ +static size_t g_cbTcpStashed = 0; +/** The size of the stashed data allocation. */ +static size_t g_cbTcpStashedAlloced = 0; +/** The stashed data. */ +static uint8_t *g_pbTcpStashed = NULL; + + + +/** + * Disconnects the current client. + */ +static void txsTcpDisconnectClient(void) +{ + int rc; + if (g_fTcpClientFromServer) + rc = RTTcpServerDisconnectClient2(g_hTcpClient); + else + rc = RTTcpClientClose(g_hTcpClient); + AssertRCSuccess(rc); + g_hTcpClient = NIL_RTSOCKET; +} + +/** + * Sets the current client socket in a safe manner. + * + * @returns NIL_RTSOCKET if consumed, other wise hTcpClient. + * @param hTcpClient The client socket. + */ +static RTSOCKET txsTcpSetClient(RTSOCKET hTcpClient) +{ + RTCritSectEnter(&g_TcpCritSect); + if ( g_hTcpClient == NIL_RTSOCKET + && !g_fTcpStopConnecting + && g_hThreadMain != NIL_RTTHREAD + ) + { + g_fTcpClientFromServer = true; + g_hTcpClient = hTcpClient; + int rc = RTThreadUserSignal(g_hThreadMain); AssertRC(rc); + hTcpClient = NIL_RTSOCKET; + } + RTCritSectLeave(&g_TcpCritSect); + return hTcpClient; +} + +/** + * Server mode connection thread. + * + * @returns iprt status code. + * @param hSelf Thread handle. Ignored. + * @param pvUser Ignored. + */ +static DECLCALLBACK(int) txsTcpServerConnectThread(RTTHREAD hSelf, void *pvUser) +{ + RTSOCKET hTcpClient; + int rc = RTTcpServerListen2(g_pTcpServer, &hTcpClient); + Log(("txsTcpConnectServerThread: RTTcpServerListen2 -> %Rrc\n", rc)); + if (RT_SUCCESS(rc)) + { + hTcpClient = txsTcpSetClient(hTcpClient); + RTTcpServerDisconnectClient2(hTcpClient); + } + + RT_NOREF2(hSelf, pvUser); + return rc; +} + +/** + * Checks if it's a fatal RTTcpClientConnect return code. + * + * @returns true / false. + * @param rc The IPRT status code. + */ +static bool txsTcpIsFatalClientConnectStatus(int rc) +{ + return rc != VERR_NET_UNREACHABLE + && rc != VERR_NET_HOST_DOWN + && rc != VERR_NET_HOST_UNREACHABLE + && rc != VERR_NET_CONNECTION_REFUSED + && rc != VERR_TIMEOUT + && rc != VERR_NET_CONNECTION_TIMED_OUT; +} + +/** + * Client mode connection thread. + * + * @returns iprt status code. + * @param hSelf Thread handle. Use to sleep on. The main thread will + * signal it to speed up thread shutdown. + * @param pvUser Ignored. + */ +static DECLCALLBACK(int) txsTcpClientConnectThread(RTTHREAD hSelf, void *pvUser) +{ + RT_NOREF1(pvUser); + + for (;;) + { + /* Stop? */ + RTCritSectEnter(&g_TcpCritSect); + bool fStop = g_fTcpStopConnecting; + RTCritSectLeave(&g_TcpCritSect); + if (fStop) + return VINF_SUCCESS; + + /* Try connect. */ /** @todo make cancelable! */ + RTSOCKET hTcpClient; + Log2(("Calling RTTcpClientConnect(%s, %u,)...\n", g_szTcpConnectAddr, g_uTcpConnectPort)); + int rc = RTTcpClientConnectEx(g_szTcpConnectAddr, g_uTcpConnectPort, &hTcpClient, + RT_SOCKETCONNECT_DEFAULT_WAIT, &g_pTcpConnectCancelCookie); + Log(("txsTcpRecvPkt: RTTcpClientConnect -> %Rrc\n", rc)); + if (RT_SUCCESS(rc)) + { + hTcpClient = txsTcpSetClient(hTcpClient); + RTTcpClientCloseEx(hTcpClient, true /* fGracefulShutdown*/); + break; + } + + if (txsTcpIsFatalClientConnectStatus(rc)) + return rc; + + /* Delay a wee bit before retrying. */ + RTThreadUserWait(hSelf, 1536); + } + return VINF_SUCCESS; +} + +/** + * Wait on the threads to complete. + * + * @returns Thread status (if collected), otherwise VINF_SUCCESS. + * @param cMillies The period to wait on each thread. + */ +static int txsTcpConnectWaitOnThreads(RTMSINTERVAL cMillies) +{ + int rcRet = VINF_SUCCESS; + + if (g_hThreadTcpConnect != NIL_RTTHREAD) + { + int rcThread; + int rc2 = RTThreadWait(g_hThreadTcpConnect, cMillies, &rcThread); + if (RT_SUCCESS(rc2)) + { + g_hThreadTcpConnect = NIL_RTTHREAD; + rcRet = rcThread; + } + } + + if (g_hThreadTcpServer != NIL_RTTHREAD) + { + int rcThread; + int rc2 = RTThreadWait(g_hThreadTcpServer, cMillies, &rcThread); + if (RT_SUCCESS(rc2)) + { + g_hThreadTcpServer = NIL_RTTHREAD; + if (RT_SUCCESS(rc2)) + rcRet = rcThread; + } + } + return rcRet; +} + +/** + * Connects to the peer. + * + * @returns VBox status code. Updates g_hTcpClient and g_fTcpClientFromServer on + * success + */ +static int txsTcpConnect(void) +{ + int rc; + if (g_enmTcpMode == TXSTCPMODE_SERVER) + { + g_fTcpClientFromServer = true; + rc = RTTcpServerListen2(g_pTcpServer, &g_hTcpClient); + Log(("txsTcpRecvPkt: RTTcpServerListen2 -> %Rrc\n", rc)); + } + else if (g_enmTcpMode == TXSTCPMODE_CLIENT) + { + g_fTcpClientFromServer = false; + for (;;) + { + Log2(("Calling RTTcpClientConnect(%s, %u,)...\n", g_szTcpConnectAddr, g_uTcpConnectPort)); + rc = RTTcpClientConnect(g_szTcpConnectAddr, g_uTcpConnectPort, &g_hTcpClient); + Log(("txsTcpRecvPkt: RTTcpClientConnect -> %Rrc\n", rc)); + if (RT_SUCCESS(rc) || txsTcpIsFatalClientConnectStatus(rc)) + break; + + /* Delay a wee bit before retrying. */ + RTThreadSleep(1536); + } + } + else + { + Assert(g_enmTcpMode == TXSTCPMODE_BOTH); + RTTHREAD hSelf = RTThreadSelf(); + + /* + * Create client threads. + */ + RTCritSectEnter(&g_TcpCritSect); + RTThreadUserReset(hSelf); + g_hThreadMain = hSelf; + g_fTcpStopConnecting = false; + RTCritSectLeave(&g_TcpCritSect); + + txsTcpConnectWaitOnThreads(32); + + rc = VINF_SUCCESS; + if (g_hThreadTcpConnect == NIL_RTTHREAD) + { + g_pTcpConnectCancelCookie = NULL; + rc = RTThreadCreate(&g_hThreadTcpConnect, txsTcpClientConnectThread, NULL, 0, RTTHREADTYPE_DEFAULT, + RTTHREADFLAGS_WAITABLE, "tcpconn"); + } + if (g_hThreadTcpServer == NIL_RTTHREAD && RT_SUCCESS(rc)) + rc = RTThreadCreate(&g_hThreadTcpServer, txsTcpServerConnectThread, NULL, 0, RTTHREADTYPE_DEFAULT, + RTTHREADFLAGS_WAITABLE, "tcpserv"); + + RTCritSectEnter(&g_TcpCritSect); + + /* + * Wait for connection to be established. + */ + while ( RT_SUCCESS(rc) + && g_hTcpClient == NIL_RTSOCKET) + { + RTCritSectLeave(&g_TcpCritSect); + RTThreadUserWait(hSelf, 1536); + rc = txsTcpConnectWaitOnThreads(0); + RTCritSectEnter(&g_TcpCritSect); + } + + /* + * Cancel the threads. + */ + g_hThreadMain = NIL_RTTHREAD; + g_fTcpStopConnecting = true; + + RTCritSectLeave(&g_TcpCritSect); + RTTcpClientCancelConnect(&g_pTcpConnectCancelCookie); + } + + AssertMsg(RT_SUCCESS(rc) ? g_hTcpClient != NIL_RTSOCKET : g_hTcpClient == NIL_RTSOCKET, ("%Rrc %p\n", rc, g_hTcpClient)); + g_cbTcpStashed = 0; + return rc; +} + +/** + * @interface_method_impl{TXSTRANSPORT,pfnNotifyReboot} + */ +static DECLCALLBACK(void) txsTcpNotifyReboot(void) +{ + Log(("txsTcpNotifyReboot: RTTcpServerDestroy(%p)\n", g_pTcpServer)); + if (g_pTcpServer) + { + int rc = RTTcpServerDestroy(g_pTcpServer); + if (RT_FAILURE(rc)) + RTMsgInfo("RTTcpServerDestroy failed in txsTcpNotifyReboot: %Rrc", rc); + g_pTcpServer = NULL; + } +} + +/** + * @interface_method_impl{TXSTRANSPORT,pfnNotifyBye} + */ +static DECLCALLBACK(void) txsTcpNotifyBye(void) +{ + Log(("txsTcpNotifyBye: txsTcpDisconnectClient %RTsock\n", g_hTcpClient)); + txsTcpDisconnectClient(); +} + +/** + * @interface_method_impl{TXSTRANSPORT,pfnNotifyHowdy} + */ +static DECLCALLBACK(void) txsTcpNotifyHowdy(void) +{ + /* nothing to do here */ +} + +/** + * @interface_method_impl{TXSTRANSPORT,pfnBabble} + */ +static DECLCALLBACK(void) txsTcpBabble(PCTXSPKTHDR pPktHdr, RTMSINTERVAL cMsSendTimeout) +{ + /* + * Quietly ignore already disconnected client. + */ + RTSOCKET hTcpClient = g_hTcpClient; + if (hTcpClient == NIL_RTSOCKET) + return; + + /* + * Try send the babble reply. + */ + NOREF(cMsSendTimeout); /** @todo implement the timeout here; non-blocking write + select-on-write. */ + int rc; + size_t cbToSend = RT_ALIGN_Z(pPktHdr->cb, TXSPKT_ALIGNMENT); + do rc = RTTcpWrite(hTcpClient, pPktHdr, cbToSend); + while (rc == VERR_INTERRUPTED); + + /* + * Disconnect the client. + */ + Log(("txsTcpBabble: txsTcpDisconnectClient(%RTsock) (RTTcpWrite rc=%Rrc)\n", g_hTcpClient, rc)); + txsTcpDisconnectClient(); +} + +/** + * @interface_method_impl{TXSTRANSPORT,pfnSendPkt} + */ +static DECLCALLBACK(int) txsTcpSendPkt(PCTXSPKTHDR pPktHdr) +{ + Assert(pPktHdr->cb >= sizeof(TXSPKTHDR)); + + /* + * Fail if no client connection. + */ + RTSOCKET hTcpClient = g_hTcpClient; + if (hTcpClient == NIL_RTSOCKET) + return VERR_NET_NOT_CONNECTED; + + /* + * Write it. + */ + size_t cbToSend = RT_ALIGN_Z(pPktHdr->cb, TXSPKT_ALIGNMENT); + int rc = RTTcpWrite(hTcpClient, pPktHdr, cbToSend); + if ( RT_FAILURE(rc) + && rc != VERR_INTERRUPTED) + { + /* assume fatal connection error. */ + Log(("RTTcpWrite -> %Rrc -> txsTcpDisconnectClient(%RTsock)\n", rc, g_hTcpClient)); + txsTcpDisconnectClient(); + } + + return rc; +} + +/** + * @interface_method_impl{TXSTRANSPORT,pfnRecvPkt} + */ +static DECLCALLBACK(int) txsTcpRecvPkt(PPTXSPKTHDR ppPktHdr) +{ + int rc = VINF_SUCCESS; + *ppPktHdr = NULL; + + /* + * Do we have to wait for a client to connect? + */ + RTSOCKET hTcpClient = g_hTcpClient; + if (hTcpClient == NIL_RTSOCKET) + { + rc = txsTcpConnect(); + if (RT_FAILURE(rc)) + return rc; + hTcpClient = g_hTcpClient; Assert(hTcpClient != NIL_RTSOCKET); + } + + /* + * Read state. + */ + size_t offData = 0; + size_t cbData = 0; + size_t cbDataAlloced; + uint8_t *pbData = NULL; + + /* + * Any stashed data? + */ + if (g_cbTcpStashedAlloced) + { + offData = g_cbTcpStashed; + cbDataAlloced = g_cbTcpStashedAlloced; + pbData = g_pbTcpStashed; + + g_cbTcpStashed = 0; + g_cbTcpStashedAlloced = 0; + g_pbTcpStashed = NULL; + } + else + { + cbDataAlloced = RT_ALIGN_Z(64, TXSPKT_ALIGNMENT); + pbData = (uint8_t *)RTMemAlloc(cbDataAlloced); + if (!pbData) + return VERR_NO_MEMORY; + } + + /* + * Read and valid the length. + */ + while (offData < sizeof(uint32_t)) + { + size_t cbRead; + rc = RTTcpRead(hTcpClient, pbData + offData, sizeof(uint32_t) - offData, &cbRead); + if (RT_FAILURE(rc)) + break; + if (cbRead == 0) + { + Log(("txsTcpRecvPkt: RTTcpRead -> %Rrc / cbRead=0 -> VERR_NET_NOT_CONNECTED (#1)\n", rc)); + rc = VERR_NET_NOT_CONNECTED; + break; + } + offData += cbRead; + } + if (RT_SUCCESS(rc)) + { + ASMCompilerBarrier(); /* paranoia^3 */ + cbData = *(uint32_t volatile *)pbData; + if (cbData >= sizeof(TXSPKTHDR) && cbData <= TXSPKT_MAX_SIZE) + { + /* + * Align the length and reallocate the return packet it necessary. + */ + cbData = RT_ALIGN_Z(cbData, TXSPKT_ALIGNMENT); + if (cbData > cbDataAlloced) + { + void *pvNew = RTMemRealloc(pbData, cbData); + if (pvNew) + { + pbData = (uint8_t *)pvNew; + cbDataAlloced = cbData; + } + else + rc = VERR_NO_MEMORY; + } + if (RT_SUCCESS(rc)) + { + /* + * Read the remainder of the data. + */ + while (offData < cbData) + { + size_t cbRead; + rc = RTTcpRead(hTcpClient, pbData + offData, cbData - offData, &cbRead); + if (RT_FAILURE(rc)) + break; + if (cbRead == 0) + { + Log(("txsTcpRecvPkt: RTTcpRead -> %Rrc / cbRead=0 -> VERR_NET_NOT_CONNECTED (#2)\n", rc)); + rc = VERR_NET_NOT_CONNECTED; + break; + } + offData += cbRead; + } + } + } + else + rc = VERR_NET_PROTOCOL_ERROR; + } + if (RT_SUCCESS(rc)) + *ppPktHdr = (PTXSPKTHDR)pbData; + else + { + /* + * Deal with errors. + */ + if (rc == VERR_INTERRUPTED) + { + /* stash it away for the next call. */ + g_cbTcpStashed = cbData; + g_cbTcpStashedAlloced = cbDataAlloced; + g_pbTcpStashed = pbData; + } + else + { + RTMemFree(pbData); + + /* assume fatal connection error. */ + Log(("txsTcpRecvPkt: RTTcpRead -> %Rrc -> txsTcpDisconnectClient(%RTsock)\n", rc, g_hTcpClient)); + txsTcpDisconnectClient(); + } + } + + return rc; +} + +/** + * @interface_method_impl{TXSTRANSPORT,pfnPollSetAdd} + */ +static DECLCALLBACK(int) txsTcpPollSetAdd(RTPOLLSET hPollSet, uint32_t idStart) +{ + return RTPollSetAddSocket(hPollSet, g_hTcpClient, RTPOLL_EVT_READ | RTPOLL_EVT_ERROR, idStart); +} + +/** + * @interface_method_impl{TXSTRANSPORT,pfnPollIn} + */ +static DECLCALLBACK(bool) txsTcpPollIn(void) +{ + RTSOCKET hTcpClient = g_hTcpClient; + if (hTcpClient == NIL_RTSOCKET) + return false; + int rc = RTTcpSelectOne(hTcpClient, 0/*cMillies*/); + return RT_SUCCESS(rc); +} + +/** + * @interface_method_impl{TXSTRANSPORT,pfnTerm} + */ +static DECLCALLBACK(void) txsTcpTerm(void) +{ + /* Signal thread */ + if (RTCritSectIsInitialized(&g_TcpCritSect)) + { + RTCritSectEnter(&g_TcpCritSect); + g_fTcpStopConnecting = true; + RTCritSectLeave(&g_TcpCritSect); + } + + if (g_hThreadTcpConnect != NIL_RTTHREAD) + { + RTThreadUserSignal(g_hThreadTcpConnect); + RTTcpClientCancelConnect(&g_pTcpConnectCancelCookie); + } + + /* Shut down the server (will wake up thread). */ + if (g_pTcpServer) + { + Log(("txsTcpTerm: Destroying server...\n")); + int rc = RTTcpServerDestroy(g_pTcpServer); + if (RT_FAILURE(rc)) + RTMsgInfo("RTTcpServerDestroy failed in txsTcpTerm: %Rrc", rc); + g_pTcpServer = NULL; + } + + /* Shut down client */ + if (g_hTcpClient != NIL_RTSOCKET) + { + if (g_fTcpClientFromServer) + { + Log(("txsTcpTerm: Disconnecting client...\n")); + int rc = RTTcpServerDisconnectClient2(g_hTcpClient); + if (RT_FAILURE(rc)) + RTMsgInfo("RTTcpServerDisconnectClient2(%RTsock) failed in txsTcpTerm: %Rrc", g_hTcpClient, rc); + } + else + { + int rc = RTTcpClientClose(g_hTcpClient); + if (RT_FAILURE(rc)) + RTMsgInfo("RTTcpClientClose(%RTsock) failed in txsTcpTerm: %Rrc", g_hTcpClient, rc); + } + g_hTcpClient = NIL_RTSOCKET; + } + + /* Clean up stashing. */ + RTMemFree(g_pbTcpStashed); + g_pbTcpStashed = NULL; + g_cbTcpStashed = 0; + g_cbTcpStashedAlloced = 0; + + /* Wait for the thread (they should've had some time to quit by now). */ + txsTcpConnectWaitOnThreads(15000); + + /* Finally, clean up the critical section. */ + if (RTCritSectIsInitialized(&g_TcpCritSect)) + RTCritSectDelete(&g_TcpCritSect); + + Log(("txsTcpTerm: done\n")); +} + +/** + * @interface_method_impl{TXSTRANSPORT,pfnInit} + */ +static DECLCALLBACK(int) txsTcpInit(void) +{ + int rc = RTCritSectInit(&g_TcpCritSect); + if (RT_SUCCESS(rc) && g_enmTcpMode != TXSTCPMODE_CLIENT) + { + rc = RTTcpServerCreateEx(g_szTcpBindAddr[0] ? g_szTcpBindAddr : NULL, g_uTcpBindPort, &g_pTcpServer); + if (RT_FAILURE(rc)) + { + if (rc == VERR_NET_DOWN) + { + RTMsgInfo("RTTcpServerCreateEx(%s, %u,) failed: %Rrc, retrying for 20 seconds...\n", + g_szTcpBindAddr[0] ? g_szTcpBindAddr : NULL, g_uTcpBindPort, rc); + uint64_t StartMs = RTTimeMilliTS(); + do + { + RTThreadSleep(1000); + rc = RTTcpServerCreateEx(g_szTcpBindAddr[0] ? g_szTcpBindAddr : NULL, g_uTcpBindPort, &g_pTcpServer); + } while ( rc == VERR_NET_DOWN + && RTTimeMilliTS() - StartMs < 20000); + if (RT_SUCCESS(rc)) + RTMsgInfo("RTTcpServerCreateEx succceeded.\n"); + } + if (RT_FAILURE(rc)) + { + g_pTcpServer = NULL; + RTCritSectDelete(&g_TcpCritSect); + RTMsgError("RTTcpServerCreateEx(%s, %u,) failed: %Rrc\n", + g_szTcpBindAddr[0] ? g_szTcpBindAddr : NULL, g_uTcpBindPort, rc); + } + } + } + + return rc; +} + +/** Options */ +enum TXSTCPOPT +{ + TXSTCPOPT_MODE = 1000, + TXSTCPOPT_BIND_ADDRESS, + TXSTCPOPT_BIND_PORT, + TXSTCPOPT_CONNECT_ADDRESS, + TXSTCPOPT_CONNECT_PORT, + + /* legacy: */ + TXSTCPOPT_LEGACY_PORT, + TXSTCPOPT_LEGACY_CONNECT +}; + +/** + * @interface_method_impl{TXSTRANSPORT,pfnOption} + */ +static DECLCALLBACK(int) txsTcpOption(int ch, PCRTGETOPTUNION pVal) +{ + int rc; + + switch (ch) + { + case TXSTCPOPT_MODE: + if (!strcmp(pVal->psz, "both")) + g_enmTcpMode = TXSTCPMODE_BOTH; + else if (!strcmp(pVal->psz, "client")) + g_enmTcpMode = TXSTCPMODE_CLIENT; + else if (!strcmp(pVal->psz, "server")) + g_enmTcpMode = TXSTCPMODE_SERVER; + else + return RTMsgErrorRc(VERR_INVALID_PARAMETER, "Invalid TCP mode: '%s'\n", pVal->psz); + return VINF_SUCCESS; + + case TXSTCPOPT_BIND_ADDRESS: + rc = RTStrCopy(g_szTcpBindAddr, sizeof(g_szTcpBindAddr), pVal->psz); + if (RT_FAILURE(rc)) + return RTMsgErrorRc(VERR_INVALID_PARAMETER, "TCP bind address is too long (%Rrc)", rc); + return VINF_SUCCESS; + + case TXSTCPOPT_BIND_PORT: + g_uTcpBindPort = pVal->u16 == 0 ? TXS_TCP_DEF_BIND_PORT : pVal->u16; + return VINF_SUCCESS; + + case TXSTCPOPT_LEGACY_CONNECT: + g_enmTcpMode = TXSTCPMODE_CLIENT; + RT_FALL_THRU(); + case TXSTCPOPT_CONNECT_ADDRESS: + rc = RTStrCopy(g_szTcpConnectAddr, sizeof(g_szTcpConnectAddr), pVal->psz); + if (RT_FAILURE(rc)) + return RTMsgErrorRc(VERR_INVALID_PARAMETER, "TCP connect address is too long (%Rrc)", rc); + if (!g_szTcpConnectAddr[0]) + strcpy(g_szTcpConnectAddr, TXS_TCP_DEF_CONNECT_ADDRESS); + return VINF_SUCCESS; + + case TXSTCPOPT_CONNECT_PORT: + g_uTcpConnectPort = pVal->u16 == 0 ? TXS_TCP_DEF_CONNECT_PORT : pVal->u16; + return VINF_SUCCESS; + + case TXSTCPOPT_LEGACY_PORT: + if (pVal->u16 == 0) + { + g_uTcpBindPort = TXS_TCP_DEF_BIND_PORT; + g_uTcpConnectPort = TXS_TCP_DEF_CONNECT_PORT; + } + else + { + g_uTcpBindPort = pVal->u16; + g_uTcpConnectPort = pVal->u16; + } + return VINF_SUCCESS; + } + return VERR_TRY_AGAIN; +} + +/** + * @interface_method_impl{TXSTRANSPORT,pfnUsage} + */ +DECLCALLBACK(void) txsTcpUsage(PRTSTREAM pStream) +{ + RTStrmPrintf(pStream, + " --tcp-mode \n" + " Selects the mode of operation.\n" + " Default: both\n" + " --tcp-bind-address
\n" + " The address(es) to listen to TCP connection on. Empty string\n" + " means any address, this is the default.\n" + " --tcp-bind-port \n" + " The port to listen to TCP connections on.\n" + " Default: %u\n" + " --tcp-connect-address
\n" + " The address of the server to try connect to in client mode.\n" + " Default: " TXS_TCP_DEF_CONNECT_ADDRESS "\n" + " --tcp-connect-port \n" + " The port on the server to connect to in client mode.\n" + " Default: %u\n" + , TXS_TCP_DEF_BIND_PORT, TXS_TCP_DEF_CONNECT_PORT); +} + +/** Command line options for the TCP/IP transport layer. */ +static const RTGETOPTDEF g_TcpOpts[] = +{ + { "--tcp-mode", TXSTCPOPT_MODE, RTGETOPT_REQ_STRING }, + { "--tcp-bind-address", TXSTCPOPT_BIND_ADDRESS, RTGETOPT_REQ_STRING }, + { "--tcp-bind-port", TXSTCPOPT_BIND_PORT, RTGETOPT_REQ_UINT16 }, + { "--tcp-connect-address", TXSTCPOPT_CONNECT_ADDRESS, RTGETOPT_REQ_STRING }, + { "--tcp-connect-port", TXSTCPOPT_CONNECT_PORT, RTGETOPT_REQ_UINT16 }, + + /* legacy */ + { "--tcp-port", TXSTCPOPT_LEGACY_PORT, RTGETOPT_REQ_UINT16 }, + { "--tcp-connect", TXSTCPOPT_LEGACY_CONNECT, RTGETOPT_REQ_STRING }, +}; + +/** TCP/IP transport layer. */ +const TXSTRANSPORT g_TcpTransport = +{ + /* .szName = */ "tcp", + /* .pszDesc = */ "TCP/IP", + /* .cOpts = */ &g_TcpOpts[0], + /* .paOpts = */ RT_ELEMENTS(g_TcpOpts), + /* .pfnUsage = */ txsTcpUsage, + /* .pfnOption = */ txsTcpOption, + /* .pfnInit = */ txsTcpInit, + /* .pfnTerm = */ txsTcpTerm, + /* .pfnPollIn = */ txsTcpPollIn, + /* .pfnPollSetAdd = */ txsTcpPollSetAdd, + /* .pfnRecvPkt = */ txsTcpRecvPkt, + /* .pfnSendPkt = */ txsTcpSendPkt, + /* .pfnBabble = */ txsTcpBabble, + /* .pfnNotifyHowdy = */ txsTcpNotifyHowdy, + /* .pfnNotifyBye = */ txsTcpNotifyBye, + /* .pfnNotifyReboot = */ txsTcpNotifyReboot, + /* .u32EndMarker = */ UINT32_C(0x12345678) +}; + diff --git a/src/VBox/ValidationKit/utils/TestExecServ/linux/vboxtxs-nat.sh b/src/VBox/ValidationKit/utils/TestExecServ/linux/vboxtxs-nat.sh new file mode 100755 index 00000000..c666e480 --- /dev/null +++ b/src/VBox/ValidationKit/utils/TestExecServ/linux/vboxtxs-nat.sh @@ -0,0 +1,174 @@ +#!/bin/sh +## @file +# VirtualBox Test Execution Service Init Script for NATted setups. +# + +# +# Copyright (C) 2006-2023 Oracle and/or its affiliates. +# +# This file is part of VirtualBox base platform packages, as +# available from https://www.virtualbox.org. +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation, in version 3 of the +# License. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, see . +# +# The contents of this file may alternatively be used under the terms +# of the Common Development and Distribution License Version 1.0 +# (CDDL), a copy of it is provided in the "COPYING.CDDL" file included +# in the VirtualBox distribution, in which case the provisions of the +# CDDL are applicable instead of those of the GPL. +# +# You may elect to license modified versions of this file under the +# terms and conditions of either the GPL or the CDDL or both. +# +# SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0 +# + +# chkconfig: 35 35 65 +# description: VirtualBox Test Execution Service +# +### BEGIN INIT INFO +# Provides: vboxtxs +# Required-Start: $network +# Required-Stop: +# Default-Start: 2 3 4 5 +# Default-Stop: 0 1 6 +# Description: VirtualBox Test Execution Service +### END INIT INFO + +PATH=$PATH:/bin:/sbin:/usr/sbin +SCRIPTNAME=vboxtxs-nat.sh + +CDROM_PATH=/media/cdrom +SCRATCH_PATH=/tmp/vboxtxs-scratch + +PIDFILE="/var/run/vboxtxs" + +# Preamble for Gentoo +if [ "`which $0`" = "/sbin/rc" ]; then + shift +fi + +begin_msg() +{ + test -n "${2}" && echo "${SCRIPTNAME}: ${1}." + logger -t "${SCRIPTNAME}" "${1}." +} + +succ_msg() +{ + logger -t "${SCRIPTNAME}" "${1}." +} + +fail_msg() +{ + echo "${SCRIPTNAME}: failed: ${1}." >&2 + logger -t "${SCRIPTNAME}" "failed: ${1}." +} + +killproc() { + kp_binary="${1##*/}" + pkill "${kp_binary}" || return 0 + sleep 1 + pkill "${kp_binary}" || return 0 + sleep 1 + pkill -9 "${kp_binary}" + return 0 +} + +case "`uname -m`" in + AMD64|amd64|X86_64|x86_64) + binary=/opt/validationkit/linux/amd64/TestExecService + ;; + + i386|x86|i486|i586|i686|i787) + binary=/opt/validationkit/linux/x86/TestExecService + ;; + + *) + binary=/opt/validationkit/linux/x86/TestExecService + ;; +esac + +fixAndTestBinary() { + chmod a+x "$binary" 2> /dev/null > /dev/null + test -x "$binary" || { + echo "Cannot run $binary" + exit 1 + } +} + +start() { + if ! test -f $PIDFILE; then + begin_msg "Starting VirtualBox Test Execution Service" console + fixAndTestBinary + mount /dev/cdrom "${CDROM_PATH}" 2> /dev/null > /dev/null + $binary --auto-upgrade --scratch="${SCRATCH_PATH}" --cdrom="${CDROM_PATH}" \ + --no-display-output --tcp-connect 10.0.2.2 > /dev/null + RETVAL=$? + test $RETVAL -eq 0 && sleep 2 && echo `pidof TestExecService` > $PIDFILE + if ! test -s "${PIDFILE}"; then + RETVAL=5 + fi + if test $RETVAL -eq 0; then + succ_msg "VirtualBox Test Execution service started" + else + fail_msg "VirtualBox Test Execution service failed to start" + fi + fi + return $RETVAL +} + +stop() { + if test -f $PIDFILE; then + begin_msg "Stopping VirtualBox Test Execution Service" console + killproc $binary + fi +} + +restart() { + stop && start +} + +status() { + echo -n "Checking for vboxtxs" + if [ -f $PIDFILE ]; then + echo " ...running" + else + echo " ...not running" + fi +} + +case "$1" in +start) + start + ;; +stop) + stop + ;; +restart) + restart + ;; +status) + status + ;; +setup) + ;; +cleanup) + ;; +*) + echo "Usage: $0 {start|stop|restart|status}" + exit 1 +esac + +exit $RETVAL diff --git a/src/VBox/ValidationKit/utils/TestExecServ/linux/vboxtxs-runvm.desktop b/src/VBox/ValidationKit/utils/TestExecServ/linux/vboxtxs-runvm.desktop new file mode 100644 index 00000000..95e7ddcb --- /dev/null +++ b/src/VBox/ValidationKit/utils/TestExecServ/linux/vboxtxs-runvm.desktop @@ -0,0 +1,6 @@ +[Desktop Entry] +Type=Application +Encoding=UTF-8 +Name=vboxtxs +Exec=sudo sh -c "/opt/validationkit/linux/vboxtxs-runvm start" + diff --git a/src/VBox/ValidationKit/utils/TestExecServ/linux/vboxtxs-runvm.sh b/src/VBox/ValidationKit/utils/TestExecServ/linux/vboxtxs-runvm.sh new file mode 100755 index 00000000..e44b1546 --- /dev/null +++ b/src/VBox/ValidationKit/utils/TestExecServ/linux/vboxtxs-runvm.sh @@ -0,0 +1,208 @@ +#!/bin/sh +## @file +# VirtualBox Test Execution Service Init Script. +# + +# +# Copyright (C) 2018-2023 Oracle and/or its affiliates. +# +# This file is part of VirtualBox base platform packages, as +# available from https://www.virtualbox.org. +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation, in version 3 of the +# License. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, see . +# +# The contents of this file may alternatively be used under the terms +# of the Common Development and Distribution License Version 1.0 +# (CDDL), a copy of it is provided in the "COPYING.CDDL" file included +# in the VirtualBox distribution, in which case the provisions of the +# CDDL are applicable instead of those of the GPL. +# +# You may elect to license modified versions of this file under the +# terms and conditions of either the GPL or the CDDL or both. +# +# SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0 +# + +# chkconfig: 35 35 65 +# description: VirtualBox Test Execution Service +# +### BEGIN INIT INFO +# Provides: vboxtxs-runvm +# Required-Start: $ALL +# Required-Stop: +# Default-Start: 5 +# Default-Stop: 0 1 6 +# Description: VirtualBox Test Execution Service +### END INIT INFO + +PATH=$PATH:/bin:/sbin:/usr/sbin +SCRIPTNAME=vboxtxs-runvm.sh + +CDROM_PATH=/media/cdrom +SCRATCH_PATH=/tmp/vboxtxs-scratch +SMOKEOUTPUT_PATH=/tmp/vboxtxs-smoketestoutput +DEVKMSG_PATH=/dev/kmsg + +PIDFILE="/var/run/vboxtxs" + +export TESTBOX_PATH_RESOURCES="/home/vbox/testrsrc" +SMOKETEST_SCRIPT="/opt/validationkit/tests/smoketests/tdSmokeTest1.py" +PYTHON_BINARY="python" + +# Preamble for Gentoo +if [ "`which $0`" = "/sbin/rc" ]; then + shift +fi + +kernlog_msg() { + test -n "$2" && echo "${SCRIPTNAME}: ${1}" + echo "${SCRIPTNAME}: ${1}" > $DEVKMSG_PATH +} + +dumpfile_to_kernlog() { + if test -f "$1"; then + kernlog_msg "---------------------- DUMP BEGIN ----------------------" + cat "$1" | while read LINE + do + kernlog_msg "${LINE}" + done + kernlog_msg "---------------------- DUMP END ------------------------" + rm -f "$1" + else + kernlog_msg "${1}: file not found" console + fi +} + +killproc() +{ + kp_binary="${1##*/}" + pkill "${kp_binary}" || return 0 + sleep 1 + pkill "${kp_binary}" || return 0 + sleep 1 + pkill -9 "${kp_binary}" + return 0 +} + +case "`uname -m`" in + AMD64|amd64|X86_64|x86_64) + binary=/opt/validationkit/linux/amd64/TestExecService + ;; + + i386|x86|i486|i586|i686|i787) + binary=/opt/validationkit/linux/x86/TestExecService + ;; + + *) + binary=/opt/validationkit/linux/x86/TestExecService + ;; +esac + +fixAndTestBinary() { + chmod a+x "$binary" 2> /dev/null > /dev/null + test -x "$binary" || { + echo "Cannot run $binary" + exit 1 + } +} + +testRsrcPath() { + test -d "$TESTBOX_PATH_RESOURCES" || { + echo "TESTBOX_PATH_RESOURCES directory not found" + exit 1 + } +} + +start() { + if ! test -f $PIDFILE; then + kernlog_msg "Starting Nested Smoke Test" console + fixAndTestBinary + testRsrcPath + $PYTHON_BINARY $SMOKETEST_SCRIPT -v -v -d --vbox-session-type gui --nic-attachment nat --quick all 1> "${SMOKEOUTPUT_PATH}" 2>&1 + RETVAL=$? + dumpfile_to_kernlog "${SMOKEOUTPUT_PATH}" + sync + sleep 15 + if test $RETVAL -eq 0; then + kernlog_msg "Nested Smoke Test done; Starting Test Execution service" console + mkdir -p "${CDROM_PATH}" + mount -o ro /dev/cdrom "${CDROM_PATH}" 2> /dev/null > /dev/null + $binary --auto-upgrade --scratch="${SCRATCH_PATH}" --cdrom="${CDROM_PATH}" --no-display-output > /dev/null + RETVAL=$? + test $RETVAL -eq 0 && sleep 3 && echo `pidof TestExecService` > $PIDFILE + if ! test -s "${PIDFILE}"; then + RETVAL=5 + fi + if test $RETVAL -eq 0; then + kernlog_msg "Test Execution service started" console + else + kernlog_msg "Test Execution service failed to start" console + RETVAL=6 + fi + else + kernlog_msg "Smoke Test failed! error code ${RETVAL}" console + RETVAL=7 + fi + else + kernlog_msg "Starting Nested Smoke Test failed! Pidfile ${PIDFILE} exists" console + RETVAL=9 + fi + return $RETVAL +} + +stop() { + if test -f $PIDFILE; then + kernlog_msg "Stopping Test Execution Service" + killproc $binary + rm -f $PIDFILE + fi +} + +restart() { + stop && start +} + +status() { + echo -n "Checking for vboxtxs" + if [ -f $PIDFILE ]; then + echo " ...running" + else + echo " ...not running" + fi +} + +case "$1" in +start) + start + ;; +stop) + stop + ;; +restart) + restart + ;; +status) + status + ;; +setup) + ;; +cleanup) + ;; +*) + echo "Usage: $0 {start|stop|restart|status}" + exit 1 +esac + +exit $RETVAL + diff --git a/src/VBox/ValidationKit/utils/TestExecServ/linux/vboxtxs.service b/src/VBox/ValidationKit/utils/TestExecServ/linux/vboxtxs.service new file mode 100644 index 00000000..b61426e9 --- /dev/null +++ b/src/VBox/ValidationKit/utils/TestExecServ/linux/vboxtxs.service @@ -0,0 +1,18 @@ +[Unit] +Description=VirtualBox Test Execution Service +SourcePath=/opt/validationkit/linux/vboxtxs + +[Service] +Type=forking +Restart=no +TimeoutSec=5min +IgnoreSIGPIPE=no +KillMode=process +GuessMainPID=no +RemainAfterExit=yes +ExecStart=/opt/validationkit/linux/vboxtxs start +ExecStop=/opt/validationkit/linux/vboxtxs stop + +[Install] +WantedBy=multi-user.target + diff --git a/src/VBox/ValidationKit/utils/TestExecServ/linux/vboxtxs.sh b/src/VBox/ValidationKit/utils/TestExecServ/linux/vboxtxs.sh new file mode 100755 index 00000000..887ac2d2 --- /dev/null +++ b/src/VBox/ValidationKit/utils/TestExecServ/linux/vboxtxs.sh @@ -0,0 +1,173 @@ +#!/bin/sh +## @file +# VirtualBox Test Execution Service Init Script. +# + +# +# Copyright (C) 2006-2023 Oracle and/or its affiliates. +# +# This file is part of VirtualBox base platform packages, as +# available from https://www.virtualbox.org. +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation, in version 3 of the +# License. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, see . +# +# The contents of this file may alternatively be used under the terms +# of the Common Development and Distribution License Version 1.0 +# (CDDL), a copy of it is provided in the "COPYING.CDDL" file included +# in the VirtualBox distribution, in which case the provisions of the +# CDDL are applicable instead of those of the GPL. +# +# You may elect to license modified versions of this file under the +# terms and conditions of either the GPL or the CDDL or both. +# +# SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0 +# + +# chkconfig: 35 35 65 +# description: VirtualBox Test Execution Service +# +### BEGIN INIT INFO +# Provides: vboxtxs +# Required-Start: $network +# Required-Stop: +# Default-Start: 2 3 4 5 +# Default-Stop: 0 1 6 +# Description: VirtualBox Test Execution Service +### END INIT INFO + +PATH=$PATH:/bin:/sbin:/usr/sbin +SCRIPTNAME=vboxtxs.sh + +CDROM_PATH=/media/cdrom +SCRATCH_PATH=/tmp/vboxtxs-scratch + +PIDFILE="/var/run/vboxtxs" + +# Preamble for Gentoo +if [ "`which $0`" = "/sbin/rc" ]; then + shift +fi + +begin_msg() +{ + test -n "${2}" && echo "${SCRIPTNAME}: ${1}." + logger -t "${SCRIPTNAME}" "${1}." +} + +succ_msg() +{ + logger -t "${SCRIPTNAME}" "${1}." +} + +fail_msg() +{ + echo "${SCRIPTNAME}: failed: ${1}." >&2 + logger -t "${SCRIPTNAME}" "failed: ${1}." +} + +killproc() { + kp_binary="${1##*/}" + pkill "${kp_binary}" || return 0 + sleep 1 + pkill "${kp_binary}" || return 0 + sleep 1 + pkill -9 "${kp_binary}" + return 0 +} + +case "`uname -m`" in + AMD64|amd64|X86_64|x86_64) + binary=/opt/validationkit/linux/amd64/TestExecService + ;; + + i386|x86|i486|i586|i686|i787) + binary=/opt/validationkit/linux/x86/TestExecService + ;; + + *) + binary=/opt/validationkit/linux/x86/TestExecService + ;; +esac + +fixAndTestBinary() { + chmod a+x "$binary" 2> /dev/null > /dev/null + test -x "$binary" || { + echo "Cannot run $binary" + exit 1 + } +} + +start() { + if ! test -f $PIDFILE; then + begin_msg "Starting VirtualBox Test Execution Service" console + fixAndTestBinary + mount /dev/cdrom "${CDROM_PATH}" 2> /dev/null > /dev/null + $binary --auto-upgrade --scratch="${SCRATCH_PATH}" --cdrom="${CDROM_PATH}" --no-display-output > /dev/null + RETVAL=$? + test $RETVAL -eq 0 && sleep 2 && echo `pidof TestExecService` > $PIDFILE + if ! test -s "${PIDFILE}"; then + RETVAL=5 + fi + if test $RETVAL -eq 0; then + succ_msg "VirtualBox Test Execution service started" + else + fail_msg "VirtualBox Test Execution service failed to start" + fi + fi + return $RETVAL +} + +stop() { + if test -f $PIDFILE; then + begin_msg "Stopping VirtualBox Test Execution Service" console + killproc $binary + fi +} + +restart() { + stop && start +} + +status() { + echo -n "Checking for vboxtxs" + if [ -f $PIDFILE ]; then + echo " ...running" + else + echo " ...not running" + fi +} + +case "$1" in +start) + start + ;; +stop) + stop + ;; +restart) + restart + ;; +status) + status + ;; +setup) + ;; +cleanup) + ;; +*) + echo "Usage: $0 {start|stop|restart|status}" + exit 1 +esac + +exit $RETVAL diff --git a/src/VBox/ValidationKit/utils/TestExecServ/solaris/vboxtxs-sol10.xml b/src/VBox/ValidationKit/utils/TestExecServ/solaris/vboxtxs-sol10.xml new file mode 100644 index 00000000..a93901f6 --- /dev/null +++ b/src/VBox/ValidationKit/utils/TestExecServ/solaris/vboxtxs-sol10.xml @@ -0,0 +1,84 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/VBox/ValidationKit/utils/TestExecServ/solaris/vboxtxs.sh b/src/VBox/ValidationKit/utils/TestExecServ/solaris/vboxtxs.sh new file mode 100755 index 00000000..f9f5b9ad --- /dev/null +++ b/src/VBox/ValidationKit/utils/TestExecServ/solaris/vboxtxs.sh @@ -0,0 +1,64 @@ +#!/bin/sh +## @file +# VirtualBox Test Execution Service Architecture Wrapper for Solaris. +# + +# +# Copyright (C) 2010-2023 Oracle and/or its affiliates. +# +# This file is part of VirtualBox base platform packages, as +# available from https://www.virtualbox.org. +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation, in version 3 of the +# License. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, see . +# +# The contents of this file may alternatively be used under the terms +# of the Common Development and Distribution License Version 1.0 +# (CDDL), a copy of it is provided in the "COPYING.CDDL" file included +# in the VirtualBox distribution, in which case the provisions of the +# CDDL are applicable instead of those of the GPL. +# +# You may elect to license modified versions of this file under the +# terms and conditions of either the GPL or the CDDL or both. +# +# SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0 +# + +# 1. Change directory to the script directory (usually /opt/VBoxTest/). +set -x +MY_DIR=`dirname "$0"` +cd "${MY_DIR}" + +# 2. Determine the architecture. +MY_ARCH=`isainfo -k` +case "${MY_ARCH}" in + amd64) + MY_ARCH=amd64 + ;; + i386) + MY_ARCH=x86 + ;; + *) + echo "vboxtxs.sh: Unsupported architecture '${MY_ARCH}' returned by isainfo -k." >&2 + exit 2; + ;; +esac + +# 3. Exec the service. +exec "./${MY_ARCH}/TestExecService" \ + --cdrom="/cdrom/cdrom0/" \ + --scratch="/var/tmp/VBoxTest/" \ + --no-display-output \ + $* +exit 3; + diff --git a/src/VBox/ValidationKit/utils/TestExecServ/solaris/vboxtxs.xml b/src/VBox/ValidationKit/utils/TestExecServ/solaris/vboxtxs.xml new file mode 100644 index 00000000..6bbc614e --- /dev/null +++ b/src/VBox/ValidationKit/utils/TestExecServ/solaris/vboxtxs.xml @@ -0,0 +1,84 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/VBox/ValidationKit/utils/TestExecServ/vboxtxs-readme.txt b/src/VBox/ValidationKit/utils/TestExecServ/vboxtxs-readme.txt new file mode 100644 index 00000000..70de141f --- /dev/null +++ b/src/VBox/ValidationKit/utils/TestExecServ/vboxtxs-readme.txt @@ -0,0 +1,145 @@ +$Id: vboxtxs-readme.txt $ + + +VirtualBox Test eXecution Service +================================= + +This readme briefly describes how to install the Test eXecution Service (TXS) +on the various systems. + +There are currently two transport options for the TXS: + + - The default is to use it in TCP server mode, i.e. the test script needs + to know the guest's IP and therefore requires guest additions to be + installed as well. (Please use the latest stable additions compatible with + the VBox host versions you intend to test.) + + - The alternative is for NATted setups where TXS will act like a TCP client + and try connect to the test script on the host. Since this require that + TXS knows which IP to connect to, it's only really possible in a NATted + setup where we know the host IP is 10.0.2.2. + +Since r85596 TXS operates in both modes by default so the nat version of +the init scripts is not required anymore. Instead the other type can be installed +for both cases. + +Linux Installation +------------------ + +1. mkdir -p /opt/validationkit +2. scp/download VBoxValidationKit*.zip there. +3. unzip VBoxValidationKit*.zip +4. chmod -R u+w,a+x /opt/validationkit/ && chown -R root.root /opt/ +5. cd /etc/init.d/ + +6 a) For init.rc distros: + Link up the right init script (see connection type above): + nat) ln -s ../../opt/validationkit/linux/vboxtxs-nat ./vboxtxs + other) ln -s ../../opt/validationkit/linux/vboxtxs ./vboxtxs +6 b) Add vboxtxs to runlevels 2, 3, 5 and any other that makes sense + on the distro. There is usually some command for doing this, e.g. + ```update-rc.d vboxtxs defaults && update-rc.d vboxtxs enable``` (Debian-based) + or + ```chkconfig --add vboxtxs``` (OL/RHEL) + + ... or ... + +7 a) For systemd distros: Link/copy up the vboxtxs.system to [/usr]/lib/systemd/, e.g. + cp /opt/validationkit/linux/vboxtxs.service /etc/systemd/system + b) Enable the vboxtxs service via: + systemctl enable vboxtxs + + For all distros again: + +8a. Check the CD-ROM location (--cdrom ) in vboxtxs and fix it so it's correct, make sure + to update in svn as well. +8b. Optional: If no suitable CD-ROM location is available on the guest yet, do a: + mkdir -p /media/cdrom; vi /etc/fstab + and enter this in /etc/fstab: + /dev/sr0/media/cdromudf,iso9660user,noauto,exec,utf800 +8c. Optional: If SELinux denies execution of TXS, make sure to allow this, based on + how the distribution handles SELinux exceptions. Often there even is a GUI for that + (e.g. Oracle Linux 8+). +9. Make sure that the package sources are still valid and up to date (apt / yum / ++) +10. reboot / done. +11. Do test. + + +OS/2 Installation +-------------------- + +1. Start an "OS/2 Window" ("OS/2 System" -> "Command Prompts") +2. md C:\Apps +3. cd C:\Apps +4. Mount the validationkit iso. +5. copy D:\os2\x86\* C:\Apps +5. copy D:\os2\x86\libc*.dll C:\OS2\DLL\ +6. Open C:\startup.cmd in an editor (tedit.exe for instance or e.exe). +7. Add the line "start /C C:\Apps\TestExecService.exe --foreground" at the top of the file. +8. reboot / done +9. Do test. + + +Solaris Installation +-------------------- + +1. Start the guest and open a root console. +2. mkdir -p /opt/VBoxTest +3. cd /opt/VBoxTest +4. scp/download VBoxValidationKit*.zip there. +5. unzip VBoxValidationKit*.zip +6. chmod -R u+w,a+x /opt/VBoxTest/ +7. Import the right service setup depending on the Solaris version: + <= 10u9) /usr/sbin/svccfg import /opt/VBoxTest/validationkit/solaris/vboxtxs-sol10.xml + >= 11.0) /usr/sbin/svccfg import /opt/VBoxTest/validationkit/solaris/vboxtxs.xml +8. /usr/sbin/svcadm enable svc:/system/virtualbox/vboxtxs +9. reboot / done. + +To remove the service before repeating steps 7 & 8: +1. /usr/sbin/svcadm disable -s svc:/system/virtualbox/vboxtxs:default +2. /usr/sbin/svccfg delete svc:/system/virtualbox/vboxtxs:default + +Note. To configure dhcp for more a new interface the files +/etc/hostname. and /etc/dhcp. have to exist. If you want the VM +to work with any network card you throw at it, create /etc/*.pcn[01] and +/etc/*.e1000g[012] as Solaris will remember it has seen the other variants +before and use a different instance number (or something to that effect). + + +Windows Installation +-------------------- + +1. Log on as Administrator. +2. Make sure you have set a secure password, which you'll need in step 9. +3. Start CMD.EXE or equivalent. +4. md C:\Apps +5. cd C:\Apps +6. Mount the validationkit iso. +7. copy D:\win\* C:\Apps +8. copy D:\win\\* C:\Apps +9. Put the password from step 2 into the right service setup (see connection + type above) and import it into the registry: + nat) start C:\Apps\vboxtxs-nat.reg + other) start C:\Apps\vboxtxs.reg +10. Make sure that the CD-ROM location is assigned to D: (via "Disk Management"). +11. reboot / done +12. Do test. + +NT 3.1 and 3.x tricks: +- Make sure the file system is NTFS. Observed issues converting 2GB partitions, + more success with smaller. +- For NT3.1 PCNET drivers can be found on the net. No DHCP, so NAT only with + IP 10.0.2.15, 10.0.2.2 as gateway, and 10.0.2.3 as DNS with --natdnsproxy1 on. +- On NT3.1 you need to add SystemDrive=C: to the environment. +- Need to perform registry edits manually. +- Use startup folder instead of non-exising Windows/Run key. + + +Testing the setup +----------------- + +1. Make sure the validationkit.iso is inserted. +2. Boot / reboot the guest. +3. Depending on the TXS transport options: + nat) python testdriver/tst-txsclient.py --reversed-setup + other) python testdriver/tst-txsclient.py --hostname diff --git a/src/VBox/ValidationKit/utils/TestExecServ/vboxtxs-runvm-readme.txt b/src/VBox/ValidationKit/utils/TestExecServ/vboxtxs-runvm-readme.txt new file mode 100644 index 00000000..f8745391 --- /dev/null +++ b/src/VBox/ValidationKit/utils/TestExecServ/vboxtxs-runvm-readme.txt @@ -0,0 +1,54 @@ +$Id: vboxtxs-runvm-readme.txt $ + + +VirtualBox Test eXecution Service +================================= + +This readme briefly describes how to install the Test eXecution Service (TXS) +for nested hardware-virtualization smoke testing on the various systems. + +The basic idea is to execute one smoke test within the VM and then launch +the regular TXS service in the VM to report success or failure to the host. + +Linux Installation +------------------ + +1. scp/download latest release build of VirtualBox and install it in the VM. +2. scp/download the required smoke test VDI from remote test-resource to + /home/vbox/testrsrc/3.0/tcp/win2k3ent-acpi.vdi +3. cd /root +3. scp/download VBoxValidationKit*.zip there. +5. unzip VBoxValidationKit*.zip +6. chmod -R u+w,a+x /opt/validationkit/ +7a. Gnome: Copy /opt/validationkit/linux/vboxtxs-runvm.desktop to /etc/xdg/autostart +7b. KDE/Others: TODO: Document other desktop managers +8. Add the vbox user to sudo group using: + sudo usermod -a -G sudo vbox +9. Ensure no password is required for vbox when using sudo: + sudo echo "vbox ALL=(ALL:ALL) NOPASSWD: ALL" > /etc/sudoers.d/username +10. Check the cdrom location and /dev/kmsg equivalent of your linux distro + in /opt/validationkit/linux/vboxtxs-runvm and fix it so it's correct +11. Reboot / done. + +TODO: Document other OSes as we add them. + +Note: vboxtxs-runvm uses a GUI session to launch the nested-VM for better +visibility when troubleshooting the nested smoke test. + +If this causes problems try troubleshooting the XAUTHORITY and DISPLAY +environment variables in vboxtxs-runvm.service. It might differ depending +on the display manager of the particular linux distro. + + + +Testing the setup +----------------- + +1. Make sure the validationkit.iso is inserted. +2. Boot / reboot the guest. +3. To test the connection - Depending on the TXS transport options: + nat) python testdriver/tst-txsclient.py --reversed-setup + other) python testdriver/tst-txsclient.py --hostname +4. To test the smoke test: + python tests/smoketests/tdSmokeTest1.py -v -v -d --vbox-session-type gui --test-vms + diff --git a/src/VBox/ValidationKit/utils/TestExecServ/win/vboxtxs-nat.cmd b/src/VBox/ValidationKit/utils/TestExecServ/win/vboxtxs-nat.cmd new file mode 100644 index 00000000..5d0ffa0c --- /dev/null +++ b/src/VBox/ValidationKit/utils/TestExecServ/win/vboxtxs-nat.cmd @@ -0,0 +1,41 @@ +@REM REM @file +@REM VirtualBox Test Execution Service Init Script for NATted VMs. +@REM + +@REM +REM +REM Copyright (C) 2006-2023 Oracle and/or its affiliates. +REM +REM This file is part of VirtualBox base platform packages, as +REM available from https://www.virtualbox.org. +REM +REM This program is free software; you can redistribute it and/or +REM modify it under the terms of the GNU General Public License +REM as published by the Free Software Foundation, in version 3 of the +REM License. +REM +REM This program is distributed in the hope that it will be useful, but +REM WITHOUT ANY WARRANTY; without even the implied warranty of +REM MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +REM General Public License for more details. +REM +REM You should have received a copy of the GNU General Public License +REM along with this program; if not, see . +REM +REM The contents of this file may alternatively be used under the terms +REM of the Common Development and Distribution License Version 1.0 +REM (CDDL), a copy of it is provided in the "COPYING.CDDL" file included +REM in the VirtualBox distribution, in which case the provisions of the +REM CDDL are applicable instead of those of the GPL. +REM +REM You may elect to license modified versions of this file under the +REM terms and conditions of either the GPL or the CDDL or both. +REM +REM SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0 +REM + +%SystemDrive%\Apps\TestExecService.exe --foreground --display-output ^ +--cdrom D:\ --scratch C:\Temp\vboxtest --auto-upgrade ^ +--tcp-connect 10.0.2.2 +pause + diff --git a/src/VBox/ValidationKit/utils/TestExecServ/win/vboxtxs-nat.reg b/src/VBox/ValidationKit/utils/TestExecServ/win/vboxtxs-nat.reg new file mode 100644 index 00000000..20ab995b --- /dev/null +++ b/src/VBox/ValidationKit/utils/TestExecServ/win/vboxtxs-nat.reg @@ -0,0 +1,13 @@ +REGEDIT4 + +[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon] +"PowerdownAfterShutdown"="1" +"AutoAdminLogon"="1" +"ForceAutoLogon"="1" +"DefaultUserName"="Administrator" +; Placeholder password for test VM, see TestExecServ/vboxtxs-readme.txt +"DefaultPassword"="Please_put_your_secure_password_here_see_TestExecServ/vboxtxs-readme.txt" + +[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run] +"NTConfiguration"="c:\\Apps\\vboxtxs-nat.cmd" + diff --git a/src/VBox/ValidationKit/utils/TestExecServ/win/vboxtxs.cmd b/src/VBox/ValidationKit/utils/TestExecServ/win/vboxtxs.cmd new file mode 100644 index 00000000..6b41153e --- /dev/null +++ b/src/VBox/ValidationKit/utils/TestExecServ/win/vboxtxs.cmd @@ -0,0 +1,40 @@ +@REM REM @file +@REM VirtualBox Test Execution Service Init Script. +@REM + +@REM +REM +REM Copyright (C) 2006-2023 Oracle and/or its affiliates. +REM +REM This file is part of VirtualBox base platform packages, as +REM available from https://www.virtualbox.org. +REM +REM This program is free software; you can redistribute it and/or +REM modify it under the terms of the GNU General Public License +REM as published by the Free Software Foundation, in version 3 of the +REM License. +REM +REM This program is distributed in the hope that it will be useful, but +REM WITHOUT ANY WARRANTY; without even the implied warranty of +REM MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +REM General Public License for more details. +REM +REM You should have received a copy of the GNU General Public License +REM along with this program; if not, see . +REM +REM The contents of this file may alternatively be used under the terms +REM of the Common Development and Distribution License Version 1.0 +REM (CDDL), a copy of it is provided in the "COPYING.CDDL" file included +REM in the VirtualBox distribution, in which case the provisions of the +REM CDDL are applicable instead of those of the GPL. +REM +REM You may elect to license modified versions of this file under the +REM terms and conditions of either the GPL or the CDDL or both. +REM +REM SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0 +REM + +%SystemDrive%\Apps\TestExecService.exe --foreground --display-output ^ +--cdrom D:\ --scratch C:\Temp\vboxtest --auto-upgrade +pause + diff --git a/src/VBox/ValidationKit/utils/TestExecServ/win/vboxtxs.reg b/src/VBox/ValidationKit/utils/TestExecServ/win/vboxtxs.reg new file mode 100644 index 00000000..037ac425 --- /dev/null +++ b/src/VBox/ValidationKit/utils/TestExecServ/win/vboxtxs.reg @@ -0,0 +1,13 @@ +REGEDIT4 + +[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon] +"PowerdownAfterShutdown"="1" +"AutoAdminLogon"="1" +"ForceAutoLogon"="1" +"DefaultUserName"="Administrator" +; Sample password for test VM, see TestExecServ/vboxtxs-readme.txt +"DefaultPassword"="Please_put_your_secure_password_here_see_TestExecServ/vboxtxs-readme.txt" + +[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run] +"NTConfiguration"="c:\\Apps\\vboxtxs.cmd" + diff --git a/src/VBox/ValidationKit/utils/TestExecServ/win/vboxtxs.xml b/src/VBox/ValidationKit/utils/TestExecServ/win/vboxtxs.xml new file mode 100644 index 00000000..4dde1f83 --- /dev/null +++ b/src/VBox/ValidationKit/utils/TestExecServ/win/vboxtxs.xml @@ -0,0 +1,75 @@ + + + + + 2019-05-27T18:41:34.3048528 + Administrator + + + + true + Administrator + + + + + Administrator + InteractiveToken + HighestAvailable + + + + IgnoreNew + false + false + true + false + false + + false + false + + true + true + false + false + false + PT0S + 7 + + + + C:\Apps\vboxtxs.cmd + + + + diff --git a/src/VBox/ValidationKit/utils/audio/Makefile.kmk b/src/VBox/ValidationKit/utils/audio/Makefile.kmk new file mode 100644 index 00000000..f249189f --- /dev/null +++ b/src/VBox/ValidationKit/utils/audio/Makefile.kmk @@ -0,0 +1,220 @@ +# $Id: Makefile.kmk $ +## @file +# VirtualBox Validation Kit - Audio Utilities. +# + +# +# Copyright (C) 2010-2023 Oracle and/or its affiliates. +# +# This file is part of VirtualBox base platform packages, as +# available from https://www.virtualbox.org. +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation, in version 3 of the +# License. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, see . +# +# The contents of this file may alternatively be used under the terms +# of the Common Development and Distribution License Version 1.0 +# (CDDL), a copy of it is provided in the "COPYING.CDDL" file included +# in the VirtualBox distribution, in which case the provisions of the +# CDDL are applicable instead of those of the GPL. +# +# You may elect to license modified versions of this file under the +# terms and conditions of either the GPL or the CDDL or both. +# +# SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0 +# + +SUB_DEPTH = ../../../../.. +include $(KBUILD_PATH)/subheader.kmk + +# +# Make sure the ValKit config file is included when the additions build +# is including just this makefile. +# +ifndef VBOX_VALIDATIONKIT_CONFIG_KMK_INCLUDED + include $(PATH_ROOT)/src/VBox/ValidationKit/Config.kmk +endif + + +# +# Globals. +# +VBOX_PATH_SRC_DEVICES = $(PATH_ROOT)/src/VBox/Devices +VKAT_PATH_AUDIO = $(VBOX_PATH_SRC_DEVICES)/Audio + + +# +# Append what we build here to PROGRAMS (at the top because it's a bit messy). +# +ifn1of ($(KBUILD_TARGET), os2 freebsd netbsd openbsd) + if defined(VBOX_ONLY_VALIDATIONKIT) || !defined(VBOX_ONLY_BUILD) + PROGRAMS += vkat + if defined(VBOX_WITH_HOST_SHIPPING_AUDIO_TEST) && !defined(VBOX_ONLY_BUILD) + PROGRAMS += vkathost + endif + endif + if defined(VBOX_WITH_ADDITIONS_SHIPPING_AUDIO_TEST) \ + && defined(VBOX_WITH_ADDITIONS) \ + && !defined(VBOX_WITH_ADDITIONS_FROM_BUILD_SERVER) \ + && (defined(VBOX_ONLY_ADDITIONS) || !defined(VBOX_ONLY_BUILD)) + PROGRAMS += vkatadd + endif +endif + + +# +# Utility to play sine wave to Default Audio Device. +# +if 0 # Disabled for now; does not work without WinMM.dll import validator files. + PROGRAMS.win += ntPlayToneWaveX + ntPlayToneWaveX_TEMPLATE = VBoxValidationKitR3 + ntPlayToneWaveX_SOURCES = ntPlayToneWaveX.cpp + ntPlayToneWaveX_LIBS += \ + WinMM.Lib +endif + + +# +# The Validation Kit Audio Test (VKAT) utility. +# +vkat_TEMPLATE = VBoxValidationKitR3 +vkat_VBOX_IMPORT_CHECKER.win.x86 = nt4 +vkat_DEFS = VBOX_AUDIO_VKAT IN_VMM_R3 IN_VMM_STATIC +vkat_INCS = \ + $(PATH_ROOT)/src/VBox/Devices/build \ + $(PATH_ROOT)/src/VBox/Devices \ + $(PATH_ROOT)/src/VBox/Devices/Audio +vkat_SOURCES = \ + vkat.cpp \ + vkatCommon.cpp \ + vkatCmdGeneric.cpp \ + vkatDriverStack.cpp \ + $(VKAT_PATH_AUDIO)/AudioTest.cpp \ + $(VKAT_PATH_AUDIO)/DrvAudio.cpp \ + $(VKAT_PATH_AUDIO)/DrvHostAudioNull.cpp \ + $(VKAT_PATH_AUDIO)/AudioMixer.cpp \ + $(VKAT_PATH_AUDIO)/AudioMixBuffer.cpp \ + $(VKAT_PATH_AUDIO)/AudioHlp.cpp + +# Debug stuff. +ifdef VBOX_WITH_AUDIO_DEBUG + vkat_DEFS += VBOX_WITH_AUDIO_DEBUG + vkat_SOURCES += \ + $(VKAT_PATH_AUDIO)/DrvHostAudioDebug.cpp +endif + +# Self-test stuff. +vkat_DEFS += VBOX_WITH_AUDIO_VALIDATIONKIT +vkat_SOURCES += \ + vkatCmdSelfTest.cpp \ + $(VKAT_PATH_AUDIO)/DrvHostAudioValidationKit.cpp \ + $(VKAT_PATH_AUDIO)/AudioTestService.cpp \ + $(VKAT_PATH_AUDIO)/AudioTestServiceClient.cpp \ + $(VKAT_PATH_AUDIO)/AudioTestServiceProtocol.cpp \ + $(VKAT_PATH_AUDIO)/AudioTestServiceTcp.cpp + +ifdef VBOX_WITH_AUDIO_PULSE + vkat_DEFS += VBOX_WITH_AUDIO_PULSE + vkat_SOURCES += \ + $(VKAT_PATH_AUDIO)/DrvHostAudioPulseAudioStubs.cpp \ + $(VKAT_PATH_AUDIO)/DrvHostAudioPulseAudio.cpp +endif + +ifdef VBOX_WITH_AUDIO_ALSA + vkat_DEFS += VBOX_WITH_AUDIO_ALSA + vkat_SOURCES += \ + $(VKAT_PATH_AUDIO)/DrvHostAudioAlsa.cpp \ + $(VKAT_PATH_AUDIO)/DrvHostAudioAlsaStubs.cpp +endif + +ifdef VBOX_WITH_AUDIO_OSS + vkat_DEFS += VBOX_WITH_AUDIO_OSS + vkat_SOURCES += \ + $(VKAT_PATH_AUDIO)/DrvHostAudioOss.cpp +endif + +vkat_SOURCES.win += \ + $(VKAT_PATH_AUDIO)/DrvHostAudioDSound.cpp \ + $(VKAT_PATH_AUDIO)/DrvHostAudioWasApi.cpp +ifdef VBOX_WITH_AUDIO_MMNOTIFICATION_CLIENT + vkat_DEFS.win += VBOX_WITH_AUDIO_MMNOTIFICATION_CLIENT + vkat_SOURCES.win += \ + $(VKAT_PATH_AUDIO)/DrvHostAudioDSoundMMNotifClient.cpp +endif + +vkat_SOURCES.darwin = \ + $(VKAT_PATH_AUDIO)/DrvHostAudioCoreAudio.cpp \ + $(VKAT_PATH_AUDIO)/DrvHostAudioCoreAudioAuth.mm +vkat_LDFLAGS.darwin = \ + -framework CoreAudio \ + -framework AudioUnit \ + -framework AudioToolbox \ + -framework Foundation +ifn1of ($(VBOX_DEF_MACOSX_VERSION_MIN), 10.4 10.5 10.6) + vkat_LDFLAGS.darwin += \ + -framework AVFoundation +endif + + +# +# The additions variant of the audio test utility. +# +# We name it VBoxAudioTest though, to not clutter up Guest Additions +# installations with cryptic binaries not sporting 'VBox' as prefix. +# +vkatadd_TEMPLATE = VBoxGuestR3Exe +vkatadd_EXTENDS = vkat +vkatadd_EXTENDS_BY = appending +vkatadd_NAME = VBoxAudioTest +vkatadd_SDKS = VBoxZlibStatic +vkatadd_LDFLAGS.darwin = -framework IOKit +vkatadd_LIBS.solaris = m + + +# +# Build the valkit vkat to bin as VBoxAudioTest, so that it can be shipped with +# the host installer too. +# +# Note: We also need to have this as a signed binary, so don't just copy the +# vkat binary to bin/ directory but built this as an own binary. +# +vkathost_TEMPLATE := VBoxR3Exe +vkathost_EXTENDS := vkat +vkathost_INST := $(INST_BIN) +vkathost_NAME := VBoxAudioTest +vkathost_SOURCES = \ + $(vkat_SOURCES) \ + $(VBOX_PATH_SRC_DEVICES)/build/VBoxDD.d +vkathost_LIBS = \ + $(LIB_RUNTIME) + + +if defined(VBOX_WITH_TESTCASES) && !defined(VBOX_ONLY_ADDITIONS) && !defined(VBOX_ONLY_SDK) \ + && 0 ## @todo r=bird: Disabled because nobody really wants or needs to run this during build other than Andy. + ## And more importantly, it breaks the build (os2, bsd*). + + PROGRAMS += tstVkatHostSelftest + tstVkatHostSelftest_EXTENDS = vkat + tstVkatHostSelftest_EXTENDS_BY = appending + tstVkatHostSelftest_INST = $(INST_TESTCASE) + tstVkatHostSelftest_DEFS.debug = VBOX_WITH_EF_WRAPS + + TESTING += $(tstVkatHostSelftest_0_OUTDIR)/tstVkatHostSelftest.run + $$(tstVkatHostSelftest_0_OUTDIR)/tstVkatHostSelftest.run: $$(tstVkatHostSelftest_1_TARGET) + export VKAT_RELEASE_LOG=-all; $(tstVkatHostSelftest_1_TARGET) selftest + $(QUIET)$(APPEND) -t "$@" "done" + +endif + +include $(FILE_KBUILD_SUB_FOOTER) + diff --git a/src/VBox/ValidationKit/utils/audio/ntPlayToneWaveX.cpp b/src/VBox/ValidationKit/utils/audio/ntPlayToneWaveX.cpp new file mode 100644 index 00000000..e07165dc --- /dev/null +++ b/src/VBox/ValidationKit/utils/audio/ntPlayToneWaveX.cpp @@ -0,0 +1,226 @@ +/* $Id: ntPlayToneWaveX.cpp $ */ +/** @file + * ???? + */ + +/* + * Copyright (C) 2012-2023 Oracle and/or its affiliates. + * + * This file is part of VirtualBox base platform packages, as + * available from https://www.virtualbox.org. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, in version 3 of the + * License. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + * + * The contents of this file may alternatively be used under the terms + * of the Common Development and Distribution License Version 1.0 + * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included + * in the VirtualBox distribution, in which case the provisions of the + * CDDL are applicable instead of those of the GPL. + * + * You may elect to license modified versions of this file under the + * terms and conditions of either the GPL or the CDDL or both. + * + * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0 + */ + + +/********************************************************************************************************************************* +* Header Files * +*********************************************************************************************************************************/ +#include + +#include +#include +#include +#include +#include + +#define _USE_MATH_DEFINES +#include + + +/********************************************************************************************************************************* +* Global Variables * +*********************************************************************************************************************************/ +uint32_t g_cSamplesPerSec = 44100; +uint32_t g_cSamplesPerPeriod = 100; // 441.0Hz for 44.1kHz +uint32_t g_cSamplesInBuffer = 4096; +double g_rdSecDuration = 5.0; + +uint32_t g_cbSample; // assuming 16-bit stereo (for now) + +HWAVEOUT g_hWaveOut; +HANDLE g_hWavEvent; + + +int main(int argc, char **argv) +{ + int rc = RTR3InitExe(argc, &argv, 0); + if (RT_FAILURE(rc)) + return RTMsgInitFailure(rc); + + static const RTGETOPTDEF s_aOptions[] = + { + { "--samples-per-sec", 's', RTGETOPT_REQ_UINT32 }, + { "--period-in-samples", 'p', RTGETOPT_REQ_UINT32 }, + { "--bufsize-in-samples", 'b', RTGETOPT_REQ_UINT32 }, + { "--total-duration-in-secs", 'd', RTGETOPT_REQ_UINT32 } + }; + + RTGETOPTSTATE State; + RTGetOptInit(&State, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, 0); + RTGETOPTUNION ValueUnion; + int chOpt; + while ((chOpt = RTGetOpt(&State, &ValueUnion)) != 0) + { + switch (chOpt) + { + case 's': g_cSamplesPerSec = ValueUnion.u32; break; + case 'p': g_cSamplesPerPeriod = ValueUnion.u32; break; + case 'b': g_cSamplesInBuffer = ValueUnion.u32; break; + case 'd': g_rdSecDuration = ValueUnion.u32; break; + case 'h': + RTPrintf("usage: ntPlayToneWaveX.exe\n" + "[-s|--samples-per-sec]\n" + "[-p|--period-in-samples]\n" + "[-b|--bufsize-in-samples]\n" + "[-d|--total-duration-in-secs]\n" + "\n" + "Plays sine tone using ancient waveX API\n"); + return 0; + + default: + return RTGetOptPrintError(chOpt, &ValueUnion); + } + } + + + WAVEFORMATEX waveFormatEx = { 0 }; + MMRESULT mmresult; + + waveFormatEx.wFormatTag = WAVE_FORMAT_PCM; + waveFormatEx.nChannels = 2; + waveFormatEx.nSamplesPerSec = g_cSamplesPerSec; + waveFormatEx.wBitsPerSample = 16; + waveFormatEx.nBlockAlign = g_cbSample = waveFormatEx.nChannels * waveFormatEx.wBitsPerSample / 8; + waveFormatEx.nAvgBytesPerSec = waveFormatEx.nBlockAlign * waveFormatEx.nSamplesPerSec; + waveFormatEx.cbSize = 0; + + g_hWavEvent = CreateEvent(NULL, FALSE, FALSE, NULL); + mmresult = waveOutOpen(&g_hWaveOut, WAVE_MAPPER, &waveFormatEx, (DWORD_PTR)g_hWavEvent, NULL, CALLBACK_EVENT); + + if (mmresult != MMSYSERR_NOERROR) + { + RTMsgError("waveOutOpen failed with 0x%X\n", mmresult); + return -1; + } + + + uint32_t ui32SamplesToPlayTotal = (uint32_t)(g_rdSecDuration * g_cSamplesPerSec); + uint32_t ui32SamplesToPlay = ui32SamplesToPlayTotal; + uint32_t ui32SamplesPlayed = 0; + uint32_t ui32SamplesForWavBuf; + + WAVEHDR waveHdr1 = {0}, waveHdr2 = {0}, *pWaveHdr, *pWaveHdrPlaying, *pWaveHdrWaiting; + uint32_t i, k; + DWORD res; + + int16_t *i16Samples1 = (int16_t *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, g_cSamplesInBuffer * g_cbSample); + int16_t *i16Samples2 = (int16_t *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, g_cSamplesInBuffer * g_cbSample); + + k = 0; // This is discrete time really!!! + + for (i = 0; i < g_cSamplesInBuffer; i++, k++) + { + i16Samples1[2 * i] = (uint16_t)(10000.0 * sin(2.0 * M_PI * k / g_cSamplesPerPeriod)); + i16Samples1[2 * i + 1] = i16Samples1[2 * i]; + } + + ui32SamplesForWavBuf = min(ui32SamplesToPlay, g_cSamplesInBuffer); + + waveHdr1.lpData = (LPSTR)i16Samples1; + waveHdr1.dwBufferLength = ui32SamplesForWavBuf * g_cbSample; + waveHdr1.dwFlags = 0; + waveHdr1.dwLoops = 0; + + ui32SamplesToPlay -= ui32SamplesForWavBuf; + ui32SamplesPlayed += ui32SamplesForWavBuf; + + pWaveHdrPlaying = &waveHdr1; + + mmresult = waveOutPrepareHeader(g_hWaveOut, pWaveHdrPlaying, sizeof(WAVEHDR)); + mmresult = waveOutWrite(g_hWaveOut, pWaveHdrPlaying, sizeof(WAVEHDR)); + //RTMsgInfo("waveOutWrite completes with %d\n", mmresult); + + res = WaitForSingleObject(g_hWavEvent, INFINITE); + //RTMsgInfo("WaitForSingleObject completes with %d\n\n", res); + + waveHdr2.lpData = (LPSTR)i16Samples2; + waveHdr2.dwBufferLength = 0; + waveHdr2.dwFlags = 0; + waveHdr2.dwLoops = 0; + + pWaveHdrWaiting = &waveHdr2; + + while (ui32SamplesToPlay > 0) + { + int16_t *i16Samples = (int16_t *)pWaveHdrWaiting->lpData; + + for (i = 0; i < g_cSamplesInBuffer; i++, k++) + { + i16Samples[2 * i] = (uint16_t)(10000.0 * sin(2.0 * M_PI * k / g_cSamplesPerPeriod)); + i16Samples[2 * i + 1] = i16Samples[2 * i]; + } + + ui32SamplesForWavBuf = min(ui32SamplesToPlay, g_cSamplesInBuffer); + + pWaveHdrWaiting->dwBufferLength = ui32SamplesForWavBuf * g_cbSample; + pWaveHdrWaiting->dwFlags = 0; + pWaveHdrWaiting->dwLoops = 0; + + + ui32SamplesToPlay -= ui32SamplesForWavBuf; + ui32SamplesPlayed += ui32SamplesForWavBuf; + + mmresult = waveOutPrepareHeader(g_hWaveOut, pWaveHdrWaiting, sizeof(WAVEHDR)); + mmresult = waveOutWrite(g_hWaveOut, pWaveHdrWaiting, sizeof(WAVEHDR)); + //RTMsgInfo("waveOutWrite completes with %d\n", mmresult); + + res = WaitForSingleObject(g_hWavEvent, INFINITE); + //RTMsgInfo("WaitForSingleObject completes with %d\n\n", res); + + mmresult = waveOutUnprepareHeader(g_hWaveOut, pWaveHdrPlaying, sizeof(WAVEHDR)); + //RTMsgInfo("waveOutUnprepareHeader completes with %d\n", mmresult); + + pWaveHdr = pWaveHdrWaiting; + pWaveHdrWaiting = pWaveHdrPlaying; + pWaveHdrPlaying = pWaveHdr; + } + + while (mmresult = waveOutUnprepareHeader(g_hWaveOut, pWaveHdrPlaying, sizeof(WAVEHDR))) + { + //Expecting WAVERR_STILLPLAYING + //RTMsgInfo("waveOutUnprepareHeader failed with 0x%X\n", mmresult); + Sleep(100); + } + + if (mmresult == MMSYSERR_NOERROR) + { + waveOutClose(g_hWaveOut); + } + + HeapFree(GetProcessHeap(), 0, i16Samples1); + HeapFree(GetProcessHeap(), 0, i16Samples2); +} + diff --git a/src/VBox/ValidationKit/utils/audio/readme.txt b/src/VBox/ValidationKit/utils/audio/readme.txt new file mode 100644 index 00000000..ba894b5a --- /dev/null +++ b/src/VBox/ValidationKit/utils/audio/readme.txt @@ -0,0 +1,2 @@ + +See docs/VBoxAudioValidationKitReadMe.txt or docs/VBoxAudioValidationKitReadMe.html. diff --git a/src/VBox/ValidationKit/utils/audio/vkat.cpp b/src/VBox/ValidationKit/utils/audio/vkat.cpp new file mode 100644 index 00000000..0ff17e4c --- /dev/null +++ b/src/VBox/ValidationKit/utils/audio/vkat.cpp @@ -0,0 +1,1649 @@ +/* $Id: vkat.cpp $ */ +/** @file + * Validation Kit Audio Test (VKAT) utility for testing and validating the audio stack. + */ + +/* + * Copyright (C) 2021-2023 Oracle and/or its affiliates. + * + * This file is part of VirtualBox base platform packages, as + * available from https://www.virtualbox.org. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, in version 3 of the + * License. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + * + * The contents of this file may alternatively be used under the terms + * of the Common Development and Distribution License Version 1.0 + * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included + * in the VirtualBox distribution, in which case the provisions of the + * CDDL are applicable instead of those of the GPL. + * + * You may elect to license modified versions of this file under the + * terms and conditions of either the GPL or the CDDL or both. + * + * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0 + */ + + +/********************************************************************************************************************************* +* Header Files * +*********************************************************************************************************************************/ +#define LOG_GROUP LOG_GROUP_AUDIO_TEST + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include "product-generated.h" + +#include +#include + +#ifdef RT_OS_WINDOWS +# include /* for CoInitializeEx and SetConsoleCtrlHandler */ +#else +# include +#endif + +#include "vkatInternal.h" + + +/********************************************************************************************************************************* +* Internal Functions * +*********************************************************************************************************************************/ +static int audioVerifyOne(const char *pszPathSetA, const char *pszPathSetB, PAUDIOTESTVERIFYOPTS pOpts); + + +/********************************************************************************************************************************* +* Global Variables * +*********************************************************************************************************************************/ +/** + * Backends description table. + * + * @note The first backend in the array is the default one for the platform. + */ +AUDIOTESTBACKENDDESC const g_aBackends[] = +{ +#ifdef VBOX_WITH_AUDIO_PULSE + { &g_DrvHostPulseAudio, "pulseaudio" }, + { &g_DrvHostPulseAudio, "pulse" }, + { &g_DrvHostPulseAudio, "pa" }, +#endif +/* + * Note: ALSA has to come second so that PulseAudio above always is the default on Linux-y OSes + * -- most distros are using an ALSA plugin for PulseAudio nowadays. + * However, some of these configurations do not seem to work by default (can't create audio streams). + * + * If PulseAudio is not available, the (optional) probing ("--probe-backends") will choose the "pure" ALSA stack instead then. + */ +#if defined(VBOX_WITH_AUDIO_ALSA) && defined(RT_OS_LINUX) + { &g_DrvHostALSAAudio, "alsa" }, +#endif +#ifdef VBOX_WITH_AUDIO_OSS + { &g_DrvHostOSSAudio, "oss" }, +#endif +#if defined(RT_OS_DARWIN) + { &g_DrvHostCoreAudio, "coreaudio" }, + { &g_DrvHostCoreAudio, "core" }, + { &g_DrvHostCoreAudio, "ca" }, +#endif +#if defined(RT_OS_WINDOWS) + { &g_DrvHostAudioWas, "wasapi" }, + { &g_DrvHostAudioWas, "was" }, + { &g_DrvHostDSound, "directsound" }, + { &g_DrvHostDSound, "dsound" }, + { &g_DrvHostDSound, "ds" }, +#endif +#ifdef VBOX_WITH_AUDIO_DEBUG + { &g_DrvHostDebugAudio, "debug" }, +#endif + { &g_DrvHostValidationKitAudio, "valkit" } +}; +AssertCompile(sizeof(g_aBackends) > 0 /* port me */); +/** Number of backends defined. */ +unsigned g_cBackends = RT_ELEMENTS(g_aBackends); + +/** + * Long option values for the 'test' command. + */ +enum +{ + VKAT_TEST_OPT_COUNT = 900, + VKAT_TEST_OPT_DEV, + VKAT_TEST_OPT_GUEST_ATS_ADDR, + VKAT_TEST_OPT_GUEST_ATS_PORT, + VKAT_TEST_OPT_HOST_ATS_ADDR, + VKAT_TEST_OPT_HOST_ATS_PORT, + VKAT_TEST_OPT_MODE, + VKAT_TEST_OPT_NO_AUDIO_OK, + VKAT_TEST_OPT_NO_VERIFY, + VKAT_TEST_OPT_OUTDIR, + VKAT_TEST_OPT_PAUSE, + VKAT_TEST_OPT_PCM_HZ, + VKAT_TEST_OPT_PCM_BIT, + VKAT_TEST_OPT_PCM_CHAN, + VKAT_TEST_OPT_PCM_SIGNED, + VKAT_TEST_OPT_PROBE_BACKENDS, + VKAT_TEST_OPT_TAG, + VKAT_TEST_OPT_TEMPDIR, + VKAT_TEST_OPT_VOL, + VKAT_TEST_OPT_TCP_BIND_ADDRESS, + VKAT_TEST_OPT_TCP_BIND_PORT, + VKAT_TEST_OPT_TCP_CONNECT_ADDRESS, + VKAT_TEST_OPT_TCP_CONNECT_PORT, + VKAT_TEST_OPT_TONE_DURATION_MS, + VKAT_TEST_OPT_TONE_VOL_PERCENT +}; + +/** + * Long option values for the 'verify' command. + */ +enum +{ + VKAT_VERIFY_OPT_MAX_DIFF_COUNT = 900, + VKAT_VERIFY_OPT_MAX_DIFF_PERCENT, + VKAT_VERIFY_OPT_MAX_SIZE_PERCENT, + VKAT_VERIFY_OPT_NORMALIZE +}; + +/** + * Common command line parameters. + */ +static const RTGETOPTDEF g_aCmdCommonOptions[] = +{ + { "--quiet", 'q', RTGETOPT_REQ_NOTHING }, + { "--verbose", 'v', RTGETOPT_REQ_NOTHING }, + { "--daemonize", AUDIO_TEST_OPT_CMN_DAEMONIZE, RTGETOPT_REQ_NOTHING }, + { "--daemonized", AUDIO_TEST_OPT_CMN_DAEMONIZED, RTGETOPT_REQ_NOTHING }, + { "--debug-audio", AUDIO_TEST_OPT_CMN_DEBUG_AUDIO_ENABLE, RTGETOPT_REQ_NOTHING }, + { "--debug-audio-path", AUDIO_TEST_OPT_CMN_DEBUG_AUDIO_PATH, RTGETOPT_REQ_STRING }, +}; + +/** + * Command line parameters for test mode. + */ +static const RTGETOPTDEF g_aCmdTestOptions[] = +{ + { "--backend", 'b', RTGETOPT_REQ_STRING }, + { "--drvaudio", 'd', RTGETOPT_REQ_NOTHING }, + { "--exclude", 'e', RTGETOPT_REQ_UINT32 }, + { "--exclude-all", 'a', RTGETOPT_REQ_NOTHING }, + { "--guest-ats-addr", VKAT_TEST_OPT_GUEST_ATS_ADDR, RTGETOPT_REQ_STRING }, + { "--guest-ats-port", VKAT_TEST_OPT_GUEST_ATS_PORT, RTGETOPT_REQ_UINT32 }, + { "--host-ats-address", VKAT_TEST_OPT_HOST_ATS_ADDR, RTGETOPT_REQ_STRING }, + { "--host-ats-port", VKAT_TEST_OPT_HOST_ATS_PORT, RTGETOPT_REQ_UINT32 }, + { "--include", 'i', RTGETOPT_REQ_UINT32 }, + { "--outdir", VKAT_TEST_OPT_OUTDIR, RTGETOPT_REQ_STRING }, + { "--count", VKAT_TEST_OPT_COUNT, RTGETOPT_REQ_UINT32 }, + { "--device", VKAT_TEST_OPT_DEV, RTGETOPT_REQ_STRING }, + { "--pause", VKAT_TEST_OPT_PAUSE, RTGETOPT_REQ_UINT32 }, + { "--pcm-bit", VKAT_TEST_OPT_PCM_BIT, RTGETOPT_REQ_UINT8 }, + { "--pcm-chan", VKAT_TEST_OPT_PCM_CHAN, RTGETOPT_REQ_UINT8 }, + { "--pcm-hz", VKAT_TEST_OPT_PCM_HZ, RTGETOPT_REQ_UINT16 }, + { "--pcm-signed", VKAT_TEST_OPT_PCM_SIGNED, RTGETOPT_REQ_BOOL }, + { "--probe-backends", VKAT_TEST_OPT_PROBE_BACKENDS, RTGETOPT_REQ_NOTHING }, + { "--mode", VKAT_TEST_OPT_MODE, RTGETOPT_REQ_STRING }, + { "--no-audio-ok", VKAT_TEST_OPT_NO_AUDIO_OK, RTGETOPT_REQ_NOTHING }, + { "--no-verify", VKAT_TEST_OPT_NO_VERIFY, RTGETOPT_REQ_NOTHING }, + { "--tag", VKAT_TEST_OPT_TAG, RTGETOPT_REQ_STRING }, + { "--tempdir", VKAT_TEST_OPT_TEMPDIR, RTGETOPT_REQ_STRING }, + { "--vol", VKAT_TEST_OPT_VOL, RTGETOPT_REQ_UINT8 }, + { "--tcp-bind-addr", VKAT_TEST_OPT_TCP_BIND_ADDRESS, RTGETOPT_REQ_STRING }, + { "--tcp-bind-port", VKAT_TEST_OPT_TCP_BIND_PORT, RTGETOPT_REQ_UINT16 }, + { "--tcp-connect-addr", VKAT_TEST_OPT_TCP_CONNECT_ADDRESS, RTGETOPT_REQ_STRING }, + { "--tcp-connect-port", VKAT_TEST_OPT_TCP_CONNECT_PORT, RTGETOPT_REQ_UINT16 }, + { "--tone-duration", VKAT_TEST_OPT_TONE_DURATION_MS, RTGETOPT_REQ_UINT32 }, + { "--tone-vol", VKAT_TEST_OPT_TONE_VOL_PERCENT, RTGETOPT_REQ_UINT8 } +}; + +/** + * Command line parameters for verification mode. + */ +static const RTGETOPTDEF g_aCmdVerifyOptions[] = +{ + { "--max-diff-count", VKAT_VERIFY_OPT_MAX_DIFF_COUNT, RTGETOPT_REQ_UINT32 }, + { "--max-diff-percent", VKAT_VERIFY_OPT_MAX_DIFF_PERCENT, RTGETOPT_REQ_UINT8 }, + { "--max-size-percent", VKAT_VERIFY_OPT_MAX_SIZE_PERCENT, RTGETOPT_REQ_UINT8 }, + { "--normalize", VKAT_VERIFY_OPT_NORMALIZE, RTGETOPT_REQ_BOOL } +}; + +/** Terminate ASAP if set. Set on Ctrl-C. */ +bool volatile g_fTerminate = false; +/** The release logger. */ +PRTLOGGER g_pRelLogger = NULL; +/** The test handle. */ +RTTEST g_hTest; +/** The current verbosity level. */ +unsigned g_uVerbosity = 0; +/** DrvAudio: Enable debug (or not). */ +bool g_fDrvAudioDebug = false; +/** DrvAudio: The debug output path. */ +const char *g_pszDrvAudioDebug = NULL; + + +/** + * Get default backend. + */ +PCPDMDRVREG AudioTestGetDefaultBackend(void) +{ + return g_aBackends[0].pDrvReg; +} + + +/** + * Helper for handling --backend options. + * + * @returns Pointer to the specified backend, NULL if not found (error + * displayed). + * @param pszBackend The backend option value. + */ +PCPDMDRVREG AudioTestFindBackendOpt(const char *pszBackend) +{ + for (uintptr_t i = 0; i < RT_ELEMENTS(g_aBackends); i++) + if ( strcmp(pszBackend, g_aBackends[i].pszName) == 0 + || strcmp(pszBackend, g_aBackends[i].pDrvReg->szName) == 0) + return g_aBackends[i].pDrvReg; + RTMsgError("Unknown backend: '%s'\n\n", pszBackend); + RTPrintf("Supported backend values are: "); + for (uintptr_t i = 0; i < RT_ELEMENTS(g_aBackends); i++) + { + if (i > 0) + RTPrintf(", "); + RTPrintf(g_aBackends[i].pszName); + } + RTPrintf("\n"); + return NULL; +} + + +/********************************************************************************************************************************* +* Test callbacks * +*********************************************************************************************************************************/ + +/** + * @copydoc FNAUDIOTESTSETUP + */ +static DECLCALLBACK(int) audioTestPlayToneSetup(PAUDIOTESTENV pTstEnv, PAUDIOTESTDESC pTstDesc, PAUDIOTESTPARMS pTstParmsAcq, void **ppvCtx) +{ + RT_NOREF(pTstDesc, ppvCtx); + + int rc = VINF_SUCCESS; + + if (strlen(pTstEnv->szDev)) + { + rc = audioTestDriverStackSetDevice(pTstEnv->pDrvStack, PDMAUDIODIR_OUT, pTstEnv->szDev); + if (RT_FAILURE(rc)) + return rc; + } + + pTstParmsAcq->enmType = AUDIOTESTTYPE_TESTTONE_PLAY; + pTstParmsAcq->enmDir = PDMAUDIODIR_OUT; + + pTstParmsAcq->TestTone = pTstEnv->ToneParms; + + pTstParmsAcq->TestTone.Hdr.idxTest = pTstEnv->idxTest; /* Assign unique test ID. */ + + return rc; +} + +/** + * @copydoc FNAUDIOTESTEXEC + */ +static DECLCALLBACK(int) audioTestPlayToneExec(PAUDIOTESTENV pTstEnv, void *pvCtx, PAUDIOTESTPARMS pTstParms) +{ + RT_NOREF(pvCtx); + + int rc = VINF_SUCCESS; + + PAUDIOTESTTONEPARMS const pToneParms = &pTstParms->TestTone; + + uint32_t const idxTest = pToneParms->Hdr.idxTest; + + RTTIMESPEC NowTimeSpec; + RTTimeExplode(&pToneParms->Hdr.tsCreated, RTTimeNow(&NowTimeSpec)); + + RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Test #%RU32: Playing test tone (%RU16Hz, %RU32ms)\n", + idxTest, (uint16_t)pToneParms->dbFreqHz, pToneParms->msDuration); + + /* + * 1. Arm the (host) ValKit ATS with the recording parameters. + */ + RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, + "Test #%RU32: Telling ValKit audio driver on host to record new tone ...\n", idxTest); + + rc = AudioTestSvcClientToneRecord(&pTstEnv->u.Host.AtsClValKit, pToneParms); + if (RT_SUCCESS(rc)) + { + /* Give the Validaiton Kit audio driver on the host a bit of time to register / arming the new test. */ + RTThreadSleep(5000); /* Fudge factor. */ + + /* + * 2. Tell VKAT on guest to start playback. + */ + RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Test #%RU32: Telling VKAT on guest to play tone ...\n", idxTest); + + rc = AudioTestSvcClientTonePlay(&pTstEnv->u.Host.AtsClGuest, pToneParms); + if (RT_FAILURE(rc)) + RTTestFailed(g_hTest, "Test #%RU32: AudioTestSvcClientTonePlay() failed with %Rrc\n", idxTest, rc); + } + else + RTTestFailed(g_hTest, "Test #%RU32: AudioTestSvcClientToneRecord() failed with %Rrc\n", idxTest, rc); + + if (RT_SUCCESS(rc)) + { + RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Test #%RU32: Playing tone done\n", idxTest); + + /* Give the audio stack a random amount of time for draining data before the next iteration. */ + if (pTstEnv->cIterations > 1) + RTThreadSleep(RTRandU32Ex(2000, 5000)); /** @todo Implement some dedicated ATS command for this? */ + } + + if (RT_FAILURE(rc)) + RTTestFailed(g_hTest, "Test #%RU32: Playing test tone failed with %Rrc\n", idxTest, rc); + + return rc; +} + +/** + * @copydoc FNAUDIOTESTDESTROY + */ +static DECLCALLBACK(int) audioTestPlayToneDestroy(PAUDIOTESTENV pTstEnv, void *pvCtx) +{ + RT_NOREF(pTstEnv, pvCtx); + + return VINF_SUCCESS; +} + +/** + * @copydoc FNAUDIOTESTSETUP + */ +static DECLCALLBACK(int) audioTestRecordToneSetup(PAUDIOTESTENV pTstEnv, PAUDIOTESTDESC pTstDesc, PAUDIOTESTPARMS pTstParmsAcq, void **ppvCtx) +{ + RT_NOREF(pTstDesc, ppvCtx); + + int rc = VINF_SUCCESS; + + if (strlen(pTstEnv->szDev)) + { + rc = audioTestDriverStackSetDevice(pTstEnv->pDrvStack, PDMAUDIODIR_IN, pTstEnv->szDev); + if (RT_FAILURE(rc)) + return rc; + } + + pTstParmsAcq->enmType = AUDIOTESTTYPE_TESTTONE_RECORD; + pTstParmsAcq->enmDir = PDMAUDIODIR_IN; + + pTstParmsAcq->TestTone = pTstEnv->ToneParms; + + pTstParmsAcq->TestTone.Hdr.idxTest = pTstEnv->idxTest; /* Assign unique test ID. */ + + return rc; +} + +/** + * @copydoc FNAUDIOTESTEXEC + */ +static DECLCALLBACK(int) audioTestRecordToneExec(PAUDIOTESTENV pTstEnv, void *pvCtx, PAUDIOTESTPARMS pTstParms) +{ + RT_NOREF(pvCtx); + + int rc = VINF_SUCCESS; + + PAUDIOTESTTONEPARMS const pToneParms = &pTstParms->TestTone; + + uint32_t const idxTest = pToneParms->Hdr.idxTest; + + RTTIMESPEC NowTimeSpec; + RTTimeExplode(&pToneParms->Hdr.tsCreated, RTTimeNow(&NowTimeSpec)); + + RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Test #%RU32: Recording test tone (%RU16Hz, %RU32ms)\n", + idxTest, (uint16_t)pToneParms->dbFreqHz, pToneParms->msDuration); + + /* + * 1. Arm the (host) ValKit ATS with the playback parameters. + */ + RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, + "Test #%RU32: Telling ValKit audio driver on host to inject recording data ...\n", idxTest); + + rc = AudioTestSvcClientTonePlay(&pTstEnv->u.Host.AtsClValKit, &pTstParms->TestTone); + if (RT_SUCCESS(rc)) + { + /* + * 2. Tell the guest ATS to start recording. + */ + RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Test #%RU32: Telling VKAT on guest to record audio ...\n", idxTest); + + rc = AudioTestSvcClientToneRecord(&pTstEnv->u.Host.AtsClGuest, &pTstParms->TestTone); + if (RT_FAILURE(rc)) + RTTestFailed(g_hTest, "Test #%RU32: AudioTestSvcClientToneRecord() failed with %Rrc\n", idxTest, rc); + } + else + RTTestFailed(g_hTest, "Test #%RU32: AudioTestSvcClientTonePlay() failed with %Rrc\n", idxTest, rc); + + if (RT_SUCCESS(rc)) + { + /* Wait a bit to let the left over audio bits being processed. */ + if (pTstEnv->cIterations > 1) + RTThreadSleep(RTRandU32Ex(2000, 5000)); /** @todo Implement some dedicated ATS command for this? */ + } + + if (RT_FAILURE(rc)) + RTTestFailed(g_hTest, "Test #%RU32: Recording test tone failed with %Rrc\n", idxTest, rc); + + return rc; +} + +/** + * @copydoc FNAUDIOTESTDESTROY + */ +static DECLCALLBACK(int) audioTestRecordToneDestroy(PAUDIOTESTENV pTstEnv, void *pvCtx) +{ + RT_NOREF(pTstEnv, pvCtx); + + return VINF_SUCCESS; +} + + +/********************************************************************************************************************************* +* Test execution * +*********************************************************************************************************************************/ + +/** Test definition table. */ +AUDIOTESTDESC g_aTests[] = +{ + /* pszTest fExcluded pfnSetup */ + { "PlayTone", false, audioTestPlayToneSetup, audioTestPlayToneExec, audioTestPlayToneDestroy }, + { "RecordTone", false, audioTestRecordToneSetup, audioTestRecordToneExec, audioTestRecordToneDestroy } +}; +/** Number of tests defined. */ +unsigned g_cTests = RT_ELEMENTS(g_aTests); + +/** + * Runs one specific audio test. + * + * @returns VBox status code. + * @param pTstEnv Test environment to use for running the test. + * @param pTstDesc Test to run. + */ +static int audioTestOne(PAUDIOTESTENV pTstEnv, PAUDIOTESTDESC pTstDesc) +{ + int rc = VINF_SUCCESS; + + AUDIOTESTPARMS TstParms; + audioTestParmsInit(&TstParms); + + RTTestSub(g_hTest, pTstDesc->pszName); + + if (pTstDesc->fExcluded) + { + RTTestSkipped(g_hTest, "Test #%RU32 is excluded from list, skipping", pTstEnv->idxTest); + return VINF_SUCCESS; + } + + pTstEnv->cIterations = pTstEnv->cIterations == 0 ? RTRandU32Ex(1, 10) : pTstEnv->cIterations; + + RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Test #%RU32 (%RU32 iterations total)\n", pTstEnv->idxTest, pTstEnv->cIterations); + + void *pvCtx = NULL; /* Test-specific opaque context. Optional and can be NULL. */ + + AssertPtr(pTstDesc->pfnExec); + for (uint32_t i = 0; i < pTstEnv->cIterations; i++) + { + int rc2; + + if (pTstDesc->pfnSetup) + { + rc2 = pTstDesc->pfnSetup(pTstEnv, pTstDesc, &TstParms, &pvCtx); + if (RT_FAILURE(rc2)) + RTTestFailed(g_hTest, "Test #%RU32 setup failed with %Rrc\n", pTstEnv->idxTest, rc2); + } + else + rc2 = VINF_SUCCESS; + + if (RT_SUCCESS(rc2)) + { + AssertPtrBreakStmt(pTstDesc->pfnExec, VERR_INVALID_POINTER); + rc2 = pTstDesc->pfnExec(pTstEnv, pvCtx, &TstParms); + if (RT_FAILURE(rc2)) + RTTestFailed(g_hTest, "Test #%RU32 execution failed with %Rrc\n", pTstEnv->idxTest, rc2); + } + + if (pTstDesc->pfnDestroy) + { + rc2 = pTstDesc->pfnDestroy(pTstEnv, pvCtx); + if (RT_FAILURE(rc2)) + RTTestFailed(g_hTest, "Test #%RU32 destruction failed with %Rrc\n", pTstEnv->idxTest, rc2); + } + + if (RT_SUCCESS(rc)) + rc = rc2; + + /* Keep going. */ + pTstEnv->idxTest++; + } + + RTTestSubDone(g_hTest); + + audioTestParmsDestroy(&TstParms); + + return rc; +} + +/** + * Runs all specified tests in a row. + * + * @returns VBox status code. + * @param pTstEnv Test environment to use for running all tests. + */ +int audioTestWorker(PAUDIOTESTENV pTstEnv) +{ + int rc = VINF_SUCCESS; + + if (pTstEnv->enmMode == AUDIOTESTMODE_GUEST) + { + RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Guest ATS running\n"); + + while (!g_fTerminate) + RTThreadSleep(100); + + RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Shutting down guest ATS ...\n"); + + int rc2 = AudioTestSvcStop(pTstEnv->pSrv); + if (RT_SUCCESS(rc)) + rc = rc2; + + RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Guest ATS shutdown complete\n"); + } + else if (pTstEnv->enmMode == AUDIOTESTMODE_HOST) + { + RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Using tag '%s'\n", pTstEnv->szTag); + + RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Telling ValKit audio driver on host to begin a new test set ...\n"); + rc = AudioTestSvcClientTestSetBegin(&pTstEnv->u.Host.AtsClValKit, pTstEnv->szTag); + if (RT_SUCCESS(rc)) + { + RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Telling VKAT on guest to begin a new test set ...\n"); + rc = AudioTestSvcClientTestSetBegin(&pTstEnv->u.Host.AtsClGuest, pTstEnv->szTag); + if (RT_FAILURE(rc)) + RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, + "Beginning test set on guest failed with %Rrc\n", rc); + } + else + RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, + "Beginning test set on host (Validation Kit audio driver) failed with %Rrc\n", rc); + + if (RT_SUCCESS(rc)) + { + for (unsigned i = 0; i < RT_ELEMENTS(g_aTests); i++) + { + int rc2 = audioTestOne(pTstEnv, &g_aTests[i]); + if (RT_SUCCESS(rc)) + rc = rc2; + + if (g_fTerminate) + break; + } + + if (RT_SUCCESS(rc)) + { + /** @todo Fudge! */ + RTMSINTERVAL const msWait = RTRandU32Ex(RT_MS_1SEC, RT_MS_5SEC); + RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, + "Waiting %RU32ms to let guest and the audio stack process remaining data ...\n", msWait); + RTThreadSleep(msWait); + } + + RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Ending test set on guest ...\n"); + int rc2 = AudioTestSvcClientTestSetEnd(&pTstEnv->u.Host.AtsClGuest, pTstEnv->szTag); + if (RT_FAILURE(rc2)) + { + RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Ending test set on guest failed with %Rrc\n", rc2); + if (RT_SUCCESS(rc)) + rc = rc2; + } + + RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Ending test set on host (Validation Kit audio driver) ...\n"); + rc2 = AudioTestSvcClientTestSetEnd(&pTstEnv->u.Host.AtsClValKit, pTstEnv->szTag); + if (RT_FAILURE(rc2)) + { + RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, + "Ending test set on host (Validation Kit audio driver) failed with %Rrc\n", rc2); + if (RT_SUCCESS(rc)) + rc = rc2; + } + + if ( !g_fTerminate + && RT_SUCCESS(rc)) + { + /* + * Download guest + Validation Kit audio driver test sets to our output directory. + */ + char szFileName[RTPATH_MAX]; + if (RTStrPrintf2(szFileName, sizeof(szFileName), "%s-guest.tar.gz", pTstEnv->szTag)) + { + rc = RTPathJoin(pTstEnv->u.Host.szPathTestSetGuest, sizeof(pTstEnv->u.Host.szPathTestSetGuest), + pTstEnv->szPathOut, szFileName); + if (RT_SUCCESS(rc)) + { + if (RTStrPrintf2(szFileName, sizeof(szFileName), "%s-host.tar.gz", pTstEnv->szTag)) + { + rc = RTPathJoin(pTstEnv->u.Host.szPathTestSetValKit, sizeof(pTstEnv->u.Host.szPathTestSetValKit), + pTstEnv->szPathOut, szFileName); + } + else + rc = VERR_BUFFER_OVERFLOW; + } + else + rc = VERR_BUFFER_OVERFLOW; + + if (RT_SUCCESS(rc)) + { + RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Downloading guest test set to '%s'\n", + pTstEnv->u.Host.szPathTestSetGuest); + rc = AudioTestSvcClientTestSetDownload(&pTstEnv->u.Host.AtsClGuest, + pTstEnv->szTag, pTstEnv->u.Host.szPathTestSetGuest); + } + + if (RT_SUCCESS(rc)) + { + RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Downloading host test set to '%s'\n", + pTstEnv->u.Host.szPathTestSetValKit); + rc = AudioTestSvcClientTestSetDownload(&pTstEnv->u.Host.AtsClValKit, + pTstEnv->szTag, pTstEnv->u.Host.szPathTestSetValKit); + } + } + else + rc = VERR_BUFFER_OVERFLOW; + + if ( RT_SUCCESS(rc) + && !pTstEnv->fSkipVerify) + { + rc = audioVerifyOne(pTstEnv->u.Host.szPathTestSetGuest, pTstEnv->u.Host.szPathTestSetValKit, NULL /* pOpts */); + } + else + RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Verification skipped\n"); + + if (!pTstEnv->fSkipVerify) + { + RTFileDelete(pTstEnv->u.Host.szPathTestSetGuest); + RTFileDelete(pTstEnv->u.Host.szPathTestSetValKit); + } + else + RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Leaving test set files behind\n"); + } + } + } + else + rc = VERR_NOT_IMPLEMENTED; + + /* Clean up. */ + RTDirRemove(pTstEnv->szPathTemp); + RTDirRemove(pTstEnv->szPathOut); + + if (RT_FAILURE(rc)) + RTTestFailed(g_hTest, "Test worker failed with %Rrc", rc); + + return rc; +} + +/** Option help for the 'test' command. */ +static DECLCALLBACK(const char *) audioTestCmdTestHelp(PCRTGETOPTDEF pOpt) +{ + switch (pOpt->iShort) + { + case 'a': return "Exclude all tests from the list (useful to enable single tests later with --include)"; + case 'b': return "The audio backend to use"; + case 'd': return "Go via DrvAudio instead of directly interfacing with the backend"; + case 'e': return "Exclude the given test id from the list"; + case 'i': return "Include the given test id in the list"; + case VKAT_TEST_OPT_COUNT: return "Number of test iterations to perform for selected tests\n" + " Default: random number"; + case VKAT_TEST_OPT_DEV: return "Name of the input/output device to use\n" + " Default: default device"; + case VKAT_TEST_OPT_TONE_DURATION_MS: return "Test tone duration to play / record (ms)\n" + " Default: random duration"; + case VKAT_TEST_OPT_TONE_VOL_PERCENT: return "Test tone volume (percent)\n" + " Default: 100"; + case VKAT_TEST_OPT_GUEST_ATS_ADDR: return "Address of guest ATS to connect to\n" + " Default: " ATS_TCP_DEF_CONNECT_GUEST_STR; + case VKAT_TEST_OPT_GUEST_ATS_PORT: return "Port of guest ATS to connect to (needs NAT port forwarding)\n" + " Default: 6042"; /* ATS_TCP_DEF_CONNECT_PORT_GUEST */ + case VKAT_TEST_OPT_HOST_ATS_ADDR: return "Address of host ATS to connect to\n" + " Default: " ATS_TCP_DEF_CONNECT_HOST_ADDR_STR; + case VKAT_TEST_OPT_HOST_ATS_PORT: return "Port of host ATS to connect to\n" + " Default: 6052"; /* ATS_TCP_DEF_BIND_PORT_VALKIT */ + case VKAT_TEST_OPT_MODE: return "Test mode to use when running the tests\n" + " Available modes:\n" + " guest: Run as a guest-side ATS\n" + " host: Run as a host-side ATS"; + case VKAT_TEST_OPT_NO_AUDIO_OK: return "Enables running without any found audio hardware (e.g. servers)"; + case VKAT_TEST_OPT_NO_VERIFY: return "Skips the verification step"; + case VKAT_TEST_OPT_OUTDIR: return "Output directory to use"; + case VKAT_TEST_OPT_PAUSE: return "Not yet implemented"; + case VKAT_TEST_OPT_PCM_HZ: return "PCM Hertz (Hz) rate to use\n" + " Default: 44100"; + case VKAT_TEST_OPT_PCM_BIT: return "PCM sample bits (i.e. 16) to use\n" + " Default: 16"; + case VKAT_TEST_OPT_PCM_CHAN: return "PCM channels to use\n" + " Default: 2"; + case VKAT_TEST_OPT_PCM_SIGNED: return "PCM samples to use (signed = true, unsigned = false)\n" + " Default: true"; + case VKAT_TEST_OPT_PROBE_BACKENDS: return "Probes all (available) backends until a working one is found"; + case VKAT_TEST_OPT_TAG: return "Test set tag to use"; + case VKAT_TEST_OPT_TEMPDIR: return "Temporary directory to use"; + case VKAT_TEST_OPT_VOL: return "Audio volume (percent) to use"; + case VKAT_TEST_OPT_TCP_BIND_ADDRESS: return "TCP address listening to (server mode)"; + case VKAT_TEST_OPT_TCP_BIND_PORT: return "TCP port listening to (server mode)"; + case VKAT_TEST_OPT_TCP_CONNECT_ADDRESS: return "TCP address to connect to (client mode)"; + case VKAT_TEST_OPT_TCP_CONNECT_PORT: return "TCP port to connect to (client mode)"; + default: + break; + } + return NULL; +} + +/** + * Main (entry) function for the testing functionality of VKAT. + * + * @returns Program exit code. + * @param pGetState RTGetOpt state. + */ +static DECLCALLBACK(RTEXITCODE) audioTestMain(PRTGETOPTSTATE pGetState) +{ + AUDIOTESTENV TstEnv; + audioTestEnvInit(&TstEnv); + + int rc; + + PCPDMDRVREG pDrvReg = AudioTestGetDefaultBackend(); + uint8_t cPcmSampleBit = 0; + uint8_t cPcmChannels = 0; + uint32_t uPcmHz = 0; + bool fPcmSigned = true; + bool fProbeBackends = false; + bool fNoAudioOk = false; + + const char *pszGuestTcpAddr = NULL; + uint16_t uGuestTcpPort = ATS_TCP_DEF_BIND_PORT_GUEST; + const char *pszValKitTcpAddr = NULL; + uint16_t uValKitTcpPort = ATS_TCP_DEF_BIND_PORT_VALKIT; + + int ch; + RTGETOPTUNION ValueUnion; + while ((ch = RTGetOpt(pGetState, &ValueUnion))) + { + switch (ch) + { + case 'a': + for (unsigned i = 0; i < RT_ELEMENTS(g_aTests); i++) + g_aTests[i].fExcluded = true; + break; + + case 'b': + pDrvReg = AudioTestFindBackendOpt(ValueUnion.psz); + if (pDrvReg == NULL) + return RTEXITCODE_SYNTAX; + break; + + case 'd': + TstEnv.IoOpts.fWithDrvAudio = true; + break; + + case 'e': + if (ValueUnion.u32 >= RT_ELEMENTS(g_aTests)) + return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Invalid test number %u passed to --exclude", ValueUnion.u32); + g_aTests[ValueUnion.u32].fExcluded = true; + break; + + case VKAT_TEST_OPT_GUEST_ATS_ADDR: + pszGuestTcpAddr = ValueUnion.psz; + break; + + case VKAT_TEST_OPT_GUEST_ATS_PORT: + uGuestTcpPort = ValueUnion.u32; + break; + + case VKAT_TEST_OPT_HOST_ATS_ADDR: + pszValKitTcpAddr = ValueUnion.psz; + break; + + case VKAT_TEST_OPT_HOST_ATS_PORT: + uValKitTcpPort = ValueUnion.u32; + break; + + case VKAT_TEST_OPT_MODE: + if (TstEnv.enmMode != AUDIOTESTMODE_UNKNOWN) + return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Test mode (guest / host) already specified"); + TstEnv.enmMode = RTStrICmp(ValueUnion.psz, "guest") == 0 ? AUDIOTESTMODE_GUEST : AUDIOTESTMODE_HOST; + break; + + case VKAT_TEST_OPT_NO_AUDIO_OK: + fNoAudioOk = true; + break; + + case VKAT_TEST_OPT_NO_VERIFY: + TstEnv.fSkipVerify = true; + break; + + case 'i': + if (ValueUnion.u32 >= RT_ELEMENTS(g_aTests)) + return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Invalid test number %u passed to --include", ValueUnion.u32); + g_aTests[ValueUnion.u32].fExcluded = false; + break; + + case VKAT_TEST_OPT_COUNT: + TstEnv.cIterations = ValueUnion.u32; + break; + + case VKAT_TEST_OPT_DEV: + rc = RTStrCopy(TstEnv.szDev, sizeof(TstEnv.szDev), ValueUnion.psz); + if (RT_FAILURE(rc)) + return RTMsgErrorExitFailure("Failed to copy out device: %Rrc", rc); + break; + + case VKAT_TEST_OPT_TONE_DURATION_MS: + TstEnv.ToneParms.msDuration = ValueUnion.u32; + break; + + case VKAT_TEST_OPT_TONE_VOL_PERCENT: + TstEnv.ToneParms.uVolumePercent = ValueUnion.u8; + break; + + case VKAT_TEST_OPT_PAUSE: + return RTMsgErrorExitFailure("Not yet implemented!"); + + case VKAT_TEST_OPT_OUTDIR: + rc = RTStrCopy(TstEnv.szPathOut, sizeof(TstEnv.szPathOut), ValueUnion.psz); + if (RT_FAILURE(rc)) + return RTMsgErrorExitFailure("Failed to copy out directory: %Rrc", rc); + break; + + case VKAT_TEST_OPT_PCM_BIT: + cPcmSampleBit = ValueUnion.u8; + break; + + case VKAT_TEST_OPT_PCM_CHAN: + cPcmChannels = ValueUnion.u8; + break; + + case VKAT_TEST_OPT_PCM_HZ: + uPcmHz = ValueUnion.u32; + break; + + case VKAT_TEST_OPT_PCM_SIGNED: + fPcmSigned = ValueUnion.f; + break; + + case VKAT_TEST_OPT_PROBE_BACKENDS: + fProbeBackends = true; + break; + + case VKAT_TEST_OPT_TAG: + rc = RTStrCopy(TstEnv.szTag, sizeof(TstEnv.szTag), ValueUnion.psz); + if (RT_FAILURE(rc)) + return RTMsgErrorExit(RTEXITCODE_FAILURE, "Tag invalid, rc=%Rrc", rc); + break; + + case VKAT_TEST_OPT_TEMPDIR: + rc = RTStrCopy(TstEnv.szPathTemp, sizeof(TstEnv.szPathTemp), ValueUnion.psz); + if (RT_FAILURE(rc)) + return RTMsgErrorExit(RTEXITCODE_FAILURE, "Temp dir invalid, rc=%Rrc", rc); + break; + + case VKAT_TEST_OPT_VOL: + TstEnv.IoOpts.uVolumePercent = ValueUnion.u8; + break; + + case VKAT_TEST_OPT_TCP_BIND_ADDRESS: + rc = RTStrCopy(TstEnv.TcpOpts.szBindAddr, sizeof(TstEnv.TcpOpts.szBindAddr), ValueUnion.psz); + if (RT_FAILURE(rc)) + return RTMsgErrorExit(RTEXITCODE_FAILURE, "Bind address invalid, rc=%Rrc", rc); + break; + + case VKAT_TEST_OPT_TCP_BIND_PORT: + TstEnv.TcpOpts.uBindPort = ValueUnion.u16; + break; + + case VKAT_TEST_OPT_TCP_CONNECT_ADDRESS: + rc = RTStrCopy(TstEnv.TcpOpts.szConnectAddr, sizeof(TstEnv.TcpOpts.szConnectAddr), ValueUnion.psz); + if (RT_FAILURE(rc)) + return RTMsgErrorExit(RTEXITCODE_FAILURE, "Connect address invalid, rc=%Rrc", rc); + break; + + case VKAT_TEST_OPT_TCP_CONNECT_PORT: + TstEnv.TcpOpts.uConnectPort = ValueUnion.u16; + break; + + AUDIO_TEST_COMMON_OPTION_CASES(ValueUnion, &g_CmdTest); + + default: + return RTGetOptPrintError(ch, &ValueUnion); + } + } + + /* + * Start testing. + */ + RTTestBanner(g_hTest); + + if (TstEnv.enmMode == AUDIOTESTMODE_UNKNOWN) + return RTMsgErrorExit(RTEXITCODE_SYNTAX, "No test mode (--mode) specified!\n"); + + /* Validate TCP options. */ + if ( TstEnv.TcpOpts.szBindAddr[0] + && TstEnv.TcpOpts.szConnectAddr[0]) + return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Only one TCP connection mode (connect as client *or* bind as server) can be specified) at a time!\n"); + + /* Set new (override standard) I/O PCM properties if set by the user. */ + if ( cPcmSampleBit + || cPcmChannels + || uPcmHz) + { + PDMAudioPropsInit(&TstEnv.IoOpts.Props, + cPcmSampleBit ? cPcmSampleBit / 2 : 2 /* 16-bit */, fPcmSigned /* fSigned */, + cPcmChannels ? cPcmChannels : 2 /* Stereo */, uPcmHz ? uPcmHz : 44100); + } + + /* Do this first before everything else below. */ + rc = AudioTestDriverStackPerformSelftest(); + if (RT_FAILURE(rc)) + { + if (!fNoAudioOk) + return RTMsgErrorExit(RTEXITCODE_FAILURE, "Testing driver stack failed: %Rrc\n", rc); + RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, + "Warning: Testing driver stack not possible (%Rrc), but --no-audio-ok was specified. Running on a server without audio hardware?\n", rc); + } + + AUDIOTESTDRVSTACK DrvStack; + if (fProbeBackends) + rc = audioTestDriverStackProbe(&DrvStack, pDrvReg, + true /* fEnabledIn */, true /* fEnabledOut */, TstEnv.IoOpts.fWithDrvAudio); /** @todo Make in/out configurable, too. */ + else + rc = audioTestDriverStackInitEx(&DrvStack, pDrvReg, + true /* fEnabledIn */, true /* fEnabledOut */, TstEnv.IoOpts.fWithDrvAudio); /** @todo Make in/out configurable, too. */ + if (RT_FAILURE(rc)) + { + if (!fNoAudioOk) + return RTMsgErrorExit(RTEXITCODE_FAILURE, "Unable to init driver stack: %Rrc\n", rc); + RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, + "Warning: Initializing driver stack not possible (%Rrc), but --no-audio-ok was specified. Running on a server without audio hardware?\n", rc); + } + + PPDMAUDIOHOSTDEV pDev; + rc = audioTestDevicesEnumerateAndCheck(&DrvStack, TstEnv.szDev, &pDev); + if (RT_FAILURE(rc)) + { + if (!fNoAudioOk) + return RTMsgErrorExit(RTEXITCODE_FAILURE, "Enumerating device(s) failed: %Rrc\n", rc); + } + + /* For now all tests have the same test environment and driver stack. */ + rc = audioTestEnvCreate(&TstEnv, &DrvStack); + if (RT_SUCCESS(rc)) + rc = audioTestWorker(&TstEnv); + + audioTestEnvDestroy(&TstEnv); + audioTestDriverStackDelete(&DrvStack); + + if (RT_FAILURE(rc)) /* Let us know that something went wrong in case we forgot to mention it. */ + RTTestFailed(g_hTest, "Testing failed with %Rrc\n", rc); + + /* + * Print summary and exit. + */ + return RTTestSummaryAndDestroy(g_hTest); +} + + +const VKATCMD g_CmdTest = +{ + "test", + audioTestMain, + "Runs audio tests and creates an audio test set.", + g_aCmdTestOptions, + RT_ELEMENTS(g_aCmdTestOptions), + audioTestCmdTestHelp, + true /* fNeedsTransport */ +}; + + +/********************************************************************************************************************************* +* Command: verify * +*********************************************************************************************************************************/ + +static int audioVerifyOpenTestSet(const char *pszPathSet, PAUDIOTESTSET pSet) +{ + int rc; + + char szPathExtracted[RTPATH_MAX]; + + RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Opening test set '%s'\n", pszPathSet); + + const bool fPacked = AudioTestSetIsPacked(pszPathSet); + + if (fPacked) + { + RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Test set is an archive and needs to be unpacked\n"); + + if (!RTFileExists(pszPathSet)) + { + RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Test set '%s' does not exist\n", pszPathSet); + rc = VERR_FILE_NOT_FOUND; + } + else + rc = VINF_SUCCESS; + + if (RT_SUCCESS(rc)) + { + char szPathTemp[RTPATH_MAX]; + rc = RTPathTemp(szPathTemp, sizeof(szPathTemp)); + if (RT_SUCCESS(rc)) + { + RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Using temporary directory '%s'\n", szPathTemp); + + rc = RTPathJoin(szPathExtracted, sizeof(szPathExtracted), szPathTemp, "vkat-testset-XXXX"); + if (RT_SUCCESS(rc)) + { + rc = RTDirCreateTemp(szPathExtracted, 0755); + if (RT_SUCCESS(rc)) + { + RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Unpacking archive to '%s'\n", szPathExtracted); + rc = AudioTestSetUnpack(pszPathSet, szPathExtracted); + if (RT_SUCCESS(rc)) + RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Archive successfully unpacked\n"); + } + } + } + } + } + else + rc = VINF_SUCCESS; + + if (RT_SUCCESS(rc)) + rc = AudioTestSetOpen(pSet, fPacked ? szPathExtracted : pszPathSet); + + if (RT_FAILURE(rc)) + RTTestFailed(g_hTest, "Unable to open / unpack test set archive: %Rrc", rc); + + return rc; +} + +/** + * Verifies one test set pair. + * + * @returns VBox status code. + * @param pszPathSetA Absolute path to test set A. + * @param pszPathSetB Absolute path to test set B. + * @param pOpts Verification options to use. Optional. + * When NULL, the (very strict) defaults will be used. + */ +static int audioVerifyOne(const char *pszPathSetA, const char *pszPathSetB, PAUDIOTESTVERIFYOPTS pOpts) +{ + RTTestSubF(g_hTest, "Verifying"); + RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Verifying test set '%s' with test set '%s'\n", pszPathSetA, pszPathSetB); + + AUDIOTESTSET SetA, SetB; + int rc = audioVerifyOpenTestSet(pszPathSetA, &SetA); + if (RT_SUCCESS(rc)) + { + rc = audioVerifyOpenTestSet(pszPathSetB, &SetB); + if (RT_SUCCESS(rc)) + { + AUDIOTESTERRORDESC errDesc; + if (pOpts) + rc = AudioTestSetVerifyEx(&SetA, &SetB, pOpts, &errDesc); + else + rc = AudioTestSetVerify(&SetA, &SetB, &errDesc); + if (RT_SUCCESS(rc)) + { + uint32_t const cErr = AudioTestErrorDescCount(&errDesc); + RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "%RU32 errors occurred while verifying\n", cErr); + + /** @todo Use some AudioTestErrorXXX API for enumeration here later. */ + PAUDIOTESTERRORENTRY pErrEntry; + RTListForEach(&errDesc.List, pErrEntry, AUDIOTESTERRORENTRY, Node) + { + if (RT_FAILURE(pErrEntry->rc)) + RTTestFailed(g_hTest, "%s\n", pErrEntry->szDesc); + else + RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "%s\n", pErrEntry->szDesc); + } + + if (cErr == 0) + RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Verification successful\n"); + + AudioTestErrorDescDestroy(&errDesc); + } + else + RTTestFailed(g_hTest, "Verification failed with %Rrc", rc); + +#ifdef DEBUG + if (g_fDrvAudioDebug) + RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, + "\n" + "Use the following command line to re-run verification in the debugger:\n" + "gdb --args ./VBoxAudioTest -vvvv --debug-audio verify \"%s\" \"%s\"\n", + SetA.szPathAbs, SetB.szPathAbs); +#endif + if (!g_fDrvAudioDebug) /* Don't wipe stuff when debugging. Can be useful for introspecting data. */ + AudioTestSetWipe(&SetB); + AudioTestSetClose(&SetB); + } + + if (!g_fDrvAudioDebug) /* Ditto. */ + AudioTestSetWipe(&SetA); + AudioTestSetClose(&SetA); + } + + RTTestSubDone(g_hTest); + + return rc; +} + +/** Option help for the 'verify' command. */ +static DECLCALLBACK(const char *) audioTestCmdVerifyHelp(PCRTGETOPTDEF pOpt) +{ + switch (pOpt->iShort) + { + case VKAT_VERIFY_OPT_MAX_DIFF_COUNT: return "Specifies the maximum number of differences\n" + " Default: 0 (strict)"; + case VKAT_VERIFY_OPT_MAX_DIFF_PERCENT: return "Specifies the maximum difference (percent)\n" + " Default: 0 (strict)"; + case VKAT_VERIFY_OPT_MAX_SIZE_PERCENT: return "Specifies the maximum size difference (percent)\n" + " Default: 1 (strict)"; + case VKAT_VERIFY_OPT_NORMALIZE: return "Enables / disables audio data normalization\n" + " Default: false"; + default: + break; + } + return NULL; +} + +/** + * Main (entry) function for the verification functionality of VKAT. + * + * @returns Program exit code. + * @param pGetState RTGetOpt state. + */ +static DECLCALLBACK(RTEXITCODE) audioVerifyMain(PRTGETOPTSTATE pGetState) +{ + /* + * Parse options and process arguments. + */ + const char *apszSets[2] = { NULL, NULL }; + unsigned iTestSet = 0; + + AUDIOTESTVERIFYOPTS Opts; + AudioTestSetVerifyOptsInit(&Opts); + + int ch; + RTGETOPTUNION ValueUnion; + while ((ch = RTGetOpt(pGetState, &ValueUnion))) + { + switch (ch) + { + case VKAT_VERIFY_OPT_MAX_DIFF_COUNT: + Opts.cMaxDiff = ValueUnion.u32; + break; + + case VKAT_VERIFY_OPT_MAX_DIFF_PERCENT: + Opts.uMaxDiffPercent = ValueUnion.u8; + break; + + case VKAT_VERIFY_OPT_MAX_SIZE_PERCENT: + Opts.uMaxSizePercent = ValueUnion.u8; + break; + + case VKAT_VERIFY_OPT_NORMALIZE: + Opts.fNormalize = ValueUnion.f; + break; + + case VINF_GETOPT_NOT_OPTION: + if (iTestSet == 0) + RTTestBanner(g_hTest); + if (iTestSet >= RT_ELEMENTS(apszSets)) + return RTMsgErrorExitFailure("Only two test sets can be verified at one time"); + apszSets[iTestSet++] = ValueUnion.psz; + break; + + AUDIO_TEST_COMMON_OPTION_CASES(ValueUnion, &g_CmdVerify); + + default: + return RTGetOptPrintError(ch, &ValueUnion); + } + } + + if (!iTestSet) + return RTMsgErrorExitFailure("At least one test set must be specified"); + + int rc = VINF_SUCCESS; + + /* + * If only test set A is given, default to the current directory + * for test set B. + */ + char szDirCur[RTPATH_MAX]; + if (iTestSet == 1) + { + rc = RTPathGetCurrent(szDirCur, sizeof(szDirCur)); + if (RT_SUCCESS(rc)) + apszSets[1] = szDirCur; + else + RTTestFailed(g_hTest, "Failed to retrieve current directory: %Rrc", rc); + } + + if (RT_SUCCESS(rc)) + audioVerifyOne(apszSets[0], apszSets[1], &Opts); + + /* + * Print summary and exit. + */ + return RTTestSummaryAndDestroy(g_hTest); +} + + +const VKATCMD g_CmdVerify = +{ + "verify", + audioVerifyMain, + "Verifies a formerly created audio test set.", + g_aCmdVerifyOptions, + RT_ELEMENTS(g_aCmdVerifyOptions), + audioTestCmdVerifyHelp, + false /* fNeedsTransport */ +}; + + +/********************************************************************************************************************************* +* Main * +*********************************************************************************************************************************/ + +/** + * Ctrl-C signal handler. + * + * This just sets g_fTerminate and hope it will be noticed soon. + * + * On non-Windows it restores the SIGINT action to default, so that a second + * Ctrl-C will have the normal effect (just in case the code doesn't respond to + * g_fTerminate). + */ +#ifdef RT_OS_WINDOWS +static BOOL CALLBACK audioTestConsoleCtrlHandler(DWORD dwCtrlType) RT_NOEXCEPT +{ + if (dwCtrlType != CTRL_C_EVENT && dwCtrlType != CTRL_BREAK_EVENT) + return false; + RTPrintf(dwCtrlType == CTRL_C_EVENT ? "Ctrl-C!\n" : "Ctrl-Break!\n"); + + ASMAtomicWriteBool(&g_fTerminate, true); + + return true; +} +#else +static void audioTestSignalHandler(int iSig) RT_NOEXCEPT +{ + Assert(iSig == SIGINT); RT_NOREF(iSig); + RTPrintf("Ctrl-C!\n"); + + ASMAtomicWriteBool(&g_fTerminate, true); + + signal(SIGINT, SIG_DFL); +} +#endif + +/** + * Commands. + */ +static const VKATCMD * const g_apCommands[] = +{ + &g_CmdTest, + &g_CmdVerify, + &g_CmdBackends, + &g_CmdEnum, + &g_CmdPlay, + &g_CmdRec, + &g_CmdSelfTest +}; + +/** + * Shows tool usage text. + */ +RTEXITCODE audioTestUsage(PRTSTREAM pStrm, PCVKATCMD pOnlyCmd) +{ + RTStrmPrintf(pStrm, "usage: %s [global options] [command-options]\n", RTProcShortName()); + RTStrmPrintf(pStrm, + "\n" + "Global Options:\n" + " --debug-audio\n" + " Enables (DrvAudio) debugging\n" + " --debug-audio-path=\n" + " Tells DrvAudio where to put its debug output (wav-files)\n" + " -q, --quiet\n" + " Sets verbosity to zero\n" + " -v, --verbose\n" + " Increase verbosity\n" + " -V, --version\n" + " Displays version\n" + " -h, -?, --help\n" + " Displays help\n" + ); + + for (uintptr_t iCmd = 0; iCmd < RT_ELEMENTS(g_apCommands); iCmd++) + { + PCVKATCMD const pCmd = g_apCommands[iCmd]; + if (!pOnlyCmd || pCmd == pOnlyCmd) + { + RTStrmPrintf(pStrm, + "\n" + "Command '%s':\n" + " %s\n" + "Options for '%s':\n", + pCmd->pszCommand, pCmd->pszDesc, pCmd->pszCommand); + PCRTGETOPTDEF const paOptions = pCmd->paOptions; + for (unsigned i = 0; i < pCmd->cOptions; i++) + { + if (RT_C_IS_PRINT(paOptions[i].iShort)) + RTStrmPrintf(pStrm, " -%c, %s\n", paOptions[i].iShort, paOptions[i].pszLong); + else + RTStrmPrintf(pStrm, " %s\n", paOptions[i].pszLong); + + const char *pszHelp = NULL; + if (pCmd->pfnOptionHelp) + pszHelp = pCmd->pfnOptionHelp(&paOptions[i]); + if (pszHelp) + RTStrmPrintf(pStrm, " %s\n", pszHelp); + } + + if (pCmd->fNeedsTransport) + for (uintptr_t iTx = 0; iTx < g_cTransports; iTx++) + g_apTransports[iTx]->pfnUsage(pStrm); + } + } + + return RTEXITCODE_SUCCESS; +} + +/** + * Lists the commands and their descriptions. + */ +static RTEXITCODE audioTestListCommands(PRTSTREAM pStrm) +{ + RTStrmPrintf(pStrm, "Commands:\n"); + for (uintptr_t iCmd = 0; iCmd < RT_ELEMENTS(g_apCommands); iCmd++) + RTStrmPrintf(pStrm, "%8s - %s\n", g_apCommands[iCmd]->pszCommand, g_apCommands[iCmd]->pszDesc); + return RTEXITCODE_SUCCESS; +} + +/** + * Shows tool version. + */ +RTEXITCODE audioTestVersion(void) +{ + RTPrintf("%s\n", RTBldCfgRevisionStr()); + return RTEXITCODE_SUCCESS; +} + +/** + * Shows the logo. + * + * @param pStream Output stream to show logo on. + */ +void audioTestShowLogo(PRTSTREAM pStream) +{ + RTStrmPrintf(pStream, VBOX_PRODUCT " VKAT (Validation Kit Audio Test) Version " VBOX_VERSION_STRING " - r%s\n" + "Copyright (C) " VBOX_C_YEAR " " VBOX_VENDOR "\n\n", RTBldCfgRevisionStr()); +} + +int main(int argc, char **argv) +{ + /* + * Init IPRT. + */ + int rc = RTR3InitExe(argc, &argv, 0); + if (RT_FAILURE(rc)) + return RTMsgInitFailure(rc); + + /* + * Handle special command line options which need parsing before + * everything else. + */ + /** @todo r=bird: this isn't at all syntactically sane, because you don't know + * how to parse past the command (can almost be done safely thought, since + * you've got the option definitions for every command at hand). So, if someone + * wants to play a file named "-v.wav", you'll incorrectly take that as two 'v' + * options. The parsing has to stop when you get to the command, i.e. first + * VINF_GETOPT_NOT_OPTION or anything that isn't a common option. Daemonizing + * when for instance encountering an invalid command, is not correct. + * + * Btw. you MUST however process the 'q' option in parallel to 'v' here, they + * are oposites. For instance '-vqvvv' is supposed to give you level 3 logging, + * not quiet! So, either you process both 'v' and 'q' here, or you pospone them + * (better option). + */ + /** @todo r=bird: Is the daemonizing needed? The testcase doesn't seem to use + * it... If you don't need it, drop it as it make the parsing complex + * and illogical. The --daemonized / --damonize options should be + * required to before the command, then okay. */ + bool fDaemonize = false; + bool fDaemonized = false; + + RTGETOPTSTATE GetState; + rc = RTGetOptInit(&GetState, argc, argv, g_aCmdCommonOptions, + RT_ELEMENTS(g_aCmdCommonOptions), 1 /*idxFirst*/, 0 /*fFlags - must not sort! */); + AssertRCReturn(rc, RTEXITCODE_INIT); + + int ch; + RTGETOPTUNION ValueUnion; + while ((ch = RTGetOpt(&GetState, &ValueUnion)) != 0) + { + switch (ch) + { + case AUDIO_TEST_OPT_CMN_DAEMONIZE: + fDaemonize = true; + break; + + case AUDIO_TEST_OPT_CMN_DAEMONIZED: + fDaemonized = true; + break; + + /* Has to be defined here and not in AUDIO_TEST_COMMON_OPTION_CASES, to get the logger + * configured before the specific command handlers down below come into play. */ + case 'v': + g_uVerbosity++; + break; + + default: + break; + } + } + + /** @todo add something to suppress this stuff. */ + audioTestShowLogo(g_pStdOut); + + if (fDaemonize) + { + if (!fDaemonized) + { + rc = RTProcDaemonize(argv, "--daemonized"); + if (RT_FAILURE(rc)) + return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTProcDaemonize() failed with %Rrc\n", rc); + + RTMsgInfo("Starting in background (daemonizing) ..."); + return RTEXITCODE_SUCCESS; + } + /* else continue running in background. */ + } + + /* + * Init test and globals. + * Note: Needs to be done *after* daemonizing, otherwise the child will fail! + */ + rc = RTTestCreate("AudioTest", &g_hTest); + if (RT_FAILURE(rc)) + return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTTestCreate() failed with %Rrc\n", rc); + +#ifdef RT_OS_WINDOWS + HRESULT hrc = CoInitializeEx(NULL /*pReserved*/, COINIT_MULTITHREADED | COINIT_SPEED_OVER_MEMORY | COINIT_DISABLE_OLE1DDE); + if (FAILED(hrc)) + RTMsgWarning("CoInitializeEx failed: %#x", hrc); +#endif + + /* + * Configure release logging to go to stdout. + */ + RTUINT fFlags = RTLOGFLAGS_PREFIX_THREAD | RTLOGFLAGS_PREFIX_TIME_PROG; +#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2) + fFlags |= RTLOGFLAGS_USECRLF; +#endif + static const char * const s_apszLogGroups[] = VBOX_LOGGROUP_NAMES; + rc = RTLogCreate(&g_pRelLogger, fFlags, "all.e.l", "VKAT_RELEASE_LOG", + RT_ELEMENTS(s_apszLogGroups), s_apszLogGroups, RTLOGDEST_STDOUT, NULL /*"vkat-release.log"*/); + if (RT_SUCCESS(rc)) + { + RTLogRelSetDefaultInstance(g_pRelLogger); + if (g_uVerbosity) + { + RTMsgInfo("Setting verbosity logging to level %u\n", g_uVerbosity); + switch (g_uVerbosity) /* Not very elegant, but has to do it for now. */ + { + case 1: + rc = RTLogGroupSettings(g_pRelLogger, + "drv_audio.e.l+drv_host_audio.e.l+" + "audio_mixer.e.l+audio_test.e.l"); + break; + + case 2: + rc = RTLogGroupSettings(g_pRelLogger, + "drv_audio.e.l.l2+drv_host_audio.e.l.l2+" + "audio_mixer.e.l.l2+audio_test.e.l.l2"); + break; + + case 3: + rc = RTLogGroupSettings(g_pRelLogger, + "drv_audio.e.l.l2.l3+drv_host_audio.e.l.l2.l3+" + "audio_mixer.e.l.l2.l3+audio_test.e.l.l2.l3"); + break; + + case 4: + RT_FALL_THROUGH(); + default: + rc = RTLogGroupSettings(g_pRelLogger, + "drv_audio.e.l.l2.l3.l4.f+drv_host_audio.e.l.l2.l3.l4.f+" + "audio_mixer.e.l.l2.l3.l4.f+audio_test.e.l.l2.l3.l4.f"); + break; + } + if (RT_FAILURE(rc)) + RTMsgError("Setting debug logging failed, rc=%Rrc\n", rc); + } + } + else + RTMsgWarning("Failed to create release logger: %Rrc", rc); + + /* + * Install a Ctrl-C signal handler. + */ +#ifdef RT_OS_WINDOWS + SetConsoleCtrlHandler(audioTestConsoleCtrlHandler, TRUE); +#else + struct sigaction sa; + RT_ZERO(sa); + sa.sa_handler = audioTestSignalHandler; + sigaction(SIGINT, &sa, NULL); +#endif + + /* + * Process common options. + */ + RT_ZERO(GetState); + rc = RTGetOptInit(&GetState, argc, argv, g_aCmdCommonOptions, + RT_ELEMENTS(g_aCmdCommonOptions), 1 /*idxFirst*/, 0 /*fFlags - must not sort! */); + AssertRCReturn(rc, RTEXITCODE_INIT); + + while ((ch = RTGetOpt(&GetState, &ValueUnion)) != 0) + { + switch (ch) + { + AUDIO_TEST_COMMON_OPTION_CASES(ValueUnion, NULL); + + case VINF_GETOPT_NOT_OPTION: + { + for (uintptr_t iCmd = 0; iCmd < RT_ELEMENTS(g_apCommands); iCmd++) + { + PCVKATCMD const pCmd = g_apCommands[iCmd]; + if (strcmp(ValueUnion.psz, pCmd->pszCommand) == 0) + { + /* Count the combined option definitions: */ + size_t cCombinedOptions = pCmd->cOptions + RT_ELEMENTS(g_aCmdCommonOptions); + if (pCmd->fNeedsTransport) + for (uintptr_t iTx = 0; iTx < g_cTransports; iTx++) + cCombinedOptions += g_apTransports[iTx]->cOpts; + + /* Combine the option definitions: */ + PRTGETOPTDEF paCombinedOptions = (PRTGETOPTDEF)RTMemAlloc(cCombinedOptions * sizeof(RTGETOPTDEF)); + if (paCombinedOptions) + { + uint32_t idxOpts = 0; + memcpy(paCombinedOptions, g_aCmdCommonOptions, sizeof(g_aCmdCommonOptions)); + idxOpts += RT_ELEMENTS(g_aCmdCommonOptions); + + memcpy(&paCombinedOptions[idxOpts], pCmd->paOptions, pCmd->cOptions * sizeof(RTGETOPTDEF)); + idxOpts += (uint32_t)pCmd->cOptions; + + if (pCmd->fNeedsTransport) + for (uintptr_t iTx = 0; iTx < g_cTransports; iTx++) + { + memcpy(&paCombinedOptions[idxOpts], + g_apTransports[iTx]->paOpts, g_apTransports[iTx]->cOpts * sizeof(RTGETOPTDEF)); + idxOpts += (uint32_t)g_apTransports[iTx]->cOpts; + } + + /* Re-initialize the option getter state and pass it to the command handler. */ + rc = RTGetOptInit(&GetState, argc, argv, paCombinedOptions, cCombinedOptions, + GetState.iNext /*idxFirst*/, RTGETOPTINIT_FLAGS_OPTS_FIRST); + if (RT_SUCCESS(rc)) + { + RTEXITCODE rcExit = pCmd->pfnHandler(&GetState); + RTMemFree(paCombinedOptions); + return rcExit; + } + RTMemFree(paCombinedOptions); + return RTMsgErrorExitFailure("RTGetOptInit failed for '%s': %Rrc", ValueUnion.psz, rc); + } + return RTMsgErrorExitFailure("Out of memory!"); + } + } + RTMsgError("Unknown command '%s'!\n", ValueUnion.psz); + audioTestListCommands(g_pStdErr); + return RTEXITCODE_SYNTAX; + } + + default: + return RTGetOptPrintError(ch, &ValueUnion); + } + } + + RTMsgError("No command specified!\n"); + audioTestListCommands(g_pStdErr); + return RTEXITCODE_SYNTAX; +} diff --git a/src/VBox/ValidationKit/utils/audio/vkatCmdGeneric.cpp b/src/VBox/ValidationKit/utils/audio/vkatCmdGeneric.cpp new file mode 100644 index 00000000..18eb9487 --- /dev/null +++ b/src/VBox/ValidationKit/utils/audio/vkatCmdGeneric.cpp @@ -0,0 +1,1169 @@ +/* $Id: vkatCmdGeneric.cpp $ */ +/** @file + * Validation Kit Audio Test (VKAT) utility for testing and validating the audio stack. + */ + +/* + * Copyright (C) 2021-2023 Oracle and/or its affiliates. + * + * This file is part of VirtualBox base platform packages, as + * available from https://www.virtualbox.org. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, in version 3 of the + * License. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + * + * The contents of this file may alternatively be used under the terms + * of the Common Development and Distribution License Version 1.0 + * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included + * in the VirtualBox distribution, in which case the provisions of the + * CDDL are applicable instead of those of the GPL. + * + * You may elect to license modified versions of this file under the + * terms and conditions of either the GPL or the CDDL or both. + * + * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0 + */ + + +/********************************************************************************************************************************* +* Header Files * +*********************************************************************************************************************************/ +#include +#include +#include +#include + +#include "vkatInternal.h" + + +/********************************************************************************************************************************* +* Command: backends * +*********************************************************************************************************************************/ + +/** + * Options for 'backends'. + */ +static const RTGETOPTDEF g_aCmdBackendsOptions[] = +{ + { "--dummy", 'd', RTGETOPT_REQ_NOTHING }, /* just a placeholder */ +}; + + +/** The 'backends' command option help. */ +static DECLCALLBACK(const char *) audioTestCmdBackendsHelp(PCRTGETOPTDEF pOpt) +{ + RT_NOREF(pOpt); + return NULL; +} + +/** + * The 'backends' command handler. + * + * @returns Program exit code. + * @param pGetState RTGetOpt state. + */ +static DECLCALLBACK(RTEXITCODE) audioTestCmdBackendsHandler(PRTGETOPTSTATE pGetState) +{ + /* + * Parse options. + */ + int ch; + RTGETOPTUNION ValueUnion; + while ((ch = RTGetOpt(pGetState, &ValueUnion)) != 0) + { + switch (ch) + { + AUDIO_TEST_COMMON_OPTION_CASES(ValueUnion, &g_CmdBackends); + + default: + return RTGetOptPrintError(ch, &ValueUnion); + } + } + + /* + * List the backends. + */ + RTPrintf("Backends (%u):\n", g_cBackends); + for (size_t i = 0; i < g_cBackends; i++) + RTPrintf(" %12s - %s\n", g_aBackends[i].pszName, g_aBackends[i].pDrvReg->pszDescription); + + return RTEXITCODE_SUCCESS; +} + + +/** + * Command table entry for 'backends'. + */ +const VKATCMD g_CmdBackends = +{ + /* .pszCommand = */ "backends", + /* .pfnHandler = */ audioTestCmdBackendsHandler, + /* .pszDesc = */ "Lists the compiled in audio backends.", + /* .paOptions = */ g_aCmdBackendsOptions, + /* .cOptions = */ 0 /*RT_ELEMENTS(g_aCmdBackendsOptions)*/, + /* .pfnOptionHelp = */ audioTestCmdBackendsHelp, + /* .fNeedsTransport = */ false +}; + + +/********************************************************************************************************************************* +* Command: enum * +*********************************************************************************************************************************/ + + + +/** + * Long option values for the 'enum' command. + */ +enum +{ + VKAT_ENUM_OPT_PROBE_BACKENDS = 900 +}; + +/** + * Options for 'enum'. + */ +static const RTGETOPTDEF g_aCmdEnumOptions[] = +{ + { "--backend", 'b', RTGETOPT_REQ_STRING }, + { "--probe-backends", VKAT_ENUM_OPT_PROBE_BACKENDS, RTGETOPT_REQ_NOTHING } +}; + + +/** The 'enum' command option help. */ +static DECLCALLBACK(const char *) audioTestCmdEnumHelp(PCRTGETOPTDEF pOpt) +{ + switch (pOpt->iShort) + { + case 'b': return "The audio backend to use"; + case VKAT_ENUM_OPT_PROBE_BACKENDS: return "Probes all (available) backends until a working one is found"; + default: return NULL; + } +} + +/** + * The 'enum' command handler. + * + * @returns Program exit code. + * @param pGetState RTGetOpt state. + */ +static DECLCALLBACK(RTEXITCODE) audioTestCmdEnumHandler(PRTGETOPTSTATE pGetState) +{ + /* + * Parse options. + */ + /* Option values: */ + PCPDMDRVREG pDrvReg = AudioTestGetDefaultBackend(); + bool fProbeBackends = false; + + /* Argument processing loop: */ + int ch; + RTGETOPTUNION ValueUnion; + while ((ch = RTGetOpt(pGetState, &ValueUnion)) != 0) + { + switch (ch) + { + case 'b': + pDrvReg = AudioTestFindBackendOpt(ValueUnion.psz); + if (pDrvReg == NULL) + return RTEXITCODE_SYNTAX; + break; + + case VKAT_ENUM_OPT_PROBE_BACKENDS: + fProbeBackends = true; + break; + + AUDIO_TEST_COMMON_OPTION_CASES(ValueUnion, &g_CmdEnum); + + default: + return RTGetOptPrintError(ch, &ValueUnion); + } + } + + int rc; + + AUDIOTESTDRVSTACK DrvStack; + if (fProbeBackends) + rc = audioTestDriverStackProbe(&DrvStack, pDrvReg, + true /* fEnabledIn */, true /* fEnabledOut */, false /* fWithDrvAudio */); + else + rc = audioTestDriverStackInitEx(&DrvStack, pDrvReg, + true /* fEnabledIn */, true /* fEnabledOut */, false /* fWithDrvAudio */); + if (RT_FAILURE(rc)) + return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Unable to init driver stack: %Rrc\n", rc); + + /* + * Do the enumeration. + */ + RTEXITCODE rcExit = RTEXITCODE_FAILURE; + + if (DrvStack.pIHostAudio->pfnGetDevices) + { + PDMAUDIOHOSTENUM Enum; + rc = DrvStack.pIHostAudio->pfnGetDevices(DrvStack.pIHostAudio, &Enum); + if (RT_SUCCESS(rc)) + { + RTPrintf("Found %u device%s\n", Enum.cDevices, Enum.cDevices != 1 ? "s" : ""); + + PPDMAUDIOHOSTDEV pHostDev; + RTListForEach(&Enum.LstDevices, pHostDev, PDMAUDIOHOSTDEV, ListEntry) + { + RTPrintf("\nDevice \"%s\":\n", pHostDev->pszName); + + char szFlags[PDMAUDIOHOSTDEV_MAX_FLAGS_STRING_LEN]; + if (pHostDev->cMaxInputChannels && !pHostDev->cMaxOutputChannels && pHostDev->enmUsage == PDMAUDIODIR_IN) + RTPrintf(" Input: max %u channels (%s)\n", + pHostDev->cMaxInputChannels, PDMAudioHostDevFlagsToString(szFlags, pHostDev->fFlags)); + else if (!pHostDev->cMaxInputChannels && pHostDev->cMaxOutputChannels && pHostDev->enmUsage == PDMAUDIODIR_OUT) + RTPrintf(" Output: max %u channels (%s)\n", + pHostDev->cMaxOutputChannels, PDMAudioHostDevFlagsToString(szFlags, pHostDev->fFlags)); + else + RTPrintf(" %s: max %u output channels, max %u input channels (%s)\n", + PDMAudioDirGetName(pHostDev->enmUsage), pHostDev->cMaxOutputChannels, + pHostDev->cMaxInputChannels, PDMAudioHostDevFlagsToString(szFlags, pHostDev->fFlags)); + + if (pHostDev->pszId && *pHostDev->pszId) + RTPrintf(" ID: \"%s\"\n", pHostDev->pszId); + } + + PDMAudioHostEnumDelete(&Enum); + } + else + rcExit = RTMsgErrorExitFailure("Enumeration failed: %Rrc\n", rc); + } + else + rcExit = RTMsgErrorExitFailure("Enumeration not supported by backend '%s'\n", pDrvReg->szName); + audioTestDriverStackDelete(&DrvStack); + + return RTEXITCODE_SUCCESS; +} + + +/** + * Command table entry for 'enum'. + */ +const VKATCMD g_CmdEnum = +{ + "enum", + audioTestCmdEnumHandler, + "Enumerates audio devices.", + g_aCmdEnumOptions, + RT_ELEMENTS(g_aCmdEnumOptions), + audioTestCmdEnumHelp, + false /* fNeedsTransport */ +}; + + + + +/********************************************************************************************************************************* +* Command: play * +*********************************************************************************************************************************/ + +/** + * Worker for audioTestPlayOne implementing the play loop. + */ +static RTEXITCODE audioTestPlayOneInner(PAUDIOTESTDRVMIXSTREAM pMix, PAUDIOTESTWAVEFILE pWaveFile, + PCPDMAUDIOSTREAMCFG pCfgAcq, const char *pszFile) +{ + uint32_t const cbPreBuffer = PDMAudioPropsFramesToBytes(pMix->pProps, pCfgAcq->Backend.cFramesPreBuffering); + uint64_t const nsStarted = RTTimeNanoTS(); + uint64_t nsDonePreBuffering = 0; + + /* + * Transfer data as quickly as we're allowed. + */ + uint8_t abSamples[16384]; + uint32_t const cbSamplesAligned = PDMAudioPropsFloorBytesToFrame(pMix->pProps, sizeof(abSamples)); + uint64_t offStream = 0; + while (!g_fTerminate) + { + /* Read a chunk from the wave file. */ + size_t cbSamples = 0; + int rc = AudioTestWaveFileRead(pWaveFile, abSamples, cbSamplesAligned, &cbSamples); + if (RT_SUCCESS(rc) && cbSamples > 0) + { + /* Pace ourselves a little. */ + if (offStream >= cbPreBuffer) + { + if (!nsDonePreBuffering) + nsDonePreBuffering = RTTimeNanoTS(); + uint64_t const cNsWritten = PDMAudioPropsBytesToNano64(pMix->pProps, offStream - cbPreBuffer); + uint64_t const cNsElapsed = RTTimeNanoTS() - nsStarted; + if (cNsWritten > cNsElapsed + RT_NS_10MS) + RTThreadSleep((cNsWritten - cNsElapsed - RT_NS_10MS / 2) / RT_NS_1MS); + } + + /* Transfer the data to the audio stream. */ + for (uint32_t offSamples = 0; offSamples < cbSamples;) + { + uint32_t const cbCanWrite = AudioTestMixStreamGetWritable(pMix); + if (cbCanWrite > 0) + { + uint32_t const cbToPlay = RT_MIN(cbCanWrite, (uint32_t)cbSamples - offSamples); + uint32_t cbPlayed = 0; + rc = AudioTestMixStreamPlay(pMix, &abSamples[offSamples], cbToPlay, &cbPlayed); + if (RT_SUCCESS(rc)) + { + if (cbPlayed) + { + offSamples += cbPlayed; + offStream += cbPlayed; + } + else + return RTMsgErrorExitFailure("Played zero bytes - %#x bytes reported playable!\n", cbCanWrite); + } + else + return RTMsgErrorExitFailure("Failed to play %#x bytes: %Rrc\n", cbToPlay, rc); + } + else if (AudioTestMixStreamIsOkay(pMix)) + RTThreadSleep(RT_MIN(RT_MAX(1, pCfgAcq->Device.cMsSchedulingHint), 256)); + else + return RTMsgErrorExitFailure("Stream is not okay!\n"); + } + } + else if (RT_SUCCESS(rc) && cbSamples == 0) + break; + else + return RTMsgErrorExitFailure("Error reading wav file '%s': %Rrc", pszFile, rc); + } + + /* + * Drain the stream. + */ + if (g_uVerbosity > 0) + RTMsgInfo("%'RU64 ns: Draining...\n", RTTimeNanoTS() - nsStarted); + int rc = AudioTestMixStreamDrain(pMix, true /*fSync*/); + if (RT_SUCCESS(rc)) + { + if (g_uVerbosity > 0) + RTMsgInfo("%'RU64 ns: Done\n", RTTimeNanoTS() - nsStarted); + } + else + return RTMsgErrorExitFailure("Draining failed: %Rrc", rc); + + return RTEXITCODE_SUCCESS; +} + +/** + * Worker for audioTestCmdPlayHandler that plays one file. + */ +static RTEXITCODE audioTestPlayOne(const char *pszFile, PCPDMDRVREG pDrvReg, const char *pszDevId, + PAUDIOTESTIOOPTS pIoOpts) +{ + char szTmp[128]; + + /* + * First we must open the file and determin the format. + */ + RTERRINFOSTATIC ErrInfo; + AUDIOTESTWAVEFILE WaveFile; + int rc = AudioTestWaveFileOpen(pszFile, &WaveFile, RTErrInfoInitStatic(&ErrInfo)); + if (RT_FAILURE(rc)) + return RTMsgErrorExitFailure("Failed to open '%s': %Rrc%#RTeim", pszFile, rc, &ErrInfo.Core); + + if (g_uVerbosity > 0) + { + RTMsgInfo("Opened '%s' for playing\n", pszFile); + RTMsgInfo("Format: %s\n", PDMAudioPropsToString(&WaveFile.Props, szTmp, sizeof(szTmp))); + RTMsgInfo("Size: %'RU32 bytes / %#RX32 / %'RU32 frames / %'RU64 ns\n", + WaveFile.cbSamples, WaveFile.cbSamples, + PDMAudioPropsBytesToFrames(&WaveFile.Props, WaveFile.cbSamples), + PDMAudioPropsBytesToNano(&WaveFile.Props, WaveFile.cbSamples)); + } + + /* + * Construct the driver stack. + */ + RTEXITCODE rcExit = RTEXITCODE_FAILURE; + AUDIOTESTDRVSTACK DrvStack; + rc = audioTestDriverStackInit(&DrvStack, pDrvReg, pIoOpts->fWithDrvAudio); + if (RT_SUCCESS(rc)) + { + /* + * Set the output device if one is specified. + */ + rc = audioTestDriverStackSetDevice(&DrvStack, PDMAUDIODIR_OUT, pszDevId); + if (RT_SUCCESS(rc)) + { + /* + * Open a stream for the output. + */ + uint8_t const cChannels = PDMAudioPropsChannels(&pIoOpts->Props); + + PDMAUDIOPCMPROPS ReqProps = WaveFile.Props; + if (cChannels != 0 && PDMAudioPropsChannels(&ReqProps) != cChannels) + PDMAudioPropsSetChannels(&ReqProps, cChannels); + + uint8_t const cbSample = PDMAudioPropsSampleSize(&pIoOpts->Props); + if (cbSample != 0) + PDMAudioPropsSetSampleSize(&ReqProps, cbSample); + + uint32_t const uHz = PDMAudioPropsHz(&pIoOpts->Props); + if (uHz != 0) + ReqProps.uHz = uHz; + + PDMAUDIOSTREAMCFG CfgAcq; + PPDMAUDIOSTREAM pStream = NULL; + rc = audioTestDriverStackStreamCreateOutput(&DrvStack, &ReqProps, pIoOpts->cMsBufferSize, + pIoOpts->cMsPreBuffer, pIoOpts->cMsSchedulingHint, &pStream, &CfgAcq); + if (RT_SUCCESS(rc)) + { + /* + * Automatically enable the mixer if the wave file and the + * output parameters doesn't match. + */ + if ( !pIoOpts->fWithMixer + && ( !PDMAudioPropsAreEqual(&WaveFile.Props, &pStream->Cfg.Props) + || pIoOpts->uVolumePercent != 100) + ) + { + RTMsgInfo("Enabling the mixer buffer.\n"); + pIoOpts->fWithMixer = true; + } + + /* + * Create a mixer wrapper. This is just a thin wrapper if fWithMixer + * is false, otherwise it's doing mixing, resampling and recoding. + */ + AUDIOTESTDRVMIXSTREAM Mix; + rc = AudioTestMixStreamInit(&Mix, &DrvStack, pStream, pIoOpts->fWithMixer ? &WaveFile.Props : NULL, 100 /*ms*/); + if (RT_SUCCESS(rc)) + { + if (g_uVerbosity > 0) + RTMsgInfo("Stream: %s cbBackend=%#RX32%s\n", + PDMAudioPropsToString(&pStream->Cfg.Props, szTmp, sizeof(szTmp)), + pStream->cbBackend, pIoOpts->fWithMixer ? " mixed" : ""); + + if (pIoOpts->fWithMixer) + AudioTestMixStreamSetVolume(&Mix, pIoOpts->uVolumePercent); + + /* + * Enable the stream and start playing. + */ + rc = AudioTestMixStreamEnable(&Mix); + if (RT_SUCCESS(rc)) + rcExit = audioTestPlayOneInner(&Mix, &WaveFile, &CfgAcq, pszFile); + else + rcExit = RTMsgErrorExitFailure("Enabling the output stream failed: %Rrc", rc); + + /* + * Clean up. + */ + AudioTestMixStreamTerm(&Mix); + } + audioTestDriverStackStreamDestroy(&DrvStack, pStream); + pStream = NULL; + } + else + rcExit = RTMsgErrorExitFailure("Creating output stream failed: %Rrc", rc); + } + else + rcExit = RTMsgErrorExitFailure("Failed to set output device to '%s': %Rrc", pszDevId, rc); + audioTestDriverStackDelete(&DrvStack); + } + else + rcExit = RTMsgErrorExitFailure("Driver stack construction failed: %Rrc", rc); + AudioTestWaveFileClose(&WaveFile); + return rcExit; +} + +/** + * Worker for audioTestCmdPlayHandler that plays one test tone. + */ +static RTEXITCODE audioTestPlayTestToneOne(PAUDIOTESTTONEPARMS pToneParms, + PCPDMDRVREG pDrvReg, const char *pszDevId, + PAUDIOTESTIOOPTS pIoOpts) +{ + char szTmp[128]; + + AUDIOTESTSTREAM TstStream; + RT_ZERO(TstStream); + + /* + * Construct the driver stack. + */ + RTEXITCODE rcExit = RTEXITCODE_FAILURE; + AUDIOTESTDRVSTACK DrvStack; + int rc = audioTestDriverStackInit(&DrvStack, pDrvReg, pIoOpts->fWithDrvAudio); + if (RT_SUCCESS(rc)) + { + /* + * Set the output device if one is specified. + */ + rc = audioTestDriverStackSetDevice(&DrvStack, PDMAUDIODIR_OUT, pszDevId); + if (RT_SUCCESS(rc)) + { + /* + * Open a stream for the output. + */ + uint8_t const cChannels = PDMAudioPropsChannels(&pIoOpts->Props); + + PDMAUDIOPCMPROPS ReqProps = pToneParms->Props; + if (cChannels != 0 && PDMAudioPropsChannels(&ReqProps) != cChannels) + PDMAudioPropsSetChannels(&ReqProps, cChannels); + + uint8_t const cbSample = PDMAudioPropsSampleSize(&pIoOpts->Props); + if (cbSample != 0) + PDMAudioPropsSetSampleSize(&ReqProps, cbSample); + + uint32_t const uHz = PDMAudioPropsHz(&pIoOpts->Props); + if (uHz != 0) + ReqProps.uHz = uHz; + + rc = audioTestDriverStackStreamCreateOutput(&DrvStack, &ReqProps, pIoOpts->cMsBufferSize, + pIoOpts->cMsPreBuffer, pIoOpts->cMsSchedulingHint, &TstStream.pStream, &TstStream.Cfg); + if (RT_SUCCESS(rc)) + { + /* + * Automatically enable the mixer if the wave file and the + * output parameters doesn't match. + */ + if ( !pIoOpts->fWithMixer + && ( !PDMAudioPropsAreEqual(&pToneParms->Props, &TstStream.pStream->Cfg.Props) + || pToneParms->uVolumePercent != 100) + ) + { + RTMsgInfo("Enabling the mixer buffer.\n"); + pIoOpts->fWithMixer = true; + } + + /* + * Create a mixer wrapper. This is just a thin wrapper if fWithMixer + * is false, otherwise it's doing mixing, resampling and recoding. + */ + rc = AudioTestMixStreamInit(&TstStream.Mix, &DrvStack, TstStream.pStream, + pIoOpts->fWithMixer ? &pToneParms->Props : NULL, 100 /*ms*/); + if (RT_SUCCESS(rc)) + { + if (g_uVerbosity > 0) + RTMsgInfo("Stream: %s cbBackend=%#RX32%s\n", + PDMAudioPropsToString(&TstStream.pStream->Cfg.Props, szTmp, sizeof(szTmp)), + TstStream.pStream->cbBackend, pIoOpts->fWithMixer ? " mixed" : ""); + + /* + * Enable the stream and start playing. + */ + rc = AudioTestMixStreamEnable(&TstStream.Mix); + if (RT_SUCCESS(rc)) + { + if (pIoOpts->fWithMixer) + AudioTestMixStreamSetVolume(&TstStream.Mix, pToneParms->uVolumePercent); + + rc = audioTestPlayTone(pIoOpts, NULL /* pTstEnv */, &TstStream, pToneParms); + if (RT_SUCCESS(rc)) + rcExit = RTEXITCODE_SUCCESS; + } + else + rcExit = RTMsgErrorExitFailure("Enabling the output stream failed: %Rrc", rc); + + /* + * Clean up. + */ + AudioTestMixStreamTerm(&TstStream.Mix); + } + audioTestDriverStackStreamDestroy(&DrvStack, TstStream.pStream); + TstStream.pStream = NULL; + } + else + rcExit = RTMsgErrorExitFailure("Creating output stream failed: %Rrc", rc); + } + else + rcExit = RTMsgErrorExitFailure("Failed to set output device to '%s': %Rrc", pszDevId, rc); + audioTestDriverStackDelete(&DrvStack); + } + else + rcExit = RTMsgErrorExitFailure("Driver stack construction failed: %Rrc", rc); + return rcExit; +} + + +/** + * Long option values for the 'play' command. + */ +enum +{ + VKAT_PLAY_OPT_TONE_DUR = 900, + VKAT_PLAY_OPT_TONE_FREQ, + VKAT_PLAY_OPT_TONE_VOL, + VKAT_PLAY_OPT_VOL +}; + + +/** + * Options for 'play'. + */ +static const RTGETOPTDEF g_aCmdPlayOptions[] = +{ + { "--backend", 'b', RTGETOPT_REQ_STRING }, + { "--channels", 'c', RTGETOPT_REQ_UINT8 }, + { "--hz", 'f', RTGETOPT_REQ_UINT32 }, + { "--frequency", 'f', RTGETOPT_REQ_UINT32 }, + { "--sample-size", 'z', RTGETOPT_REQ_UINT8 }, + { "--test-tone", 't', RTGETOPT_REQ_NOTHING }, + { "--tone-dur", VKAT_PLAY_OPT_TONE_DUR, RTGETOPT_REQ_UINT32 }, + { "--tone-freq", VKAT_PLAY_OPT_TONE_FREQ, RTGETOPT_REQ_UINT32 }, + { "--tone-vol", VKAT_PLAY_OPT_TONE_VOL, RTGETOPT_REQ_UINT32 }, + { "--output-device", 'o', RTGETOPT_REQ_STRING }, + { "--with-drv-audio", 'd', RTGETOPT_REQ_NOTHING }, + { "--with-mixer", 'm', RTGETOPT_REQ_NOTHING }, + { "--vol", VKAT_PLAY_OPT_VOL, RTGETOPT_REQ_UINT8 } +}; + + +/** The 'play' command option help. */ +static DECLCALLBACK(const char *) audioTestCmdPlayHelp(PCRTGETOPTDEF pOpt) +{ + switch (pOpt->iShort) + { + case 'b': return "The audio backend to use"; + case 'c': return "Number of backend output channels"; + case 'd': return "Go via DrvAudio instead of directly interfacing with the backend"; + case 'f': return "Output frequency (Hz)"; + case 'z': return "Output sample size (bits)"; + case 't': return "Plays a test tone. Can be specified multiple times"; + case 'm': return "Go via the mixer"; + case 'o': return "The ID of the output device to use"; + case VKAT_PLAY_OPT_TONE_DUR: return "Test tone duration (ms)"; + case VKAT_PLAY_OPT_TONE_FREQ: return "Test tone frequency (Hz)"; + case VKAT_PLAY_OPT_TONE_VOL: return "Test tone volume (percent)"; + case VKAT_PLAY_OPT_VOL: return "Playback volume (percent)"; + default: return NULL; + } +} + + +/** + * The 'play' command handler. + * + * @returns Program exit code. + * @param pGetState RTGetOpt state. + */ +static DECLCALLBACK(RTEXITCODE) audioTestCmdPlayHandler(PRTGETOPTSTATE pGetState) +{ + /* Option values: */ + PCPDMDRVREG pDrvReg = AudioTestGetDefaultBackend(); + const char *pszDevId = NULL; + uint32_t cTestTones = 0; + uint8_t cbSample = 0; + uint8_t cChannels = 0; + uint32_t uHz = 0; + + AUDIOTESTIOOPTS IoOpts; + audioTestIoOptsInitDefaults(&IoOpts); + + AUDIOTESTTONEPARMS ToneParms; + audioTestToneParmsInit(&ToneParms); + + /* Argument processing loop: */ + int ch; + RTGETOPTUNION ValueUnion; + while ((ch = RTGetOpt(pGetState, &ValueUnion)) != 0) + { + switch (ch) + { + case 'b': + pDrvReg = AudioTestFindBackendOpt(ValueUnion.psz); + if (pDrvReg == NULL) + return RTEXITCODE_SYNTAX; + break; + + case 'c': + cChannels = ValueUnion.u8; + break; + + case 'd': + IoOpts.fWithDrvAudio = true; + break; + + case 'f': + uHz = ValueUnion.u32; + break; + + case 'm': + IoOpts.fWithMixer = true; + break; + + case 'o': + pszDevId = ValueUnion.psz; + break; + + case 't': + cTestTones++; + break; + + case 'z': + cbSample = ValueUnion.u8 / 8; + break; + + case VKAT_PLAY_OPT_TONE_DUR: + ToneParms.msDuration = ValueUnion.u32; + break; + + case VKAT_PLAY_OPT_TONE_FREQ: + ToneParms.dbFreqHz = ValueUnion.u32; + break; + + case VKAT_PLAY_OPT_TONE_VOL: + ToneParms.uVolumePercent = ValueUnion.u8; + if (ToneParms.uVolumePercent > 100) + return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Invalid tonevolume (0-100)"); + break; + + case VKAT_PLAY_OPT_VOL: + IoOpts.uVolumePercent = ValueUnion.u8; + if (IoOpts.uVolumePercent > 100) + return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Invalid playback volume (0-100)"); + break; + + case VINF_GETOPT_NOT_OPTION: + { + if (cTestTones) + return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Playing test tones (-t) cannot be combined with playing files"); + + /* Set new (override standard) I/O PCM properties if set by the user. */ + PDMAudioPropsInit(&IoOpts.Props, + cbSample ? cbSample : 2 /* 16-bit */, true /* fSigned */, + cChannels ? cChannels : 2 /* Stereo */, uHz ? uHz : 44100); + + RTEXITCODE rcExit = audioTestPlayOne(ValueUnion.psz, pDrvReg, pszDevId, &IoOpts); + if (rcExit != RTEXITCODE_SUCCESS) + return rcExit; + break; + } + + AUDIO_TEST_COMMON_OPTION_CASES(ValueUnion, &g_CmdPlay); + + default: + return RTGetOptPrintError(ch, &ValueUnion); + } + } + + while (cTestTones--) + { + /* Use some sane defaults if no PCM props are set by the user. */ + PDMAudioPropsInit(&ToneParms.Props, + cbSample ? cbSample : 2 /* 16-bit */, true /* fSigned */, + cChannels ? cChannels : 2 /* Stereo */, uHz ? uHz : 44100); + + RTEXITCODE rcExit = audioTestPlayTestToneOne(&ToneParms, pDrvReg, pszDevId, &IoOpts); + if (rcExit != RTEXITCODE_SUCCESS) + return rcExit; + } + + return RTEXITCODE_SUCCESS; +} + + +/** + * Command table entry for 'play'. + */ +const VKATCMD g_CmdPlay = +{ + "play", + audioTestCmdPlayHandler, + "Plays one or more wave files.", + g_aCmdPlayOptions, + RT_ELEMENTS(g_aCmdPlayOptions), + audioTestCmdPlayHelp, + false /* fNeedsTransport */ +}; + + +/********************************************************************************************************************************* +* Command: rec * +*********************************************************************************************************************************/ + +/** + * Worker for audioTestRecOne implementing the recording loop. + */ +static RTEXITCODE audioTestRecOneInner(PAUDIOTESTDRVMIXSTREAM pMix, PAUDIOTESTWAVEFILE pWaveFile, + PCPDMAUDIOSTREAMCFG pCfgAcq, uint64_t cMaxFrames, const char *pszFile) +{ + int rc; + uint64_t const nsStarted = RTTimeNanoTS(); + + /* + * Transfer data as quickly as we're allowed. + */ + uint8_t abSamples[16384]; + uint32_t const cbSamplesAligned = PDMAudioPropsFloorBytesToFrame(pMix->pProps, sizeof(abSamples)); + uint64_t cFramesCapturedTotal = 0; + while (!g_fTerminate && cFramesCapturedTotal < cMaxFrames) + { + /* + * Anything we can read? + */ + uint32_t const cbCanRead = AudioTestMixStreamGetReadable(pMix); + if (cbCanRead) + { + uint32_t const cbToRead = RT_MIN(cbCanRead, cbSamplesAligned); + uint32_t cbCaptured = 0; + rc = AudioTestMixStreamCapture(pMix, abSamples, cbToRead, &cbCaptured); + if (RT_SUCCESS(rc)) + { + if (cbCaptured) + { + uint32_t cFramesCaptured = PDMAudioPropsBytesToFrames(pMix->pProps, cbCaptured); + if (cFramesCaptured + cFramesCaptured < cMaxFrames) + { /* likely */ } + else + { + cFramesCaptured = cMaxFrames - cFramesCaptured; + cbCaptured = PDMAudioPropsFramesToBytes(pMix->pProps, cFramesCaptured); + } + + rc = AudioTestWaveFileWrite(pWaveFile, abSamples, cbCaptured); + if (RT_SUCCESS(rc)) + cFramesCapturedTotal += cFramesCaptured; + else + return RTMsgErrorExitFailure("Error writing to '%s': %Rrc", pszFile, rc); + } + else + return RTMsgErrorExitFailure("Captured zero bytes - %#x bytes reported readable!\n", cbCanRead); + } + else + return RTMsgErrorExitFailure("Failed to capture %#x bytes: %Rrc (%#x available)\n", cbToRead, rc, cbCanRead); + } + else if (AudioTestMixStreamIsOkay(pMix)) + RTThreadSleep(RT_MIN(RT_MAX(1, pCfgAcq->Device.cMsSchedulingHint), 256)); + else + return RTMsgErrorExitFailure("Stream is not okay!\n"); + } + + /* + * Disable the stream. + */ + rc = AudioTestMixStreamDisable(pMix); + if (RT_SUCCESS(rc) && g_uVerbosity > 0) + RTMsgInfo("%'RU64 ns: Stopped after recording %RU64 frames%s\n", RTTimeNanoTS() - nsStarted, cFramesCapturedTotal, + g_fTerminate ? " - Ctrl-C" : "."); + else if (RT_FAILURE(rc)) + return RTMsgErrorExitFailure("Disabling stream failed: %Rrc", rc); + + return RTEXITCODE_SUCCESS; +} + + +/** + * Worker for audioTestCmdRecHandler that recs one file. + */ +static RTEXITCODE audioTestRecOne(const char *pszFile, uint8_t cWaveChannels, uint8_t cbWaveSample, uint32_t uWaveHz, + PCPDMDRVREG pDrvReg, const char *pszDevId, PAUDIOTESTIOOPTS pIoOpts, + uint64_t cMaxFrames, uint64_t cNsMaxDuration) +{ + /* + * Construct the driver stack. + */ + RTEXITCODE rcExit = RTEXITCODE_FAILURE; + AUDIOTESTDRVSTACK DrvStack; + int rc = audioTestDriverStackInit(&DrvStack, pDrvReg, pIoOpts->fWithDrvAudio); + if (RT_SUCCESS(rc)) + { + /* + * Set the input device if one is specified. + */ + rc = audioTestDriverStackSetDevice(&DrvStack, PDMAUDIODIR_IN, pszDevId); + if (RT_SUCCESS(rc)) + { + /* + * Create an input stream. + */ + PDMAUDIOPCMPROPS ReqProps; + PDMAudioPropsInit(&ReqProps, + pIoOpts->Props.cbSampleX ? pIoOpts->Props.cbSampleX : cbWaveSample ? cbWaveSample : 2, + pIoOpts->Props.fSigned, + pIoOpts->Props.cChannelsX ? pIoOpts->Props.cChannelsX : cWaveChannels ? cWaveChannels : 2, + pIoOpts->Props.uHz ? pIoOpts->Props.uHz : uWaveHz ? uWaveHz : 44100); + + PDMAUDIOSTREAMCFG CfgAcq; + PPDMAUDIOSTREAM pStream = NULL; + rc = audioTestDriverStackStreamCreateInput(&DrvStack, &ReqProps, pIoOpts->cMsBufferSize, + pIoOpts->cMsPreBuffer, pIoOpts->cMsSchedulingHint, &pStream, &CfgAcq); + if (RT_SUCCESS(rc)) + { + /* + * Determine the wave file properties. If it differs from the stream + * properties, make sure the mixer is enabled. + */ + PDMAUDIOPCMPROPS WaveProps; + PDMAudioPropsInit(&WaveProps, + cbWaveSample ? cbWaveSample : PDMAudioPropsSampleSize(&CfgAcq.Props), + true /*fSigned*/, + cWaveChannels ? cWaveChannels : PDMAudioPropsChannels(&CfgAcq.Props), + uWaveHz ? uWaveHz : PDMAudioPropsHz(&CfgAcq.Props)); + if (!pIoOpts->fWithMixer && !PDMAudioPropsAreEqual(&WaveProps, &CfgAcq.Props)) + { + RTMsgInfo("Enabling the mixer buffer.\n"); + pIoOpts->fWithMixer = true; + } + + /* Console the max duration into frames now that we've got the wave file format. */ + if (cMaxFrames != UINT64_MAX && cNsMaxDuration != UINT64_MAX) + { + uint64_t cMaxFrames2 = PDMAudioPropsNanoToBytes64(&WaveProps, cNsMaxDuration); + cMaxFrames = RT_MAX(cMaxFrames, cMaxFrames2); + } + else if (cNsMaxDuration != UINT64_MAX) + cMaxFrames = PDMAudioPropsNanoToBytes64(&WaveProps, cNsMaxDuration); + + /* + * Create a mixer wrapper. This is just a thin wrapper if fWithMixer + * is false, otherwise it's doing mixing, resampling and recoding. + */ + AUDIOTESTDRVMIXSTREAM Mix; + rc = AudioTestMixStreamInit(&Mix, &DrvStack, pStream, pIoOpts->fWithMixer ? &WaveProps : NULL, 100 /*ms*/); + if (RT_SUCCESS(rc)) + { + char szTmp[128]; + if (g_uVerbosity > 0) + RTMsgInfo("Stream: %s cbBackend=%#RX32%s\n", + PDMAudioPropsToString(&pStream->Cfg.Props, szTmp, sizeof(szTmp)), + pStream->cbBackend, pIoOpts->fWithMixer ? " mixed" : ""); + + /* + * Open the wave output file. + */ + AUDIOTESTWAVEFILE WaveFile; + RTERRINFOSTATIC ErrInfo; + rc = AudioTestWaveFileCreate(pszFile, &WaveProps, &WaveFile, RTErrInfoInitStatic(&ErrInfo)); + if (RT_SUCCESS(rc)) + { + if (g_uVerbosity > 0) + { + RTMsgInfo("Opened '%s' for playing\n", pszFile); + RTMsgInfo("Format: %s\n", PDMAudioPropsToString(&WaveFile.Props, szTmp, sizeof(szTmp))); + } + + /* + * Enable the stream and start recording. + */ + rc = AudioTestMixStreamEnable(&Mix); + if (RT_SUCCESS(rc)) + rcExit = audioTestRecOneInner(&Mix, &WaveFile, &CfgAcq, cMaxFrames, pszFile); + else + rcExit = RTMsgErrorExitFailure("Enabling the input stream failed: %Rrc", rc); + if (rcExit != RTEXITCODE_SUCCESS) + AudioTestMixStreamDisable(&Mix); + + /* + * Clean up. + */ + rc = AudioTestWaveFileClose(&WaveFile); + if (RT_FAILURE(rc)) + rcExit = RTMsgErrorExitFailure("Error closing '%s': %Rrc", pszFile, rc); + } + else + rcExit = RTMsgErrorExitFailure("Failed to open '%s': %Rrc%#RTeim", pszFile, rc, &ErrInfo.Core.pszMsg); + + AudioTestMixStreamTerm(&Mix); + } + audioTestDriverStackStreamDestroy(&DrvStack, pStream); + pStream = NULL; + } + else + rcExit = RTMsgErrorExitFailure("Creating output stream failed: %Rrc", rc); + } + else + rcExit = RTMsgErrorExitFailure("Failed to set output device to '%s': %Rrc", pszDevId, rc); + audioTestDriverStackDelete(&DrvStack); + } + else + rcExit = RTMsgErrorExitFailure("Driver stack construction failed: %Rrc", rc); + return rcExit; +} + + +/** + * Options for 'rec'. + */ +static const RTGETOPTDEF g_aCmdRecOptions[] = +{ + { "--backend", 'b', RTGETOPT_REQ_STRING }, + { "--channels", 'c', RTGETOPT_REQ_UINT8 }, + { "--hz", 'f', RTGETOPT_REQ_UINT32 }, + { "--frequency", 'f', RTGETOPT_REQ_UINT32 }, + { "--sample-size", 'z', RTGETOPT_REQ_UINT8 }, + { "--input-device", 'i', RTGETOPT_REQ_STRING }, + { "--wav-channels", 'C', RTGETOPT_REQ_UINT8 }, + { "--wav-hz", 'F', RTGETOPT_REQ_UINT32 }, + { "--wav-frequency", 'F', RTGETOPT_REQ_UINT32 }, + { "--wav-sample-size", 'Z', RTGETOPT_REQ_UINT8 }, + { "--with-drv-audio", 'd', RTGETOPT_REQ_NOTHING }, + { "--with-mixer", 'm', RTGETOPT_REQ_NOTHING }, + { "--max-frames", 'r', RTGETOPT_REQ_UINT64 }, + { "--max-sec", 's', RTGETOPT_REQ_UINT64 }, + { "--max-seconds", 's', RTGETOPT_REQ_UINT64 }, + { "--max-ms", 't', RTGETOPT_REQ_UINT64 }, + { "--max-milliseconds", 't', RTGETOPT_REQ_UINT64 }, + { "--max-ns", 'T', RTGETOPT_REQ_UINT64 }, + { "--max-nanoseconds", 'T', RTGETOPT_REQ_UINT64 }, +}; + + +/** The 'rec' command option help. */ +static DECLCALLBACK(const char *) audioTestCmdRecHelp(PCRTGETOPTDEF pOpt) +{ + switch (pOpt->iShort) + { + case 'b': return "The audio backend to use."; + case 'c': return "Number of backend input channels"; + case 'C': return "Number of wave-file channels"; + case 'd': return "Go via DrvAudio instead of directly interfacing with the backend."; + case 'f': return "Input frequency (Hz)"; + case 'F': return "Wave-file frequency (Hz)"; + case 'z': return "Input sample size (bits)"; + case 'Z': return "Wave-file sample size (bits)"; + case 'm': return "Go via the mixer."; + case 'i': return "The ID of the input device to use."; + case 'r': return "Max recording duration in frames."; + case 's': return "Max recording duration in seconds."; + case 't': return "Max recording duration in milliseconds."; + case 'T': return "Max recording duration in nanoseconds."; + default: return NULL; + } +} + + +/** + * The 'rec' command handler. + * + * @returns Program exit code. + * @param pGetState RTGetOpt state. + */ +static DECLCALLBACK(RTEXITCODE) audioTestCmdRecHandler(PRTGETOPTSTATE pGetState) +{ + /* Option values: */ + PCPDMDRVREG pDrvReg = AudioTestGetDefaultBackend(); + const char *pszDevId = NULL; + uint8_t cbSample = 0; + uint8_t cChannels = 0; + uint32_t uHz = 0; + uint8_t cbWaveSample = 0; + uint8_t cWaveChannels = 0; + uint32_t uWaveHz = 0; + uint64_t cMaxFrames = UINT64_MAX; + uint64_t cNsMaxDuration = UINT64_MAX; + + AUDIOTESTIOOPTS IoOpts; + audioTestIoOptsInitDefaults(&IoOpts); + + /* Argument processing loop: */ + int ch; + RTGETOPTUNION ValueUnion; + while ((ch = RTGetOpt(pGetState, &ValueUnion)) != 0) + { + switch (ch) + { + case 'b': + pDrvReg = AudioTestFindBackendOpt(ValueUnion.psz); + if (pDrvReg == NULL) + return RTEXITCODE_SYNTAX; + break; + + case 'c': + cChannels = ValueUnion.u8; + break; + + case 'C': + cWaveChannels = ValueUnion.u8; + break; + + case 'd': + IoOpts.fWithDrvAudio = true; + break; + + case 'f': + uHz = ValueUnion.u32; + break; + + case 'F': + uWaveHz = ValueUnion.u32; + break; + + case 'i': + pszDevId = ValueUnion.psz; + break; + + case 'm': + IoOpts.fWithMixer = true; + break; + + case 'r': + cMaxFrames = ValueUnion.u64; + break; + + case 's': + cNsMaxDuration = ValueUnion.u64 >= UINT64_MAX / RT_NS_1SEC ? UINT64_MAX : ValueUnion.u64 * RT_NS_1SEC; + break; + + case 't': + cNsMaxDuration = ValueUnion.u64 >= UINT64_MAX / RT_NS_1MS ? UINT64_MAX : ValueUnion.u64 * RT_NS_1MS; + break; + + case 'T': + cNsMaxDuration = ValueUnion.u64; + break; + + case 'z': + cbSample = ValueUnion.u8 / 8; + break; + + case 'Z': + cbWaveSample = ValueUnion.u8 / 8; + break; + + case VINF_GETOPT_NOT_OPTION: + { + if ( cbSample + || cChannels + || uHz) + { + /* Set new (override standard) I/O PCM properties if set by the user. */ + PDMAudioPropsInit(&IoOpts.Props, + cbSample ? cbSample : 2 /* 16-bit */, true /* fSigned */, + cChannels ? cChannels : 2 /* Stereo */, uHz ? uHz : 44100); + } + + RTEXITCODE rcExit = audioTestRecOne(ValueUnion.psz, cWaveChannels, cbWaveSample, uWaveHz, + pDrvReg, pszDevId, &IoOpts, + cMaxFrames, cNsMaxDuration); + if (rcExit != RTEXITCODE_SUCCESS) + return rcExit; + break; + } + + AUDIO_TEST_COMMON_OPTION_CASES(ValueUnion, &g_CmdRec); + + default: + return RTGetOptPrintError(ch, &ValueUnion); + } + } + return RTEXITCODE_SUCCESS; +} + + +/** + * Command table entry for 'rec'. + */ +const VKATCMD g_CmdRec = +{ + "rec", + audioTestCmdRecHandler, + "Records audio to a wave file.", + g_aCmdRecOptions, + RT_ELEMENTS(g_aCmdRecOptions), + audioTestCmdRecHelp, + false /* fNeedsTransport */ +}; + diff --git a/src/VBox/ValidationKit/utils/audio/vkatCmdSelfTest.cpp b/src/VBox/ValidationKit/utils/audio/vkatCmdSelfTest.cpp new file mode 100644 index 00000000..3259398d --- /dev/null +++ b/src/VBox/ValidationKit/utils/audio/vkatCmdSelfTest.cpp @@ -0,0 +1,481 @@ +/* $Id: vkatCmdSelfTest.cpp $ */ +/** @file + * Validation Kit Audio Test (VKAT) - Self test. + * + * Self-test which does a complete audio testing framework run without the need + * of a VM or other infrastructure, i.e. all required parts are running locally + * on the same machine. + * + * This self-test does the following: + * - 1. Creates a separate thread for the guest side VKAT and connects to the ATS instance on + * the host side at port 6052 (ATS_TCP_DEF_BIND_PORT_HOST). + * - 2. Uses the Validation Kit audio backend, which in turn creates an ATS instance + * listening at port 6062 (ATS_TCP_DEF_BIND_PORT_VALKIT). + * - 3. Uses the host test environment which creates an ATS instance + * listening at port 6052 (ATS_TCP_DEF_BIND_PORT_HOST). + * - 4. Executes a complete test run locally (e.g. without any guest (VM) involved). + */ + +/* + * Copyright (C) 2021-2023 Oracle and/or its affiliates. + * + * This file is part of VirtualBox base platform packages, as + * available from https://www.virtualbox.org. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, in version 3 of the + * License. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + * + * The contents of this file may alternatively be used under the terms + * of the Common Development and Distribution License Version 1.0 + * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included + * in the VirtualBox distribution, in which case the provisions of the + * CDDL are applicable instead of those of the GPL. + * + * You may elect to license modified versions of this file under the + * terms and conditions of either the GPL or the CDDL or both. + * + * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0 + */ + + +/********************************************************************************************************************************* +* Header Files * +*********************************************************************************************************************************/ + +#include +#include +#include +#include +#include +#include + +#include "Audio/AudioHlp.h" +#include "Audio/AudioTest.h" +#include "Audio/AudioTestService.h" +#include "Audio/AudioTestServiceClient.h" + +#include "vkatInternal.h" + + +/********************************************************************************************************************************* +* Internal structures * +*********************************************************************************************************************************/ + +/** + * Structure for keeping a VKAT self test context. + */ +typedef struct SELFTESTCTX +{ + /** Common tag for guest and host side. */ + char szTag[AUDIOTEST_TAG_MAX]; + /** The driver stack in use. */ + AUDIOTESTDRVSTACK DrvStack; + /** Audio driver to use. + * Defaults to the platform's default driver. */ + PCPDMDRVREG pDrvReg; + struct + { + AUDIOTESTENV TstEnv; + /** Where to bind the address of the guest ATS instance to. + * Defaults to localhost (127.0.0.1) if empty. */ + char szAtsAddr[64]; + /** Port of the guest ATS instance. + * Defaults to ATS_ALT_PORT if not set. */ + uint32_t uAtsPort; + } Guest; + struct + { + AUDIOTESTENV TstEnv; + /** Address of the guest ATS instance. + * Defaults to localhost (127.0.0.1) if not set. */ + char szGuestAtsAddr[64]; + /** Port of the guest ATS instance. + * Defaults to ATS_DEFAULT_PORT if not set. */ + uint32_t uGuestAtsPort; + /** Address of the Validation Kit audio driver ATS instance. + * Defaults to localhost (127.0.0.1) if not set. */ + char szValKitAtsAddr[64]; + /** Port of the Validation Kit audio driver ATS instance. + * Defaults to ATS_ALT_PORT if not set. */ + uint32_t uValKitAtsPort; + } Host; +} SELFTESTCTX; +/** Pointer to a VKAT self test context. */ +typedef SELFTESTCTX *PSELFTESTCTX; + + +/********************************************************************************************************************************* +* Global Variables * +*********************************************************************************************************************************/ + +/** The global self-text context. */ +static SELFTESTCTX g_Ctx; + + +/********************************************************************************************************************************* +* Driver stack self-test implementation * +*********************************************************************************************************************************/ + +/** + * Performs a (quick) audio driver stack self test. + * + * Local only, no guest/host communication involved. + * + * @returns VBox status code. + */ +int AudioTestDriverStackPerformSelftest(void) +{ + PCPDMDRVREG pDrvReg = AudioTestGetDefaultBackend(); + + RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Testing driver stack started\n"); + + AUDIOTESTDRVSTACK DrvStack; + int rc = audioTestDriverStackProbe(&DrvStack, pDrvReg, + true /* fEnabledIn */, true /* fEnabledOut */, false /* fWithDrvAudio */); + RTTEST_CHECK_RC_OK_RET(g_hTest, rc, rc); + + AUDIOTESTIOOPTS IoOpts; + audioTestIoOptsInitDefaults(&IoOpts); + + PPDMAUDIOSTREAM pStream; + PDMAUDIOSTREAMCFG CfgAcq; + rc = audioTestDriverStackStreamCreateOutput(&DrvStack, &IoOpts.Props, + IoOpts.cMsBufferSize, IoOpts.cMsPreBuffer, IoOpts.cMsSchedulingHint, + &pStream, &CfgAcq); + AssertRCReturn(rc, rc); + + rc = audioTestDriverStackStreamEnable(&DrvStack, pStream); + RTTEST_CHECK_RC_OK_RET(g_hTest, rc, rc); + + RTTEST_CHECK_RET(g_hTest, audioTestDriverStackStreamIsOkay(&DrvStack, pStream), VERR_AUDIO_STREAM_NOT_READY); + + uint8_t abBuf[_4K]; + memset(abBuf, 0x42, sizeof(abBuf)); + + uint32_t cbWritten; + rc = audioTestDriverStackStreamPlay(&DrvStack, pStream, abBuf, sizeof(abBuf), &cbWritten); + RTTEST_CHECK_RC_OK_RET(g_hTest, rc, rc); + RTTEST_CHECK_RET(g_hTest, cbWritten == sizeof(abBuf), VERR_AUDIO_STREAM_NOT_READY); + + audioTestDriverStackStreamDrain(&DrvStack, pStream, true /* fSync */); + audioTestDriverStackStreamDestroy(&DrvStack, pStream); + + audioTestDriverStackDelete(&DrvStack); + + RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Testing driver stack ended with %Rrc\n", rc); + return rc; +} + + +/********************************************************************************************************************************* +* Self-test implementation * +*********************************************************************************************************************************/ + +/** + * Thread callback for mocking the guest (VM) side of things. + * + * @returns VBox status code. + * @param hThread Thread handle. + * @param pvUser Pointer to user-supplied data. + */ +static DECLCALLBACK(int) audioTestSelftestGuestAtsThread(RTTHREAD hThread, void *pvUser) +{ + RT_NOREF(hThread); + PSELFTESTCTX pCtx = (PSELFTESTCTX)pvUser; + + PAUDIOTESTENV pTstEnvGst = &pCtx->Guest.TstEnv; + + audioTestEnvInit(pTstEnvGst); + + /* Flag the environment for self test mode. */ + pTstEnvGst->fSelftest = true; + + /* Tweak the address the guest ATS is trying to connect to the host if anything else is specified. + * Note: The host also runs on the same host (this self-test is completely self-contained and does not need a VM). */ + if (!pTstEnvGst->TcpOpts.szConnectAddr[0]) + RTStrCopy(pTstEnvGst->TcpOpts.szConnectAddr, sizeof(pTstEnvGst->TcpOpts.szConnectAddr), "127.0.0.1"); + + /* Generate tag for guest side. */ + int rc = RTStrCopy(pTstEnvGst->szTag, sizeof(pTstEnvGst->szTag), pCtx->szTag); + RTTEST_CHECK_RC_OK_RET(g_hTest, rc, rc); + + rc = AudioTestPathCreateTemp(pTstEnvGst->szPathTemp, sizeof(pTstEnvGst->szPathTemp), "selftest-guest"); + RTTEST_CHECK_RC_OK_RET(g_hTest, rc, rc); + + rc = AudioTestPathCreateTemp(pTstEnvGst->szPathOut, sizeof(pTstEnvGst->szPathOut), "selftest-out"); + RTTEST_CHECK_RC_OK_RET(g_hTest, rc, rc); + + pTstEnvGst->enmMode = AUDIOTESTMODE_GUEST; + + rc = audioTestEnvCreate(pTstEnvGst, &pCtx->DrvStack); + if (RT_SUCCESS(rc)) + { + RTThreadUserSignal(hThread); + + rc = audioTestWorker(pTstEnvGst); + RTTEST_CHECK_RC_OK_RET(g_hTest, rc, rc); + + audioTestEnvDestroy(pTstEnvGst); + } + + return rc; +} + +/** + * Main function for performing the self test. + * + * @returns RTEXITCODE + * @param pCtx Self test context to use. + */ +RTEXITCODE audioTestDoSelftest(PSELFTESTCTX pCtx) +{ + RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Running self test ...\n"); + + /* Generate a common tag for guest and host side. */ + int rc = AudioTestGenTag(pCtx->szTag, sizeof(pCtx->szTag)); + RTTEST_CHECK_RC_OK_RET(g_hTest, rc, RTEXITCODE_FAILURE); + + PAUDIOTESTENV pTstEnvHst = &pCtx->Host.TstEnv; + + audioTestEnvInit(pTstEnvHst); + + /* Flag the environment for self test mode. */ + pTstEnvHst->fSelftest = true; + + /* One test iteration with a 5s maximum test tone is enough for a (quick) self test. */ + pTstEnvHst->cIterations = 1; + pTstEnvHst->ToneParms.msDuration = RTRandU32Ex(500, RT_MS_5SEC); + + /* Generate tag for host side. */ + rc = RTStrCopy(pTstEnvHst->szTag, sizeof(pTstEnvHst->szTag), pCtx->szTag); + RTTEST_CHECK_RC_OK_RET(g_hTest, rc, RTEXITCODE_FAILURE); + + rc = AudioTestPathCreateTemp(pTstEnvHst->szPathTemp, sizeof(pTstEnvHst->szPathTemp), "selftest-tmp"); + RTTEST_CHECK_RC_OK_RET(g_hTest, rc, RTEXITCODE_FAILURE); + + rc = AudioTestPathCreateTemp(pTstEnvHst->szPathOut, sizeof(pTstEnvHst->szPathOut), "selftest-out"); + RTTEST_CHECK_RC_OK_RET(g_hTest, rc, RTEXITCODE_FAILURE); + + /* + * Step 1. + */ + RTTHREAD hThreadGstAts = NIL_RTTHREAD; + + bool const fStartGuestAts = RTStrNLen(pCtx->Host.szGuestAtsAddr, sizeof(pCtx->Host.szGuestAtsAddr)) == 0; + if (fStartGuestAts) + { + /* Step 1b. */ + rc = RTThreadCreate(&hThreadGstAts, audioTestSelftestGuestAtsThread, pCtx, 0, RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE, + "VKATGstAts"); + if (RT_SUCCESS(rc)) + rc = RTThreadUserWait(hThreadGstAts, RT_MS_30SEC); + } + + RTThreadSleep(2000); /* Fudge: Wait until guest ATS is up. 2 seconds should be enough (tm). */ + + if (RT_SUCCESS(rc)) + { + /* + * Steps 2 + 3. + */ + pTstEnvHst->enmMode = AUDIOTESTMODE_HOST; + + rc = audioTestEnvCreate(pTstEnvHst, &pCtx->DrvStack); + if (RT_SUCCESS(rc)) + { + /* + * Step 4. + */ + rc = audioTestWorker(pTstEnvHst); + RTTEST_CHECK_RC_OK(g_hTest, rc); + + audioTestEnvDestroy(pTstEnvHst); + } + } + + /* + * Shutting down. + */ + RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Shutting down self test\n"); + + /* If we started the guest ATS ourselves, wait for it to terminate properly. */ + if (fStartGuestAts) + { + int rcThread; + int rc2 = RTThreadWait(hThreadGstAts, RT_MS_30SEC, &rcThread); + if (RT_SUCCESS(rc2)) + rc2 = rcThread; + if (RT_FAILURE(rc2)) + RTTestFailed(g_hTest, "Shutting down guest ATS failed with %Rrc\n", rc2); + if (RT_SUCCESS(rc)) + rc = rc2; + } + + if (RT_FAILURE(rc)) + RTTestFailed(g_hTest, "Self test failed with %Rrc\n", rc); + + return RT_SUCCESS(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE; +} + + +/********************************************************************************************************************************* +* Command: selftest * +*********************************************************************************************************************************/ + +/** + * Command line parameters for self-test mode. + */ +static const RTGETOPTDEF s_aCmdSelftestOptions[] = +{ + { "--exclude-all", 'a', RTGETOPT_REQ_NOTHING }, + { "--backend", 'b', RTGETOPT_REQ_STRING }, + { "--with-drv-audio", 'd', RTGETOPT_REQ_NOTHING }, + { "--with-mixer", 'm', RTGETOPT_REQ_NOTHING }, + { "--exclude", 'e', RTGETOPT_REQ_UINT32 }, + { "--include", 'i', RTGETOPT_REQ_UINT32 } +}; + +/** the 'selftest' command option help. */ +static DECLCALLBACK(const char *) audioTestCmdSelftestHelp(PCRTGETOPTDEF pOpt) +{ + switch (pOpt->iShort) + { + case 'a': return "Exclude all tests from the list (useful to enable single tests later with --include)"; + case 'b': return "The audio backend to use"; + case 'd': return "Go via DrvAudio instead of directly interfacing with the backend"; + case 'e': return "Exclude the given test id from the list"; + case 'i': return "Include the given test id in the list"; + case 'm': return "Use the internal mixing engine explicitly"; + default: return NULL; + } +} + +/** + * The 'selftest' command handler. + * + * @returns Program exit code. + * @param pGetState RTGetOpt state. + */ +DECLCALLBACK(RTEXITCODE) audioTestCmdSelftestHandler(PRTGETOPTSTATE pGetState) +{ + RT_ZERO(g_Ctx); + + audioTestEnvInit(&g_Ctx.Guest.TstEnv); + audioTestEnvInit(&g_Ctx.Host.TstEnv); + + AUDIOTESTIOOPTS IoOpts; + audioTestIoOptsInitDefaults(&IoOpts); + + /* Argument processing loop: */ + int rc; + RTGETOPTUNION ValueUnion; + while ((rc = RTGetOpt(pGetState, &ValueUnion)) != 0) + { + switch (rc) + { + case 'a': + for (unsigned i = 0; i < g_cTests; i++) + g_aTests[i].fExcluded = true; + break; + + case 'b': + g_Ctx.pDrvReg = AudioTestFindBackendOpt(ValueUnion.psz); + if (g_Ctx.pDrvReg == NULL) + return RTEXITCODE_SYNTAX; + break; + + case 'd': + IoOpts.fWithDrvAudio = true; + break; + + case 'e': + if (ValueUnion.u32 >= g_cTests) + return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Invalid test number %u passed to --exclude", ValueUnion.u32); + g_aTests[ValueUnion.u32].fExcluded = true; + break; + + case 'i': + if (ValueUnion.u32 >= g_cTests) + return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Invalid test number %u passed to --include", ValueUnion.u32); + g_aTests[ValueUnion.u32].fExcluded = false; + break; + + case 'm': + IoOpts.fWithMixer = true; + break; + + AUDIO_TEST_COMMON_OPTION_CASES(ValueUnion, &g_CmdSelfTest); + + default: + return RTGetOptPrintError(rc, &ValueUnion); + } + } + + /* For simplicity both test environments, guest and host, will have the same I/O options. + ** @todo Make this indepedent by a prefix, "--[guest|host]-