47 #include "params/Process.hh" 51 std::string
const& errout)
52 : _fdArray(), _input(input), _output(output), _errout(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;
74 auto ffd = std::make_shared<FileFDEntry>(sim_fd, O_RDONLY, input,
false);
86 ffd = std::make_shared<FileFDEntry>(sim_fd, O_WRONLY | O_CREAT | O_TRUNC,
88 _fdArray[STDOUT_FILENO] = ffd;
97 ffd = std::make_shared<FileFDEntry>(sim_fd, O_WRONLY | O_CREAT | O_TRUNC,
99 _fdArray[STDERR_FILENO] = ffd;
111 auto ffd = std::dynamic_pointer_cast<
FileFDEntry>(fdp);
121 ffd->setFileOffset(lseek(sim_fd, 0, SEEK_CUR));
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)) {
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);
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);
316 O_WRONLY | O_CREAT | O_TRUNC, 0664);
319 std::shared_ptr<FDEntry>
322 assert(0 <= tgt_fd && tgt_fd <
_fdArray.size());
329 assert(0 <= tgt_fd && tgt_fd <
_fdArray.size());
336 if (tgt_fd >=
_fdArray.size() || tgt_fd < 0)
346 status = close(sim_fd);
int openInputFile(std::string const &file_name) const
std::string resolve(const std::string &name) const
Returns relative file names prepended with name of this directory.
#define fatal(...)
This implements a cprintf based fatal() function.
std::shared_ptr< FDEntry > getFDEntry(int tgt_fd)
Return the file descriptor entry object associated with the index provided.
static void output(const char *filename)
int openOutputFile(std::string const &file_name) const
void setFDEntry(int tgt_fd, std::shared_ptr< FDEntry > fdep)
Put the pointer specified by fdep into the _fdArray entry indexed by tgt_fd.
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.
Holds file descriptors for host-backed files; host-backed files are files which were opened on the ph...
void setSimFD(int sim_fd)
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...
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...
std::map< std::string, int > _imap
Hold strings which represent the default values which are checked against to initialize the standard ...
std::string _input
Hold param strings passed from the Process class which indicate the filename for each of the correspo...
Extends the base class to include a host-backed file descriptor field that records the integer used t...
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 > _oemap
std::array< std::shared_ptr< FDEntry >, _numFDs > _fdArray