Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Close all the fds of the child before exec
This patch closes all the fds (starting from 3) of the child before exec in InstanceTask. Please see the bug description for more details. Change-Id: I9f18f88c9c58540ad6358a2a989f344f60be86d8 Closes-bug: #1394300
- Loading branch information
1 parent
fa8101b
commit 519e6c5
Showing
6 changed files
with
110 additions
and
0 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
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
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 @@ | ||
#include "oper/instance_task.h" | ||
#include "testing/gunit.h" | ||
|
||
#include <boost/filesystem.hpp> | ||
#include <boost/bind.hpp> | ||
#include <sstream> | ||
#include <fstream> | ||
|
||
#include <cstdlib> | ||
#include <boost/uuid/random_generator.hpp> | ||
|
||
#include "io/event_manager.h" | ||
#include "base/logging.h" | ||
#include "base/os.h" | ||
#include "base/test/task_test_util.h" | ||
|
||
using namespace std; | ||
using namespace boost::filesystem; | ||
|
||
class InstanceTaskFdTest : public ::testing::Test { | ||
public: | ||
virtual void SetUp() { | ||
memset(tmpfilename, 0, sizeof(tmpfilename)); | ||
strcpy(tmpfilename, "task_fd_XXXXXX"); | ||
mkstemp(tmpfilename); | ||
std::fstream testfile(tmpfilename); | ||
testfile << "while true; do sleep 1; done"; | ||
testfile.close(); | ||
std::stringstream cmd_str; | ||
cmd_str << "/bin/sh" << " " <<tmpfilename; | ||
task_ = new InstanceTask(cmd_str.str(), 1, &evm); | ||
} | ||
|
||
virtual void TearDown() { | ||
delete task_; | ||
remove(tmpfilename); | ||
} | ||
|
||
int StartTask() { | ||
/* create few fds */ | ||
if(pipe(pipe_fds) == -1) | ||
return -1; | ||
sock_fd = socket(AF_INET, SOCK_DGRAM, 0); | ||
if (sock_fd < 0) | ||
return -1; | ||
/* run the task now */ | ||
task_pid = task_->Run(); | ||
return task_pid; | ||
} | ||
|
||
int StopTask() { | ||
task_->Stop(); | ||
close(pipe_fds[0]); | ||
close(pipe_fds[1]); | ||
close(sock_fd); | ||
} | ||
|
||
int GetTaskFds() { | ||
std::stringstream proc_path; | ||
int no_of_fds; | ||
proc_path << "/proc/" <<task_pid<<"/fd/"; | ||
path the_path(proc_path.str()); | ||
no_of_fds = std::count_if(directory_iterator(the_path), | ||
directory_iterator(), | ||
bind( static_cast<bool(*)(const path&)>(is_other), | ||
bind( &directory_entry::path, _1))); | ||
return no_of_fds; | ||
} | ||
|
||
protected: | ||
int pipe_fds[2]; | ||
int sock_fd; | ||
InstanceTask *task_; | ||
int task_pid; | ||
EventManager evm; | ||
char tmpfilename[L_tmpnam]; | ||
}; | ||
|
||
TEST_F(InstanceTaskFdTest, TestCloseTaskFds) { | ||
task_pid = StartTask(); | ||
EXPECT_TRUE(task_pid > 0); | ||
int task_open_fds = GetTaskFds(); | ||
StopTask(); | ||
/* The task should have only 1 opened fd (i.e fd 2) */ | ||
EXPECT_EQ(1U, task_open_fds); | ||
}; | ||
|
||
int main(int argc, char **argv) { | ||
LoggingInit(); | ||
::testing::InitGoogleTest(&argc, argv); | ||
|
||
int result = RUN_ALL_TESTS(); | ||
return result; | ||
} |
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