1 //=- RPCChannel.inc - LLVM out-of-process JIT execution for Unix --=//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Implementation of the Unix-specific parts of the RPCChannel class
11 // which executes JITed code in a separate process from where it was built.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Support/Errno.h"
16 #include "llvm/Support/raw_ostream.h"
25 struct ConnectionData_t {
29 ConnectionData_t(int in, int out) : InputPipe(in), OutputPipe(out) {}
36 bool RPCChannel::createServer() {
41 if (pipe(PipeFD[0]) != 0 || pipe(PipeFD[1]) != 0)
42 perror("Error creating pipe: ");
49 // Close the parent ends of the pipes
53 // Use our pipes as stdin and stdout
54 if (PipeFD[0][0] != STDIN_FILENO) {
55 dup2(PipeFD[0][0], STDIN_FILENO);
58 if (PipeFD[1][1] != STDOUT_FILENO) {
59 dup2(PipeFD[1][1], STDOUT_FILENO);
63 // Execute the child process.
64 char *args[1] = { NULL };
65 int rc = execv(ChildName.c_str(), args);
67 perror("Error executing child process: ");
71 // Close the child ends of the pipes
75 // Store the parent ends of the pipes
76 ConnectionData = (void *)new ConnectionData_t(PipeFD[1][0], PipeFD[0][1]);
82 bool RPCChannel::createClient() {
83 // Store the parent ends of the pipes
84 ConnectionData = (void *)new ConnectionData_t(STDIN_FILENO, STDOUT_FILENO);
88 void RPCChannel::Wait() { wait(NULL); }
90 static bool CheckError(int rc, size_t Size, const char *Desc) {
92 llvm::errs() << "IO Error: " << Desc << ": " << sys::StrError() << '\n';
94 } else if ((size_t)rc != Size) {
96 char Number[10] = { 0 };
97 ErrorMsg += "Expecting ";
98 sprintf(Number, "%d", (uint32_t)Size);
100 ErrorMsg += " bytes, Got ";
101 sprintf(Number, "%d", rc);
103 llvm::errs() << "RPC Error: " << Desc << ": " << ErrorMsg << '\n';
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");
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");
119 RPCChannel::~RPCChannel() {
120 delete static_cast<ConnectionData_t *>(ConnectionData);