1 | /* GNU Make remote job exportation interface to the Customs daemon.
|
---|
2 | THIS CODE IS NOT SUPPORTED BY THE GNU PROJECT.
|
---|
3 | Please do not send bug reports or questions about it to
|
---|
4 | the Make maintainers.
|
---|
5 |
|
---|
6 | Copyright (C) 1988-2016 Free Software Foundation, Inc.
|
---|
7 | This file is part of GNU Make.
|
---|
8 |
|
---|
9 | GNU Make is free software; you can redistribute it and/or modify it under the
|
---|
10 | terms of the GNU General Public License as published by the Free Software
|
---|
11 | Foundation; either version 3 of the License, or (at your option) any later
|
---|
12 | version.
|
---|
13 |
|
---|
14 | GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
|
---|
15 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
---|
16 | A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
---|
17 |
|
---|
18 | You should have received a copy of the GNU General Public License along with
|
---|
19 | this program. If not, see <http://www.gnu.org/licenses/>. */
|
---|
20 |
|
---|
21 | #include "makeint.h"
|
---|
22 | #include "filedef.h"
|
---|
23 | #include "commands.h"
|
---|
24 | #include "job.h"
|
---|
25 | #include "debug.h"
|
---|
26 |
|
---|
27 | #include <sys/time.h>
|
---|
28 | #include <netdb.h>
|
---|
29 |
|
---|
30 | #include "customs.h"
|
---|
31 |
|
---|
32 | char *remote_description = "Customs";
|
---|
33 |
|
---|
34 | /* File name of the Customs 'export' client command.
|
---|
35 | A full path name can be used to avoid some path-searching overhead. */
|
---|
36 | #define EXPORT_COMMAND "/usr/local/bin/export"
|
---|
37 |
|
---|
38 | /* ExportPermit gotten by start_remote_job_p, and used by start_remote_job. */
|
---|
39 | static ExportPermit permit;
|
---|
40 |
|
---|
41 | /* Normalized path name of the current directory. */
|
---|
42 | static char *normalized_cwd;
|
---|
43 |
|
---|
44 | /* Call once at startup even if no commands are run. */
|
---|
45 |
|
---|
46 | void
|
---|
47 | remote_setup (void)
|
---|
48 | {
|
---|
49 | }
|
---|
50 |
|
---|
51 | /* Called before exit. */
|
---|
52 |
|
---|
53 | void
|
---|
54 | remote_cleanup (void)
|
---|
55 | {
|
---|
56 | }
|
---|
57 | |
---|
58 |
|
---|
59 | /* Return nonzero if the next job should be done remotely. */
|
---|
60 |
|
---|
61 | int
|
---|
62 | start_remote_job_p (int first_p)
|
---|
63 | {
|
---|
64 | static int inited = 0;
|
---|
65 | int status;
|
---|
66 | int njobs;
|
---|
67 |
|
---|
68 | if (!inited)
|
---|
69 | {
|
---|
70 | /* Allow the user to turn off job exportation (useful while he is
|
---|
71 | debugging Customs, for example). */
|
---|
72 | if (getenv ("GNU_MAKE_NO_CUSTOMS") != 0)
|
---|
73 | {
|
---|
74 | inited = -1;
|
---|
75 | return 0;
|
---|
76 | }
|
---|
77 |
|
---|
78 | /* For secure Customs, make is installed setuid root and
|
---|
79 | Customs requires a privileged source port be used. */
|
---|
80 | make_access ();
|
---|
81 |
|
---|
82 | if (ISDB (DB_JOBS))
|
---|
83 | Rpc_Debug (1);
|
---|
84 |
|
---|
85 | /* Ping the daemon once to see if it is there. */
|
---|
86 | inited = Customs_Ping () == RPC_SUCCESS ? 1 : -1;
|
---|
87 |
|
---|
88 | /* Return to normal user access. */
|
---|
89 | user_access ();
|
---|
90 |
|
---|
91 | if (starting_directory == 0)
|
---|
92 | /* main couldn't figure it out. */
|
---|
93 | inited = -1;
|
---|
94 | else
|
---|
95 | {
|
---|
96 | /* Normalize the current directory path name to something
|
---|
97 | that should work on all machines exported to. */
|
---|
98 |
|
---|
99 | normalized_cwd = xmalloc (GET_PATH_MAX);
|
---|
100 | strcpy (normalized_cwd, starting_directory);
|
---|
101 | if (Customs_NormPath (normalized_cwd, GET_PATH_MAX) < 0)
|
---|
102 | /* Path normalization failure means using Customs
|
---|
103 | won't work, but it's not really an error. */
|
---|
104 | inited = -1;
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | if (inited < 0)
|
---|
109 | return 0;
|
---|
110 |
|
---|
111 | njobs = job_slots_used;
|
---|
112 | if (!first_p)
|
---|
113 | njobs -= 1; /* correction for being called from reap_children() */
|
---|
114 |
|
---|
115 | /* the first job should run locally, or, if the -l flag is given, we use
|
---|
116 | that as clue as to how many local jobs should be scheduled locally */
|
---|
117 | if (max_load_average < 0 && njobs == 0 || njobs < max_load_average)
|
---|
118 | return 0;
|
---|
119 |
|
---|
120 | status = Customs_Host (EXPORT_SAME, &permit);
|
---|
121 | if (status != RPC_SUCCESS)
|
---|
122 | {
|
---|
123 | DB (DB_JOBS, (_("Customs won't export: %s\n"),
|
---|
124 | Rpc_ErrorMessage (status)));
|
---|
125 | return 0;
|
---|
126 | }
|
---|
127 |
|
---|
128 | return !CUSTOMS_FAIL (&permit.addr);
|
---|
129 | }
|
---|
130 | |
---|
131 |
|
---|
132 | /* Start a remote job running the command in ARGV, with environment from
|
---|
133 | ENVP. It gets standard input from STDIN_FD. On failure, return
|
---|
134 | nonzero. On success, return zero, and set *USED_STDIN to nonzero if it
|
---|
135 | will actually use STDIN_FD, zero if not, set *ID_PTR to a unique
|
---|
136 | identification, and set *IS_REMOTE to nonzero if the job is remote, zero
|
---|
137 | if it is local (meaning *ID_PTR is a process ID). */
|
---|
138 |
|
---|
139 | int
|
---|
140 | start_remote_job (char **argv, char **envp, int stdin_fd,
|
---|
141 | int *is_remote, int *id_ptr, int *used_stdin)
|
---|
142 | {
|
---|
143 | char waybill[MAX_DATA_SIZE], msg[128];
|
---|
144 | struct hostent *host;
|
---|
145 | struct timeval timeout;
|
---|
146 | struct sockaddr_in sin;
|
---|
147 | int len;
|
---|
148 | int retsock, retport, sock;
|
---|
149 | Rpc_Stat status;
|
---|
150 | int pid;
|
---|
151 |
|
---|
152 | /* Create the return socket. */
|
---|
153 | retsock = Rpc_UdpCreate (True, 0);
|
---|
154 | if (retsock < 0)
|
---|
155 | {
|
---|
156 | O (error, NILF, "exporting: Couldn't create return socket.");
|
---|
157 | return 1;
|
---|
158 | }
|
---|
159 |
|
---|
160 | /* Get the return socket's port number. */
|
---|
161 | len = sizeof (sin);
|
---|
162 | if (getsockname (retsock, (struct sockaddr *) &sin, &len) < 0)
|
---|
163 | {
|
---|
164 | (void) close (retsock);
|
---|
165 | perror_with_name ("exporting: ", "getsockname");
|
---|
166 | return 1;
|
---|
167 | }
|
---|
168 | retport = sin.sin_port;
|
---|
169 |
|
---|
170 | /* Create the TCP socket for talking to the remote child. */
|
---|
171 | sock = Rpc_TcpCreate (False, 0);
|
---|
172 |
|
---|
173 | /* Create a WayBill to give to the server. */
|
---|
174 | len = Customs_MakeWayBill (&permit, normalized_cwd, argv[0], argv,
|
---|
175 | envp, retport, waybill);
|
---|
176 |
|
---|
177 | /* Modify the waybill as if the remote child had done 'child_access ()'. */
|
---|
178 | {
|
---|
179 | WayBill *wb = (WayBill *) waybill;
|
---|
180 | wb->ruid = wb->euid;
|
---|
181 | wb->rgid = wb->egid;
|
---|
182 | }
|
---|
183 |
|
---|
184 | /* Send the request to the server, timing out in 20 seconds. */
|
---|
185 | timeout.tv_usec = 0;
|
---|
186 | timeout.tv_sec = 20;
|
---|
187 | sin.sin_family = AF_INET;
|
---|
188 | sin.sin_port = htons (Customs_Port ());
|
---|
189 | sin.sin_addr = permit.addr;
|
---|
190 | status = Rpc_Call (sock, &sin, (Rpc_Proc) CUSTOMS_IMPORT,
|
---|
191 | len, (Rpc_Opaque) waybill,
|
---|
192 | sizeof (msg), (Rpc_Opaque) msg,
|
---|
193 | 1, &timeout);
|
---|
194 |
|
---|
195 | host = gethostbyaddr ((char *)&permit.addr, sizeof(permit.addr), AF_INET);
|
---|
196 |
|
---|
197 | {
|
---|
198 | const char *hnm = host ? host->h_name : inet_ntoa (permit.addr);
|
---|
199 | size_t hlen = strlen (hnm);
|
---|
200 |
|
---|
201 | if (status != RPC_SUCCESS)
|
---|
202 | {
|
---|
203 | const char *err = Rpc_ErrorMessage (status);
|
---|
204 | (void) close (retsock);
|
---|
205 | (void) close (sock);
|
---|
206 | error (NILF, hlen + strlen (err),
|
---|
207 | "exporting to %s: %s", hnm, err);
|
---|
208 | return 1;
|
---|
209 | }
|
---|
210 | else if (msg[0] != 'O' || msg[1] != 'k' || msg[2] != '\0')
|
---|
211 | {
|
---|
212 | (void) close (retsock);
|
---|
213 | (void) close (sock);
|
---|
214 | error (NILF, hlen + strlen (msg), "exporting to %s: %s", hnm, msg);
|
---|
215 | return 1;
|
---|
216 | }
|
---|
217 | else
|
---|
218 | {
|
---|
219 | error (NILF, hlen + INTSTR_LENGTH,
|
---|
220 | "*** exported to %s (id %u)", hnm, permit.id);
|
---|
221 | }
|
---|
222 |
|
---|
223 | fflush (stdout);
|
---|
224 | fflush (stderr);
|
---|
225 | }
|
---|
226 |
|
---|
227 | pid = vfork ();
|
---|
228 | if (pid < 0)
|
---|
229 | {
|
---|
230 | /* The fork failed! */
|
---|
231 | perror_with_name ("fork", "");
|
---|
232 | return 1;
|
---|
233 | }
|
---|
234 | else if (pid == 0)
|
---|
235 | {
|
---|
236 | /* Child side. Run 'export' to handle the connection. */
|
---|
237 | static char sock_buf[20], retsock_buf[20], id_buf[20];
|
---|
238 | static char *new_argv[6] =
|
---|
239 | { EXPORT_COMMAND, "-id", sock_buf, retsock_buf, id_buf, 0 };
|
---|
240 |
|
---|
241 | /* Set up the arguments. */
|
---|
242 | (void) sprintf (sock_buf, "%d", sock);
|
---|
243 | (void) sprintf (retsock_buf, "%d", retsock);
|
---|
244 | (void) sprintf (id_buf, "%x", permit.id);
|
---|
245 |
|
---|
246 | /* Get the right stdin. */
|
---|
247 | if (stdin_fd != 0)
|
---|
248 | (void) dup2 (stdin_fd, 0);
|
---|
249 |
|
---|
250 | /* Unblock signals in the child. */
|
---|
251 | unblock_sigs ();
|
---|
252 |
|
---|
253 | /* Run the command. */
|
---|
254 | exec_command (new_argv, envp);
|
---|
255 | }
|
---|
256 |
|
---|
257 | /* Parent side. Return the 'export' process's ID. */
|
---|
258 | (void) close (retsock);
|
---|
259 | (void) close (sock);
|
---|
260 | *is_remote = 0;
|
---|
261 | *id_ptr = pid;
|
---|
262 | *used_stdin = 1;
|
---|
263 | return 0;
|
---|
264 | }
|
---|
265 | |
---|
266 |
|
---|
267 | /* Get the status of a dead remote child. Block waiting for one to die
|
---|
268 | if BLOCK is nonzero. Set *EXIT_CODE_PTR to the exit status, *SIGNAL_PTR
|
---|
269 | to the termination signal or zero if it exited normally, and *COREDUMP_PTR
|
---|
270 | nonzero if it dumped core. Return the ID of the child that died,
|
---|
271 | 0 if we would have to block and !BLOCK, or < 0 if there were none. */
|
---|
272 |
|
---|
273 | int
|
---|
274 | remote_status (int *exit_code_ptr, int *signal_ptr, int *coredump_ptr,
|
---|
275 | int block)
|
---|
276 | {
|
---|
277 | return -1;
|
---|
278 | }
|
---|
279 |
|
---|
280 | /* Block asynchronous notification of remote child death.
|
---|
281 | If this notification is done by raising the child termination
|
---|
282 | signal, do not block that signal. */
|
---|
283 | void
|
---|
284 | block_remote_children (void)
|
---|
285 | {
|
---|
286 | return;
|
---|
287 | }
|
---|
288 |
|
---|
289 | /* Restore asynchronous notification of remote child death.
|
---|
290 | If this is done by raising the child termination signal,
|
---|
291 | do not unblock that signal. */
|
---|
292 | void
|
---|
293 | unblock_remote_children (void)
|
---|
294 | {
|
---|
295 | return;
|
---|
296 | }
|
---|
297 |
|
---|
298 | /* Send signal SIG to child ID. Return 0 if successful, -1 if not. */
|
---|
299 | int
|
---|
300 | remote_kill (int id, int sig)
|
---|
301 | {
|
---|
302 | return -1;
|
---|
303 | }
|
---|