#include <priv.h>
#endif
+#ifdef WIN32
+#define popen _popen
+#define pclose _pclose
+#endif
+
SWITCH_DECLARE_DATA switch_directories SWITCH_GLOBAL_dirs = { 0 };
SWITCH_DECLARE_DATA switch_filenames SWITCH_GLOBAL_filenames = { 0 };
SWITCH_DECLARE(int) switch_stream_system_fork(const char *cmd, switch_stream_handle_t *stream)
{
-#ifdef WIN32
- return switch_system(cmd, SWITCH_TRUE);
-#else
- int fds[2], pid = 0;
-
- if (pipe(fds)) {
- goto end;
- } else { /* good to go */
- pid = switch_fork();
-
- if (pid < 0) { /* ok maybe not */
- close(fds[0]);
- close(fds[1]);
- goto end;
- } else if (pid) { /* parent */
- char buf[1024] = "";
- int bytes;
- close(fds[1]);
- while ((bytes = read(fds[0], buf, sizeof(buf))) > 0) {
- stream->raw_write_function(stream, (unsigned char *)buf, bytes);
- }
- close(fds[0]);
- waitpid(pid, NULL, 0);
- } else { /* child */
- switch_close_extra_files(fds, 2);
- close(fds[0]);
- dup2(fds[1], STDOUT_FILENO);
- switch_system(cmd, SWITCH_TRUE);
- close(fds[1]);
- exit(0);
- }
- }
-
- end:
-
- return 0;
-
-#endif
-
+ return switch_stream_system(cmd, stream);
}
SWITCH_DECLARE(switch_status_t) switch_core_get_stacksizes(switch_size_t *cur, switch_size_t *max)
SWITCH_DECLARE(int) switch_stream_system(const char *cmd, switch_stream_handle_t *stream)
{
-#ifdef WIN32
- stream->write_function(stream, "Capturing output not supported.\n");
- return switch_system(cmd, SWITCH_TRUE);
-#else
- return switch_stream_system_fork(cmd, stream);
-#endif
+ char buffer[128];
+ size_t bytes;
+ FILE* pipe = popen(cmd, "r");
+ if (!pipe) return 1;
+
+ while (!feof(pipe)) {
+ while ((bytes = fread(buffer, 1, 128, pipe)) > 0) {
+ if (stream != NULL) {
+ stream->raw_write_function(stream, (unsigned char *)buffer, bytes);
+ }
+ }
+ }
+ if (ferror(pipe)) {
+ pclose(pipe);
+ return 1;
+ }
+
+ pclose(pipe);
+ return 0;
}
SWITCH_DECLARE(uint16_t) switch_core_get_rtp_port_range_start_port()
--- /dev/null
+/*
+ * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
+ * Copyright (C) 2005-2018, Anthony Minessale II <anthm@freeswitch.org>
+ *
+ * Version: MPL 1.1
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
+ *
+ * The Initial Developer of the Original Code is
+ * Anthony Minessale II <anthm@freeswitch.org>
+ * Portions created by the Initial Developer are Copyright (C)
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ * Chris Rienzo <chris@signalwire.com>
+ * Seven Du <dujinfang@gmail.com>
+ *
+ *
+ * switch_core.c -- tests core functions
+ *
+ */
+#include <switch.h>
+#include <stdlib.h>
+
+#include <test/switch_test.h>
+
+FST_CORE_BEGIN("./conf")
+{
+ FST_SUITE_BEGIN(switch_ivr_originate)
+ {
+ FST_SETUP_BEGIN()
+ {
+ }
+ FST_SETUP_END()
+
+ FST_TEARDOWN_BEGIN()
+ {
+ }
+ FST_TEARDOWN_END()
+
+#ifndef WIN32
+ FST_TEST_BEGIN(test_fork)
+ {
+ switch_stream_handle_t exec_result = { 0 };
+ SWITCH_STANDARD_STREAM(exec_result);
+ fst_requires(switch_stream_system_fork("ip ad sh", &exec_result) == 0);
+ fst_requires(!zstr(exec_result.data));
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%s\n", (char *)exec_result.data);
+
+ fst_requires(switch_stream_system_fork("ip ad sh | grep link", &exec_result) == 0);
+ fst_requires(!zstr(exec_result.data));
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%s\n", (char *)exec_result.data);
+
+ switch_safe_free(exec_result.data);
+ }
+ FST_TEST_END()
+#endif
+
+ FST_TEST_BEGIN(test_non_fork_exec_set)
+ {
+ char *var_test = switch_core_get_variable_dup("test");
+ char *var_default_password = switch_core_get_variable_dup("default_password");
+
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "global_getvar test: %s\n", switch_str_nil(var_test));
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "global_getvar default_password: %s\n", switch_str_nil(var_default_password));
+
+ fst_check_string_not_equals(var_test, "");
+ fst_check_string_not_equals(var_default_password, "");
+ fst_check_string_equals(var_test, var_default_password);
+
+ switch_safe_free(var_test);
+ switch_safe_free(var_default_password);
+ }
+ FST_TEST_END()
+ }
+ FST_SUITE_END()
+}
+FST_CORE_END()
--- /dev/null
+<?xml version="1.0" encoding="utf-8"?>\r
+<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">\r
+ <ItemGroup Label="ProjectConfigurations">\r
+ <ProjectConfiguration Include="Debug|Win32">\r
+ <Configuration>Debug</Configuration>\r
+ <Platform>Win32</Platform>\r
+ </ProjectConfiguration>\r
+ <ProjectConfiguration Include="Debug|x64">\r
+ <Configuration>Debug</Configuration>\r
+ <Platform>x64</Platform>\r
+ </ProjectConfiguration>\r
+ <ProjectConfiguration Include="Release|Win32">\r
+ <Configuration>Release</Configuration>\r
+ <Platform>Win32</Platform>\r
+ </ProjectConfiguration>\r
+ <ProjectConfiguration Include="Release|x64">\r
+ <Configuration>Release</Configuration>\r
+ <Platform>x64</Platform>\r
+ </ProjectConfiguration>\r
+ </ItemGroup>\r
+ <PropertyGroup Label="Globals">\r
+ <ProjectName>test_switch_core</ProjectName>\r
+ <RootNamespace>test_switch_core</RootNamespace>\r
+ <Keyword>Win32Proj</Keyword>\r
+ <WindowsTargetPlatformVersion>10.0.17134.0</WindowsTargetPlatformVersion>\r
+ <ProjectGuid>{EF62B845-A0CE-44FD-B8E6-475FE87D06C3}</ProjectGuid>\r
+ </PropertyGroup>\r
+ <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />\r
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">\r
+ <ConfigurationType>Application</ConfigurationType>\r
+ <CharacterSet>MultiByte</CharacterSet>\r
+ <PlatformToolset>v141</PlatformToolset>\r
+ </PropertyGroup>\r
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">\r
+ <ConfigurationType>Application</ConfigurationType>\r
+ <CharacterSet>MultiByte</CharacterSet>\r
+ <PlatformToolset>v141</PlatformToolset>\r
+ </PropertyGroup>\r
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">\r
+ <ConfigurationType>Application</ConfigurationType>\r
+ <CharacterSet>MultiByte</CharacterSet>\r
+ <PlatformToolset>v141</PlatformToolset>\r
+ </PropertyGroup>\r
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">\r
+ <ConfigurationType>Application</ConfigurationType>\r
+ <CharacterSet>MultiByte</CharacterSet>\r
+ <PlatformToolset>v141</PlatformToolset>\r
+ </PropertyGroup>\r
+ <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />\r
+ <ImportGroup Label="ExtensionSettings">\r
+ </ImportGroup>\r
+ <ImportGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="PropertySheets">\r
+ <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />\r
+ <Import Project="$(SolutionDir)w32\winlibs.props" />\r
+ </ImportGroup>\r
+ <ImportGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="PropertySheets">\r
+ <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />\r
+ <Import Project="$(SolutionDir)w32\winlibs.props" />\r
+ </ImportGroup>\r
+ <ImportGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="PropertySheets">\r
+ <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />\r
+ <Import Project="$(SolutionDir)w32\winlibs.props" />\r
+ </ImportGroup>\r
+ <ImportGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="PropertySheets">\r
+ <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />\r
+ <Import Project="$(SolutionDir)w32\winlibs.props" />\r
+ </ImportGroup>\r
+ <PropertyGroup Label="UserMacros" />\r
+ <PropertyGroup>\r
+ <_ProjectFileVersion>10.0.30319.1</_ProjectFileVersion>\r
+ <OutDir Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(SolutionDir)$(PlatformName)\$(Configuration)\</OutDir>\r
+ <IntDir Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(PlatformName)\$(Configuration)\</IntDir>\r
+ <LinkIncremental Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">false</LinkIncremental>\r
+ <OutDir Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">$(SolutionDir)$(PlatformName)\$(Configuration)\</OutDir>\r
+ <IntDir Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">$(Platform)\$(Configuration)\</IntDir>\r
+ <LinkIncremental Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">false</LinkIncremental>\r
+ <OutDir Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">$(SolutionDir)$(PlatformName)\$(Configuration)\</OutDir>\r
+ <IntDir Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">$(PlatformName)\$(Configuration)\</IntDir>\r
+ <LinkIncremental Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">false</LinkIncremental>\r
+ <OutDir Condition="'$(Configuration)|$(Platform)'=='Release|x64'">$(SolutionDir)$(Platform)\$(Configuration)\</OutDir>\r
+ <IntDir Condition="'$(Configuration)|$(Platform)'=='Release|x64'">$(Platform)\$(Configuration)\</IntDir>\r
+ <LinkIncremental Condition="'$(Configuration)|$(Platform)'=='Release|x64'">false</LinkIncremental>\r
+ </PropertyGroup>\r
+ <ItemDefinitionGroup>\r
+ <ClCompile>\r
+ <AdditionalIncludeDirectories>$(SolutionDir)src\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>\r
+ <PreprocessorDefinitions>SWITCH_TEST_BASE_DIR_FOR_CONF="..\\..\\tests\\unit\\";%(PreprocessorDefinitions)</PreprocessorDefinitions>\r
+ </ClCompile>\r
+ </ItemDefinitionGroup>\r
+ <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">\r
+ <BuildLog />\r
+ <ClCompile>\r
+ <Optimization>Disabled</Optimization>\r
+ <PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>\r
+ <MinimalRebuild>true</MinimalRebuild>\r
+ <BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks>\r
+ <RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>\r
+ <PrecompiledHeader>\r
+ </PrecompiledHeader>\r
+ <WarningLevel>Level4</WarningLevel>\r
+ <DebugInformationFormat>ProgramDatabase</DebugInformationFormat>\r
+ <EnablePREfast>true</EnablePREfast>\r
+ <DisableSpecificWarnings>6031;6340;6246;6011;6387;%(DisableSpecificWarnings)</DisableSpecificWarnings>\r
+ </ClCompile>\r
+ <Link>\r
+ <AdditionalLibraryDirectories>$(OutDir);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>\r
+ <GenerateDebugInformation>true</GenerateDebugInformation>\r
+ <SubSystem>Console</SubSystem>\r
+ <RandomizedBaseAddress>true</RandomizedBaseAddress>\r
+ <DataExecutionPrevention>\r
+ </DataExecutionPrevention>\r
+ <TargetMachine>MachineX86</TargetMachine>\r
+ </Link>\r
+ </ItemDefinitionGroup>\r
+ <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">\r
+ <BuildLog />\r
+ <Midl>\r
+ <TargetEnvironment>X64</TargetEnvironment>\r
+ </Midl>\r
+ <ClCompile>\r
+ <Optimization>Disabled</Optimization>\r
+ <PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>\r
+ <MinimalRebuild>true</MinimalRebuild>\r
+ <BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks>\r
+ <RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>\r
+ <PrecompiledHeader>\r
+ </PrecompiledHeader>\r
+ <WarningLevel>Level4</WarningLevel>\r
+ <DebugInformationFormat>ProgramDatabase</DebugInformationFormat>\r
+ <EnablePREfast>true</EnablePREfast>\r
+ <DisableSpecificWarnings>6031;6340;6246;6011;6387;%(DisableSpecificWarnings)</DisableSpecificWarnings>\r
+ </ClCompile>\r
+ <Link>\r
+ <AdditionalLibraryDirectories>$(OutDir);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>\r
+ <GenerateDebugInformation>true</GenerateDebugInformation>\r
+ <SubSystem>Console</SubSystem>\r
+ <RandomizedBaseAddress>true</RandomizedBaseAddress>\r
+ <DataExecutionPrevention>\r
+ </DataExecutionPrevention>\r
+ <TargetMachine>MachineX64</TargetMachine>\r
+ </Link>\r
+ </ItemDefinitionGroup>\r
+ <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">\r
+ <BuildLog />\r
+ <ClCompile>\r
+ <PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>\r
+ <RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>\r
+ <PrecompiledHeader>\r
+ </PrecompiledHeader>\r
+ <WarningLevel>Level4</WarningLevel>\r
+ <DebugInformationFormat>ProgramDatabase</DebugInformationFormat>\r
+ <DisableSpecificWarnings>6031;6340;6246;6011;6387;%(DisableSpecificWarnings)</DisableSpecificWarnings>\r
+ </ClCompile>\r
+ <Link>\r
+ <AdditionalLibraryDirectories>$(OutDir);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>\r
+ <GenerateDebugInformation>false</GenerateDebugInformation>\r
+ <SubSystem>Console</SubSystem>\r
+ <OptimizeReferences>true</OptimizeReferences>\r
+ <EnableCOMDATFolding>true</EnableCOMDATFolding>\r
+ <RandomizedBaseAddress>true</RandomizedBaseAddress>\r
+ <DataExecutionPrevention>\r
+ </DataExecutionPrevention>\r
+ <TargetMachine>MachineX86</TargetMachine>\r
+ </Link>\r
+ </ItemDefinitionGroup>\r
+ <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">\r
+ <BuildLog />\r
+ <Midl>\r
+ <TargetEnvironment>X64</TargetEnvironment>\r
+ </Midl>\r
+ <ClCompile>\r
+ <PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>\r
+ <RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>\r
+ <PrecompiledHeader>\r
+ </PrecompiledHeader>\r
+ <WarningLevel>Level4</WarningLevel>\r
+ <DebugInformationFormat>ProgramDatabase</DebugInformationFormat>\r
+ <DisableSpecificWarnings>6031;6340;6246;6011;6387;%(DisableSpecificWarnings)</DisableSpecificWarnings>\r
+ </ClCompile>\r
+ <Link>\r
+ <AdditionalLibraryDirectories>$(OutDir);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>\r
+ <GenerateDebugInformation>false</GenerateDebugInformation>\r
+ <SubSystem>Console</SubSystem>\r
+ <OptimizeReferences>true</OptimizeReferences>\r
+ <EnableCOMDATFolding>true</EnableCOMDATFolding>\r
+ <RandomizedBaseAddress>true</RandomizedBaseAddress>\r
+ <DataExecutionPrevention>\r
+ </DataExecutionPrevention>\r
+ <TargetMachine>MachineX64</TargetMachine>\r
+ </Link>\r
+ </ItemDefinitionGroup>\r
+ <ItemGroup>\r
+ <ClCompile Include="switch_core.c" />\r
+ </ItemGroup>\r
+ <ItemGroup>\r
+ <ProjectReference Include="$(SolutionDir)w32\Library\FreeSwitchCore.2017.vcxproj">\r
+ <Project>{202d7a4e-760d-4d0e-afa1-d7459ced30ff}</Project>\r
+ </ProjectReference>\r
+ </ItemGroup>\r
+ <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />\r
+ <ImportGroup Label="ExtensionTargets">\r
+ </ImportGroup>\r
+</Project>
\ No newline at end of file