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
6 Copyright (C) 1988-2022 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 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
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.
18 You should have received a copy of the GNU General Public License along with
19 this program. If not, see <https://www.gnu.org/licenses/>. */
29 # include <sys/time.h>
35 char *remote_description = "Customs";
37 /* File name of the Customs 'export' client command.
38 A full path name can be used to avoid some path-searching overhead. */
39 #define EXPORT_COMMAND "/usr/local/bin/export"
41 /* ExportPermit gotten by start_remote_job_p, and used by start_remote_job. */
42 static ExportPermit permit;
44 /* Normalized path name of the current directory. */
45 static char *normalized_cwd;
47 /* Call once at startup even if no commands are run. */
54 /* Called before exit. */
61 /* Return nonzero if the next job should be done remotely. */
64 start_remote_job_p (int first_p)
66 static int inited = 0;
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)
83 /* Ping the daemon once to see if it is there. */
84 inited = Customs_Ping () == RPC_SUCCESS ? 1 : -1;
86 if (starting_directory == 0)
87 /* main couldn't figure it out. */
91 /* Normalize the current directory path name to something
92 that should work on all machines exported to. */
94 normalized_cwd = xmalloc (GET_PATH_MAX);
95 strcpy (normalized_cwd, starting_directory);
96 if (Customs_NormPath (normalized_cwd, GET_PATH_MAX) < 0)
97 /* Path normalization failure means using Customs
98 won't work, but it's not really an error. */
106 njobs = job_slots_used;
108 njobs -= 1; /* correction for being called from reap_children() */
110 /* the first job should run locally, or, if the -l flag is given, we use
111 that as clue as to how many local jobs should be scheduled locally */
112 if (max_load_average < 0 && njobs == 0 || njobs < max_load_average)
115 status = Customs_Host (EXPORT_SAME, &permit);
116 if (status != RPC_SUCCESS)
118 DB (DB_JOBS, (_("Customs won't export: %s\n"),
119 Rpc_ErrorMessage (status)));
123 return !CUSTOMS_FAIL (&permit.addr);
126 /* Start a remote job running the command in ARGV, with environment from
127 ENVP. It gets standard input from STDIN_FD. On failure, return
128 nonzero. On success, return zero, and set *USED_STDIN to nonzero if it
129 will actually use STDIN_FD, zero if not, set *ID_PTR to a unique
130 identification, and set *IS_REMOTE to nonzero if the job is remote, zero
131 if it is local (meaning *ID_PTR is a process ID). */
134 start_remote_job (char **argv, char **envp, int stdin_fd,
135 int *is_remote, pid_t *id_ptr, int *used_stdin)
137 char waybill[MAX_DATA_SIZE], msg[128];
138 struct hostent *host;
139 struct timeval timeout;
140 struct sockaddr_in sin;
142 int retsock, retport, sock;
146 /* Create the return socket. */
147 retsock = Rpc_UdpCreate (True, 0);
150 O (error, NILF, "exporting: Couldn't create return socket.");
154 /* Get the return socket's port number. */
156 if (getsockname (retsock, (struct sockaddr *) &sin, &len) < 0)
158 (void) close (retsock);
159 perror_with_name ("exporting: ", "getsockname");
162 retport = sin.sin_port;
164 /* Create the TCP socket for talking to the remote child. */
165 sock = Rpc_TcpCreate (False, 0);
167 /* Create a WayBill to give to the server. */
168 len = Customs_MakeWayBill (&permit, normalized_cwd, argv[0], argv,
169 envp, retport, waybill);
171 /* Modify the waybill for the child's uid/gid. */
173 WayBill *wb = (WayBill *) waybill;
178 /* Send the request to the server, timing out in 20 seconds. */
181 sin.sin_family = AF_INET;
182 sin.sin_port = htons (Customs_Port ());
183 sin.sin_addr = permit.addr;
184 status = Rpc_Call (sock, &sin, (Rpc_Proc) CUSTOMS_IMPORT,
185 len, (Rpc_Opaque) waybill,
186 sizeof (msg), (Rpc_Opaque) msg,
189 host = gethostbyaddr ((char *)&permit.addr, sizeof(permit.addr), AF_INET);
192 const char *hnm = host ? host->h_name : inet_ntoa (permit.addr);
193 size_t hlen = strlen (hnm);
195 if (status != RPC_SUCCESS)
197 const char *err = Rpc_ErrorMessage (status);
198 (void) close (retsock);
200 error (NILF, hlen + strlen (err),
201 "exporting to %s: %s", hnm, err);
204 else if (msg[0] != 'O' || msg[1] != 'k' || msg[2] != '\0')
206 (void) close (retsock);
208 error (NILF, hlen + strlen (msg), "exporting to %s: %s", hnm, msg);
213 error (NILF, hlen + INTSTR_LENGTH,
214 "*** exported to %s (id %u)", hnm, permit.id);
224 /* The fork failed! */
225 perror_with_name ("fork", "");
230 /* Child side. Run 'export' to handle the connection. */
231 static char sock_buf[INTSTR_LENGTH], retsock_buf[INTSTR_LENGTH];
232 static char id_buf[INTSTR_LENGTH];
233 static char *new_argv[6] =
234 { EXPORT_COMMAND, "-id", sock_buf, retsock_buf, id_buf, 0 };
236 /* Set up the arguments. */
237 (void) sprintf (sock_buf, "%d", sock);
238 (void) sprintf (retsock_buf, "%d", retsock);
239 (void) sprintf (id_buf, "%x", permit.id);
241 /* Get the right stdin. */
243 (void) dup2 (stdin_fd, 0);
245 /* Unblock signals in the child. */
248 /* Run the command. */
249 exec_command (new_argv, envp);
252 /* Parent side. Return the 'export' process's ID. */
253 (void) close (retsock);
261 /* Get the status of a dead remote child. Block waiting for one to die
262 if BLOCK is nonzero. Set *EXIT_CODE_PTR to the exit status, *SIGNAL_PTR
263 to the termination signal or zero if it exited normally, and *COREDUMP_PTR
264 nonzero if it dumped core. Return the ID of the child that died,
265 0 if we would have to block and !BLOCK, or < 0 if there were none. */
268 remote_status (int *exit_code_ptr, int *signal_ptr, int *coredump_ptr,
274 /* Block asynchronous notification of remote child death.
275 If this notification is done by raising the child termination
276 signal, do not block that signal. */
278 block_remote_children (void)
283 /* Restore asynchronous notification of remote child death.
284 If this is done by raising the child termination signal,
285 do not unblock that signal. */
287 unblock_remote_children (void)
292 /* Send signal SIG to child ID. Return 0 if successful, -1 if not. */
294 remote_kill (pid_t id, int sig)