/* $Id: VBoxServiceToolBox.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */ /** @file * VBoxServiceToolbox - Internal (BusyBox-like) toolbox. */ /* * Copyright (C) 2012-2024 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 . * * SPDX-License-Identifier: GPL-3.0-only */ /********************************************************************************************************************************* * Header Files * *********************************************************************************************************************************/ #include #include #include #include #include #include #include #include #include #include #include #include #ifndef RT_OS_WINDOWS # include /* need umask */ #endif #include #include #include #include "VBoxServiceInternal.h" #include "VBoxServiceToolBox.h" #include "VBoxServiceUtils.h" using namespace guestControl; /********************************************************************************************************************************* * Defined Constants And Macros * *********************************************************************************************************************************/ /** Generic option indices for commands. */ enum { VBOXSERVICETOOLBOXOPT_MACHINE_READABLE = 1000, VBOXSERVICETOOLBOXOPT_VERBOSE }; /** Options indices for "vbox_cat". */ typedef enum VBOXSERVICETOOLBOXCATOPT { VBOXSERVICETOOLBOXCATOPT_NO_CONTENT_INDEXED = 1000 } VBOXSERVICETOOLBOXCATOPT; /** Flags for "vbox_ls". */ typedef enum VBOXSERVICETOOLBOXLSFLAG { VBOXSERVICETOOLBOXLSFLAG_NONE, VBOXSERVICETOOLBOXLSFLAG_RECURSIVE, VBOXSERVICETOOLBOXLSFLAG_SYMLINKS } VBOXSERVICETOOLBOXLSFLAG; /** Flags for fs object output. */ typedef enum VBOXSERVICETOOLBOXOUTPUTFLAG { VBOXSERVICETOOLBOXOUTPUTFLAG_NONE, VBOXSERVICETOOLBOXOUTPUTFLAG_LONG, VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE } VBOXSERVICETOOLBOXOUTPUTFLAG; /** The size of the directory entry buffer we're using. */ #define VBOXSERVICETOOLBOX_DIRENTRY_BUF_SIZE (sizeof(RTDIRENTRYEX) + RTPATH_MAX) /********************************************************************************************************************************* * Structures and Typedefs * *********************************************************************************************************************************/ /** Pointer to a tool handler function. */ typedef RTEXITCODE (*PFNHANDLER)(int , char **); /** Definition for a specific toolbox tool. */ typedef struct VBOXSERVICETOOLBOXTOOL { /** Friendly name of the tool. */ const char *pszName; /** Main handler to be invoked to use the tool. */ RTEXITCODE (*pfnHandler)(int argc, char **argv); /** Conversion routine to convert the tool's exit code back to an IPRT rc. Optional. * * @todo r=bird: You better revert this, i.e. having pfnHandler return a VBox * status code and have a routine for converting it to RTEXITCODE. * Unless, what you really want to do here is to get a cached status, in * which case you better call it what it is. */ int (*pfnExitCodeConvertToRc)(RTEXITCODE rcExit); } VBOXSERVICETOOLBOXTOOL; /** Pointer to a const tool definition. */ typedef VBOXSERVICETOOLBOXTOOL const *PCVBOXSERVICETOOLBOXTOOL; /** * An file/directory entry. Used to cache * file names/paths for later processing. */ typedef struct VBOXSERVICETOOLBOXPATHENTRY { /** Our node. */ RTLISTNODE Node; /** Name of the entry. */ char *pszName; } VBOXSERVICETOOLBOXPATHENTRY, *PVBOXSERVICETOOLBOXPATHENTRY; /********************************************************************************************************************************* * Internal Functions * *********************************************************************************************************************************/ static RTEXITCODE vgsvcToolboxCat(int argc, char **argv); static RTEXITCODE vgsvcToolboxLs(int argc, char **argv); static RTEXITCODE vgsvcToolboxRm(int argc, char **argv); static RTEXITCODE vgsvcToolboxMkTemp(int argc, char **argv); static RTEXITCODE vgsvcToolboxMkDir(int argc, char **argv); static RTEXITCODE vgsvcToolboxStat(int argc, char **argv); /********************************************************************************************************************************* * Global Variables * *********************************************************************************************************************************/ /** Tool definitions. */ static VBOXSERVICETOOLBOXTOOL const g_aTools[] = { { VBOXSERVICE_TOOL_CAT, vgsvcToolboxCat , NULL }, { VBOXSERVICE_TOOL_LS, vgsvcToolboxLs , NULL }, { VBOXSERVICE_TOOL_RM, vgsvcToolboxRm , NULL }, { VBOXSERVICE_TOOL_MKTEMP, vgsvcToolboxMkTemp, NULL }, { VBOXSERVICE_TOOL_MKDIR, vgsvcToolboxMkDir , NULL }, { VBOXSERVICE_TOOL_STAT, vgsvcToolboxStat , NULL } }; /** * Displays a common header for all help text to stdout. */ static void vgsvcToolboxShowUsageHeader(void) { RTPrintf(VBOX_PRODUCT " Guest Toolbox Version " VBOX_VERSION_STRING "\n" "Copyright (C) " VBOX_C_YEAR " " VBOX_VENDOR "\n\n"); RTPrintf("Usage:\n\n"); } /** * Displays a help text to stdout. */ static void vgsvcToolboxShowUsage(void) { vgsvcToolboxShowUsageHeader(); RTPrintf(" VBoxService [--use-toolbox] vbox_ [] \n\n" "General options:\n\n" " --machinereadable produce all output in machine-readable form\n" " -V print version number and exit\n" "\n" "Commands:\n\n" " vbox_cat [] ...\n" " vbox_ls [] [--dereference|-L] [-l] [-R]\n" " [--verbose|-v] [...]\n" " vbox_rm [] [-r|-R] ...\n" " vbox_mktemp [] [--directory|-d] [--mode|-m ]\n" " [--secure|-s] [--tmpdir|-t ]