1//===-- sanitizer_file.h ---------------------------------------*- C++ -*-===//
2//
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
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_common.h"
19#include "sanitizer_internal_defs.h"
20#include "sanitizer_libc.h"
21#include "sanitizer_mutex.h"
22
23namespace __sanitizer {
24
25struct ReportFile {
26 void Write(const char *buffer, uptr length);
27 bool SupportsColors();
28 void SetReportPath(const char *path);
29 const char *GetReportPath();
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 // Set to true if the last attempt to open the logfile failed, perhaps due to
47 // permission errors
48 bool fallbackToStderrActive = false;
49
50 private:
51 void ReopenIfNecessary();
52};
53extern ReportFile report_file;
54
55enum FileAccessMode {
56 RdOnly,
57 WrOnly,
58 RdWr
59};
60
61// Returns kInvalidFd on error.
62fd_t OpenFile(const char *filename, FileAccessMode mode,
63 error_t *errno_p = nullptr);
64void CloseFile(fd_t);
65
66// Return true on success, false on error.
67bool ReadFromFile(fd_t fd, void *buff, uptr buff_size,
68 uptr *bytes_read = nullptr, error_t *error_p = nullptr);
69bool WriteToFile(fd_t fd, const void *buff, uptr buff_size,
70 uptr *bytes_written = nullptr, error_t *error_p = nullptr);
71
72// Scoped file handle closer.
73struct FileCloser {
74 explicit FileCloser(fd_t fd) : fd(fd) {}
75 ~FileCloser() { CloseFile(fd); }
76 fd_t fd;
77};
78
79bool SupportsColoredOutput(fd_t fd);
80
81// OS
82const char *GetPwd();
83bool FileExists(const char *filename);
84bool DirExists(const char *path);
85char *FindPathToBinary(const char *name);
86bool IsPathSeparator(const char c);
87bool IsAbsolutePath(const char *path);
88// Returns true on success, false on failure.
89bool CreateDir(const char *pathname);
90// Starts a subprocess and returns its pid.
91// If *_fd parameters are not kInvalidFd their corresponding input/output
92// streams will be redirect to the file. The files will always be closed
93// in parent process even in case of an error.
94// The child process will close all fds after STDERR_FILENO
95// before passing control to a program.
96pid_t StartSubprocess(const char *filename, const char *const argv[],
97 const char *const envp[], fd_t stdin_fd = kInvalidFd,
98 fd_t stdout_fd = kInvalidFd, fd_t stderr_fd = kInvalidFd);
99// Checks if specified process is still running
100bool IsProcessRunning(pid_t pid);
101// Waits for the process to finish and returns its exit code.
102// Returns -1 in case of an error.
103int WaitForProcess(pid_t pid);
104
105// Maps given file to virtual memory, and returns pointer to it
106// (or NULL if mapping fails). Stores the size of mmaped region
107// in '*buff_size'.
108void *MapFileToMemory(const char *file_name, uptr *buff_size);
109void *MapWritableFileToMemory(void *addr, uptr size, fd_t fd, OFF_T offset);
110
111} // namespace __sanitizer
112
113#endif // SANITIZER_FILE_H
114