Skip to content

Commit 3fd78d7

Browse files
committed
Fix #354, Split shell API for optional implement
Defaults to not include OS_ShellOutputToFile implementation
1 parent f59b16b commit 3fd78d7

14 files changed

+419
-220
lines changed

src/os/portable/os-impl-no-shell.c

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) 2018, United States government as represented by the
3+
* administrator of the National Aeronautics Space Administration.
4+
* All rights reserved. This software was created at NASA Glenn
5+
* Research Center pursuant to government contracts.
6+
*
7+
* This is governed by the NASA Open Source Agreement and may be used,
8+
* distributed and modified only according to the terms of that agreement.
9+
*/
10+
11+
#include "osapi.h"
12+
13+
/**
14+
* \file os-impl-no-shell.c
15+
*
16+
* Purpose: No shell implementation, returns OS_ERR_NOT_IMPLEMENTED for calls
17+
*/
18+
19+
/*----------------------------------------------------------------
20+
*
21+
* Function: OS_ShellOutputToFile_Impl
22+
*
23+
* Purpose: Implemented per internal OSAL API
24+
* See description in os-impl.h for argument/return detail
25+
*
26+
*-----------------------------------------------------------------*/
27+
int32 OS_ShellOutputToFile_Impl(uint32 file_id, const char* Cmd)
28+
{
29+
return OS_ERR_NOT_IMPLEMENTED;
30+
}

src/os/posix/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ add_library(osal_posix_impl OBJECT
1515
osnetwork.c
1616
osselect.c
1717
ostimer.c
18+
../portable/os-impl-no-shell.c
1819
)
19-

src/os/posix/osfileapi.c

-68
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@
2626
#include <fcntl.h>
2727
#include <dirent.h>
2828
#include <sys/stat.h>
29-
#include <sys/types.h>
30-
#include <sys/wait.h>
31-
3229

3330
/****************************************************************************************
3431
GLOBALS
@@ -137,68 +134,3 @@ int32 OS_Posix_DirAPI_Impl_Init(void)
137134
memset(OS_impl_dir_table, 0, sizeof(OS_impl_dir_table));
138135
return OS_SUCCESS;
139136
} /* end OS_Posix_DirAPI_Impl_Init */
140-
141-
142-
143-
144-
/*----------------------------------------------------------------
145-
*
146-
* Function: OS_ShellOutputToFile_Impl
147-
*
148-
* Purpose: Implemented per internal OSAL API
149-
* See prototype in os-impl.h for argument/return detail
150-
*
151-
*-----------------------------------------------------------------*/
152-
int32 OS_ShellOutputToFile_Impl(uint32 file_id, const char* Cmd)
153-
{
154-
pid_t cpid;
155-
uint32 local_id;
156-
int wstat;
157-
const char *shell = getenv("SHELL");
158-
159-
if (shell == NULL)
160-
{
161-
shell = "/bin/sh";
162-
}
163-
164-
cpid = fork();
165-
if (cpid < 0)
166-
{
167-
OS_DEBUG("%s(): Error during fork(): %s\n", __func__, strerror(errno));
168-
return OS_ERROR;
169-
}
170-
171-
if (cpid == 0)
172-
{
173-
/* child process */
174-
dup2(OS_impl_filehandle_table[file_id].fd, STDOUT_FILENO);
175-
dup2(OS_impl_filehandle_table[file_id].fd, STDERR_FILENO);
176-
177-
/* close all _other_ filehandles */
178-
for (local_id = 0; local_id < OS_MAX_NUM_OPEN_FILES; ++local_id)
179-
{
180-
if (OS_global_stream_table[local_id].active_id != 0)
181-
{
182-
close(OS_impl_filehandle_table[local_id].fd);
183-
}
184-
}
185-
186-
execl(shell, "sh", "-c", Cmd, NULL); /* does not return if successful */
187-
exit(EXIT_FAILURE);
188-
}
189-
190-
if (waitpid(cpid, &wstat, 0) != cpid)
191-
{
192-
OS_DEBUG("%s(): Error during waitpid(): %s\n", __func__, strerror(errno));
193-
return OS_ERROR;
194-
}
195-
196-
if (!WIFEXITED(wstat) || WEXITSTATUS(wstat) != 0)
197-
{
198-
OS_DEBUG("%s(): Error from child process: %d\n", __func__, wstat);
199-
return OS_ERROR;
200-
}
201-
202-
return OS_SUCCESS;
203-
} /* end OS_ShellOutputToFile_Impl */
204-

src/os/posix/osshell.c

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Copyright (c) 2018, United States government as represented by the
3+
* administrator of the National Aeronautics Space Administration.
4+
* All rights reserved. This software was created at NASA Glenn
5+
* Research Center pursuant to government contracts.
6+
*
7+
* This is governed by the NASA Open Source Agreement and may be used,
8+
* distributed and modified only according to the terms of that agreement.
9+
*/
10+
11+
/**
12+
* \file osshell.c
13+
*
14+
* Purpose: Implements shell-related calls that can be optionally built
15+
* for distributions that choose to support them. Alternatively
16+
* build the portable no-shell implementation to exclude this
17+
* functionality.
18+
*/
19+
20+
/****************************************************************************************
21+
INCLUDE FILES
22+
***************************************************************************************/
23+
24+
#include "os-posix.h"
25+
26+
#include <sys/types.h>
27+
#include <sys/wait.h>
28+
29+
/****************************************************************************************
30+
IMPLEMENTATION-SPECIFIC ROUTINES
31+
These are specific to this particular operating system
32+
****************************************************************************************/
33+
34+
/*----------------------------------------------------------------
35+
*
36+
* Function: OS_ShellOutputToFile_Impl
37+
*
38+
* Purpose: Implemented per internal OSAL API
39+
* See prototype in os-impl.h for argument/return detail
40+
*
41+
*-----------------------------------------------------------------*/
42+
int32 OS_ShellOutputToFile_Impl(uint32 file_id, const char* Cmd)
43+
{
44+
pid_t cpid;
45+
uint32 local_id;
46+
int wstat;
47+
const char *shell = getenv("SHELL");
48+
49+
if (shell == NULL)
50+
{
51+
shell = "/bin/sh";
52+
}
53+
54+
cpid = fork();
55+
if (cpid < 0)
56+
{
57+
OS_DEBUG("%s(): Error during fork(): %s\n", __func__, strerror(errno));
58+
return OS_ERROR;
59+
}
60+
61+
if (cpid == 0)
62+
{
63+
/* child process */
64+
dup2(OS_impl_filehandle_table[file_id].fd, STDOUT_FILENO);
65+
dup2(OS_impl_filehandle_table[file_id].fd, STDERR_FILENO);
66+
67+
/* close all _other_ filehandles */
68+
for (local_id = 0; local_id < OS_MAX_NUM_OPEN_FILES; ++local_id)
69+
{
70+
if (OS_global_stream_table[local_id].active_id != 0)
71+
{
72+
close(OS_impl_filehandle_table[local_id].fd);
73+
}
74+
}
75+
76+
execl(shell, "sh", "-c", Cmd, NULL); /* does not return if successful */
77+
exit(EXIT_FAILURE);
78+
}
79+
80+
if (waitpid(cpid, &wstat, 0) != cpid)
81+
{
82+
OS_DEBUG("%s(): Error during waitpid(): %s\n", __func__, strerror(errno));
83+
return OS_ERROR;
84+
}
85+
86+
if (!WIFEXITED(wstat) || WEXITSTATUS(wstat) != 0)
87+
{
88+
OS_DEBUG("%s(): Error from child process: %d\n", __func__, wstat);
89+
return OS_ERROR;
90+
}
91+
92+
return OS_SUCCESS;
93+
} /* end OS_ShellOutputToFile_Impl */
94+

src/os/rtems/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ add_library(osal_rtems_impl OBJECT
1414
osnetwork.c
1515
osselect.c
1616
ostimer.c
17+
../portable/os-impl-no-shell.c
1718
)

src/os/rtems/osfileapi.c

-44
Original file line numberDiff line numberDiff line change
@@ -137,47 +137,3 @@ int32 OS_Rtems_DirAPI_Impl_Init(void)
137137

138138

139139
/* FIXME - need to do something better here */
140-
141-
142-
/*----------------------------------------------------------------
143-
*
144-
* Function: OS_ShellOutputToFile_Impl
145-
*
146-
* Purpose: Implemented per internal OSAL API
147-
* See prototype in os-impl.h for argument/return detail
148-
*
149-
*-----------------------------------------------------------------*/
150-
int32 OS_ShellOutputToFile_Impl(uint32 file_id, const char *Cmd)
151-
{
152-
/*
153-
** this is a #define to avoid a 'variable length array' warning
154-
** 15 is for the size of the redirection string that is added
155-
** to the command
156-
*/
157-
char LocalCmd [OS_MAX_CMD_LEN + OS_REDIRECTSTRSIZE];
158-
int32 Result;
159-
160-
strncpy(LocalCmd,Cmd,OS_MAX_CMD_LEN +OS_REDIRECTSTRSIZE);
161-
162-
/* Make sure that we are able to access this file */
163-
fchmod(OS_impl_filehandle_table[file_id].fd, 0666);
164-
165-
/*
166-
** add in the extra chars necessary to perform the redirection
167-
** 1 for stdout and 2 for stderr. they are redirected to the
168-
** file descriptor passed in
169-
*/
170-
snprintf(LocalCmd, sizeof(LocalCmd), "%s 1>&%d 2>&%d",
171-
Cmd,
172-
OS_impl_filehandle_table[file_id].fd,
173-
OS_impl_filehandle_table[file_id].fd);
174-
175-
Result = system(LocalCmd);
176-
177-
if (Result != 0)
178-
{
179-
return OS_FS_ERROR;
180-
}
181-
return OS_SUCCESS;
182-
} /* end OS_ShellOutputToFile_Impl */
183-

src/os/rtems/osshell.c

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright (c) 2018, United States government as represented by the
3+
* administrator of the National Aeronautics Space Administration.
4+
* All rights reserved. This software was created at NASA Glenn
5+
* Research Center pursuant to government contracts.
6+
*
7+
* This is governed by the NASA Open Source Agreement and may be used,
8+
* distributed and modified only according to the terms of that agreement.
9+
*/
10+
11+
/**
12+
* \file osshell.c
13+
*
14+
* Purpose: Implements shell-related calls that can be optionally built
15+
* for distributions that choose to support them. Alternatively
16+
* build the portable no-shell implementation to exclude this
17+
* functionality.
18+
*/
19+
20+
/****************************************************************************************
21+
INCLUDE FILES
22+
***************************************************************************************/
23+
24+
#include "os-rtems.h"
25+
26+
#include <sys/stat.h>
27+
#include <fcntl.h>
28+
29+
/****************************************************************************************
30+
IMPLEMENTATION-SPECIFIC ROUTINES
31+
These are specific to this particular operating system
32+
****************************************************************************************/
33+
34+
/*----------------------------------------------------------------
35+
*
36+
* Function: OS_ShellOutputToFile_Impl
37+
*
38+
* Purpose: Implemented per internal OSAL API
39+
* See prototype in os-impl.h for argument/return detail
40+
*
41+
*-----------------------------------------------------------------*/
42+
int32 OS_ShellOutputToFile_Impl(uint32 file_id, const char* Cmd)
43+
{
44+
/*
45+
** this is a #define to avoid a 'variable length array' warning
46+
** 15 is for the size of the redirection string that is added
47+
** to the command
48+
*/
49+
char LocalCmd [OS_MAX_CMD_LEN + OS_REDIRECTSTRSIZE];
50+
int32 Result;
51+
52+
strncpy(LocalCmd,Cmd,OS_MAX_CMD_LEN +OS_REDIRECTSTRSIZE);
53+
54+
/* Make sure that we are able to access this file */
55+
fchmod(OS_impl_filehandle_table[file_id].fd, 0666);
56+
57+
/*
58+
** add in the extra chars necessary to perform the redirection
59+
** 1 for stdout and 2 for stderr. they are redirected to the
60+
** file descriptor passed in
61+
*/
62+
snprintf(LocalCmd, sizeof(LocalCmd), "%s 1>&%d 2>&%d",
63+
Cmd,
64+
OS_impl_filehandle_table[file_id].fd,
65+
OS_impl_filehandle_table[file_id].fd);
66+
67+
Result = system(LocalCmd);
68+
69+
if (Result != 0)
70+
{
71+
return OS_FS_ERROR;
72+
}
73+
return OS_SUCCESS;
74+
} /* end OS_ShellOutputToFile_Impl */
75+

src/os/vxworks/CMakeLists.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ add_library(osal_vxworks_impl OBJECT
1414
osnetwork.c
1515
osselect.c
1616
ostimer.c
17-
)
17+
../portable/os-impl-no-shell.c
18+
)

0 commit comments

Comments
 (0)