VCL_BOOL vmod_barrier_sync(VRT_CTX, VCL_STRING addr) { const char *err; char buf[32]; int sock, i; ssize_t sz; CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); AN(addr); AN(*addr); VSLb(ctx->vsl, SLT_Debug, "barrier_sync(\"%s\")", addr); sock = VTCP_open(addr, NULL, 0., &err); if (sock < 0) { VSLb(ctx->vsl, SLT_Error, "Barrier connection failed: %s", err); return (0); } sz = read(sock, buf, sizeof buf); i = errno; AZ(close(sock)); if (sz == 0) return (1); if (sz < 0) VSLb(ctx->vsl, SLT_Error, "Barrier read failed: %s (errno=%d)", strerror(i), i); if (sz > 0) VSLb(ctx->vsl, SLT_Error, "Barrier unexpected data (%zdB)", sz); return (0); }
/* * This function establishes a connection to the specified ip and port and * sends a command to varnishd. If varnishd returns an OK status, the result * is printed and 0 returned. Else, an error message is printed and 1 is * returned */ static int cli_sock(const char *T_arg, const char *S_arg) { int fd; int sock; unsigned status; char *answer = NULL; char buf[CLI_AUTH_RESPONSE_LEN + 1]; const char *err; sock = VTCP_open(T_arg, NULL, timeout, &err); if (sock < 0) { fprintf(stderr, "Connection failed (%s): %s\n", T_arg, err); return (-1); } (void)VCLI_ReadResult(sock, &status, &answer, timeout); if (status == CLIS_AUTH) { if (S_arg == NULL) { fprintf(stderr, "Authentication required\n"); AZ(close(sock)); return(-1); } fd = open(S_arg, O_RDONLY); if (fd < 0) { fprintf(stderr, "Cannot open \"%s\": %s\n", S_arg, strerror(errno)); AZ(close(sock)); return (-1); } VCLI_AuthResponse(fd, answer, buf); AZ(close(fd)); free(answer); cli_write(sock, "auth "); cli_write(sock, buf); cli_write(sock, "\n"); (void)VCLI_ReadResult(sock, &status, &answer, timeout); } if (status != CLIS_OK) { fprintf(stderr, "Rejected %u\n%s\n", status, answer); AZ(close(sock)); free(answer); return (-1); } free(answer); cli_write(sock, "ping\n"); (void)VCLI_ReadResult(sock, &status, &answer, timeout); if (status != CLIS_OK || strstr(answer, "PONG") == NULL) { fprintf(stderr, "No pong received from server\n"); AZ(close(sock)); free(answer); return (-1); } free(answer); return (sock); }