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