Skip to content

Commit

Permalink
the parent process should keep track of its own offset
Browse files Browse the repository at this point in the history
the behavior of fseek(child_stdout, 0, SEEK_CUR) seemed to be that it
seeked to the child's position in the file sometimes.
  • Loading branch information
Ben Osheroff committed Dec 5, 2015
1 parent 188a55b commit 670b8d6
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions wrapper/wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,24 @@
#define TMPFILE_TEMPLATE "/tmp/onetimeserver.XXXXXX"
#define JSON_PREFIX "_onetimeserver_json: "

int tee_child(FILE *child_stdout, int debug) {
void tee_child(FILE *child_stdout, int debug, off_t *offset) {
char *line = NULL;
size_t linecap = 0;
ssize_t linelen;

/* clear EOF */
fseek(child_stdout, 0, SEEK_CUR);
fseek(child_stdout, *offset, SEEK_SET);
while ((linelen = getline(&line, &linecap, child_stdout)) > 0) {
if ( strncmp(line, JSON_PREFIX, strlen(JSON_PREFIX)) == 0 ) {
fwrite(line + strlen(JSON_PREFIX), linelen - strlen(JSON_PREFIX), 1, stdout);
exit(0);
} else if ( debug ) {
fwrite(line, linelen, 1, stderr);
}
*offset += linelen;
}

sleep(1);

return 0;
}


Expand Down Expand Up @@ -83,10 +82,11 @@ int main(int argc, char **argv)

if ( (child = fork()) ) {
child_file = fdopen(child_stdout_fd, "r");
off_t child_offset = 0;
while ( 1 ) {
tee_child(child_file, debug);
tee_child(child_file, debug, &child_offset);
if ( wait4(child, NULL, WNOHANG, NULL) != 0 ) {
tee_child(child_file, debug);
tee_child(child_file, debug, &child_offset);

/* if tee_child didn't exit, we never got booted: true */
fprintf(stderr, "Child exited without printing info!\n");
Expand Down

0 comments on commit 670b8d6

Please sign in to comment.