diff options
author | Mattias Andrée <maandree@kth.se> | 2021-09-04 11:41:21 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2021-09-04 11:41:38 +0200 |
commit | fc2201769e850bca4b718305a1f8e776f5d0786b (patch) | |
tree | e37090b9b1cde35a208604e45df6efb0a68feb41 | |
parent | Communicate whether file transfers were completed (diff) | |
download | editasroot-fc2201769e850bca4b718305a1f8e776f5d0786b.tar.gz editasroot-fc2201769e850bca4b718305a1f8e776f5d0786b.tar.bz2 editasroot-fc2201769e850bca4b718305a1f8e776f5d0786b.tar.xz |
Use MSG_PEEK instead of sending unnecessary data
Signed-off-by: Mattias Andrée <maandree@kth.se>
-rw-r--r-- | copier.c | 12 | ||||
-rw-r--r-- | editasroot.c | 6 |
2 files changed, 9 insertions, 9 deletions
@@ -22,6 +22,7 @@ main(int argc, char *argv[]) path = argv[0]; parentfd = atoi(argv[1]); + /* Send content of file to parent */ filefd = open(path, O_RDWR); /* O_RDWR establishes that we can indeed write to the file */ if (filefd < 0) { fprintf(stderr, "%s: open %s O_RDWR: %s\n", argv0, path, strerror(errno)); @@ -37,13 +38,18 @@ main(int argc, char *argv[]) exit(1); } - r = read(parentfd, &b, 1); - if (r != 1) { - if (r) + /* Wait for parent to start sending the new file content, + * so that O_TRUNC does not cause the file to be truncated + * without new content being sent in case the parent exited + * abnormally. */ + r = recv(parentfd, &b, 1, MSG_PEEK); + if (r <= 0) { + if (r < 0) fprintf(stderr, "%s: read %s: %s\n", argv0, path, strerror(errno)); exit(1); } + /* Rewrite file with content sent from parent */ filefd = open(path, O_WRONLY | O_TRUNC); if (filefd < 0) { fprintf(stderr, "%s: open %s O_WRONLY|O_TRUNC: %s\n", argv0, path, strerror(errno)); diff --git a/editasroot.c b/editasroot.c index 09479f8..0a092cd 100644 --- a/editasroot.c +++ b/editasroot.c @@ -230,12 +230,6 @@ main(int argc, char *argv[]) /* Start file editor */ run_editor(editor, path, fds[0]); - /* Signal to child that editor exited as expected */ - if (write(fds[0], &(char){1}, 1) != 1) { - fprintf(stderr, "%s: write <socket to child>: %s", argv0, strerror(errno)); - exit(1); - } - /* Rewrite file from tmpfile and unlink tmpfile */ fd = open(path, O_RDONLY); if (fd < 0) { |