lli: Factor portable messaging into a new RPCChannel facility
[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 <stdio.h>
16 #include <stdlib.h>
17 #include <sys/wait.h>
18 #include <unistd.h>
19
20 namespace {
21
22 struct ConnectionData_t {
23   int InputPipe;
24   int OutputPipe;
25
26   ConnectionData_t(int in, int out) : InputPipe(in), OutputPipe(out) {}
27 };
28
29 } // namespace
30
31 namespace llvm {
32
33 bool RPCChannel::createServer() {
34   int PipeFD[2][2];
35   pid_t ChildPID;
36
37   // Create two pipes.
38   if (pipe(PipeFD[0]) != 0 || pipe(PipeFD[1]) != 0)
39     perror("Error creating pipe: ");
40
41   ChildPID = fork();
42
43   if (ChildPID == 0) {
44     // In the child...
45
46     // Close the parent ends of the pipes
47     close(PipeFD[0][1]);
48     close(PipeFD[1][0]);
49
50     // Use our pipes as stdin and stdout
51     if (PipeFD[0][0] != STDIN_FILENO) {
52       dup2(PipeFD[0][0], STDIN_FILENO);
53       close(PipeFD[0][0]);
54     }
55     if (PipeFD[1][1] != STDOUT_FILENO) {
56       dup2(PipeFD[1][1], STDOUT_FILENO);
57       close(PipeFD[1][1]);
58     }
59
60     // Execute the child process.
61     char *args[1] = { NULL };
62     int rc = execv(ChildName.c_str(), args);
63     if (rc != 0)
64       perror("Error executing child process: ");
65   } else {
66     // In the parent...
67
68     // Close the child ends of the pipes
69     close(PipeFD[0][0]);
70     close(PipeFD[1][1]);
71
72     // Store the parent ends of the pipes
73     ConnectionData = (void *)new ConnectionData_t(PipeFD[1][0], PipeFD[0][1]);
74     return true;
75   }
76   return false;
77 }
78
79 bool RPCChannel::createClient() {
80   // Store the parent ends of the pipes
81   ConnectionData = (void *)new ConnectionData_t(STDIN_FILENO, STDOUT_FILENO);
82   return true;
83 }
84
85 void RPCChannel::ReportError(int rc, size_t Size, std::string &ErrorMsg) {
86   if (rc == -1) {
87     if (errno == EPIPE)
88       ErrorMsg += "pipe closed";
89     else if (errno == EINTR)
90       ErrorMsg += "interrupted";
91     else
92       ErrorMsg += "file descriptor error";
93   } else {
94     char Number[10] = { 0 };
95     ErrorMsg += "Expecting ";
96     sprintf(Number, "%d", (uint32_t)Size);
97     ErrorMsg += Number;
98     ErrorMsg += " bytes, Got ";
99     sprintf(Number, "%d", rc);
100     ErrorMsg += Number;
101   }
102 }
103
104 int RPCChannel::WriteBytes(const void *Data, size_t Size) {
105   return write(((ConnectionData_t *)ConnectionData)->OutputPipe, Data, Size);
106 }
107
108 int RPCChannel::ReadBytes(void *Data, size_t Size) {
109   return read(((ConnectionData_t *)ConnectionData)->InputPipe, Data, Size);
110 }
111
112 void RPCChannel::Wait() { wait(NULL); }
113
114 RPCChannel::~RPCChannel() {
115   delete static_cast<ConnectionData_t *>(ConnectionData);
116 }
117
118 } // namespace llvm