FuzzerUtilLinux.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. //===- FuzzerUtilLinux.cpp - Misc utils for Linux. ------------------------===//
  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. // Misc utils for Linux.
  9. //===----------------------------------------------------------------------===//
  10. #include "FuzzerPlatform.h"
  11. #if LIBFUZZER_LINUX || LIBFUZZER_NETBSD || LIBFUZZER_FREEBSD || \
  12. LIBFUZZER_EMSCRIPTEN
  13. #include "FuzzerCommand.h"
  14. #include <stdlib.h>
  15. #include <sys/types.h>
  16. #include <sys/wait.h>
  17. #include <unistd.h>
  18. namespace fuzzer {
  19. int ExecuteCommand(const Command &Cmd) {
  20. std::string CmdLine = Cmd.toString();
  21. int exit_code = system(CmdLine.c_str());
  22. if (WIFEXITED(exit_code))
  23. return WEXITSTATUS(exit_code);
  24. return exit_code;
  25. }
  26. void DiscardOutput(int Fd) {
  27. FILE* Temp = fopen("/dev/null", "w");
  28. if (!Temp)
  29. return;
  30. dup2(fileno(Temp), Fd);
  31. fclose(Temp);
  32. }
  33. } // namespace fuzzer
  34. #endif