Skip to content

Commit

Permalink
Fix nasa#354, Split shell API for optional implement
Browse files Browse the repository at this point in the history
Defaults to not include OS_ShellOutputToFile implementation
  • Loading branch information
skliper committed Apr 20, 2020
1 parent a66eb2d commit 1f5bdcb
Show file tree
Hide file tree
Showing 14 changed files with 419 additions and 220 deletions.
30 changes: 30 additions & 0 deletions src/os/portable/os-impl-no-shell.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (c) 2018, United States government as represented by the
* administrator of the National Aeronautics Space Administration.
* All rights reserved. This software was created at NASA Glenn
* Research Center pursuant to government contracts.
*
* This is governed by the NASA Open Source Agreement and may be used,
* distributed and modified only according to the terms of that agreement.
*/

#include "osapi.h"

/**
* \file os-impl-no-shell.c
*
* Purpose: No shell implementation, returns OS_ERR_NOT_IMPLEMENTED for calls
*/

/*----------------------------------------------------------------
*
* Function: OS_ShellOutputToFile_Impl
*
* Purpose: Implemented per internal OSAL API
* See description in os-impl.h for argument/return detail
*
*-----------------------------------------------------------------*/
int32 OS_ShellOutputToFile_Impl(uint32 file_id, const char* Cmd)
{
return OS_ERR_NOT_IMPLEMENTED;
}
2 changes: 1 addition & 1 deletion src/os/posix/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ add_library(osal_posix_impl OBJECT
osnetwork.c
osselect.c
ostimer.c
../portable/os-impl-no-shell.c
)

68 changes: 0 additions & 68 deletions src/os/posix/osfileapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@
#include <fcntl.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>


/****************************************************************************************
GLOBALS
Expand Down Expand Up @@ -137,68 +134,3 @@ int32 OS_Posix_DirAPI_Impl_Init(void)
memset(OS_impl_dir_table, 0, sizeof(OS_impl_dir_table));
return OS_SUCCESS;
} /* end OS_Posix_DirAPI_Impl_Init */




/*----------------------------------------------------------------
*
* Function: OS_ShellOutputToFile_Impl
*
* Purpose: Implemented per internal OSAL API
* See prototype in os-impl.h for argument/return detail
*
*-----------------------------------------------------------------*/
int32 OS_ShellOutputToFile_Impl(uint32 file_id, const char* Cmd)
{
pid_t cpid;
uint32 local_id;
int wstat;
const char *shell = getenv("SHELL");

if (shell == NULL)
{
shell = "/bin/sh";
}

cpid = fork();
if (cpid < 0)
{
OS_DEBUG("%s(): Error during fork(): %s\n", __func__, strerror(errno));
return OS_ERROR;
}

if (cpid == 0)
{
/* child process */
dup2(OS_impl_filehandle_table[file_id].fd, STDOUT_FILENO);
dup2(OS_impl_filehandle_table[file_id].fd, STDERR_FILENO);

/* close all _other_ filehandles */
for (local_id = 0; local_id < OS_MAX_NUM_OPEN_FILES; ++local_id)
{
if (OS_global_stream_table[local_id].active_id != 0)
{
close(OS_impl_filehandle_table[local_id].fd);
}
}

execl(shell, "sh", "-c", Cmd, NULL); /* does not return if successful */
exit(EXIT_FAILURE);
}

if (waitpid(cpid, &wstat, 0) != cpid)
{
OS_DEBUG("%s(): Error during waitpid(): %s\n", __func__, strerror(errno));
return OS_ERROR;
}

if (!WIFEXITED(wstat) || WEXITSTATUS(wstat) != 0)
{
OS_DEBUG("%s(): Error from child process: %d\n", __func__, wstat);
return OS_ERROR;
}

return OS_SUCCESS;
} /* end OS_ShellOutputToFile_Impl */

94 changes: 94 additions & 0 deletions src/os/posix/osshell.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright (c) 2018, United States government as represented by the
* administrator of the National Aeronautics Space Administration.
* All rights reserved. This software was created at NASA Glenn
* Research Center pursuant to government contracts.
*
* This is governed by the NASA Open Source Agreement and may be used,
* distributed and modified only according to the terms of that agreement.
*/

/**
* \file osshell.c
*
* Purpose: Implements shell-related calls that can be optionally built
* for distributions that choose to support them. Alternatively
* build the portable no-shell implementation to exclude this
* functionality.
*/

/****************************************************************************************
INCLUDE FILES
***************************************************************************************/

#include "os-posix.h"

#include <sys/types.h>
#include <sys/wait.h>

/****************************************************************************************
IMPLEMENTATION-SPECIFIC ROUTINES
These are specific to this particular operating system
****************************************************************************************/

/*----------------------------------------------------------------
*
* Function: OS_ShellOutputToFile_Impl
*
* Purpose: Implemented per internal OSAL API
* See prototype in os-impl.h for argument/return detail
*
*-----------------------------------------------------------------*/
int32 OS_ShellOutputToFile_Impl(uint32 file_id, const char* Cmd)
{
pid_t cpid;
uint32 local_id;
int wstat;
const char *shell = getenv("SHELL");

if (shell == NULL)
{
shell = "/bin/sh";
}

cpid = fork();
if (cpid < 0)
{
OS_DEBUG("%s(): Error during fork(): %s\n", __func__, strerror(errno));
return OS_ERROR;
}

if (cpid == 0)
{
/* child process */
dup2(OS_impl_filehandle_table[file_id].fd, STDOUT_FILENO);
dup2(OS_impl_filehandle_table[file_id].fd, STDERR_FILENO);

/* close all _other_ filehandles */
for (local_id = 0; local_id < OS_MAX_NUM_OPEN_FILES; ++local_id)
{
if (OS_global_stream_table[local_id].active_id != 0)
{
close(OS_impl_filehandle_table[local_id].fd);
}
}

execl(shell, "sh", "-c", Cmd, NULL); /* does not return if successful */
exit(EXIT_FAILURE);
}

if (waitpid(cpid, &wstat, 0) != cpid)
{
OS_DEBUG("%s(): Error during waitpid(): %s\n", __func__, strerror(errno));
return OS_ERROR;
}

if (!WIFEXITED(wstat) || WEXITSTATUS(wstat) != 0)
{
OS_DEBUG("%s(): Error from child process: %d\n", __func__, wstat);
return OS_ERROR;
}

return OS_SUCCESS;
} /* end OS_ShellOutputToFile_Impl */

1 change: 1 addition & 0 deletions src/os/rtems/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ add_library(osal_rtems_impl OBJECT
osnetwork.c
osselect.c
ostimer.c
../portable/os-impl-no-shell.c
)
44 changes: 0 additions & 44 deletions src/os/rtems/osfileapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,47 +137,3 @@ int32 OS_Rtems_DirAPI_Impl_Init(void)


/* FIXME - need to do something better here */


/*----------------------------------------------------------------
*
* Function: OS_ShellOutputToFile_Impl
*
* Purpose: Implemented per internal OSAL API
* See prototype in os-impl.h for argument/return detail
*
*-----------------------------------------------------------------*/
int32 OS_ShellOutputToFile_Impl(uint32 file_id, const char *Cmd)
{
/*
** this is a #define to avoid a 'variable length array' warning
** 15 is for the size of the redirection string that is added
** to the command
*/
char LocalCmd [OS_MAX_CMD_LEN + OS_REDIRECTSTRSIZE];
int32 Result;

strncpy(LocalCmd,Cmd,OS_MAX_CMD_LEN +OS_REDIRECTSTRSIZE);

/* Make sure that we are able to access this file */
fchmod(OS_impl_filehandle_table[file_id].fd, 0666);

/*
** add in the extra chars necessary to perform the redirection
** 1 for stdout and 2 for stderr. they are redirected to the
** file descriptor passed in
*/
snprintf(LocalCmd, sizeof(LocalCmd), "%s 1>&%d 2>&%d",
Cmd,
OS_impl_filehandle_table[file_id].fd,
OS_impl_filehandle_table[file_id].fd);

Result = system(LocalCmd);

if (Result != 0)
{
return OS_FS_ERROR;
}
return OS_SUCCESS;
} /* end OS_ShellOutputToFile_Impl */

75 changes: 75 additions & 0 deletions src/os/rtems/osshell.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright (c) 2018, United States government as represented by the
* administrator of the National Aeronautics Space Administration.
* All rights reserved. This software was created at NASA Glenn
* Research Center pursuant to government contracts.
*
* This is governed by the NASA Open Source Agreement and may be used,
* distributed and modified only according to the terms of that agreement.
*/

/**
* \file osshell.c
*
* Purpose: Implements shell-related calls that can be optionally built
* for distributions that choose to support them. Alternatively
* build the portable no-shell implementation to exclude this
* functionality.
*/

/****************************************************************************************
INCLUDE FILES
***************************************************************************************/

#include "os-rtems.h"

#include <sys/stat.h>
#include <fcntl.h>

/****************************************************************************************
IMPLEMENTATION-SPECIFIC ROUTINES
These are specific to this particular operating system
****************************************************************************************/

/*----------------------------------------------------------------
*
* Function: OS_ShellOutputToFile_Impl
*
* Purpose: Implemented per internal OSAL API
* See prototype in os-impl.h for argument/return detail
*
*-----------------------------------------------------------------*/
int32 OS_ShellOutputToFile_Impl(uint32 file_id, const char* Cmd)
{
/*
** this is a #define to avoid a 'variable length array' warning
** 15 is for the size of the redirection string that is added
** to the command
*/
char LocalCmd [OS_MAX_CMD_LEN + OS_REDIRECTSTRSIZE];
int32 Result;

strncpy(LocalCmd,Cmd,OS_MAX_CMD_LEN +OS_REDIRECTSTRSIZE);

/* Make sure that we are able to access this file */
fchmod(OS_impl_filehandle_table[file_id].fd, 0666);

/*
** add in the extra chars necessary to perform the redirection
** 1 for stdout and 2 for stderr. they are redirected to the
** file descriptor passed in
*/
snprintf(LocalCmd, sizeof(LocalCmd), "%s 1>&%d 2>&%d",
Cmd,
OS_impl_filehandle_table[file_id].fd,
OS_impl_filehandle_table[file_id].fd);

Result = system(LocalCmd);

if (Result != 0)
{
return OS_FS_ERROR;
}
return OS_SUCCESS;
} /* end OS_ShellOutputToFile_Impl */

3 changes: 2 additions & 1 deletion src/os/vxworks/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ add_library(osal_vxworks_impl OBJECT
osnetwork.c
osselect.c
ostimer.c
)
../portable/os-impl-no-shell.c
)
Loading

0 comments on commit 1f5bdcb

Please sign in to comment.