2a5d47650ffe71dca1556302c95ede8e0518089c
[oota-llvm.git] / tools / lli / Unix / RPCChannel.inc
1 //=- RPCChannel.inc - LLVM out-of-process JIT execution for Unix --=//
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 //
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.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/Support/Errno.h"
16 #include "llvm/Support/raw_ostream.h"
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <sys/wait.h>
21 #include <unistd.h>
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
36 bool RPCChannel::createServer() {
37   int PipeFD[2][2];
38   pid_t ChildPID;
39
40   // Create two pipes.
41   if (pipe(PipeFD[0]) != 0 || pipe(PipeFD[1]) != 0)
42     perror("Error creating pipe: ");
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: ");
68   } else {
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
76     ConnectionData = (void *)new ConnectionData_t(PipeFD[1][0], PipeFD[0][1]);
77     return true;
78   }
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);
85   return true;
86 }
87
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;
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;
103     llvm::errs() << "RPC Error: " << Desc << ": " << ErrorMsg << '\n';
104     return false;
105   }
106   return true;
107 }
108
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");
112 }
113
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");
117 }
118
119 RPCChannel::~RPCChannel() {
120   delete static_cast<ConnectionData_t *>(ConnectionData);
121 }
122
123 } // namespace llvm