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