10ffa2763Smrg //===-- sanitizer_file.h ---------------------------------------*- C++ -*-===// 20ffa2763Smrg // 3*490215a3Smrg // This file is distributed under the University of Illinois Open Source 4*490215a3Smrg // License. See LICENSE.TXT for details. 50ffa2763Smrg // 60ffa2763Smrg //===---------------------------------------------------------------------===// 70ffa2763Smrg // 80ffa2763Smrg // This file is shared between run-time libraries of sanitizers. 90ffa2763Smrg // It declares filesystem-related interfaces. This is separate from 100ffa2763Smrg // sanitizer_common.h so that it's simpler to disable all the filesystem 110ffa2763Smrg // support code for a port that doesn't use it. 120ffa2763Smrg // 130ffa2763Smrg //===---------------------------------------------------------------------===// 140ffa2763Smrg #ifndef SANITIZER_FILE_H 150ffa2763Smrg #define SANITIZER_FILE_H 160ffa2763Smrg 170ffa2763Smrg #include "sanitizer_interface_internal.h" 180ffa2763Smrg #include "sanitizer_internal_defs.h" 190ffa2763Smrg #include "sanitizer_libc.h" 200ffa2763Smrg #include "sanitizer_mutex.h" 210ffa2763Smrg 220ffa2763Smrg namespace __sanitizer { 230ffa2763Smrg 240ffa2763Smrg struct ReportFile { 250ffa2763Smrg void Write(const char *buffer, uptr length); 260ffa2763Smrg bool SupportsColors(); 270ffa2763Smrg void SetReportPath(const char *path); 280ffa2763Smrg 290ffa2763Smrg // Don't use fields directly. They are only declared public to allow 300ffa2763Smrg // aggregate initialization. 310ffa2763Smrg 320ffa2763Smrg // Protects fields below. 330ffa2763Smrg StaticSpinMutex *mu; 340ffa2763Smrg // Opened file descriptor. Defaults to stderr. It may be equal to 350ffa2763Smrg // kInvalidFd, in which case new file will be opened when necessary. 360ffa2763Smrg fd_t fd; 370ffa2763Smrg // Path prefix of report file, set via __sanitizer_set_report_path. 380ffa2763Smrg char path_prefix[kMaxPathLength]; 390ffa2763Smrg // Full path to report, obtained as <path_prefix>.PID 400ffa2763Smrg char full_path[kMaxPathLength]; 410ffa2763Smrg // PID of the process that opened fd. If a fork() occurs, 420ffa2763Smrg // the PID of child will be different from fd_pid. 430ffa2763Smrg uptr fd_pid; 440ffa2763Smrg 450ffa2763Smrg private: 460ffa2763Smrg void ReopenIfNecessary(); 470ffa2763Smrg }; 480ffa2763Smrg extern ReportFile report_file; 490ffa2763Smrg 500ffa2763Smrg enum FileAccessMode { 510ffa2763Smrg RdOnly, 520ffa2763Smrg WrOnly, 530ffa2763Smrg RdWr 540ffa2763Smrg }; 550ffa2763Smrg 560ffa2763Smrg // Returns kInvalidFd on error. 570ffa2763Smrg fd_t OpenFile(const char *filename, FileAccessMode mode, 580ffa2763Smrg error_t *errno_p = nullptr); 590ffa2763Smrg void CloseFile(fd_t); 600ffa2763Smrg 610ffa2763Smrg // Return true on success, false on error. 620ffa2763Smrg bool ReadFromFile(fd_t fd, void *buff, uptr buff_size, 630ffa2763Smrg uptr *bytes_read = nullptr, error_t *error_p = nullptr); 640ffa2763Smrg bool WriteToFile(fd_t fd, const void *buff, uptr buff_size, 650ffa2763Smrg uptr *bytes_written = nullptr, error_t *error_p = nullptr); 660ffa2763Smrg 67*490215a3Smrg bool RenameFile(const char *oldpath, const char *newpath, 68*490215a3Smrg error_t *error_p = nullptr); 69*490215a3Smrg 700ffa2763Smrg // Scoped file handle closer. 710ffa2763Smrg struct FileCloser { FileCloserFileCloser720ffa2763Smrg explicit FileCloser(fd_t fd) : fd(fd) {} ~FileCloserFileCloser730ffa2763Smrg ~FileCloser() { CloseFile(fd); } 740ffa2763Smrg fd_t fd; 750ffa2763Smrg }; 760ffa2763Smrg 770ffa2763Smrg bool SupportsColoredOutput(fd_t fd); 780ffa2763Smrg 790ffa2763Smrg // OS 800ffa2763Smrg const char *GetPwd(); 810ffa2763Smrg bool FileExists(const char *filename); 820ffa2763Smrg char *FindPathToBinary(const char *name); 830ffa2763Smrg bool IsPathSeparator(const char c); 840ffa2763Smrg bool IsAbsolutePath(const char *path); 850ffa2763Smrg // Starts a subprocess and returs its pid. 860ffa2763Smrg // If *_fd parameters are not kInvalidFd their corresponding input/output 870ffa2763Smrg // streams will be redirect to the file. The files will always be closed 880ffa2763Smrg // in parent process even in case of an error. 890ffa2763Smrg // The child process will close all fds after STDERR_FILENO 900ffa2763Smrg // before passing control to a program. 910ffa2763Smrg pid_t StartSubprocess(const char *filename, const char *const argv[], 920ffa2763Smrg fd_t stdin_fd = kInvalidFd, fd_t stdout_fd = kInvalidFd, 930ffa2763Smrg fd_t stderr_fd = kInvalidFd); 940ffa2763Smrg // Checks if specified process is still running 950ffa2763Smrg bool IsProcessRunning(pid_t pid); 960ffa2763Smrg // Waits for the process to finish and returns its exit code. 970ffa2763Smrg // Returns -1 in case of an error. 980ffa2763Smrg int WaitForProcess(pid_t pid); 990ffa2763Smrg 1000ffa2763Smrg // Maps given file to virtual memory, and returns pointer to it 1010ffa2763Smrg // (or NULL if mapping fails). Stores the size of mmaped region 1020ffa2763Smrg // in '*buff_size'. 1030ffa2763Smrg void *MapFileToMemory(const char *file_name, uptr *buff_size); 1040ffa2763Smrg void *MapWritableFileToMemory(void *addr, uptr size, fd_t fd, OFF_T offset); 1050ffa2763Smrg 1060ffa2763Smrg } // namespace __sanitizer 1070ffa2763Smrg 1080ffa2763Smrg #endif // SANITIZER_FILE_H 109