#include "make.h"
#include "job.h"
#include "filedef.h"
#include "commands.h"
#include "job.h"
#include "debug.h"
#include <sys/time.h>
#include <netdb.h>
#include "customs.h"
char *remote_description = "Customs";
#define EXPORT_COMMAND "/usr/local/bin/export"
static ExportPermit permit;
static char *normalized_cwd;
void
remote_setup ()
{
}
void
remote_cleanup ()
{
}
int
start_remote_job_p (first_p)
int first_p;
{
static int inited = 0;
int status;
int njobs;
if (!inited)
{
if (getenv ("GNU_MAKE_NO_CUSTOMS") != 0)
{
inited = -1;
return 0;
}
make_access ();
if (ISDB (DB_EXTRA))
Rpc_Debug(1);
inited = Customs_Ping () == RPC_SUCCESS ? 1 : -1;
user_access ();
if (starting_directory == 0)
inited = -1;
else
{
normalized_cwd = (char *) xmalloc (GET_PATH_MAX);
strcpy (normalized_cwd, starting_directory);
if (Customs_NormPath (normalized_cwd, GET_PATH_MAX) < 0)
inited = -1;
}
}
if (inited < 0)
return 0;
njobs = job_slots_used;
if (!first_p)
njobs -= 1;
if (max_load_average < 0 && njobs == 0 || njobs < max_load_average)
return 0;
status = Customs_Host (EXPORT_SAME, &permit);
if (status != RPC_SUCCESS)
{
DB (DB_EXTRA, (_("Customs won't export: %s\n"),
Rpc_ErrorMessage (status)));
return 0;
}
return !CUSTOMS_FAIL (&permit.addr);
}
int
start_remote_job (argv, envp, stdin_fd, is_remote, id_ptr, used_stdin)
char **argv, **envp;
int stdin_fd;
int *is_remote;
int *id_ptr;
int *used_stdin;
{
char waybill[MAX_DATA_SIZE], msg[128];
struct hostent *host;
struct timeval timeout;
struct sockaddr_in sin;
int len;
int retsock, retport, sock;
Rpc_Stat status;
int pid;
retsock = Rpc_UdpCreate (True, 0);
if (retsock < 0)
{
error (NILF, "exporting: Couldn't create return socket.");
return 1;
}
len = sizeof (sin);
if (getsockname (retsock, (struct sockaddr *) &sin, &len) < 0)
{
(void) close (retsock);
perror_with_name ("exporting: ", "getsockname");
return 1;
}
retport = sin.sin_port;
sock = Rpc_TcpCreate (False, 0);
len = Customs_MakeWayBill (&permit, normalized_cwd, argv[0], argv,
envp, retport, waybill);
{
WayBill *wb = (WayBill *) waybill;
wb->ruid = wb->euid;
wb->rgid = wb->egid;
}
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);
host = gethostbyaddr((char *)&permit.addr, sizeof(permit.addr), AF_INET);
if (status != RPC_SUCCESS)
{
(void) close (retsock);
(void) close (sock);
error (NILF, "exporting to %s: %s",
host ? host->h_name : inet_ntoa (permit.addr),
Rpc_ErrorMessage (status));
return 1;
}
else if (msg[0] != 'O' || msg[1] != 'k' || msg[2] != '\0')
{
(void) close (retsock);
(void) close (sock);
error (NILF, "exporting to %s: %s",
host ? host->h_name : inet_ntoa (permit.addr),
msg);
return 1;
}
else
{
error (NILF, "*** exported to %s (id %u)",
host ? host->h_name : inet_ntoa (permit.addr),
permit.id);
}
fflush (stdout);
fflush (stderr);
pid = vfork ();
if (pid < 0)
{
perror_with_name ("vfork", "");
return 1;
}
else if (pid == 0)
{
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 };
(void) sprintf (sock_buf, "%d", sock);
(void) sprintf (retsock_buf, "%d", retsock);
(void) sprintf (id_buf, "%x", permit.id);
if (stdin_fd != 0)
(void) dup2 (stdin_fd, 0);
unblock_sigs ();
exec_command (new_argv, envp);
}
(void) close (retsock);
(void) close (sock);
*is_remote = 0;
*id_ptr = pid;
*used_stdin = 1;
return 0;
}
int
remote_status (exit_code_ptr, signal_ptr, coredump_ptr, block)
int *exit_code_ptr, *signal_ptr, *coredump_ptr;
int block;
{
return -1;
}
void
block_remote_children ()
{
return;
}
void
unblock_remote_children ()
{
return;
}
int
remote_kill (id, sig)
int id;
int sig;
{
return -1;
}