Fix bug #2846.
[make.git] / remote-cstms.c
blobdae3535b44754f22376e2e0b48a8efa9010e7548
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.
6 Copyright (C) 1988, 1989, 1992, 1993 Free Software Foundation, Inc.
7 This file is part of GNU Make.
9 GNU Make is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
14 GNU Make is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GNU Make; see the file COPYING. If not, write to
21 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
24 #include "make.h"
25 #include "job.h"
26 #include "filedef.h"
27 #include "commands.h"
28 #include "job.h"
29 #include "debug.h"
31 #include <sys/time.h>
32 #include <netdb.h>
34 #include "customs.h"
36 char *remote_description = "Customs";
38 /* File name of the Customs `export' client command.
39 A full path name can be used to avoid some path-searching overhead. */
40 #define EXPORT_COMMAND "/usr/local/bin/export"
42 /* ExportPermit gotten by start_remote_job_p, and used by start_remote_job. */
43 static ExportPermit permit;
45 /* Normalized path name of the current directory. */
46 static char *normalized_cwd;
48 /* Call once at startup even if no commands are run. */
50 void
51 remote_setup (void)
55 /* Called before exit. */
57 void
58 remote_cleanup (void)
62 /* Return nonzero if the next job should be done remotely. */
64 int
65 start_remote_job_p (int first_p)
67 static int inited = 0;
68 int status;
69 int njobs;
71 if (!inited)
73 /* Allow the user to turn off job exportation (useful while he is
74 debugging Customs, for example). */
75 if (getenv ("GNU_MAKE_NO_CUSTOMS") != 0)
77 inited = -1;
78 return 0;
81 /* For secure Customs, make is installed setuid root and
82 Customs requires a privileged source port be used. */
83 make_access ();
85 if (ISDB (DB_JOBS))
86 Rpc_Debug(1);
88 /* Ping the daemon once to see if it is there. */
89 inited = Customs_Ping () == RPC_SUCCESS ? 1 : -1;
91 /* Return to normal user access. */
92 user_access ();
94 if (starting_directory == 0)
95 /* main couldn't figure it out. */
96 inited = -1;
97 else
99 /* Normalize the current directory path name to something
100 that should work on all machines exported to. */
102 normalized_cwd = (char *) xmalloc (GET_PATH_MAX);
103 strcpy (normalized_cwd, starting_directory);
104 if (Customs_NormPath (normalized_cwd, GET_PATH_MAX) < 0)
105 /* Path normalization failure means using Customs
106 won't work, but it's not really an error. */
107 inited = -1;
111 if (inited < 0)
112 return 0;
114 njobs = job_slots_used;
115 if (!first_p)
116 njobs -= 1; /* correction for being called from reap_children() */
118 /* the first job should run locally, or, if the -l flag is given, we use
119 that as clue as to how many local jobs should be scheduled locally */
120 if (max_load_average < 0 && njobs == 0 || njobs < max_load_average)
121 return 0;
123 status = Customs_Host (EXPORT_SAME, &permit);
124 if (status != RPC_SUCCESS)
126 DB (DB_JOBS, (_("Customs won't export: %s\n"),
127 Rpc_ErrorMessage (status)));
128 return 0;
131 return !CUSTOMS_FAIL (&permit.addr);
134 /* Start a remote job running the command in ARGV, with environment from
135 ENVP. It gets standard input from STDIN_FD. On failure, return
136 nonzero. On success, return zero, and set *USED_STDIN to nonzero if it
137 will actually use STDIN_FD, zero if not, set *ID_PTR to a unique
138 identification, and set *IS_REMOTE to nonzero if the job is remote, zero
139 if it is local (meaning *ID_PTR is a process ID). */
142 start_remote_job (char **argv, char **envp, int stdin_fd,
143 int *is_remote, int *id_ptr, int *used_stdin)
145 char waybill[MAX_DATA_SIZE], msg[128];
146 struct hostent *host;
147 struct timeval timeout;
148 struct sockaddr_in sin;
149 int len;
150 int retsock, retport, sock;
151 Rpc_Stat status;
152 int pid;
154 /* Create the return socket. */
155 retsock = Rpc_UdpCreate (True, 0);
156 if (retsock < 0)
158 error (NILF, "exporting: Couldn't create return socket.");
159 return 1;
162 /* Get the return socket's port number. */
163 len = sizeof (sin);
164 if (getsockname (retsock, (struct sockaddr *) &sin, &len) < 0)
166 (void) close (retsock);
167 perror_with_name ("exporting: ", "getsockname");
168 return 1;
170 retport = sin.sin_port;
172 /* Create the TCP socket for talking to the remote child. */
173 sock = Rpc_TcpCreate (False, 0);
175 /* Create a WayBill to give to the server. */
176 len = Customs_MakeWayBill (&permit, normalized_cwd, argv[0], argv,
177 envp, retport, waybill);
179 /* Modify the waybill as if the remote child had done `child_access ()'. */
181 WayBill *wb = (WayBill *) waybill;
182 wb->ruid = wb->euid;
183 wb->rgid = wb->egid;
186 /* Send the request to the server, timing out in 20 seconds. */
187 timeout.tv_usec = 0;
188 timeout.tv_sec = 20;
189 sin.sin_family = AF_INET;
190 sin.sin_port = htons (Customs_Port ());
191 sin.sin_addr = permit.addr;
192 status = Rpc_Call (sock, &sin, (Rpc_Proc) CUSTOMS_IMPORT,
193 len, (Rpc_Opaque) waybill,
194 sizeof(msg), (Rpc_Opaque) msg,
195 1, &timeout);
197 host = gethostbyaddr((char *)&permit.addr, sizeof(permit.addr), AF_INET);
199 if (status != RPC_SUCCESS)
201 (void) close (retsock);
202 (void) close (sock);
203 error (NILF, "exporting to %s: %s",
204 host ? host->h_name : inet_ntoa (permit.addr),
205 Rpc_ErrorMessage (status));
206 return 1;
208 else if (msg[0] != 'O' || msg[1] != 'k' || msg[2] != '\0')
210 (void) close (retsock);
211 (void) close (sock);
212 error (NILF, "exporting to %s: %s",
213 host ? host->h_name : inet_ntoa (permit.addr),
214 msg);
215 return 1;
217 else
219 error (NILF, "*** exported to %s (id %u)",
220 host ? host->h_name : inet_ntoa (permit.addr),
221 permit.id);
224 fflush (stdout);
225 fflush (stderr);
227 pid = vfork ();
228 if (pid < 0)
230 /* The fork failed! */
231 perror_with_name ("vfork", "");
232 return 1;
234 else if (pid == 0)
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 };
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);
246 /* Get the right stdin. */
247 if (stdin_fd != 0)
248 (void) dup2 (stdin_fd, 0);
250 /* Unblock signals in the child. */
251 unblock_sigs ();
253 /* Run the command. */
254 exec_command (new_argv, envp);
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;
266 /* Get the status of a dead remote child. Block waiting for one to die
267 if BLOCK is nonzero. Set *EXIT_CODE_PTR to the exit status, *SIGNAL_PTR
268 to the termination signal or zero if it exited normally, and *COREDUMP_PTR
269 nonzero if it dumped core. Return the ID of the child that died,
270 0 if we would have to block and !BLOCK, or < 0 if there were none. */
273 remote_status (int *exit_code_ptr, int *signal_ptr, int *coredump_ptr,
274 int block)
276 return -1;
279 /* Block asynchronous notification of remote child death.
280 If this notification is done by raising the child termination
281 signal, do not block that signal. */
282 void
283 block_remote_children (void)
285 return;
288 /* Restore asynchronous notification of remote child death.
289 If this is done by raising the child termination signal,
290 do not unblock that signal. */
291 void
292 unblock_remote_children (void)
294 return;
297 /* Send signal SIG to child ID. Return 0 if successful, -1 if not. */
299 remote_kill (int id, int sig)
301 return -1;