(sendcmd): Cope gracefully with short writes and EINTR. Don't just

continue after real errors.
This commit is contained in:
Markus Armbruster 2007-12-14 06:42:57 +00:00
parent 92a14cca4d
commit 43cceac785

View file

@ -34,6 +34,7 @@
#include <config.h> #include <config.h>
#include <ctype.h> #include <ctype.h>
#include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -126,7 +127,9 @@ void
sendcmd(int s, char *cmd, char *arg) sendcmd(int s, char *cmd, char *arg)
{ {
char buf[128]; char buf[128];
int cc, len; char *p;
ssize_t n;
int len;
len = snprintf(buf, sizeof(buf), "%s %s\n", len = snprintf(buf, sizeof(buf), "%s %s\n",
cmd, arg != NULL ? arg : ""); cmd, arg != NULL ? arg : "");
@ -134,11 +137,17 @@ sendcmd(int s, char *cmd, char *arg)
fprintf(stderr, "%s too long\n", cmd); fprintf(stderr, "%s too long\n", cmd);
exit(1); exit(1);
} }
cc = write(s, buf, len); p = buf;
if (cc < 0) { while (len > 0) {
n = write(s, buf, len);
if (n < 0) {
if (errno != EINTR) {
perror("sendcmd: write"); perror("sendcmd: write");
exit(1);
} }
if (cc != len) { n = 0;
fprintf(stderr, "sendcmd: short write (%d not %d)\n", cc, len); }
p += n;
len -= n;
} }
} }