xref: /freebsd/contrib/llvm-project/compiler-rt/lib/fuzzer/FuzzerUtilLinux.cpp (revision 6be3386466ab79a84b48429ae66244f21526d3df)
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_OPENBSD || LIBFUZZER_EMSCRIPTEN
13 #include "FuzzerCommand.h"
14 
15 #include <stdlib.h>
16 #include <sys/types.h>
17 #include <sys/wait.h>
18 #include <unistd.h>
19 
20 
21 namespace fuzzer {
22 
23 int ExecuteCommand(const Command &Cmd) {
24   std::string CmdLine = Cmd.toString();
25   int exit_code = system(CmdLine.c_str());
26   if (WIFEXITED(exit_code))
27     return WEXITSTATUS(exit_code);
28   return exit_code;
29 }
30 
31 void DiscardOutput(int Fd) {
32   FILE* Temp = fopen("/dev/null", "w");
33   if (!Temp)
34     return;
35   dup2(fileno(Temp), Fd);
36   fclose(Temp);
37 }
38 
39 } // namespace fuzzer
40 
41 #endif
42