blob: 2a5d47650ffe71dca1556302c95ede8e0518089c [file] [log] [blame]
Alp Toker632c6cd2014-01-23 22:19:45 +00001//=- RPCChannel.inc - LLVM out-of-process JIT execution for Unix --=//
Andrew Kaylorc2ebf3f2013-10-02 17:12:36 +00002//
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 Toker632c6cd2014-01-23 22:19:45 +000010// Implementation of the Unix-specific parts of the RPCChannel class
Andrew Kaylorc2ebf3f2013-10-02 17:12:36 +000011// which executes JITed code in a separate process from where it was built.
12//
13//===----------------------------------------------------------------------===//
14
Alp Tokerad6aa472014-01-24 17:18:52 +000015#include "llvm/Support/Errno.h"
16#include "llvm/Support/raw_ostream.h"
17
Andrew Kaylorc2ebf3f2013-10-02 17:12:36 +000018#include <stdio.h>
19#include <stdlib.h>
20#include <sys/wait.h>
Chandler Carruth07baed52014-01-13 08:04:33 +000021#include <unistd.h>
Andrew Kaylorc2ebf3f2013-10-02 17:12:36 +000022
23namespace {
24
25struct 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
34namespace llvm {
35
Alp Toker632c6cd2014-01-23 22:19:45 +000036bool RPCChannel::createServer() {
Andrew Kaylorc2ebf3f2013-10-02 17:12:36 +000037 int PipeFD[2][2];
38 pid_t ChildPID;
39
Benjamin Kramer3cfafb82013-10-04 19:10:03 +000040 // Create two pipes.
41 if (pipe(PipeFD[0]) != 0 || pipe(PipeFD[1]) != 0)
42 perror("Error creating pipe: ");
Andrew Kaylorc2ebf3f2013-10-02 17:12:36 +000043
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 Toker632c6cd2014-01-23 22:19:45 +000068 } else {
Andrew Kaylorc2ebf3f2013-10-02 17:12:36 +000069 // 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 Toker632c6cd2014-01-23 22:19:45 +000076 ConnectionData = (void *)new ConnectionData_t(PipeFD[1][0], PipeFD[0][1]);
77 return true;
Renato Golin695895c2014-01-14 22:43:43 +000078 }
Alp Toker632c6cd2014-01-23 22:19:45 +000079 return false;
80}
81
82bool RPCChannel::createClient() {
83 // Store the parent ends of the pipes
84 ConnectionData = (void *)new ConnectionData_t(STDIN_FILENO, STDOUT_FILENO);
Renato Golin695895c2014-01-14 22:43:43 +000085 return true;
86}
87
Alp Tokerad6aa472014-01-24 17:18:52 +000088void RPCChannel::Wait() { wait(NULL); }
89
90static 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 Golin695895c2014-01-14 22:43:43 +000096 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 Tokerad6aa472014-01-24 17:18:52 +0000103 llvm::errs() << "RPC Error: " << Desc << ": " << ErrorMsg << '\n';
104 return false;
Andrew Kaylorc2ebf3f2013-10-02 17:12:36 +0000105 }
Alp Tokerad6aa472014-01-24 17:18:52 +0000106 return true;
Andrew Kaylorc2ebf3f2013-10-02 17:12:36 +0000107}
108
Alp Tokerad6aa472014-01-24 17:18:52 +0000109bool 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 Kaylorc2ebf3f2013-10-02 17:12:36 +0000112}
113
Alp Tokerad6aa472014-01-24 17:18:52 +0000114bool RPCChannel::ReadBytes(void *Data, size_t Size) {
115 int rc = read(((ConnectionData_t *)ConnectionData)->InputPipe, Data, Size);
116 return CheckError(rc, Size, "ReadBytes");
Andrew Kaylorc2ebf3f2013-10-02 17:12:36 +0000117}
118
Alp Toker632c6cd2014-01-23 22:19:45 +0000119RPCChannel::~RPCChannel() {
Benjamin Kramerc83946f2013-10-05 11:53:20 +0000120 delete static_cast<ConnectionData_t *>(ConnectionData);
121}
122
Benjamin Kramerc12c7d02013-10-02 21:58:02 +0000123} // namespace llvm