46 #include "params/Process.hh"
50 std::string
const& errout)
51 : _input(input), _output(output), _errout(errout), _fdArray(),
53 {
"cin", STDIN_FILENO},
54 {
"stdin", STDIN_FILENO}},
56 {
"cout", STDOUT_FILENO},
57 {
"stdout", STDOUT_FILENO},
58 {
"cerr", STDERR_FILENO},
59 {
"stderr", STDERR_FILENO}}
62 std::map<std::string, int>::iterator it;
68 if ((it = imap.find(input)) != imap.end())
71 sim_fd = openInputFile(input);
73 auto ffd = std::make_shared<FileFDEntry>(sim_fd, O_RDONLY, input,
false);
74 _fdArray[STDIN_FILENO] = ffd;
80 if ((it = oemap.find(
output)) != oemap.end())
83 sim_fd = openOutputFile(
output);
85 ffd = std::make_shared<FileFDEntry>(sim_fd, O_WRONLY | O_CREAT | O_TRUNC,
87 _fdArray[STDOUT_FILENO] = ffd;
91 else if ((it = oemap.find(errout)) != oemap.end())
94 sim_fd = openOutputFile(errout);
96 ffd = std::make_shared<FileFDEntry>(sim_fd, O_WRONLY | O_CREAT | O_TRUNC,
98 _fdArray[STDERR_FILENO] = ffd;
104 for (
auto& fdp : _fdArray) {
110 auto ffd = std::dynamic_pointer_cast<
FileFDEntry>(fdp);
120 ffd->setFileOffset(lseek(sim_fd, 0, SEEK_CUR));
134 void (*seek)(std::shared_ptr<FileFDEntry>)
135 = [] (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)) {
230 if (pfd->getEndType() == PipeFDEntry::EndType::write)
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);
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);
287 for (
int i = 0;
i < _fdArray.size();
i++) {
288 std::shared_ptr<FDEntry> fdp = _fdArray[
i];
294 fatal(
"Out of target file descriptors");
300 int sim_fd = open(filename.c_str(), flags,
mode);
303 fatal(
"Unable to open %s with mode %O", filename, mode);
309 return openFile(filename, O_RDONLY, 00);
315 return openFile(filename, O_WRONLY | O_CREAT | O_TRUNC, 0664);
318 std::shared_ptr<FDEntry>
321 assert(0 <= tgt_fd && tgt_fd < _fdArray.size());
322 return _fdArray[tgt_fd];
328 assert(0 <= tgt_fd && tgt_fd < _fdArray.size());
329 _fdArray[tgt_fd] = fdep;
335 if (tgt_fd >= _fdArray.size() || tgt_fd < 0)
339 auto hbfdp = std::dynamic_pointer_cast<
HBFDEntry>(_fdArray[tgt_fd]);
345 status = close(sim_fd);
348 _fdArray[tgt_fd] =
nullptr;
int openOutputFile(std::string const &file_name) const
static void output(const char *filename)
void setFDEntry(int tgt_fd, std::shared_ptr< FDEntry > fdep)
Put the pointer specified by fdep into the _fdArray entry indexed by tgt_fd.
Holds file descriptors for host-backed files; host-backed files are files which were opened on the ph...
void setSimFD(int sim_fd)
int openInputFile(std::string const &file_name) const
int closeFDEntry(int tgt_fd)
Try to close the host file descriptor.
void restoreFileOffsets()
Restore all offsets for currently open files during the unserialize phase for the owning process clas...
int allocFD(std::shared_ptr< FDEntry > fdp)
Step through the file descriptor array and find the first available entry which is denoted as being f...
int openFile(std::string const &file_name, int flags, mode_t mode) const
Help clarify our intention when opening files in the init and restoration code.
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...
Extends the base class to include a host-backed file descriptor field that records the integer used t...
std::map< std::string, int > oemap
Holds the metadata needed to maintain the mappings for file descriptors allocated with the pipe() sys...
void updateFileOffsets()
Figure out the file offsets for all currently open files and save them the offsets during the calls t...
std::map< std::string, int > imap
Hold strings which represent the default values which are checked against to initialize the standard ...
std::shared_ptr< FDEntry > getFDEntry(int tgt_fd)
Return the file descriptor entry object associated with the index provided.