summaryrefslogtreecommitdiff
path: root/attach.c
diff options
context:
space:
mode:
authorNed T. Crigler <crigler@gmail.com>2025-06-19 19:45:22 -0700
committerNed T. Crigler <crigler@gmail.com>2025-06-19 20:30:18 -0700
commitdc84bf8b0f70af92f894209d2cf68eaff55db9f0 (patch)
tree4805f9238336fa21f396beb14fd278a4feac9911 /attach.c
parent6e04acf9ed089f065b19ff4e42be97bd17529ff2 (diff)
Always check the return value of the write() system call
dtach was assuming that writes with a small byte count could never fail, and was not handling partial writes for larger byte counts in a few places. This should also suppress unused result warnings from gcc on systems that define _FORTIFY_SOURCE by default.
Diffstat (limited to 'attach.c')
-rw-r--r--attach.c24
1 files changed, 14 insertions, 10 deletions
diff --git a/attach.c b/attach.c
index 8ac3bf3..217838e 100644
--- a/attach.c
+++ b/attach.c
@@ -111,7 +111,7 @@ process_kbd(int s, struct packet *pkt)
{
/* Tell the master that we are suspending. */
pkt->type = MSG_DETACH;
- write(s, pkt, sizeof(struct packet));
+ write_packet_or_fail(s, pkt);
/* And suspend... */
tcsetattr(0, TCSADRAIN, &orig_term);
@@ -121,13 +121,13 @@ process_kbd(int s, struct packet *pkt)
/* Tell the master that we are returning. */
pkt->type = MSG_ATTACH;
- write(s, pkt, sizeof(struct packet));
+ write_packet_or_fail(s, pkt);
/* We would like a redraw, too. */
pkt->type = MSG_REDRAW;
pkt->len = redraw_method;
ioctl(0, TIOCGWINSZ, &pkt->u.ws);
- write(s, pkt, sizeof(struct packet));
+ write_packet_or_fail(s, pkt);
return;
}
/* Detach char? */
@@ -141,7 +141,7 @@ process_kbd(int s, struct packet *pkt)
win_changed = 1;
/* Push it out */
- write(s, pkt, sizeof(struct packet));
+ write_packet_or_fail(s, pkt);
}
int
@@ -218,18 +218,18 @@ attach_main(int noerror)
tcsetattr(0, TCSADRAIN, &cur_term);
/* Clear the screen. This assumes VT100. */
- write(1, "\33[H\33[J", 6);
+ write_buf_or_fail(1, "\33[H\33[J", 6);
/* Tell the master that we want to attach. */
memset(&pkt, 0, sizeof(struct packet));
pkt.type = MSG_ATTACH;
- write(s, &pkt, sizeof(struct packet));
+ write_packet_or_fail(s, &pkt);
/* We would like a redraw, too. */
pkt.type = MSG_REDRAW;
pkt.len = redraw_method;
ioctl(0, TIOCGWINSZ, &pkt.u.ws);
- write(s, &pkt, sizeof(struct packet));
+ write_packet_or_fail(s, &pkt);
/* Wait for things to happen */
while (1)
@@ -263,7 +263,7 @@ attach_main(int noerror)
exit(1);
}
/* Send the data to the terminal. */
- write(1, buf, len);
+ write_buf_or_fail(1, buf, len);
n--;
}
/* stdin activity */
@@ -290,7 +290,7 @@ attach_main(int noerror)
pkt.type = MSG_WINCH;
ioctl(0, TIOCGWINSZ, &pkt.u.ws);
- write(s, &pkt, sizeof(pkt));
+ write_packet_or_fail(s, &pkt);
}
}
return 0;
@@ -358,8 +358,12 @@ push_main()
}
pkt.len = len;
- if (write(s, &pkt, sizeof(struct packet)) < 0)
+ len = write(s, &pkt, sizeof(struct packet));
+ if (len != sizeof(struct packet))
{
+ if (len >= 0)
+ errno = EPIPE;
+
printf("%s: %s: %s\n", progname, sockname,
strerror(errno));
return 1;