blob: b7dec37d93d9b5147bc1a401d33bdea99d5b7478 [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
Andrew Kaylorc2ebf3f2013-10-02 17:12:36 +000015#include <stdio.h>
16#include <stdlib.h>
17#include <sys/wait.h>
Chandler Carruth07baed52014-01-13 08:04:33 +000018#include <unistd.h>
Andrew Kaylorc2ebf3f2013-10-02 17:12:36 +000019
20namespace {
21
22struct 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
31namespace llvm {
32
Alp Toker632c6cd2014-01-23 22:19:45 +000033bool RPCChannel::createServer() {
Andrew Kaylorc2ebf3f2013-10-02 17:12:36 +000034 int PipeFD[2][2];
35 pid_t ChildPID;
36
Benjamin Kramer3cfafb82013-10-04 19:10:03 +000037 // Create two pipes.
38 if (pipe(PipeFD[0]) != 0 || pipe(PipeFD[1]) != 0)
39 perror("Error creating pipe: ");
Andrew Kaylorc2ebf3f2013-10-02 17:12:36 +000040
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: ");
Alp Toker632c6cd2014-01-23 22:19:45 +000065 } else {
Andrew Kaylorc2ebf3f2013-10-02 17:12:36 +000066 // 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
Alp Toker632c6cd2014-01-23 22:19:45 +000073 ConnectionData = (void *)new ConnectionData_t(PipeFD[1][0], PipeFD[0][1]);
74 return true;
Renato Golin695895c2014-01-14 22:43:43 +000075 }
Alp Toker632c6cd2014-01-23 22:19:45 +000076 return false;
77}
78
79bool RPCChannel::createClient() {
80 // Store the parent ends of the pipes
81 ConnectionData = (void *)new ConnectionData_t(STDIN_FILENO, STDOUT_FILENO);
Renato Golin695895c2014-01-14 22:43:43 +000082 return true;
83}
84
Alp Toker632c6cd2014-01-23 22:19:45 +000085void RPCChannel::ReportError(int rc, size_t Size, std::string &ErrorMsg) {
Renato Golin695895c2014-01-14 22:43:43 +000086 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;
Andrew Kaylorc2ebf3f2013-10-02 17:12:36 +0000101 }
102}
103
Alp Toker632c6cd2014-01-23 22:19:45 +0000104int RPCChannel::WriteBytes(const void *Data, size_t Size) {
105 return write(((ConnectionData_t *)ConnectionData)->OutputPipe, Data, Size);
Andrew Kaylorc2ebf3f2013-10-02 17:12:36 +0000106}
107
Alp Toker632c6cd2014-01-23 22:19:45 +0000108int RPCChannel::ReadBytes(void *Data, size_t Size) {
109 return read(((ConnectionData_t *)ConnectionData)->InputPipe, Data, Size);
Andrew Kaylorc2ebf3f2013-10-02 17:12:36 +0000110}
111
Alp Toker632c6cd2014-01-23 22:19:45 +0000112void RPCChannel::Wait() { wait(NULL); }
Andrew Kaylorc2ebf3f2013-10-02 17:12:36 +0000113
Alp Toker632c6cd2014-01-23 22:19:45 +0000114RPCChannel::~RPCChannel() {
Benjamin Kramerc83946f2013-10-05 11:53:20 +0000115 delete static_cast<ConnectionData_t *>(ConnectionData);
116}
117
Benjamin Kramerc12c7d02013-10-02 21:58:02 +0000118} // namespace llvm