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