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