Alp Toker | 632c6cd | 2014-01-23 22:19:45 +0000 | [diff] [blame] | 1 | //=- RPCChannel.inc - LLVM out-of-process JIT execution for Unix --=// |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Alp Toker | 632c6cd | 2014-01-23 22:19:45 +0000 | [diff] [blame] | 10 | // Implementation of the Unix-specific parts of the RPCChannel class |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 11 | // which executes JITed code in a separate process from where it was built. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Alp Toker | ad6aa47 | 2014-01-24 17:18:52 +0000 | [diff] [blame^] | 15 | #include "llvm/Support/Errno.h" |
| 16 | #include "llvm/Support/raw_ostream.h" |
| 17 | |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 18 | #include <stdio.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <sys/wait.h> |
Chandler Carruth | 07baed5 | 2014-01-13 08:04:33 +0000 | [diff] [blame] | 21 | #include <unistd.h> |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 22 | |
| 23 | namespace { |
| 24 | |
| 25 | struct ConnectionData_t { |
| 26 | int InputPipe; |
| 27 | int OutputPipe; |
| 28 | |
| 29 | ConnectionData_t(int in, int out) : InputPipe(in), OutputPipe(out) {} |
| 30 | }; |
| 31 | |
| 32 | } // namespace |
| 33 | |
| 34 | namespace llvm { |
| 35 | |
Alp Toker | 632c6cd | 2014-01-23 22:19:45 +0000 | [diff] [blame] | 36 | bool RPCChannel::createServer() { |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 37 | int PipeFD[2][2]; |
| 38 | pid_t ChildPID; |
| 39 | |
Benjamin Kramer | 3cfafb8 | 2013-10-04 19:10:03 +0000 | [diff] [blame] | 40 | // Create two pipes. |
| 41 | if (pipe(PipeFD[0]) != 0 || pipe(PipeFD[1]) != 0) |
| 42 | perror("Error creating pipe: "); |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 43 | |
| 44 | ChildPID = fork(); |
| 45 | |
| 46 | if (ChildPID == 0) { |
| 47 | // In the child... |
| 48 | |
| 49 | // Close the parent ends of the pipes |
| 50 | close(PipeFD[0][1]); |
| 51 | close(PipeFD[1][0]); |
| 52 | |
| 53 | // Use our pipes as stdin and stdout |
| 54 | if (PipeFD[0][0] != STDIN_FILENO) { |
| 55 | dup2(PipeFD[0][0], STDIN_FILENO); |
| 56 | close(PipeFD[0][0]); |
| 57 | } |
| 58 | if (PipeFD[1][1] != STDOUT_FILENO) { |
| 59 | dup2(PipeFD[1][1], STDOUT_FILENO); |
| 60 | close(PipeFD[1][1]); |
| 61 | } |
| 62 | |
| 63 | // Execute the child process. |
| 64 | char *args[1] = { NULL }; |
| 65 | int rc = execv(ChildName.c_str(), args); |
| 66 | if (rc != 0) |
| 67 | perror("Error executing child process: "); |
Alp Toker | 632c6cd | 2014-01-23 22:19:45 +0000 | [diff] [blame] | 68 | } else { |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 69 | // In the parent... |
| 70 | |
| 71 | // Close the child ends of the pipes |
| 72 | close(PipeFD[0][0]); |
| 73 | close(PipeFD[1][1]); |
| 74 | |
| 75 | // Store the parent ends of the pipes |
Alp Toker | 632c6cd | 2014-01-23 22:19:45 +0000 | [diff] [blame] | 76 | ConnectionData = (void *)new ConnectionData_t(PipeFD[1][0], PipeFD[0][1]); |
| 77 | return true; |
Renato Golin | 695895c | 2014-01-14 22:43:43 +0000 | [diff] [blame] | 78 | } |
Alp Toker | 632c6cd | 2014-01-23 22:19:45 +0000 | [diff] [blame] | 79 | return false; |
| 80 | } |
| 81 | |
| 82 | bool RPCChannel::createClient() { |
| 83 | // Store the parent ends of the pipes |
| 84 | ConnectionData = (void *)new ConnectionData_t(STDIN_FILENO, STDOUT_FILENO); |
Renato Golin | 695895c | 2014-01-14 22:43:43 +0000 | [diff] [blame] | 85 | return true; |
| 86 | } |
| 87 | |
Alp Toker | ad6aa47 | 2014-01-24 17:18:52 +0000 | [diff] [blame^] | 88 | void RPCChannel::Wait() { wait(NULL); } |
| 89 | |
| 90 | static bool CheckError(int rc, size_t Size, const char *Desc) { |
| 91 | if (rc < 0) { |
| 92 | llvm::errs() << "IO Error: " << Desc << ": " << sys::StrError() << '\n'; |
| 93 | return false; |
| 94 | } else if ((size_t)rc != Size) { |
| 95 | std::string ErrorMsg; |
Renato Golin | 695895c | 2014-01-14 22:43:43 +0000 | [diff] [blame] | 96 | char Number[10] = { 0 }; |
| 97 | ErrorMsg += "Expecting "; |
| 98 | sprintf(Number, "%d", (uint32_t)Size); |
| 99 | ErrorMsg += Number; |
| 100 | ErrorMsg += " bytes, Got "; |
| 101 | sprintf(Number, "%d", rc); |
| 102 | ErrorMsg += Number; |
Alp Toker | ad6aa47 | 2014-01-24 17:18:52 +0000 | [diff] [blame^] | 103 | llvm::errs() << "RPC Error: " << Desc << ": " << ErrorMsg << '\n'; |
| 104 | return false; |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 105 | } |
Alp Toker | ad6aa47 | 2014-01-24 17:18:52 +0000 | [diff] [blame^] | 106 | return true; |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Alp Toker | ad6aa47 | 2014-01-24 17:18:52 +0000 | [diff] [blame^] | 109 | bool RPCChannel::WriteBytes(const void *Data, size_t Size) { |
| 110 | int rc = write(((ConnectionData_t *)ConnectionData)->OutputPipe, Data, Size); |
| 111 | return CheckError(rc, Size, "WriteBytes"); |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Alp Toker | ad6aa47 | 2014-01-24 17:18:52 +0000 | [diff] [blame^] | 114 | bool RPCChannel::ReadBytes(void *Data, size_t Size) { |
| 115 | int rc = read(((ConnectionData_t *)ConnectionData)->InputPipe, Data, Size); |
| 116 | return CheckError(rc, Size, "ReadBytes"); |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Alp Toker | 632c6cd | 2014-01-23 22:19:45 +0000 | [diff] [blame] | 119 | RPCChannel::~RPCChannel() { |
Benjamin Kramer | c83946f | 2013-10-05 11:53:20 +0000 | [diff] [blame] | 120 | delete static_cast<ConnectionData_t *>(ConnectionData); |
| 121 | } |
| 122 | |
Benjamin Kramer | c12c7d0 | 2013-10-02 21:58:02 +0000 | [diff] [blame] | 123 | } // namespace llvm |