51 std::string
const& errout)
54 {
"cin", STDIN_FILENO},
55 {
"stdin", STDIN_FILENO}},
57 {
"cout", STDOUT_FILENO},
58 {
"stdout", STDOUT_FILENO},
59 {
"cerr", STDERR_FILENO},
60 {
"stderr", STDERR_FILENO}}
63 std::map<std::string, int>::iterator it;
69 if ((it = _imap.find(input)) != _imap.end())
72 sim_fd = openInputFile(input);
74 auto ffd = std::make_shared<FileFDEntry>(sim_fd, O_RDONLY, input,
false);
75 _fdArray[STDIN_FILENO] = ffd;
81 if ((it = _oemap.find(output)) != _oemap.end())
84 sim_fd = openOutputFile(output);
86 ffd = std::make_shared<FileFDEntry>(sim_fd, O_WRONLY | O_CREAT | O_TRUNC,
88 _fdArray[STDOUT_FILENO] = ffd;
92 else if ((it = _oemap.find(errout)) != _oemap.end())
95 sim_fd = openOutputFile(errout);
97 ffd = std::make_shared<FileFDEntry>(sim_fd, O_WRONLY | O_CREAT | O_TRUNC,
99 _fdArray[STDERR_FILENO] = ffd;
135 auto seek = [] (std::shared_ptr<FileFDEntry> ffd)
137 if (lseek(ffd->getSimFD(), ffd->getFileOffset(), SEEK_SET) < 0)
138 fatal(
"Unable to seek to location in %s", ffd->getFileName());
141 std::map<std::string, int>::iterator it;
150 std::shared_ptr<FDEntry> stdin_fde =
_fdArray[STDIN_FILENO];
151 auto stdin_ffd = std::dynamic_pointer_cast<FileFDEntry>(stdin_fde);
153 if (
_input != stdin_ffd->getFileName()) {
154 warn(
"Using new input file (%s) rather than checkpointed (%s)\n",
155 _input, stdin_ffd->getFileName());
156 stdin_ffd->setFileName(
_input);
157 stdin_ffd->setFileOffset(0);
160 if ((it =
_imap.find(stdin_ffd->getFileName())) !=
_imap.end()) {
161 stdin_ffd->setSimFD(it->second);
163 stdin_ffd->setSimFD(
openInputFile(stdin_ffd->getFileName()));
174 std::shared_ptr<FDEntry> stdout_fde =
_fdArray[STDOUT_FILENO];
175 auto stdout_ffd = std::dynamic_pointer_cast<FileFDEntry>(stdout_fde);
177 if (
_output != stdout_ffd->getFileName()) {
178 warn(
"Using new output file (%s) rather than checkpointed (%s)\n",
179 _output, stdout_ffd->getFileName());
180 stdout_ffd->setFileName(
_output);
181 stdout_ffd->setFileOffset(0);
184 if ((it =
_oemap.find(stdout_ffd->getFileName())) !=
_oemap.end()) {
185 stdout_ffd->setSimFD(it->second);
198 std::shared_ptr<FDEntry> stderr_fde =
_fdArray[STDERR_FILENO];
199 auto stderr_ffd = std::dynamic_pointer_cast<FileFDEntry>(stderr_fde);
201 if (
_errout != stderr_ffd->getFileName()) {
202 warn(
"Using new error file (%s) rather than checkpointed (%s)\n",
203 _errout, stderr_ffd->getFileName());
204 stderr_ffd->setFileName(
_errout);
205 stderr_ffd->setFileOffset(0);
208 if (stdout_ffd->getFileName() == stderr_ffd->getFileName()) {
210 stderr_ffd->setSimFD(stdout_ffd->getSimFD());
211 }
else if ((it =
_oemap.find(stderr_ffd->getFileName())) !=
_oemap.end()) {
212 stderr_ffd->setSimFD(it->second);
218 for (
int tgt_fd = 3; tgt_fd <
_fdArray.size(); tgt_fd++) {
219 std::shared_ptr<FDEntry> fdp =
_fdArray[tgt_fd];
224 if (
auto pfd = std::dynamic_pointer_cast<PipeFDEntry>(fdp)) {
235 if (pipe(fd_pair) < 0)
236 fatal(
"Unable to create new pipe");
242 pfd->setSimFD(fd_pair[0]);
248 int prs = pfd->getPipeReadSource();
249 std::shared_ptr<FDEntry> write_fdp =
_fdArray[prs];
252 auto write_pfd = std::dynamic_pointer_cast<PipeFDEntry>(write_fdp);
255 write_pfd->setSimFD(fd_pair[1]);
259 if (
auto dfd = std::dynamic_pointer_cast<DeviceFDEntry>(fdp)) {
265 fatal(
"Unable to restore checkpoints with emulated drivers");
269 if (
auto ffd = std::dynamic_pointer_cast<FileFDEntry>(fdp)) {
277 int sim_fd =
openFile(ffd->getFileName(), ffd->getFlags(), 0664);
278 ffd->setSimFD(sim_fd);
376 "FDArray sizes do not match at unserialize!");
378 for (
int tgt_fd = 0; tgt_fd <
_fdArray.size(); tgt_fd++) {
379 if (tgt_fd == STDIN_FILENO || tgt_fd == STDOUT_FILENO ||
380 tgt_fd == STDERR_FILENO)
385 std::shared_ptr<FDEntry> fdep;
389 panic(
"Abstract fd entry was serialized");
392 fdep = std::make_shared<HBFDEntry>(0, 0);
395 fdep = std::make_shared<FileFDEntry>(0, 0,
"", 0, 00);
398 fdep = std::make_shared<DeviceFDEntry>(
nullptr,
"");
401 fdep = std::make_shared<PipeFDEntry>(
405 fdep = std::make_shared<SocketFDEntry>(0, 0, 0, 0);
410 panic(
"Unrecognized fd class");
414 fdep->unserialize(
cp);
416 auto this_ffd = std::dynamic_pointer_cast<FileFDEntry>(fdep);
421 mode_t
mode = this_ffd->getFileMode();
431 path = this_ffd->getFileName();
434 int flags = this_ffd->getFlags();
440 this_ffd->setSimFD(sim_fd);
443 uint64_t file_offset = this_ffd->getFileOffset();
444 lseek(sim_fd, file_offset, SEEK_SET);
FDArray(std::string const &input, std::string const &output, std::string const &errout)
Initialize the file descriptor array and set the standard file descriptors to defaults or values pass...