write start cookie when socket connect succeeds

This commit is contained in:
Daniel Barlow 2024-09-15 21:54:21 +01:00
parent 1f6cfc3679
commit 44762d38fc
1 changed files with 23 additions and 8 deletions

View File

@ -49,14 +49,23 @@ int main(int argc, char * argv[]) {
int out_bytes = 0; int out_bytes = 0;
int tee_bytes = 0; int tee_bytes = 0;
if(argc > 1 && (strlen(argv[1]) > 108)) { if(argc != 3) {
error(1, 0, "socket pathname \"%s\" is too long, max 108 bytes", error(1, 0, "usage: logtee /path/to/socket cookie-text");
argv[1]); }
}; char * socket_pathname = argv[1];
signal(SIGPIPE, SIG_IGN); char * cookie = argv[2];
char * start_cookie = malloc(strlen(cookie) + 8);
char * stop_cookie = malloc(strlen(cookie) + 7);
char * start_cookie = "COOKIE-START\n"; if(strlen(socket_pathname) > 108) {
char * stop_cookie = "COOKIE-STOP\n"; error(1, 0, "socket pathname \"%s\" is too long, max 108 bytes",
socket_pathname);
};
strcpy(start_cookie, cookie); strcat(start_cookie, " START\n");
strcpy(stop_cookie, cookie); strcat(stop_cookie, " STOP\n");
signal(SIGPIPE, SIG_IGN);
int flags = fcntl(STDOUT_FILENO, F_GETFL); int flags = fcntl(STDOUT_FILENO, F_GETFL);
fcntl(STDOUT_FILENO, F_SETFL, flags | O_NONBLOCK); fcntl(STDOUT_FILENO, F_SETFL, flags | O_NONBLOCK);
@ -92,7 +101,13 @@ int main(int argc, char * argv[]) {
}; };
} else { } else {
if(! is_connected()) { if(! is_connected()) {
if(argc>1) fds[2].fd = open_shipper_socket(argv[1]); fds[2].fd = open_shipper_socket(argv[1]);
if(is_connected()) {
/* write cookie to stdout so that the backfill
* process knows we are now logging realtime
*/
write(fds[1].fd, start_cookie, strlen(start_cookie));
}
} }
}; };
}; };