forked from icecc/icecream
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserve.cpp
More file actions
260 lines (206 loc) · 7.01 KB
/
Copy pathserve.cpp
File metadata and controls
260 lines (206 loc) · 7.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/* -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 99; -*- */
/* vim: set ts=4 sw=4 et tw=99: */
/*
This file is part of Icecream.
Copyright (c) 2004 Stephan Kulow <coolo@suse.de>
2002, 2003 by Martin Pool <mbp@samba.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <setjmp.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
#include <cassert>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#ifdef HAVE_SYS_SIGNAL_H
# include <sys/signal.h>
#endif /* HAVE_SYS_SIGNAL_H */
#include <sys/param.h>
#include <unistd.h>
#include <job.h>
#include <comm.h>
#include "environment.h"
#include "exitcode.h"
#include "tempfile.h"
#include "workit.h"
#include "logging.h"
#include "serve.h"
#include "util.h"
#include <sys/time.h>
#ifdef __FreeBSD__
#include <sys/socket.h>
#include <sys/uio.h>
#endif
#ifndef O_LARGEFILE
#define O_LARGEFILE 0
#endif
#ifndef _PATH_TMP
#define _PATH_TMP "/tmp"
#endif
using namespace std;
int nice_level = 5;
static void
error_client(MsgChannel *client, string error)
{
if (IS_PROTOCOL_22(client)) {
client->send_msg(StatusTextMsg(error));
}
}
static void write_output_file( const string& file, MsgChannel* client )
{
int obj_fd = -1;
try {
obj_fd = open(file.c_str(), O_RDONLY | O_LARGEFILE);
if (obj_fd == -1) {
log_error() << "open failed" << endl;
error_client(client, "open of object file failed");
throw myexception(EXIT_DISTCC_FAILED);
}
unsigned char buffer[100000];
do {
ssize_t bytes = read(obj_fd, buffer, sizeof(buffer));
if (bytes < 0) {
if (errno == EINTR) {
continue;
}
throw myexception(EXIT_DISTCC_FAILED);
}
if (!bytes) {
if( !client->send_msg(EndMsg())) {
log_info() << "write of obj end failed " << endl;
throw myexception(EXIT_DISTCC_FAILED);
}
break;
}
FileChunkMsg fcmsg(buffer, bytes);
if (!client->send_msg(fcmsg)) {
log_info() << "write of obj chunk failed " << bytes << endl;
throw myexception(EXIT_DISTCC_FAILED);
}
} while (1);
} catch(...) {
if( obj_fd != -1 )
close( obj_fd );
throw;
}
}
/**
* Read a request, run the compiler, and send a response.
**/
int handle_connection(const string &basedir, CompileJob *job,
MsgChannel *client, int &out_fd,
unsigned int mem_limit, uid_t user_uid, gid_t user_gid)
{
int socket[2];
if (pipe(socket) == -1) {
return -1;
}
flush_debug();
pid_t pid = fork();
assert(pid >= 0);
if (pid > 0) { // parent
close(socket[1]);
out_fd = socket[0];
fcntl(out_fd, F_SETFD, FD_CLOEXEC);
return pid;
}
reset_debug(0);
close(socket[0]);
out_fd = socket[1];
/* internal communication channel, don't inherit to gcc */
fcntl(out_fd, F_SETFD, FD_CLOEXEC);
errno = 0;
int niceval = nice(nice_level);
(void) niceval;
if (errno != 0) {
log_warning() << "failed to set nice value: " << strerror(errno)
<< endl;
}
Msg *msg = 0; // The current read message
unsigned int job_id = 0;
string obj_file;
try {
if (job->environmentVersion().size()) {
string dirname = basedir + "/target=" + job->targetPlatform() + "/" + job->environmentVersion();
if (::access(string(dirname + "/usr/bin/as").c_str(), X_OK)) {
error_client(client, dirname + "/usr/bin/as is not executable");
log_error() << "I don't have environment " << job->environmentVersion() << "(" << job->targetPlatform() << ") " << job->jobID() << endl;
throw myexception(EXIT_DISTCC_FAILED); // the scheduler didn't listen to us!
}
chdir_to_environment(client, dirname, user_uid, user_gid);
} else {
error_client(client, "empty environment");
log_error() << "Empty environment (" << job->targetPlatform() << ") " << job->jobID() << endl;
throw myexception(EXIT_DISTCC_FAILED);
}
if (::access(_PATH_TMP + 1, W_OK)) {
error_client(client, "can't write to " _PATH_TMP);
log_error() << "can't write into " << _PATH_TMP << " " << strerror(errno) << endl;
throw myexception(-1);
}
int ret;
unsigned int job_stat[8];
CompileResultMsg rmsg;
job_id = job->jobID();
memset(job_stat, 0, sizeof(job_stat));
char *tmp_output = 0;
char prefix_output[32]; // 20 for 2^64 + 6 for "icecc-" + 1 for trailing NULL
sprintf(prefix_output, "icecc-%d", job_id);
if ((ret = dcc_make_tmpnam(prefix_output, ".o", &tmp_output, 0)) == 0) {
obj_file = tmp_output;
ret = work_it(*job, job_stat, client, rmsg, obj_file, mem_limit, client->fd, -1);
free(tmp_output);
}
delete job;
job = 0;
if (ret) {
if (ret == EXIT_OUT_OF_MEMORY) { // we catch that as special case
rmsg.was_out_of_memory = true;
} else {
throw myexception(ret);
}
}
if (!client->send_msg(rmsg)) {
log_info() << "write of result failed" << endl;
throw myexception(EXIT_DISTCC_FAILED);
}
struct stat st;
if (!stat(obj_file.c_str(), &st)) {
job_stat[JobStatistics::out_uncompressed] = st.st_size;
}
/* wake up parent and tell him that compile finished */
/* if the write failed, well, doesn't matter */
ignore_result(write(out_fd, job_stat, sizeof(job_stat)));
close(out_fd);
if (rmsg.status == 0) {
write_output_file(obj_file, client);
}
throw myexception(rmsg.status);
} catch (myexception e) {
delete client;
client = 0;
delete msg;
delete job;
if (!obj_file.empty()) {
unlink(obj_file.c_str());
}
_exit(e.exitcode());
}
}