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