make/remote-cstms.c

301 lines
8.4 KiB
C
Raw Normal View History

1994-03-23 14:12:55 +00:00
/* GNU Make remote job exportation interface to the Customs daemon.
1992-03-09 23:06:05 +00:00
THIS CODE IS NOT SUPPORTED BY THE GNU PROJECT.
Please do not send bug reports or questions about it to
the Make maintainers.
2014-09-30 12:33:21 +00:00
Copyright (C) 1988-2014 Free Software Foundation, Inc.
1992-03-09 23:06:05 +00:00
This file is part of GNU Make.
GNU Make is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation; either version 3 of the License, or (at your option) any later
version.
1992-03-09 23:06:05 +00:00
GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
1992-03-09 23:06:05 +00:00
You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>. */
1992-03-09 23:06:05 +00:00
#include "makeint.h"
1998-07-30 20:54:47 +00:00
#include "filedef.h"
1992-03-09 23:06:05 +00:00
#include "commands.h"
1993-02-01 23:58:39 +00:00
#include "job.h"
#include "debug.h"
1992-03-09 23:06:05 +00:00
#include <sys/time.h>
1993-02-01 23:58:39 +00:00
#include <netdb.h>
#include "customs.h"
1992-03-09 23:06:05 +00:00
char *remote_description = "Customs";
/* File name of the Customs 'export' client command.
1993-02-01 23:58:39 +00:00
A full path name can be used to avoid some path-searching overhead. */
#define EXPORT_COMMAND "/usr/local/bin/export"
1992-03-09 23:06:05 +00:00
/* ExportPermit gotten by start_remote_job_p, and used by start_remote_job. */
static ExportPermit permit;
1993-02-04 18:31:53 +00:00
/* Normalized path name of the current directory. */
static char *normalized_cwd;
1998-07-30 20:54:47 +00:00
/* Call once at startup even if no commands are run. */
void
remote_setup (void)
1998-07-30 20:54:47 +00:00
{
}
/* Called before exit. */
void
remote_cleanup (void)
1998-07-30 20:54:47 +00:00
{
}
1992-03-09 23:06:05 +00:00
/* Return nonzero if the next job should be done remotely. */
int
start_remote_job_p (int first_p)
1992-03-09 23:06:05 +00:00
{
1993-02-01 23:58:39 +00:00
static int inited = 0;
int status;
1998-07-30 20:54:47 +00:00
int njobs;
1993-02-01 23:58:39 +00:00
if (!inited)
{
1998-07-30 20:54:47 +00:00
/* Allow the user to turn off job exportation (useful while he is
debugging Customs, for example). */
if (getenv ("GNU_MAKE_NO_CUSTOMS") != 0)
{
inited = -1;
return 0;
}
1993-02-01 23:58:39 +00:00
/* For secure Customs, make is installed setuid root and
Customs requires a privileged source port be used. */
1993-02-01 23:58:39 +00:00
make_access ();
if (ISDB (DB_JOBS))
Rpc_Debug (1);
1998-07-30 20:54:47 +00:00
1993-02-01 23:58:39 +00:00
/* Ping the daemon once to see if it is there. */
inited = Customs_Ping () == RPC_SUCCESS ? 1 : -1;
/* Return to normal user access. */
user_access ();
1993-02-04 18:31:53 +00:00
if (starting_directory == 0)
/* main couldn't figure it out. */
inited = -1;
1993-02-04 18:31:53 +00:00
else
{
/* Normalize the current directory path name to something
that should work on all machines exported to. */
normalized_cwd = xmalloc (GET_PATH_MAX);
strcpy (normalized_cwd, starting_directory);
if (Customs_NormPath (normalized_cwd, GET_PATH_MAX) < 0)
/* Path normalization failure means using Customs
won't work, but it's not really an error. */
inited = -1;
}
1993-02-01 23:58:39 +00:00
}
if (inited < 0)
1992-03-09 23:06:05 +00:00
return 0;
1998-07-30 20:54:47 +00:00
njobs = job_slots_used;
if (!first_p)
njobs -= 1; /* correction for being called from reap_children() */
1998-07-30 20:54:47 +00:00
/* the first job should run locally, or, if the -l flag is given, we use
that as clue as to how many local jobs should be scheduled locally */
if (max_load_average < 0 && njobs == 0 || njobs < max_load_average)
return 0;
1993-02-01 23:58:39 +00:00
status = Customs_Host (EXPORT_SAME, &permit);
if (status != RPC_SUCCESS)
{
DB (DB_JOBS, (_("Customs won't export: %s\n"),
Rpc_ErrorMessage (status)));
1993-02-01 23:58:39 +00:00
return 0;
}
1992-03-09 23:06:05 +00:00
return !CUSTOMS_FAIL (&permit.addr);
}
1993-02-01 23:58:39 +00:00
/* Start a remote job running the command in ARGV, with environment from
ENVP. It gets standard input from STDIN_FD. On failure, return
nonzero. On success, return zero, and set *USED_STDIN to nonzero if it
will actually use STDIN_FD, zero if not, set *ID_PTR to a unique
identification, and set *IS_REMOTE to nonzero if the job is remote, zero
if it is local (meaning *ID_PTR is a process ID). */
1992-03-09 23:06:05 +00:00
int
start_remote_job (char **argv, char **envp, int stdin_fd,
int *is_remote, int *id_ptr, int *used_stdin)
1992-03-09 23:06:05 +00:00
{
char waybill[MAX_DATA_SIZE], msg[128];
1998-07-30 20:54:47 +00:00
struct hostent *host;
1992-03-09 23:06:05 +00:00
struct timeval timeout;
struct sockaddr_in sin;
int len;
int retsock, retport, sock;
Rpc_Stat status;
int pid;
/* Create the return socket. */
retsock = Rpc_UdpCreate (True, 0);
if (retsock < 0)
{
O (error, NILF, "exporting: Couldn't create return socket.");
1992-03-09 23:06:05 +00:00
return 1;
}
/* Get the return socket's port number. */
1993-02-01 23:58:39 +00:00
len = sizeof (sin);
1992-03-09 23:06:05 +00:00
if (getsockname (retsock, (struct sockaddr *) &sin, &len) < 0)
{
(void) close (retsock);
perror_with_name ("exporting: ", "getsockname");
return 1;
}
retport = sin.sin_port;
/* Create the TCP socket for talking to the remote child. */
sock = Rpc_TcpCreate (False, 0);
/* Create a WayBill to give to the server. */
1993-02-04 18:31:53 +00:00
len = Customs_MakeWayBill (&permit, normalized_cwd, argv[0], argv,
envp, retport, waybill);
1992-03-09 23:06:05 +00:00
/* Modify the waybill as if the remote child had done 'child_access ()'. */
1993-02-01 23:58:39 +00:00
{
WayBill *wb = (WayBill *) waybill;
1998-07-30 20:54:47 +00:00
wb->ruid = wb->euid;
wb->rgid = wb->egid;
1993-02-01 23:58:39 +00:00
}
1992-03-09 23:06:05 +00:00
/* Send the request to the server, timing out in 20 seconds. */
timeout.tv_usec = 0;
timeout.tv_sec = 20;
sin.sin_family = AF_INET;
sin.sin_port = htons (Customs_Port ());
sin.sin_addr = permit.addr;
status = Rpc_Call (sock, &sin, (Rpc_Proc) CUSTOMS_IMPORT,
len, (Rpc_Opaque) waybill,
sizeof (msg), (Rpc_Opaque) msg,
1, &timeout);
1998-07-30 20:54:47 +00:00
host = gethostbyaddr ((char *)&permit.addr, sizeof(permit.addr), AF_INET);
1998-07-30 20:54:47 +00:00
{
const char *hnm = host ? host->h_name : inet_ntoa (permit.addr);
size_t hlen = strlen (hnm);
if (status != RPC_SUCCESS)
{
const char *err = Rpc_ErrorMessage (status);
(void) close (retsock);
(void) close (sock);
error (NILF, hlen + strlen (err),
"exporting to %s: %s", hnm, err);
return 1;
}
else if (msg[0] != 'O' || msg[1] != 'k' || msg[2] != '\0')
{
(void) close (retsock);
(void) close (sock);
error (NILF, hlen + strlen (msg), "exporting to %s: %s", hnm, msg);
return 1;
}
else
{
error (NILF, hlen + INTSTR_LENGTH,
"*** exported to %s (id %u)", hnm, permit.id);
}
fflush (stdout);
fflush (stderr);
}
1992-03-09 23:06:05 +00:00
pid = fork ();
1992-03-09 23:06:05 +00:00
if (pid < 0)
{
/* The fork failed! */
perror_with_name ("fork", "");
1992-03-09 23:06:05 +00:00
return 1;
}
else if (pid == 0)
{
/* Child side. Run 'export' to handle the connection. */
1992-03-09 23:06:05 +00:00
static char sock_buf[20], retsock_buf[20], id_buf[20];
static char *new_argv[6] =
{ EXPORT_COMMAND, "-id", sock_buf, retsock_buf, id_buf, 0 };
1992-03-09 23:06:05 +00:00
/* Set up the arguments. */
(void) sprintf (sock_buf, "%d", sock);
(void) sprintf (retsock_buf, "%d", retsock);
(void) sprintf (id_buf, "%x", permit.id);
1993-02-01 23:58:39 +00:00
/* Get the right stdin. */
if (stdin_fd != 0)
(void) dup2 (stdin_fd, 0);
1993-02-01 23:58:39 +00:00
/* Unblock signals in the child. */
unblock_sigs ();
1992-03-09 23:06:05 +00:00
/* Run the command. */
1993-02-01 23:58:39 +00:00
exec_command (new_argv, envp);
1992-03-09 23:06:05 +00:00
}
/* Parent side. Return the 'export' process's ID. */
1992-03-09 23:06:05 +00:00
(void) close (retsock);
(void) close (sock);
*is_remote = 0;
*id_ptr = pid;
1998-07-30 20:54:47 +00:00
*used_stdin = 1;
1992-03-09 23:06:05 +00:00
return 0;
}
/* Get the status of a dead remote child. Block waiting for one to die
if BLOCK is nonzero. Set *EXIT_CODE_PTR to the exit status, *SIGNAL_PTR
to the termination signal or zero if it exited normally, and *COREDUMP_PTR
nonzero if it dumped core. Return the ID of the child that died,
0 if we would have to block and !BLOCK, or < 0 if there were none. */
int
remote_status (int *exit_code_ptr, int *signal_ptr, int *coredump_ptr,
int block)
1992-03-09 23:06:05 +00:00
{
return -1;
}
/* Block asynchronous notification of remote child death.
If this notification is done by raising the child termination
signal, do not block that signal. */
void
block_remote_children (void)
1992-03-09 23:06:05 +00:00
{
return;
}
/* Restore asynchronous notification of remote child death.
If this is done by raising the child termination signal,
do not unblock that signal. */
void
unblock_remote_children (void)
1992-03-09 23:06:05 +00:00
{
return;
}
/* Send signal SIG to child ID. Return 0 if successful, -1 if not. */
int
remote_kill (int id, int sig)
1992-03-09 23:06:05 +00:00
{
return -1;
}