0406798ba312a59b92b5554345094a793fcef5d8
3 * $Id: sw_rsh.c,v 1.4 1999/06/24 15:51:17 mdw Exp $
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of sw-tools.
14 * sw-tools is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * sw-tools is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with sw-tools; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.4 1999/06/24 15:51:17 mdw
33 * Fix signal handlers so they don't corrupt `errno'.
35 * Revision 1.3 1999/06/18 18:58:54 mdw
36 * Signal handling fixes.
38 * Revision 1.2 1999/06/02 17:03:29 mdw
39 * Fix use of `octet' now that mLib includes `bits.h' (as of version 1.3.5
40 * release). Also use the mLib load and store macros rather than doing it
43 * Revision 1.1.1.1 1999/06/02 16:53:34 mdw
48 /*----- Header files ------------------------------------------------------*/
60 #include <sys/types.h>
65 #include <sys/socket.h>
69 extern char **environ
;
72 #include <mLib/alloc.h>
73 #include <mLib/bits.h>
74 #include <mLib/dstr.h>
76 #include <mLib/mdwopt.h>
77 #include <mLib/quis.h>
78 #include <mLib/report.h>
87 /*----- Data structures ---------------------------------------------------*/
91 typedef struct pkhead
{
96 /*----- Static variables --------------------------------------------------*/
98 static int handler
= 0;
99 static rcmd
*rcmds
= RCMD_LINK
;
101 /*----- Packet interface --------------------------------------------------*/
103 /* --- @pksend@ --- *
105 * Arguments: @sw_remote@ = pointer to the remote block
106 * @int type@ = packet type to send
107 * @const void *p@ = pointer to packet data
108 * @size_t sz@ = size of data to send
110 * Returns: Zero if it worked, nonzero otherwise.
112 * Use: Sends a data packet. If the type is `data', then `sz' may be
113 * arbitrarily large and is divided into small eenough chunks.
114 * Otherwise it's an error to send a packet that's too big.
117 int pksend(sw_remote
*r
, int type
, const void *p
, size_t sz
)
123 /* --- Sort out error conditions --- */
125 if (sz
> PKMAX
&& type
!= PKTYPE_DATA
) {
130 /* --- Main output loop --- */
135 /* --- Set up the packet header --- */
137 chunk
= (sz
> PKMAX ? PKMAX
: sz
);
138 STORE16(h
.len
, chunk
);
140 /* --- Write the packet header --- */
143 if (write(r
->fdout
, &h
, PKHEADSZ
) < PKHEADSZ
) {
149 /* --- Write the payload, if there is one --- *
151 * Maybe the OS won't want to bite it all off in one go.
155 ssize_t n
= write(r
->fdout
, q
, chunk
);
156 if (n
< 0 && errno
!= EINTR
)
171 /* --- @pkrecv@ --- *
173 * Arguments: @sw_remote *r@ = remote block
175 * Returns: Packet type received, or @-1@ for an error.
177 * Use: Receives a packet from the remote host. The packet data is
178 * placed in the block's buffer, the block's packet length is
179 * diddled appropriately.
182 int pkrecv(sw_remote
*r
)
189 /* --- Read the packet header --- */
194 n
= read(r
->fdin
, p
, sz
);
195 if (n
< 0 && errno
!= EINTR
)
205 /* --- Hack for error messages --- *
207 * If it doesn't look like a valid packet, read a `chunk' and pretend it's
208 * data. This isn't too bad, because all the packet types are control
209 * codes, and are unlikely to be in a textual message.
211 * Normally what happens here is that something sitting before the `sw'
212 * program fails, reports a plain textual error, and exits. Grabbing the
213 * `last gasp' like this, then, traps that error message and allows
214 * something to report it. The rest ought to be completely silent, so I
215 * get an `unexpected eof' and then drop everything.
217 * This is certainly better than the behaviour otherwise, which is an
218 * @E2BIG@ message reported when the packet size is really ASCII
222 if (h
.type
>= PKTYPE_BOGUS
) {
223 memcpy(r
->buf
, &h
, PKHEADSZ
);
224 n
= read(r
->fdin
, r
->buf
+ PKHEADSZ
, sizeof(r
->buf
) - PKHEADSZ
);
227 r
->sz
= n
+ PKHEADSZ
;
228 return (PKTYPE_DATA
);
231 /* --- Sort out what's going on --- */
242 /* --- Read the packet payload --- */
246 n
= read(r
->fdin
, p
, sz
);
247 if (n
< 0 && errno
!= EINTR
)
260 /*----- Error reporting and exit statuses --------------------------------*/
262 /* --- @swexit@ --- *
264 * Arguments: @sw_remote *r@ = remote context
265 * @int status@ = exit status to return
269 * Use: Reports the exit status via packet protocol and quits.
272 void swexit(sw_remote
*r
, int status
)
274 unsigned char s
= status
;
275 pksend(r
, PKTYPE_STATUS
, &s
, 1);
279 /* --- @swsignal@ --- *
281 * Arguments: @sw_remote *r@ = remote context
282 * @int sig@ = signal ocurrence to return
286 * Use: Reports a signalled-to-death status via packet protocol and
290 void swsignal(sw_remote
*r
, int sig
)
292 #if defined(HAVE_STRSIGNAL)
293 char *s
= strsignal(sig
);
294 #elif defined(HAVE__SYS_SIGLIST)
295 char *s
= _sys_siglist
[sig
];
298 sprintf(s
, "signal %i", sig
);
301 pksend(r
, PKTYPE_STATUS
, s
, strlen(s
) + 1);
305 /* --- @swwait@ --- *
307 * Arguments: @sw_remote *r@ = remote context
308 * @int status@ = status answer from @wait@(2)
312 * Use: Reports a child's demise appropriately, and quits.
315 void swwait(sw_remote
*r
, int status
)
317 if (WIFEXITED(status
))
318 swexit(r
, WEXITSTATUS(status
));
319 else if (WIFSIGNALED(status
))
320 swsignal(r
, WTERMSIG(status
));
325 /* --- @swvprintf@ --- *
327 * Arguments: @sw_remote *r@ = remote context
328 * @const char *format@ = format string
329 * @va_list ap@ = things to format
333 * Use: Writes a string to the remote end. This is the low-level bit
337 void swvprintf(sw_remote
*r
, const char *format
, va_list ap
)
340 dstr_vputf(&d
, format
, ap
);
341 pksend(r
, PKTYPE_DATA
, d
.buf
, d
.len
);
345 /* --- @swprintf@ --- *
347 * Arguments: @sw_remote *r@ = remote context
348 * @const char *format@ = format string
349 * @...@ = other arguments
353 * Use: Writes a string to the remote end.
356 void swprintf(sw_remote
*r
, const char *format
, ...)
359 va_start(ap
, format
);
360 swvprintf(r
, format
, ap
);
366 * Arguments: @sw_remote *r@ = remote context
367 * @int status@ = exit status to report
368 * @const char *format@ = format string to fill in
369 * @...@ = other arguments
373 * Use: Reports a message and quits.
376 void swdie(sw_remote
*r
, int status
, const char *format
, ...)
381 va_start(ap
, format
);
382 dstr_putf(&d
, "%s [remote]: ", QUIS
);
383 dstr_vputf(&d
, format
, ap
);
387 pksend(r
, PKTYPE_DATA
, d
.buf
, d
.len
);
392 /*----- Command handling and dispatch -------------------------------------*/
394 /* --- @remote@ --- *
396 * Arguments: @sw_remote *r@ = pointer to remote block
397 * @const char *cmd@ = command to run
398 * @char *argv[]@ = argument array
399 * @char *env[]@ = environment variables
401 * Returns: Doesn't. Reports an exit status through packet protocol and
404 * Use: Dispatches a remote command. At this point, the two code
405 * paths for local and remote invokation have joined again.
408 static void remote(sw_remote
*r
, const char *cmd
, char *argv
[], char *env
[])
410 struct rcmd
*p
, *chosen
= 0;
411 size_t sz
= strlen(cmd
);
413 /* --- Make sure that I can get the exit status of children --- */
415 signal(SIGCHLD
, SIG_DFL
);
417 /* --- Fix up the environment --- */
423 if (env
!= environ
) {
425 env_import(&t
, environ
);
427 env_put(&t
, "SW_ARCH", ARCH
);
428 env_file(&t
, DATADIR
"/sw-env");
429 env
= env_export(&t
);
432 /* --- Dispatch to the command handler --- */
434 for (p
= rcmds
; p
; p
= p
->next
) {
435 if (strncmp(cmd
, p
->name
, sz
) == 0) {
436 if (p
->name
[sz
] == 0) {
440 swdie(r
, 1, "ambiguous remote command name `%s'", cmd
);
446 swdie(r
, 1, "unknown remote command name `%s'", cmd
);
447 chosen
->rcmd(r
, argv
, env
);
450 /*----- Remote invocation -------------------------------------------------*/
452 /* --- @sendargv@ --- *
454 * Arguments: @sw_remote *r@ = pointer to the remote context
455 * @int type@ = packet type to send with
456 * @char *v[]@ = pointer to the array to send
458 * Returns: Zero if OK, nonzero if it failed.
460 * Use: Sends something @argv@-shaped; i.e., an array of strings
461 * terminated by a null pointer.
464 static int sendargv(sw_remote
*r
, int type
, char *v
[])
471 d
.len
++; /* Make the null `real' */
474 e
= pksend(r
, type
, d
.buf
, d
.len
);
479 /* --- @snarfargv@ --- *
481 * Arguments: @const char *buf@ = pointer to buffer
482 * @size_t sz@ = size of buffer
484 * Returns: Pointer to argument array (allocated with @malloc@).
486 * Use: Snarfs the null-terminated strings in the buffer and returns
487 * an array of them. The whole lot, strings and array, is
488 * returned in one big chunk allocated from the heap. Note that
489 * this means it's OK to throw the initial buffer away.
492 static char **snarfargv(const char *buf
, size_t sz
)
494 /* --- Initial setup --- */
501 /* --- Pass one: count the number of arguments --- */
517 /* --- Allocate memory for everything --- */
519 v
= xmalloc((c
+ 1) * sizeof(char *) + sz
+ 1);
520 q
= (char *)(v
+ c
+ 1);
523 /* --- Pass two: set up the arrays --- */
545 /* --- @swrsh_remote@ --- *
547 * Arguments: @const char *cmd@ = the command to perform
549 * Returns: Doesn't. Reports an exit status through packet protocol and
552 * Use: Handles the remote end of a remote job invokation.
555 void swrsh_remote(const char *cmd
)
558 static char *dummy
= 0;
565 /* --- Read packets from the remote host --- */
571 swdie(&r
, 1, "error reading packet: %s", strerror(errno
));
578 argv
= snarfargv(r
.buf
, r
.sz
);
583 env
= snarfargv(r
.buf
, r
.sz
);
589 dir
= xstrdup(r
.buf
);
595 swdie(&r
, 1, "internal error: unexpected packet");
600 /* --- Sort out any missing arguments --- */
610 /* --- Run the command --- */
613 remote(&r
, cmd
, argv
, env
);
614 CATCH
switch (exc_type
) {
616 static char msg
[] = "\nsw [remote]: not enough memory\n";
617 pksend(&r
, PKTYPE_DATA
, msg
, sizeof(msg
) - 1);
621 swdie(&r
, 1, "uncaught exception, type = %lx", exc_type
);
625 /*----- Starting remote jobs ----------------------------------------------*/
627 /* --- @sigchld@ --- *
629 * Arguments: @int sig@ = the signal number
633 * Use: Catches @SIGCHLD@ and reaps any children that have lost.
636 static void sigchld(int sig
)
641 while (waitpid(-1, &status
, WNOHANG
) > 0) {
642 if (WIFEXITED(status
)) {
643 fprintf(stderr
, "reap child with exit status %i\n",
644 WEXITSTATUS(status
));
645 } else if (WIFSIGNALED(status
)) {
646 fprintf(stderr
, "reap child killed by signal %s\n",
647 strsignal(WTERMSIG(status
)));
649 fprintf(stderr
, "reaped bizarre child which is still alive\n");
652 while (waitpid(-1, 0, WNOHANG
) > 0)
660 * Arguments: @sw_remote *r@ = remote process block to look after
661 * @const char *host@ = host to run on (0 for this one)
662 * @const char *cmd@ = remote command to run
663 * @char *argv[]@ = arguments to pass on
665 * Returns: Zero if it worked, nonzero if not.
667 * Use: Runs a command on a remote host. The argument array is
668 * mangled to come out OK at the far end. The environment and
669 * current directory are also passed along, and pop out the
670 * other end unmolested.
673 int swrsh(sw_remote
*r
, const char *host
, const char *cmd
, char *argv
[])
678 /* --- Get a socket pair for communicating with the other end --- */
680 if (socketpair(PF_UNIX
, SOCK_STREAM
, 0, sk
))
683 /* --- Set up a signal handler --- */
687 sa
.sa_handler
= sigchld
;
688 sa
.sa_flags
= SA_NOCLDSTOP
;
690 sa
.sa_flags
|= SA_RESTART
;
692 sigemptyset(&sa
.sa_mask
);
693 sigaction(SIGCHLD
, &sa
, 0);
697 /* --- Fork off a child to cope with stuff --- */
703 /* --- Handle the child process --- *
705 * If this is a local job, then just loop around inside to handle the
706 * `remote' command. Otherwise crank up `rsh' and pass the command over to
707 * a remote copy of myself.
709 * (Why do I need a separate process for local jobs? I don't really, but
710 * it makes everything much simpler when running multiple jobs at the same
717 /* --- Child end of a local job --- */
720 r
->fdin
= r
->fdout
= sk
[1];
721 signal(SIGINT
, SIG_DFL
);
722 signal(SIGQUIT
, SIG_DFL
);
723 remote(r
, cmd
, argv
, environ
);
726 /* --- Local child end of a remote job --- */
735 rsh
= getenv("SW_RSH");
738 execlp(rsh
, rsh
, host
, PATH_SW
, "--remote", cmd
, (char *)0);
741 /* --- I don't expect either to come back --- */
746 /* --- Local sort out of what to do --- *
748 * Either way, I've now got a socket tied to something which speaks my
749 * communication protocol. However, if this is a local job, then I can get
750 * going right away; otherwise, I've got to transmit various bits of
751 * information over the protocol.
754 r
->fdin
= r
->fdout
= sk
[0];
759 if (!getcwd(buf
, sizeof(buf
)))
761 sendargv(r
, PKTYPE_ARGS
, argv
);
762 sendargv(r
, PKTYPE_ENV
, environ
);
763 pksend(r
, PKTYPE_DIR
, buf
, strlen(buf
) + 1);
764 pksend(r
, PKTYPE_GO
, 0, 0);
767 /* --- Ready to rock'n'roll --- */
772 /* --- Tidy up if it failed --- */
781 /*----- Subcommands -------------------------------------------------------*/
783 /* --- @swrsh_rsh@ --- */
785 void rsw_rsh(sw_remote
*r
, char *argv
[], char *env
[])
791 /* --- Create a pipe --- */
794 swdie(r
, 1, "couldn't create pipe: %s", strerror(errno
));
796 /* --- Start the child process up --- */
800 swdie(r
, 1, "fork failed: %s", strerror(errno
));
804 /* --- Use my new environment --- */
806 environ
= env
; /* Yuk. */
808 /* --- Fiddle with pipe file descriptors --- */
816 /* --- Make sure it doesn't get any input --- */
819 fd
= open("/dev/null", O_RDONLY
);
825 /* --- Run the program --- */
827 execvp(argv
[0], argv
);
828 die(1, "couldn't exec `%s': %s", argv
[0], strerror(errno
));
831 /* --- Read the data from the pipe until it closes --- */
835 ssize_t n
= read(pfd
[0], r
->buf
, sizeof(r
->buf
));
837 swdie(r
, 1, "read error: %s", strerror(errno
));
841 pksend(r
, PKTYPE_DATA
, r
->buf
, n
);
845 /* --- Finally, reap the exit status and pass it on --- */
852 swdie(r
, 1, "error reaping child: %s", strerror(errno
));
857 /* --- @sw_rsh@ --- */
859 int sw_rsh(int argc
, char *argv
[])
865 /* --- Check the arguments --- */
868 die(1, "Usage: rsh HOST|ARCH COMMAND [ARGS...]");
870 /* --- Translate architecture names into hostnames --- */
872 if (strcmp(argv
[1], "-") == 0)
875 archent
*a
= arch_lookup(argv
[1], 0);
878 else if (a
->flags
& archFlag_home
)
884 /* --- Start the remote process --- */
886 if (swrsh(&r
, h
, "rsh", argv
+ 2))
887 die(1, "remote shell failed: %s", strerror(errno
));
889 /* --- Cope with packets from the remote process --- */
896 die(1, "error reading packet: %s", strerror(errno
));
898 write(STDOUT_FILENO
, r
.buf
, r
.sz
);
904 moan("command exited due to signal: %s", r
.buf
);
907 moan("command exited with status %i", r
.buf
[0]);
911 moan("command exited unexpectedly");
914 die(1, "unexpected packet type");
918 /* --- Finished --- */
925 /*----- That's all, folks -------------------------------------------------*/