-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix nasa#354, Split shell API for optional implement
- Loading branch information
Showing
10 changed files
with
301 additions
and
184 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,5 +15,5 @@ add_library(osal_posix_impl OBJECT | |
osnetwork.c | ||
osselect.c | ||
ostimer.c | ||
../portable/os-impl-no-shell.c | ||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 */ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,4 +14,5 @@ add_library(osal_rtems_impl OBJECT | |
osnetwork.c | ||
osselect.c | ||
ostimer.c | ||
../portable/os-impl-no-shell.c | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 */ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,4 +14,5 @@ add_library(osal_vxworks_impl OBJECT | |
osnetwork.c | ||
osselect.c | ||
ostimer.c | ||
) | ||
../portable/os-impl-no-shell.c | ||
) |
Oops, something went wrong.