chiark
/
gitweb
/
~mdw
/
sw-tools
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
sw_build: Bug squashing.
[sw-tools]
/
src
/
sw_rsh.c
diff --git
a/src/sw_rsh.c
b/src/sw_rsh.c
index 3c910df42c12f43225f0c35bf3bde44765e36256..e0543511c4fd48062c5c597f3fa7e2c0d52dec3c 100644
(file)
--- a/
src/sw_rsh.c
+++ b/
src/sw_rsh.c
@@
-1,6
+1,6
@@
/* -*-c-*-
*
/* -*-c-*-
*
- * $Id
: sw_rsh.c,v 1.1 1999/06/02 16:53:34 mdw Exp
$
+ * $Id$
*
* Run remote commands
*
*
* Run remote commands
*
@@
-26,14
+26,6
@@
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
-/*----- Revision history --------------------------------------------------*
- *
- * $Log: sw_rsh.c,v $
- * Revision 1.1 1999/06/02 16:53:34 mdw
- * Initial revision
- *
- */
-
/*----- Header files ------------------------------------------------------*/
#include "config.h"
/*----- Header files ------------------------------------------------------*/
#include "config.h"
@@
-59,6
+51,7
@@
#endif
#include <mLib/alloc.h>
#endif
#include <mLib/alloc.h>
+#include <mLib/bits.h>
#include <mLib/dstr.h>
#include <mLib/exc.h>
#include <mLib/mdwopt.h>
#include <mLib/dstr.h>
#include <mLib/exc.h>
#include <mLib/mdwopt.h>
@@
-74,15
+67,8
@@
/*----- Data structures ---------------------------------------------------*/
/*----- Data structures ---------------------------------------------------*/
-typedef unsigned char octet;
-
#define PKHEADSZ 3
#define PKHEADSZ 3
-typedef struct pkhead {
- octet len[2];
- octet type;
-} pkhead;
-
/*----- Static variables --------------------------------------------------*/
static int handler = 0;
/*----- Static variables --------------------------------------------------*/
static int handler = 0;
@@
-100,13
+86,13
@@
static rcmd *rcmds = RCMD_LINK;
* Returns: Zero if it worked, nonzero otherwise.
*
* Use: Sends a data packet. If the type is `data', then `sz' may be
* Returns: Zero if it worked, nonzero otherwise.
*
* Use: Sends a data packet. If the type is `data', then `sz' may be
- * arbitrarily large and is divided into small e
e
nough chunks.
+ * arbitrarily large and is divided into small enough chunks.
* Otherwise it's an error to send a packet that's too big.
*/
int pksend(sw_remote *r, int type, const void *p, size_t sz)
{
* Otherwise it's an error to send a packet that's too big.
*/
int pksend(sw_remote *r, int type, const void *p, size_t sz)
{
-
pkhead h
;
+
octet h[PKHEADSZ]
;
const char *q = p;
size_t chunk;
const char *q = p;
size_t chunk;
@@
-119,14
+105,13
@@
int pksend(sw_remote *r, int type, const void *p, size_t sz)
/* --- Main output loop --- */
/* --- Main output loop --- */
- h
.type
= type;
+ h
[2]
= type;
do {
/* --- Set up the packet header --- */
chunk = (sz > PKMAX ? PKMAX : sz);
do {
/* --- Set up the packet header --- */
chunk = (sz > PKMAX ? PKMAX : sz);
- h.len[0] = chunk & 0xff;
- h.len[1] = (chunk >> 8) & 0xff;
+ STORE16(h, chunk);
/* --- Write the packet header --- */
/* --- Write the packet header --- */
@@
-172,7
+157,7
@@
int pksend(sw_remote *r, int type, const void *p, size_t sz)
int pkrecv(sw_remote *r)
{
int pkrecv(sw_remote *r)
{
-
pkhead h
;
+
octet h[PKHEADSZ]
;
size_t sz;
char *p;
ssize_t n;
size_t sz;
char *p;
ssize_t n;
@@
-210,7
+195,7
@@
int pkrecv(sw_remote *r)
* characters.
*/
* characters.
*/
- if (h
.type
>= PKTYPE_BOGUS) {
+ if (h
[2]
>= PKTYPE_BOGUS) {
memcpy(r->buf, &h, PKHEADSZ);
n = read(r->fdin, r->buf + PKHEADSZ, sizeof(r->buf) - PKHEADSZ);
if (n < 0)
memcpy(r->buf, &h, PKHEADSZ);
n = read(r->fdin, r->buf + PKHEADSZ, sizeof(r->buf) - PKHEADSZ);
if (n < 0)
@@
-221,10
+206,10
@@
int pkrecv(sw_remote *r)
/* --- Sort out what's going on --- */
/* --- Sort out what's going on --- */
- sz =
h.len[0] | (h.len[1] << 8
);
+ sz =
LOAD16(h
);
r->sz = sz;
if (!sz)
r->sz = sz;
if (!sz)
- return (h
.type
);
+ return (h
[2]
);
if (sz > PKMAX) {
errno = E2BIG;
return (-1);
if (sz > PKMAX) {
errno = E2BIG;
return (-1);
@@
-245,7
+230,7
@@
int pkrecv(sw_remote *r)
}
}
}
}
- return (h
.type
);
+ return (h
[2]
);
}
/*----- Error reporting and exit statuses --------------------------------*/
}
/*----- Error reporting and exit statuses --------------------------------*/
@@
-328,7
+313,7
@@
void swwait(sw_remote *r, int status)
void swvprintf(sw_remote *r, const char *format, va_list ap)
{
dstr d = DSTR_INIT;
void swvprintf(sw_remote *r, const char *format, va_list ap)
{
dstr d = DSTR_INIT;
- dstr_vputf(&d, format, ap);
+ dstr_vputf(&d, format,
&
ap);
pksend(r, PKTYPE_DATA, d.buf, d.len);
dstr_destroy(&d);
}
pksend(r, PKTYPE_DATA, d.buf, d.len);
dstr_destroy(&d);
}
@@
-371,7
+356,7
@@
void swdie(sw_remote *r, int status, const char *format, ...)
va_start(ap, format);
dstr_putf(&d, "%s [remote]: ", QUIS);
va_start(ap, format);
dstr_putf(&d, "%s [remote]: ", QUIS);
- dstr_vputf(&d, format, ap);
+ dstr_vputf(&d, format,
&
ap);
dstr_putc(&d, '\n');
dstr_putz(&d);
va_end(ap);
dstr_putc(&d, '\n');
dstr_putz(&d);
va_end(ap);
@@
-411,7
+396,7
@@
static void remote(sw_remote *r, const char *cmd, char *argv[], char *env[])
sym_table t;
sym_create(&t);
env_import(&t, env);
sym_table t;
sym_create(&t);
env_import(&t, env);
- if (env != environ) {
+ if (env
[0] && env
!= environ) {
free(env);
env_import(&t, environ);
}
free(env);
env_import(&t, environ);
}
@@
-553,7
+538,15
@@
void swrsh_remote(const char *cmd)
r.fdin = 0;
r.fdout = 1;
r.fdin = 0;
r.fdout = 1;
- /* --- Read packets from the remote host --- */
+ /* --- Object if stdin is a terminal --- */
+
+ if (isatty(STDIN_FILENO)) {
+ die(EXIT_FAILURE,
+ "don't use the `--remote' option unless "
+ "you know what you're doing.");
+ }
+
+ /* --- Read packets from the remote host --- */
for (;;) {
int t = pkrecv(&r);
for (;;) {
int t = pkrecv(&r);
@@
-626,6
+619,7
@@
done:
static void sigchld(int sig)
{
static void sigchld(int sig)
{
+ int e = errno;
#ifdef DEBUG_SIGCHLD
int status;
while (waitpid(-1, &status, WNOHANG) > 0) {
#ifdef DEBUG_SIGCHLD
int status;
while (waitpid(-1, &status, WNOHANG) > 0) {
@@
-642,6
+636,7
@@
static void sigchld(int sig)
while (waitpid(-1, 0, WNOHANG) > 0)
;
#endif
while (waitpid(-1, 0, WNOHANG) > 0)
;
#endif
+ errno = e;
}
/* --- @swrsh@ --- *
}
/* --- @swrsh@ --- *
@@
-674,7
+669,10
@@
int swrsh(sw_remote *r, const char *host, const char *cmd, char *argv[])
if (!handler) {
struct sigaction sa;
sa.sa_handler = sigchld;
if (!handler) {
struct sigaction sa;
sa.sa_handler = sigchld;
- sa.sa_flags = 0;
+ sa.sa_flags = SA_NOCLDSTOP;
+#ifdef SA_RESTART
+ sa.sa_flags |= SA_RESTART;
+#endif
sigemptyset(&sa.sa_mask);
sigaction(SIGCHLD, &sa, 0);
handler = 1;
sigemptyset(&sa.sa_mask);
sigaction(SIGCHLD, &sa, 0);
handler = 1;
@@
-702,6
+700,9
@@
int swrsh(sw_remote *r, const char *host, const char *cmd, char *argv[])
/* --- Child end of a local job --- */
/* --- Child end of a local job --- */
+ signal(SIGINT, SIG_DFL);
+ signal(SIGQUIT, SIG_DFL);
+
if (!host) {
r->fdin = r->fdout = sk[1];
remote(r, cmd, argv, environ);
if (!host) {
r->fdin = r->fdout = sk[1];
remote(r, cmd, argv, environ);