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