* Added the test suite to the main distribution.
[make.git] / remote-cstms.c
blob18d4ed6e24b04d722a0e5cde509a09945f86ec76
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 <sys/time.h>
30 #include <netdb.h>
32 #include "customs.h"
34 char *remote_description = "Customs";
36 /* File name of the Customs `export' client command.
37 A full path name can be used to avoid some path-searching overhead. */
38 #define EXPORT_COMMAND "/usr/local/bin/export"
40 /* ExportPermit gotten by start_remote_job_p, and used by start_remote_job. */
41 static ExportPermit permit;
43 /* Normalized path name of the current directory. */
44 static char *normalized_cwd;
46 /* Call once at startup even if no commands are run. */
48 void
49 remote_setup ()
53 /* Called before exit. */
55 void
56 remote_cleanup ()
60 /* Return nonzero if the next job should be done remotely. */
62 int
63 start_remote_job_p (first_p)
64 int first_p;
66 static int inited = 0;
67 int status;
68 int njobs;
70 if (!inited)
72 /* Allow the user to turn off job exportation (useful while he is
73 debugging Customs, for example). */
74 if (getenv ("GNU_MAKE_NO_CUSTOMS") != 0)
76 inited = -1;
77 return 0;
80 /* For secure Customs, make is installed setuid root and
81 Customs requires a privileged source port be used. */
82 make_access ();
84 if (debug_flag)
85 Rpc_Debug(1);
87 /* Ping the daemon once to see if it is there. */
88 inited = Customs_Ping () == RPC_SUCCESS ? 1 : -1;
90 /* Return to normal user access. */
91 user_access ();
93 if (starting_directory == 0)
94 /* main couldn't figure it out. */
95 inited = -1;
96 else
98 /* Normalize the current directory path name to something
99 that should work on all machines exported to. */
101 normalized_cwd = (char *) xmalloc (GET_PATH_MAX);
102 strcpy (normalized_cwd, starting_directory);
103 if (Customs_NormPath (normalized_cwd, GET_PATH_MAX) < 0)
104 /* Path normalization failure means using Customs
105 won't work, but it's not really an error. */
106 inited = -1;
110 if (inited < 0)
111 return 0;
113 njobs = job_slots_used;
114 if (!first_p)
115 njobs -= 1; /* correction for being called from reap_children() */
117 /* the first job should run locally, or, if the -l flag is given, we use
118 that as clue as to how many local jobs should be scheduled locally */
119 if (max_load_average < 0 && njobs == 0 || njobs < max_load_average)
120 return 0;
122 status = Customs_Host (EXPORT_SAME, &permit);
123 if (status != RPC_SUCCESS)
125 if (debug_flag)
126 printf ("Customs won't export: %s\n", Rpc_ErrorMessage (status));
127 return 0;
130 return !CUSTOMS_FAIL (&permit.addr);
133 /* Start a remote job running the command in ARGV, with environment from
134 ENVP. It gets standard input from STDIN_FD. On failure, return
135 nonzero. On success, return zero, and set *USED_STDIN to nonzero if it
136 will actually use STDIN_FD, zero if not, set *ID_PTR to a unique
137 identification, and set *IS_REMOTE to nonzero if the job is remote, zero
138 if it is local (meaning *ID_PTR is a process ID). */
141 start_remote_job (argv, envp, stdin_fd, is_remote, id_ptr, used_stdin)
142 char **argv, **envp;
143 int stdin_fd;
144 int *is_remote;
145 int *id_ptr;
146 int *used_stdin;
148 char waybill[MAX_DATA_SIZE], msg[128];
149 struct hostent *host;
150 struct timeval timeout;
151 struct sockaddr_in sin;
152 int len;
153 int retsock, retport, sock;
154 Rpc_Stat status;
155 int pid;
157 /* Create the return socket. */
158 retsock = Rpc_UdpCreate (True, 0);
159 if (retsock < 0)
161 error (NILF, "exporting: Couldn't create return socket.");
162 return 1;
165 /* Get the return socket's port number. */
166 len = sizeof (sin);
167 if (getsockname (retsock, (struct sockaddr *) &sin, &len) < 0)
169 (void) close (retsock);
170 perror_with_name ("exporting: ", "getsockname");
171 return 1;
173 retport = sin.sin_port;
175 /* Create the TCP socket for talking to the remote child. */
176 sock = Rpc_TcpCreate (False, 0);
178 /* Create a WayBill to give to the server. */
179 len = Customs_MakeWayBill (&permit, normalized_cwd, argv[0], argv,
180 envp, retport, waybill);
182 /* Modify the waybill as if the remote child had done `child_access ()'. */
184 WayBill *wb = (WayBill *) waybill;
185 wb->ruid = wb->euid;
186 wb->rgid = wb->egid;
189 /* Send the request to the server, timing out in 20 seconds. */
190 timeout.tv_usec = 0;
191 timeout.tv_sec = 20;
192 sin.sin_family = AF_INET;
193 sin.sin_port = htons (Customs_Port ());
194 sin.sin_addr = permit.addr;
195 status = Rpc_Call (sock, &sin, (Rpc_Proc) CUSTOMS_IMPORT,
196 len, (Rpc_Opaque) waybill,
197 sizeof(msg), (Rpc_Opaque) msg,
198 1, &timeout);
200 host = gethostbyaddr((char *)&permit.addr, sizeof(permit.addr), AF_INET);
202 if (status != RPC_SUCCESS)
204 (void) close (retsock);
205 (void) close (sock);
206 error (NILF, "exporting to %s: %s",
207 host ? host->h_name : inet_ntoa (permit.addr),
208 Rpc_ErrorMessage (status));
209 return 1;
211 else if (msg[0] != 'O' || msg[1] != 'k' || msg[2] != '\0')
213 (void) close (retsock);
214 (void) close (sock);
215 error (NILF, "exporting to %s: %s",
216 host ? host->h_name : inet_ntoa (permit.addr),
217 msg);
218 return 1;
220 else
222 error (NILF, "*** exported to %s (id %u)",
223 host ? host->h_name : inet_ntoa (permit.addr),
224 permit.id);
227 fflush (stdout);
228 fflush (stderr);
230 pid = vfork ();
231 if (pid < 0)
233 /* The fork failed! */
234 perror_with_name ("vfork", "");
235 return 1;
237 else if (pid == 0)
239 /* Child side. Run `export' to handle the connection. */
240 static char sock_buf[20], retsock_buf[20], id_buf[20];
241 static char *new_argv[6] =
242 { EXPORT_COMMAND, "-id", sock_buf, retsock_buf, id_buf, 0 };
244 /* Set up the arguments. */
245 (void) sprintf (sock_buf, "%d", sock);
246 (void) sprintf (retsock_buf, "%d", retsock);
247 (void) sprintf (id_buf, "%x", permit.id);
249 /* Get the right stdin. */
250 if (stdin_fd != 0)
251 (void) dup2 (stdin_fd, 0);
253 /* Unblock signals in the child. */
254 unblock_sigs ();
256 /* Run the command. */
257 exec_command (new_argv, envp);
260 /* Parent side. Return the `export' process's ID. */
261 (void) close (retsock);
262 (void) close (sock);
263 *is_remote = 0;
264 *id_ptr = pid;
265 *used_stdin = 1;
266 return 0;
269 /* Get the status of a dead remote child. Block waiting for one to die
270 if BLOCK is nonzero. Set *EXIT_CODE_PTR to the exit status, *SIGNAL_PTR
271 to the termination signal or zero if it exited normally, and *COREDUMP_PTR
272 nonzero if it dumped core. Return the ID of the child that died,
273 0 if we would have to block and !BLOCK, or < 0 if there were none. */
276 remote_status (exit_code_ptr, signal_ptr, coredump_ptr, block)
277 int *exit_code_ptr, *signal_ptr, *coredump_ptr;
278 int block;
280 return -1;
283 /* Block asynchronous notification of remote child death.
284 If this notification is done by raising the child termination
285 signal, do not block that signal. */
286 void
287 block_remote_children ()
289 return;
292 /* Restore asynchronous notification of remote child death.
293 If this is done by raising the child termination signal,
294 do not unblock that signal. */
295 void
296 unblock_remote_children ()
298 return;
301 /* Send signal SIG to child ID. Return 0 if successful, -1 if not. */
303 remote_kill (id, sig)
304 int id;
305 int sig;
307 return -1;