111856Sbrandon.potter@amd.com/*
211856Sbrandon.potter@amd.com * Copyright (c) 2016 Advanced Micro Devices, Inc.
311856Sbrandon.potter@amd.com * All rights reserved.
411856Sbrandon.potter@amd.com *
511856Sbrandon.potter@amd.com * For use for simulation and test purposes only
611856Sbrandon.potter@amd.com *
711856Sbrandon.potter@amd.com * Redistribution and use in source and binary forms, with or without
811856Sbrandon.potter@amd.com * modification, are permitted provided that the following conditions are met:
911856Sbrandon.potter@amd.com *
1011856Sbrandon.potter@amd.com * 1. Redistributions of source code must retain the above copyright notice,
1111856Sbrandon.potter@amd.com * this list of conditions and the following disclaimer.
1211856Sbrandon.potter@amd.com *
1311856Sbrandon.potter@amd.com * 2. Redistributions in binary form must reproduce the above copyright notice,
1411856Sbrandon.potter@amd.com * this list of conditions and the following disclaimer in the documentation
1511856Sbrandon.potter@amd.com * and/or other materials provided with the distribution.
1611856Sbrandon.potter@amd.com *
1711856Sbrandon.potter@amd.com * 3. Neither the name of the copyright holder nor the names of its
1811856Sbrandon.potter@amd.com * contributors may be used to endorse or promote products derived from this
1911856Sbrandon.potter@amd.com * software without specific prior written permission.
2011856Sbrandon.potter@amd.com *
2111856Sbrandon.potter@amd.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
2211856Sbrandon.potter@amd.com * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2311856Sbrandon.potter@amd.com * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2411856Sbrandon.potter@amd.com * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
2511856Sbrandon.potter@amd.com * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2611856Sbrandon.potter@amd.com * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2711856Sbrandon.potter@amd.com * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2811856Sbrandon.potter@amd.com * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2911856Sbrandon.potter@amd.com * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3011856Sbrandon.potter@amd.com * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3111856Sbrandon.potter@amd.com * POSSIBILITY OF SUCH DAMAGE.
3211856Sbrandon.potter@amd.com *
3312697Santhony.gutierrez@amd.com * Authors: Brandon Potter
3411856Sbrandon.potter@amd.com */
3511856Sbrandon.potter@amd.com
3611856Sbrandon.potter@amd.com#ifndef __FD_ARRAY_HH__
3711856Sbrandon.potter@amd.com#define __FD_ARRAY_HH__
3811856Sbrandon.potter@amd.com
3911856Sbrandon.potter@amd.com#include <array>
4011856Sbrandon.potter@amd.com#include <memory>
4111856Sbrandon.potter@amd.com#include <string>
4211856Sbrandon.potter@amd.com
4311856Sbrandon.potter@amd.com#include "sim/fd_entry.hh"
4411856Sbrandon.potter@amd.com
4511856Sbrandon.potter@amd.comclass FDArray
4611856Sbrandon.potter@amd.com{
4711856Sbrandon.potter@amd.com  public:
4811856Sbrandon.potter@amd.com    /**
4911856Sbrandon.potter@amd.com     * Initialize the file descriptor array and set the standard file
5011856Sbrandon.potter@amd.com     * descriptors to defaults or values passed in with the process
5111856Sbrandon.potter@amd.com     * params.
5211856Sbrandon.potter@amd.com     * @param input Used to initialize the stdin file descriptor
5311856Sbrandon.potter@amd.com     * @param output Used to initialize the stdout file descriptor
5411856Sbrandon.potter@amd.com     * @param errout Used to initialize the stderr file descriptor
5511856Sbrandon.potter@amd.com     */
5611856Sbrandon.potter@amd.com    FDArray(std::string const& input, std::string const& output,
5711856Sbrandon.potter@amd.com            std::string const& errout);
5811856Sbrandon.potter@amd.com
5911856Sbrandon.potter@amd.com    /**
6011856Sbrandon.potter@amd.com     * Figure out the file offsets for all currently open files and save them
6111856Sbrandon.potter@amd.com     * the offsets during the calls to drain by the owning process.
6211856Sbrandon.potter@amd.com     */
6311856Sbrandon.potter@amd.com    void updateFileOffsets();
6411856Sbrandon.potter@amd.com
6511856Sbrandon.potter@amd.com    /**
6611856Sbrandon.potter@amd.com     * Restore all offsets for currently open files during the unserialize
6711856Sbrandon.potter@amd.com     * phase for the owning process class.
6811856Sbrandon.potter@amd.com     */
6911856Sbrandon.potter@amd.com    void restoreFileOffsets();
7011856Sbrandon.potter@amd.com
7111856Sbrandon.potter@amd.com    /**
7213029Sbrandon.potter@amd.com     * Put the pointer specified by fdep into the _fdArray entry indexed
7313029Sbrandon.potter@amd.com     * by tgt_fd.
7413029Sbrandon.potter@amd.com     * @param tgt_fd Use target file descriptors to index the array.
7513029Sbrandon.potter@amd.com     * @param fdep Incoming pointer used to set the entry pointed to by tgt_fd.
7613029Sbrandon.potter@amd.com     */
7713029Sbrandon.potter@amd.com    void setFDEntry(int tgt_fd, std::shared_ptr<FDEntry> fdep);
7813029Sbrandon.potter@amd.com
7913029Sbrandon.potter@amd.com    /**
8011856Sbrandon.potter@amd.com     * Treat this object like a normal array in using the subscript operator
8111856Sbrandon.potter@amd.com     * to pull entries out of it.
8211856Sbrandon.potter@amd.com     * @param tgt_fd Use target file descriptors to index the array.
8311856Sbrandon.potter@amd.com     */
8413029Sbrandon.potter@amd.com    std::shared_ptr<FDEntry>
8511856Sbrandon.potter@amd.com    operator[](int tgt_fd)
8611856Sbrandon.potter@amd.com    {
8711856Sbrandon.potter@amd.com        return getFDEntry(tgt_fd);
8811856Sbrandon.potter@amd.com    }
8911856Sbrandon.potter@amd.com
9011856Sbrandon.potter@amd.com    /**
9113029Sbrandon.potter@amd.com     * Return the size of the _fdArray field
9213029Sbrandon.potter@amd.com     */
9313029Sbrandon.potter@amd.com    int getSize() const { return _fdArray.size(); }
9413029Sbrandon.potter@amd.com
9513029Sbrandon.potter@amd.com    /**
9611856Sbrandon.potter@amd.com     * Step through the file descriptor array and find the first available
9711856Sbrandon.potter@amd.com     * entry which is denoted as being free by being a 'nullptr'. That file
9811856Sbrandon.potter@amd.com     * descriptor entry is the new target file descriptor entry that we
9911856Sbrandon.potter@amd.com     * return as the return parameter.
10011856Sbrandon.potter@amd.com     * @param fdp Allocated beforehand and passed into this method;
10111856Sbrandon.potter@amd.com     * the fdp is meant to be a generic pointer capable of pointing to
10211856Sbrandon.potter@amd.com     * different types of file descriptors. Must cast the pointer to the
10311856Sbrandon.potter@amd.com     * correct type before dereferencing to access the needed fields.
10411856Sbrandon.potter@amd.com     */
10511856Sbrandon.potter@amd.com    int allocFD(std::shared_ptr<FDEntry> fdp);
10611856Sbrandon.potter@amd.com
10711856Sbrandon.potter@amd.com    /**
10811856Sbrandon.potter@amd.com     * Try to close the host file descriptor. If successful, set the
10911856Sbrandon.potter@amd.com     * specified file descriptor entry object pointer to nullptr.
11011856Sbrandon.potter@amd.com     * Used to "close" the target file descriptor.
11111856Sbrandon.potter@amd.com     * @param tgt_fd Use target file descriptors to index the array.
11211856Sbrandon.potter@amd.com     */
11311856Sbrandon.potter@amd.com    int closeFDEntry(int tgt_fd);
11411856Sbrandon.potter@amd.com
11511856Sbrandon.potter@amd.com  private:
11611856Sbrandon.potter@amd.com    /**
11711856Sbrandon.potter@amd.com     * Help clarify our intention when opening files in the init and
11811856Sbrandon.potter@amd.com     * restoration code. These are helper functions which are not meant to
11911856Sbrandon.potter@amd.com     * be exposed to other objects or files.
12011856Sbrandon.potter@amd.com     */
12111856Sbrandon.potter@amd.com    int openFile(std::string const& file_name, int flags, mode_t mode) const;
12211856Sbrandon.potter@amd.com    int openInputFile(std::string const& file_name) const;
12311856Sbrandon.potter@amd.com    int openOutputFile(std::string const& file_name) const;
12411856Sbrandon.potter@amd.com
12511856Sbrandon.potter@amd.com    /**
12611856Sbrandon.potter@amd.com     * Return the file descriptor entry object associated with the index
12711856Sbrandon.potter@amd.com     * provided. (The index is protected with bounds checking on the array
12811856Sbrandon.potter@amd.com     * size without the use of the array's at operator.)
12911856Sbrandon.potter@amd.com     * @param tgt_fd Use target file descriptors to index the array.
13011856Sbrandon.potter@amd.com     */
13111856Sbrandon.potter@amd.com    std::shared_ptr<FDEntry> getFDEntry(int tgt_fd);
13211856Sbrandon.potter@amd.com
13311856Sbrandon.potter@amd.com    /**
13411856Sbrandon.potter@amd.com     * Hold pointers to the file descriptor entries. The array size is
13511856Sbrandon.potter@amd.com     * statically defined by the operating system.
13611856Sbrandon.potter@amd.com     */
13713029Sbrandon.potter@amd.com    static constexpr size_t _numFDs {1024};
13813029Sbrandon.potter@amd.com    std::array<std::shared_ptr<FDEntry>, _numFDs> _fdArray;
13913029Sbrandon.potter@amd.com
14013029Sbrandon.potter@amd.com    /**
14113029Sbrandon.potter@amd.com     * Hold param strings passed from the Process class which indicate
14213029Sbrandon.potter@amd.com     * the filename for each of the corresponding files or some keyword
14313029Sbrandon.potter@amd.com     * indicating the use of standard file descriptors.
14413029Sbrandon.potter@amd.com     */
14513029Sbrandon.potter@amd.com    std::string _input;
14613029Sbrandon.potter@amd.com    std::string _output;
14713029Sbrandon.potter@amd.com    std::string _errout;
14811856Sbrandon.potter@amd.com
14911856Sbrandon.potter@amd.com    /**
15011856Sbrandon.potter@amd.com     * Hold strings which represent the default values which are checked
15111856Sbrandon.potter@amd.com     * against to initialize the standard file descriptors. If the string
15211856Sbrandon.potter@amd.com     * provided doesn't hit against these maps, then a file is opened on the
15311856Sbrandon.potter@amd.com     * host instead of using the host's standard file descriptors.
15411856Sbrandon.potter@amd.com     */
15513029Sbrandon.potter@amd.com    std::map<std::string, int> _imap;
15613029Sbrandon.potter@amd.com    std::map<std::string, int> _oemap;
15711856Sbrandon.potter@amd.com};
15811856Sbrandon.potter@amd.com
15911856Sbrandon.potter@amd.com#endif // __FD_ARRAY_HH__
160