1 //===-- sanitizer_file.h ---------------------------------------*- C++ -*-===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===---------------------------------------------------------------------===//
7 //
8 // This file is shared between run-time libraries of sanitizers.
9 // It declares filesystem-related interfaces.  This is separate from
10 // sanitizer_common.h so that it's simpler to disable all the filesystem
11 // support code for a port that doesn't use it.
12 //
13 //===---------------------------------------------------------------------===//
14 #ifndef SANITIZER_FILE_H
15 #define SANITIZER_FILE_H
16 
17 #include "sanitizer_interface_internal.h"
18 #include "sanitizer_internal_defs.h"
19 #include "sanitizer_libc.h"
20 #include "sanitizer_mutex.h"
21 
22 namespace __sanitizer {
23 
24 struct ReportFile {
25   void Write(const char *buffer, uptr length);
26   bool SupportsColors();
27   void SetReportPath(const char *path);
28 
29   // Don't use fields directly. They are only declared public to allow
30   // aggregate initialization.
31 
32   // Protects fields below.
33   StaticSpinMutex *mu;
34   // Opened file descriptor. Defaults to stderr. It may be equal to
35   // kInvalidFd, in which case new file will be opened when necessary.
36   fd_t fd;
37   // Path prefix of report file, set via __sanitizer_set_report_path.
38   char path_prefix[kMaxPathLength];
39   // Full path to report, obtained as <path_prefix>.PID
40   char full_path[kMaxPathLength];
41   // PID of the process that opened fd. If a fork() occurs,
42   // the PID of child will be different from fd_pid.
43   uptr fd_pid;
44 
45  private:
46   void ReopenIfNecessary();
47 };
48 extern ReportFile report_file;
49 
50 enum FileAccessMode {
51   RdOnly,
52   WrOnly,
53   RdWr
54 };
55 
56 // Returns kInvalidFd on error.
57 fd_t OpenFile(const char *filename, FileAccessMode mode,
58               error_t *errno_p = nullptr);
59 void CloseFile(fd_t);
60 
61 // Return true on success, false on error.
62 bool ReadFromFile(fd_t fd, void *buff, uptr buff_size,
63                   uptr *bytes_read = nullptr, error_t *error_p = nullptr);
64 bool WriteToFile(fd_t fd, const void *buff, uptr buff_size,
65                  uptr *bytes_written = nullptr, error_t *error_p = nullptr);
66 
67 bool RenameFile(const char *oldpath, const char *newpath,
68                 error_t *error_p = nullptr);
69 
70 // Scoped file handle closer.
71 struct FileCloser {
FileCloserFileCloser72   explicit FileCloser(fd_t fd) : fd(fd) {}
~FileCloserFileCloser73   ~FileCloser() { CloseFile(fd); }
74   fd_t fd;
75 };
76 
77 bool SupportsColoredOutput(fd_t fd);
78 
79 // OS
80 const char *GetPwd();
81 bool FileExists(const char *filename);
82 char *FindPathToBinary(const char *name);
83 bool IsPathSeparator(const char c);
84 bool IsAbsolutePath(const char *path);
85 // Starts a subprocess and returs its pid.
86 // If *_fd parameters are not kInvalidFd their corresponding input/output
87 // streams will be redirect to the file. The files will always be closed
88 // in parent process even in case of an error.
89 // The child process will close all fds after STDERR_FILENO
90 // before passing control to a program.
91 pid_t StartSubprocess(const char *filename, const char *const argv[],
92                       fd_t stdin_fd = kInvalidFd, fd_t stdout_fd = kInvalidFd,
93                       fd_t stderr_fd = kInvalidFd);
94 // Checks if specified process is still running
95 bool IsProcessRunning(pid_t pid);
96 // Waits for the process to finish and returns its exit code.
97 // Returns -1 in case of an error.
98 int WaitForProcess(pid_t pid);
99 
100 // Maps given file to virtual memory, and returns pointer to it
101 // (or NULL if mapping fails). Stores the size of mmaped region
102 // in '*buff_size'.
103 void *MapFileToMemory(const char *file_name, uptr *buff_size);
104 void *MapWritableFileToMemory(void *addr, uptr size, fd_t fd, OFF_T offset);
105 
106 }  // namespace __sanitizer
107 
108 #endif  // SANITIZER_FILE_H
109