1 /* This file implements an interface to the Customs daemon to do
2 remote execution of commands under GNU make.
3 THIS CODE IS NOT SUPPORTED BY THE GNU PROJECT.
4 Please do not send bug reports or questions about it to
7 Copyright (C) 1988, 1989, 1992, 1993 Free Software Foundation, Inc.
8 This file is part of GNU Make.
10 GNU Make is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2, or (at your option)
15 GNU Make is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GNU Make; see the file COPYING. If not, write to
22 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
30 #define __STRICT_BSD__ /* Don't make conflicting declarations. */
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 /* Return nonzero if the next job should be done remotely. */
51 static int inited
= 0;
54 /* Allow the user to turn off job exportation
55 (useful while he is debugging Customs, for example). */
56 if (getenv ("GNU_MAKE_NO_CUSTOMS") != 0)
61 /* For secure Customs, make is installed setuid root and
62 Customs requires a privileged source port be used. */
65 /* Ping the daemon once to see if it is there. */
66 inited
= Customs_Ping () == RPC_SUCCESS
? 1 : -1;
68 /* Return to normal user access. */
71 if (starting_directory
== 0)
72 /* main couldn't figure it out. */
76 /* Normalize the current directory path name to something
77 that should work on all machines exported to. */
79 normalized_cwd
= (char *) xmalloc (GET_PATH_MAX
);
80 strcpy (normalized_cwd
, starting_directory
);
81 if (Customs_NormPath (normalized_cwd
, GET_PATH_MAX
) < 0)
82 /* Path normalization failure means using Customs
83 won't work, but it's not really an error. */
91 status
= Customs_Host (EXPORT_SAME
, &permit
);
92 if (status
!= RPC_SUCCESS
)
95 printf ("Customs won't export: %s\n", Rpc_ErrorMessage (status
));
99 return !CUSTOMS_FAIL (&permit
.addr
);
102 /* Start a remote job running the command in ARGV, with environment from
103 ENVP. It gets standard input from STDIN_FD. On failure, return
104 nonzero. On success, return zero, and set *USED_STDIN to nonzero if it
105 will actually use STDIN_FD, zero if not, set *ID_PTR to a unique
106 identification, and set *IS_REMOTE to nonzero if the job is remote, zero
107 if it is local (meaning *ID_PTR is a process ID). */
110 start_remote_job (argv
, envp
, stdin_fd
, is_remote
, id_ptr
, used_stdin
)
117 extern int vfork (), execve ();
118 char waybill
[MAX_DATA_SIZE
], msg
[128];
119 struct timeval timeout
;
120 struct sockaddr_in sin
;
122 int retsock
, retport
, sock
;
126 /* Create the return socket. */
127 retsock
= Rpc_UdpCreate (True
, 0);
130 error ("exporting: Couldn't create return socket.");
134 /* Get the return socket's port number. */
136 if (getsockname (retsock
, (struct sockaddr
*) &sin
, &len
) < 0)
138 (void) close (retsock
);
139 perror_with_name ("exporting: ", "getsockname");
142 retport
= sin
.sin_port
;
144 /* Create the TCP socket for talking to the remote child. */
145 sock
= Rpc_TcpCreate (False
, 0);
147 /* Create a WayBill to give to the server. */
148 len
= Customs_MakeWayBill (&permit
, normalized_cwd
, argv
[0], argv
,
149 envp
, retport
, waybill
);
151 /* Modify the waybill as if the remote child had done `child_access ()'. */
153 WayBill
*wb
= (WayBill
*) waybill
;
158 /* Send the request to the server, timing out in 20 seconds. */
161 sin
.sin_family
= AF_INET
;
162 sin
.sin_port
= htons (Customs_Port ());
163 sin
.sin_addr
= permit
.addr
;
164 status
= Rpc_Call (sock
, &sin
, (Rpc_Proc
) CUSTOMS_IMPORT
,
165 len
, (Rpc_Opaque
) waybill
,
166 sizeof(msg
), (Rpc_Opaque
) msg
,
168 if (status
!= RPC_SUCCESS
)
170 (void) close (retsock
);
172 error ("exporting: %s", Rpc_ErrorMessage (status
));
175 else if (msg
[0] != 'O' || msg
[1] != 'k' || msg
[2] != '\0')
177 (void) close (retsock
);
179 error ("CUSTOMS_IMPORT: %s", msg
);
184 struct hostent
*host
= gethostbyaddr (&permit
.addr
, sizeof (permit
.addr
),
186 printf ("Job exported to %s ID %u\n",
187 host
== 0 ? inet_ntoa (permit
.addr
) : host
->h_name
,
197 /* The fork failed! */
198 perror_with_name ("vfork", "");
203 /* Child side. Run `export' to handle the connection. */
204 static char sock_buf
[20], retsock_buf
[20], id_buf
[20];
205 static char *new_argv
[6] =
206 { EXPORT_COMMAND
, "-id", sock_buf
, retsock_buf
, id_buf
, 0 };
208 /* Set up the arguments. */
209 (void) sprintf (sock_buf
, "%d", sock
);
210 (void) sprintf (retsock_buf
, "%d", retsock
);
211 (void) sprintf (id_buf
, "%x", permit
.id
);
213 /* Get the right stdin. */
215 (void) dup2 (stdin_fd
, 0);
217 /* Unblock signals in the child. */
220 /* Run the command. */
221 exec_command (new_argv
, envp
);
224 /* Parent side. Return the `export' process's ID. */
225 (void) close (retsock
);
232 /* Get the status of a dead remote child. Block waiting for one to die
233 if BLOCK is nonzero. Set *EXIT_CODE_PTR to the exit status, *SIGNAL_PTR
234 to the termination signal or zero if it exited normally, and *COREDUMP_PTR
235 nonzero if it dumped core. Return the ID of the child that died,
236 0 if we would have to block and !BLOCK, or < 0 if there were none. */
239 remote_status (exit_code_ptr
, signal_ptr
, coredump_ptr
, block
)
240 int *exit_code_ptr
, *signal_ptr
, *coredump_ptr
;
246 /* Block asynchronous notification of remote child death.
247 If this notification is done by raising the child termination
248 signal, do not block that signal. */
250 block_remote_children ()
255 /* Restore asynchronous notification of remote child death.
256 If this is done by raising the child termination signal,
257 do not unblock that signal. */
259 unblock_remote_children ()
264 /* Send signal SIG to child ID. Return 0 if successful, -1 if not. */
266 remote_kill (id
, sig
)