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-2012 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 <http://www.gnu.org/licenses/>. */
33 char *remote_description
= "Customs";
35 /* File name of the Customs 'export' client command.
36 A full path name can be used to avoid some path-searching overhead. */
37 #define EXPORT_COMMAND "/usr/local/bin/export"
39 /* ExportPermit gotten by start_remote_job_p, and used by start_remote_job. */
40 static ExportPermit permit
;
42 /* Normalized path name of the current directory. */
43 static char *normalized_cwd
;
45 /* Call once at startup even if no commands are run. */
52 /* Called before exit. */
59 /* Return nonzero if the next job should be done remotely. */
62 start_remote_job_p (int first_p
)
64 static int inited
= 0;
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)
78 /* For secure Customs, make is installed setuid root and
79 Customs requires a privileged source port be used. */
85 /* Ping the daemon once to see if it is there. */
86 inited
= Customs_Ping () == RPC_SUCCESS
? 1 : -1;
88 /* Return to normal user access. */
91 if (starting_directory
== 0)
92 /* main couldn't figure it out. */
96 /* Normalize the current directory path name to something
97 that should work on all machines exported to. */
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. */
111 njobs
= job_slots_used
;
113 njobs
-= 1; /* correction for being called from reap_children() */
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
)
120 status
= Customs_Host (EXPORT_SAME
, &permit
);
121 if (status
!= RPC_SUCCESS
)
123 DB (DB_JOBS
, (_("Customs won't export: %s\n"),
124 Rpc_ErrorMessage (status
)));
128 return !CUSTOMS_FAIL (&permit
.addr
);
131 /* Start a remote job running the command in ARGV, with environment from
132 ENVP. It gets standard input from STDIN_FD. On failure, return
133 nonzero. On success, return zero, and set *USED_STDIN to nonzero if it
134 will actually use STDIN_FD, zero if not, set *ID_PTR to a unique
135 identification, and set *IS_REMOTE to nonzero if the job is remote, zero
136 if it is local (meaning *ID_PTR is a process ID). */
139 start_remote_job (char **argv
, char **envp
, int stdin_fd
,
140 int *is_remote
, int *id_ptr
, int *used_stdin
)
142 char waybill
[MAX_DATA_SIZE
], msg
[128];
143 struct hostent
*host
;
144 struct timeval timeout
;
145 struct sockaddr_in sin
;
147 int retsock
, retport
, sock
;
151 /* Create the return socket. */
152 retsock
= Rpc_UdpCreate (True
, 0);
155 error (NILF
, "exporting: Couldn't create return socket.");
159 /* Get the return socket's port number. */
161 if (getsockname (retsock
, (struct sockaddr
*) &sin
, &len
) < 0)
163 (void) close (retsock
);
164 perror_with_name ("exporting: ", "getsockname");
167 retport
= sin
.sin_port
;
169 /* Create the TCP socket for talking to the remote child. */
170 sock
= Rpc_TcpCreate (False
, 0);
172 /* Create a WayBill to give to the server. */
173 len
= Customs_MakeWayBill (&permit
, normalized_cwd
, argv
[0], argv
,
174 envp
, retport
, waybill
);
176 /* Modify the waybill as if the remote child had done 'child_access ()'. */
178 WayBill
*wb
= (WayBill
*) waybill
;
183 /* Send the request to the server, timing out in 20 seconds. */
186 sin
.sin_family
= AF_INET
;
187 sin
.sin_port
= htons (Customs_Port ());
188 sin
.sin_addr
= permit
.addr
;
189 status
= Rpc_Call (sock
, &sin
, (Rpc_Proc
) CUSTOMS_IMPORT
,
190 len
, (Rpc_Opaque
) waybill
,
191 sizeof(msg
), (Rpc_Opaque
) msg
,
194 host
= gethostbyaddr((char *)&permit
.addr
, sizeof(permit
.addr
), AF_INET
);
196 if (status
!= RPC_SUCCESS
)
198 (void) close (retsock
);
200 error (NILF
, "exporting to %s: %s",
201 host
? host
->h_name
: inet_ntoa (permit
.addr
),
202 Rpc_ErrorMessage (status
));
205 else if (msg
[0] != 'O' || msg
[1] != 'k' || msg
[2] != '\0')
207 (void) close (retsock
);
209 error (NILF
, "exporting to %s: %s",
210 host
? host
->h_name
: inet_ntoa (permit
.addr
),
216 error (NILF
, "*** exported to %s (id %u)",
217 host
? host
->h_name
: inet_ntoa (permit
.addr
),
227 /* The fork failed! */
228 perror_with_name ("vfork", "");
233 /* Child side. Run 'export' to handle the connection. */
234 static char sock_buf
[20], retsock_buf
[20], id_buf
[20];
235 static char *new_argv
[6] =
236 { EXPORT_COMMAND
, "-id", sock_buf
, retsock_buf
, id_buf
, 0 };
238 /* Set up the arguments. */
239 (void) sprintf (sock_buf
, "%d", sock
);
240 (void) sprintf (retsock_buf
, "%d", retsock
);
241 (void) sprintf (id_buf
, "%x", permit
.id
);
243 /* Get the right stdin. */
245 (void) dup2 (stdin_fd
, 0);
247 /* Unblock signals in the child. */
250 /* Run the command. */
251 exec_command (new_argv
, envp
);
254 /* Parent side. Return the 'export' process's ID. */
255 (void) close (retsock
);
263 /* Get the status of a dead remote child. Block waiting for one to die
264 if BLOCK is nonzero. Set *EXIT_CODE_PTR to the exit status, *SIGNAL_PTR
265 to the termination signal or zero if it exited normally, and *COREDUMP_PTR
266 nonzero if it dumped core. Return the ID of the child that died,
267 0 if we would have to block and !BLOCK, or < 0 if there were none. */
270 remote_status (int *exit_code_ptr
, int *signal_ptr
, int *coredump_ptr
,
276 /* Block asynchronous notification of remote child death.
277 If this notification is done by raising the child termination
278 signal, do not block that signal. */
280 block_remote_children (void)
285 /* Restore asynchronous notification of remote child death.
286 If this is done by raising the child termination signal,
287 do not unblock that signal. */
289 unblock_remote_children (void)
294 /* Send signal SIG to child ID. Return 0 if successful, -1 if not. */
296 remote_kill (int id
, int sig
)